Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * NSA Security-Enhanced Linux (SELinux) security module |
| 4 | * |
| 5 | * This file contains the SELinux hook function implementations. |
| 6 | * |
Stephen Smalley | 7efbb60 | 2017-08-17 13:32:36 -0400 | [diff] [blame] | 7 | * Authors: Stephen Smalley, <sds@tycho.nsa.gov> |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 8 | * Chris Vance, <cvance@nai.com> |
| 9 | * Wayne Salamon, <wsalamon@nai.com> |
| 10 | * James Morris <jmorris@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * Copyright (C) 2001,2002 Networks Associates Technology, Inc. |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 13 | * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 14 | * Eric Paris <eparis@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 16 | * <dgoeddel@trustedcs.com> |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 17 | * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 18 | * Paul Moore <paul@paul-moore.com> |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 19 | * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 20 | * Yuichi Nakamura <ynakam@hitachisoft.jp> |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 21 | * Copyright (C) 2016 Mellanox Technologies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 25 | #include <linux/kd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 27 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 29 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 30 | #include <linux/sched/task.h> |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 31 | #include <linux/lsm_hooks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/xattr.h> |
| 33 | #include <linux/capability.h> |
| 34 | #include <linux/unistd.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/mman.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/pagemap.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 39 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/spinlock.h> |
| 42 | #include <linux/syscalls.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 43 | #include <linux/dcache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 45 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/namei.h> |
| 47 | #include <linux/mount.h> |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 48 | #include <linux/fs_context.h> |
| 49 | #include <linux/fs_parser.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/netfilter_ipv4.h> |
| 51 | #include <linux/netfilter_ipv6.h> |
| 52 | #include <linux/tty.h> |
| 53 | #include <net/icmp.h> |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 54 | #include <net/ip.h> /* for local_port_range[] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 56 | #include <net/inet_connection_sock.h> |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 57 | #include <net/net_namespace.h> |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 58 | #include <net/netlabel.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 59 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <asm/ioctls.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 61 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #include <linux/bitops.h> |
| 63 | #include <linux/interrupt.h> |
| 64 | #include <linux/netdevice.h> /* for network interface checks */ |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 65 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <linux/tcp.h> |
| 67 | #include <linux/udp.h> |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 68 | #include <linux/dccp.h> |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 69 | #include <linux/sctp.h> |
| 70 | #include <net/sctp/structs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #include <linux/quota.h> |
| 72 | #include <linux/un.h> /* for Unix socket types */ |
| 73 | #include <net/af_unix.h> /* for Unix socket types */ |
| 74 | #include <linux/parser.h> |
| 75 | #include <linux/nfs_mount.h> |
| 76 | #include <net/ipv6.h> |
| 77 | #include <linux/hugetlb.h> |
| 78 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #include <linux/audit.h> |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 80 | #include <linux/string.h> |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 81 | #include <linux/mutex.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 82 | #include <linux/posix-timers.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 83 | #include <linux/syslog.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 84 | #include <linux/user_namespace.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 85 | #include <linux/export.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 86 | #include <linux/msg.h> |
| 87 | #include <linux/shm.h> |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 88 | #include <linux/bpf.h> |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 89 | #include <linux/kernfs.h> |
| 90 | #include <linux/stringhash.h> /* for hashlen_string() */ |
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 | 3e112172 | 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 | |
Kees Cook | be6ec88 | 2018-10-01 17:08:57 -0700 | [diff] [blame] | 124 | int selinux_enabled __lsm_ro_after_init = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | static int __init selinux_enabled_setup(char *str) |
| 127 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 128 | unsigned long enabled; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 129 | if (!kstrtoul(str, 0, &enabled)) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 130 | selinux_enabled = enabled ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | __setup("selinux=", selinux_enabled_setup); |
| 134 | #endif |
| 135 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 136 | static unsigned int selinux_checkreqprot_boot = |
| 137 | CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 138 | |
| 139 | static int __init checkreqprot_setup(char *str) |
| 140 | { |
| 141 | unsigned long checkreqprot; |
| 142 | |
| 143 | if (!kstrtoul(str, 0, &checkreqprot)) |
| 144 | selinux_checkreqprot_boot = checkreqprot ? 1 : 0; |
| 145 | return 1; |
| 146 | } |
| 147 | __setup("checkreqprot=", checkreqprot_setup); |
| 148 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 149 | /** |
| 150 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled |
| 151 | * |
| 152 | * Description: |
| 153 | * This function checks the SECMARK reference counter to see if any SECMARK |
| 154 | * targets are currently configured, if the reference counter is greater than |
| 155 | * 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] | 156 | * enabled, false (0) if SECMARK is disabled. If the always_check_network |
| 157 | * policy capability is enabled, SECMARK is always considered enabled. |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 158 | * |
| 159 | */ |
| 160 | static int selinux_secmark_enabled(void) |
| 161 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 162 | return (selinux_policycap_alwaysnetwork() || |
| 163 | atomic_read(&selinux_secmark_refcount)); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled |
| 168 | * |
| 169 | * Description: |
| 170 | * This function checks if NetLabel or labeled IPSEC is enabled. Returns true |
| 171 | * (1) if any are enabled or false (0) if neither are enabled. If the |
| 172 | * always_check_network policy capability is enabled, peer labeling |
| 173 | * is always considered enabled. |
| 174 | * |
| 175 | */ |
| 176 | static int selinux_peerlbl_enabled(void) |
| 177 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 178 | return (selinux_policycap_alwaysnetwork() || |
| 179 | netlbl_enabled() || selinux_xfrm_enabled()); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 182 | static int selinux_netcache_avc_callback(u32 event) |
| 183 | { |
| 184 | if (event == AVC_CALLBACK_RESET) { |
| 185 | sel_netif_flush(); |
| 186 | sel_netnode_flush(); |
| 187 | sel_netport_flush(); |
| 188 | synchronize_net(); |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 193 | static int selinux_lsm_notifier_avc_callback(u32 event) |
| 194 | { |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 195 | if (event == AVC_CALLBACK_RESET) { |
| 196 | sel_ib_pkey_flush(); |
Janne Karhunen | 42df744 | 2019-06-14 15:20:14 +0300 | [diff] [blame] | 197 | call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 198 | } |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 203 | /* |
| 204 | * initialise the security for the init task |
| 205 | */ |
| 206 | static void cred_init_security(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 208 | struct cred *cred = (struct cred *) current->real_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | struct task_security_struct *tsec; |
| 210 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 211 | tsec = selinux_cred(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 212 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 215 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 216 | * get the security ID of a set of credentials |
| 217 | */ |
| 218 | static inline u32 cred_sid(const struct cred *cred) |
| 219 | { |
| 220 | const struct task_security_struct *tsec; |
| 221 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 222 | tsec = selinux_cred(cred); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 223 | return tsec->sid; |
| 224 | } |
| 225 | |
| 226 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 227 | * get the objective security ID of a task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 228 | */ |
| 229 | static inline u32 task_sid(const struct task_struct *task) |
| 230 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 231 | u32 sid; |
| 232 | |
| 233 | rcu_read_lock(); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 234 | sid = cred_sid(__task_cred(task)); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 235 | rcu_read_unlock(); |
| 236 | return sid; |
| 237 | } |
| 238 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 239 | /* Allocate and free functions for each kind of security blob. */ |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | static int inode_alloc_security(struct inode *inode) |
| 242 | { |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 243 | struct inode_security_struct *isec = selinux_inode(inode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 244 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 246 | spin_lock_init(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | INIT_LIST_HEAD(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | isec->inode = inode; |
| 249 | isec->sid = SECINITSID_UNLABELED; |
| 250 | isec->sclass = SECCLASS_FILE; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 251 | isec->task_sid = sid; |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 252 | isec->initialized = LABEL_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 257 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); |
| 258 | |
| 259 | /* |
| 260 | * Try reloading inode security labels that have been marked as invalid. The |
| 261 | * @may_sleep parameter indicates when sleeping and thus reloading labels is |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 262 | * allowed; when set to false, returns -ECHILD when the label is |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 263 | * 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] | 264 | */ |
| 265 | static int __inode_security_revalidate(struct inode *inode, |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 266 | struct dentry *dentry, |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 267 | bool may_sleep) |
| 268 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 269 | struct inode_security_struct *isec = selinux_inode(inode); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 270 | |
| 271 | might_sleep_if(may_sleep); |
| 272 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 273 | if (selinux_state.initialized && |
| 274 | isec->initialized != LABEL_INITIALIZED) { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 275 | if (!may_sleep) |
| 276 | return -ECHILD; |
| 277 | |
| 278 | /* |
| 279 | * Try reloading the inode security label. This will fail if |
| 280 | * @opt_dentry is NULL and no dentry for this inode can be |
| 281 | * found; in that case, continue using the old label. |
| 282 | */ |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 283 | inode_doinit_with_dentry(inode, dentry); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 284 | } |
| 285 | return 0; |
| 286 | } |
| 287 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 288 | static struct inode_security_struct *inode_security_novalidate(struct inode *inode) |
| 289 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 290 | return selinux_inode(inode); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) |
| 294 | { |
| 295 | int error; |
| 296 | |
| 297 | error = __inode_security_revalidate(inode, NULL, !rcu); |
| 298 | if (error) |
| 299 | return ERR_PTR(error); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 300 | return selinux_inode(inode); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 303 | /* |
| 304 | * Get the security label of an inode. |
| 305 | */ |
| 306 | static struct inode_security_struct *inode_security(struct inode *inode) |
| 307 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 308 | __inode_security_revalidate(inode, NULL, true); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 309 | return selinux_inode(inode); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 312 | static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) |
| 313 | { |
| 314 | struct inode *inode = d_backing_inode(dentry); |
| 315 | |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 316 | return selinux_inode(inode); |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 319 | /* |
| 320 | * Get the security label of a dentry's backing inode. |
| 321 | */ |
| 322 | static struct inode_security_struct *backing_inode_security(struct dentry *dentry) |
| 323 | { |
| 324 | struct inode *inode = d_backing_inode(dentry); |
| 325 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 326 | __inode_security_revalidate(inode, dentry, true); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 327 | return selinux_inode(inode); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | static void inode_free_security(struct inode *inode) |
| 331 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 332 | struct inode_security_struct *isec = selinux_inode(inode); |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 333 | struct superblock_security_struct *sbsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 335 | if (!isec) |
| 336 | return; |
| 337 | sbsec = inode->i_sb->s_security; |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 338 | /* |
| 339 | * As not all inode security structures are in a list, we check for |
| 340 | * empty list outside of the lock to make sure that we won't waste |
| 341 | * time taking a lock doing nothing. |
| 342 | * |
| 343 | * The list_del_init() function can be safely called more than once. |
| 344 | * It should not be possible for this function to be called with |
| 345 | * concurrent list_add(), but for better safety against future changes |
| 346 | * in the code, we use list_empty_careful() here. |
| 347 | */ |
| 348 | if (!list_empty_careful(&isec->list)) { |
| 349 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | list_del_init(&isec->list); |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 351 | spin_unlock(&sbsec->isec_lock); |
| 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int file_alloc_security(struct file *file) |
| 356 | { |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame] | 357 | struct file_security_struct *fsec = selinux_file(file); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 358 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 360 | fsec->sid = sid; |
| 361 | fsec->fown_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | static int superblock_alloc_security(struct super_block *sb) |
| 367 | { |
| 368 | struct superblock_security_struct *sbsec; |
| 369 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 370 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (!sbsec) |
| 372 | return -ENOMEM; |
| 373 | |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 374 | mutex_init(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | INIT_LIST_HEAD(&sbsec->isec_head); |
| 376 | spin_lock_init(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | sbsec->sb = sb; |
| 378 | sbsec->sid = SECINITSID_UNLABELED; |
| 379 | sbsec->def_sid = SECINITSID_FILE; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 380 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | sb->s_security = sbsec; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static void superblock_free_security(struct super_block *sb) |
| 387 | { |
| 388 | struct superblock_security_struct *sbsec = sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | sb->s_security = NULL; |
| 390 | kfree(sbsec); |
| 391 | } |
| 392 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 393 | struct selinux_mnt_opts { |
| 394 | const char *fscontext, *context, *rootcontext, *defcontext; |
| 395 | }; |
| 396 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 397 | static void selinux_free_mnt_opts(void *mnt_opts) |
| 398 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 399 | struct selinux_mnt_opts *opts = mnt_opts; |
| 400 | kfree(opts->fscontext); |
| 401 | kfree(opts->context); |
| 402 | kfree(opts->rootcontext); |
| 403 | kfree(opts->defcontext); |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 404 | kfree(opts); |
| 405 | } |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | static inline int inode_doinit(struct inode *inode) |
| 408 | { |
| 409 | return inode_doinit_with_dentry(inode, NULL); |
| 410 | } |
| 411 | |
| 412 | enum { |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 413 | Opt_error = -1, |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 414 | Opt_context = 0, |
| 415 | Opt_defcontext = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | Opt_fscontext = 2, |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 417 | Opt_rootcontext = 3, |
| 418 | Opt_seclabel = 4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | }; |
| 420 | |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 421 | #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 422 | static struct { |
| 423 | const char *name; |
| 424 | int len; |
| 425 | int opt; |
| 426 | bool has_arg; |
| 427 | } tokens[] = { |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 428 | A(context, true), |
| 429 | A(fscontext, true), |
| 430 | A(defcontext, true), |
| 431 | A(rootcontext, true), |
| 432 | A(seclabel, false), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | }; |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 434 | #undef A |
| 435 | |
| 436 | static int match_opt_prefix(char *s, int l, char **arg) |
| 437 | { |
| 438 | int i; |
| 439 | |
| 440 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 441 | size_t len = tokens[i].len; |
| 442 | if (len > l || memcmp(s, tokens[i].name, len)) |
| 443 | continue; |
| 444 | if (tokens[i].has_arg) { |
| 445 | if (len == l || s[len] != '=') |
| 446 | continue; |
| 447 | *arg = s + len + 1; |
| 448 | } else if (len != l) |
| 449 | continue; |
| 450 | return tokens[i].opt; |
| 451 | } |
| 452 | return Opt_error; |
| 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" |
| 456 | |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 457 | static int may_context_mount_sb_relabel(u32 sid, |
| 458 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 459 | const struct cred *cred) |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 460 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 461 | const struct task_security_struct *tsec = selinux_cred(cred); |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 462 | int rc; |
| 463 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 464 | rc = avc_has_perm(&selinux_state, |
| 465 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 466 | FILESYSTEM__RELABELFROM, NULL); |
| 467 | if (rc) |
| 468 | return rc; |
| 469 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 470 | rc = avc_has_perm(&selinux_state, |
| 471 | tsec->sid, sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 472 | FILESYSTEM__RELABELTO, NULL); |
| 473 | return rc; |
| 474 | } |
| 475 | |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 476 | static int may_context_mount_inode_relabel(u32 sid, |
| 477 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 478 | const struct cred *cred) |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 479 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 480 | const struct task_security_struct *tsec = selinux_cred(cred); |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 481 | int rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 482 | rc = avc_has_perm(&selinux_state, |
| 483 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 484 | FILESYSTEM__RELABELFROM, NULL); |
| 485 | if (rc) |
| 486 | return rc; |
| 487 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 488 | rc = avc_has_perm(&selinux_state, |
| 489 | sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 490 | FILESYSTEM__ASSOCIATE, NULL); |
| 491 | return rc; |
| 492 | } |
| 493 | |
Ondrej Mosnacek | a83d6dd | 2018-12-21 21:18:52 +0100 | [diff] [blame] | 494 | static int selinux_is_genfs_special_handling(struct super_block *sb) |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 495 | { |
Ondrej Mosnacek | a83d6dd | 2018-12-21 21:18:52 +0100 | [diff] [blame] | 496 | /* Special handling. Genfs but also in-core setxattr handler */ |
| 497 | return !strcmp(sb->s_type->name, "sysfs") || |
Mark Salyzyn | d5f3a5f | 2015-02-04 11:34:30 -0500 | [diff] [blame] | 498 | !strcmp(sb->s_type->name, "pstore") || |
| 499 | !strcmp(sb->s_type->name, "debugfs") || |
Yongqin Liu | a2c7c6f | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 500 | !strcmp(sb->s_type->name, "tracefs") || |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 501 | !strcmp(sb->s_type->name, "rootfs") || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 502 | (selinux_policycap_cgroupseclabel() && |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 503 | (!strcmp(sb->s_type->name, "cgroup") || |
| 504 | !strcmp(sb->s_type->name, "cgroup2"))); |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Ondrej Mosnacek | a83d6dd | 2018-12-21 21:18:52 +0100 | [diff] [blame] | 507 | static int selinux_is_sblabel_mnt(struct super_block *sb) |
| 508 | { |
| 509 | struct superblock_security_struct *sbsec = sb->s_security; |
| 510 | |
| 511 | /* |
| 512 | * IMPORTANT: Double-check logic in this function when adding a new |
| 513 | * SECURITY_FS_USE_* definition! |
| 514 | */ |
| 515 | BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7); |
| 516 | |
| 517 | switch (sbsec->behavior) { |
| 518 | case SECURITY_FS_USE_XATTR: |
| 519 | case SECURITY_FS_USE_TRANS: |
| 520 | case SECURITY_FS_USE_TASK: |
| 521 | case SECURITY_FS_USE_NATIVE: |
| 522 | return 1; |
| 523 | |
| 524 | case SECURITY_FS_USE_GENFS: |
| 525 | return selinux_is_genfs_special_handling(sb); |
| 526 | |
| 527 | /* Never allow relabeling on context mounts */ |
| 528 | case SECURITY_FS_USE_MNTPOINT: |
| 529 | case SECURITY_FS_USE_NONE: |
| 530 | default: |
| 531 | return 0; |
| 532 | } |
| 533 | } |
| 534 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 535 | static int sb_finish_set_opts(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
| 537 | struct superblock_security_struct *sbsec = sb->s_security; |
| 538 | struct dentry *root = sb->s_root; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 539 | struct inode *root_inode = d_backing_inode(root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | int rc = 0; |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 543 | /* Make sure that the xattr handler exists and that no |
| 544 | error other than -ENODATA is returned by getxattr on |
| 545 | the root directory. -ENODATA is ok, as this may be |
| 546 | the first boot of the SELinux kernel before we have |
| 547 | assigned xattr values to the filesystem. */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 548 | if (!(root_inode->i_opflags & IOP_XATTR)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 549 | pr_warn("SELinux: (dev %s, type %s) has no " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 550 | "xattr support\n", sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | rc = -EOPNOTSUPP; |
| 552 | goto out; |
| 553 | } |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 554 | |
| 555 | rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (rc < 0 && rc != -ENODATA) { |
| 557 | if (rc == -EOPNOTSUPP) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 558 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 559 | "%s) has no security xattr handler\n", |
| 560 | sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 562 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 563 | "%s) getxattr errno %d\n", sb->s_id, |
| 564 | sb->s_type->name, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | goto out; |
| 566 | } |
| 567 | } |
| 568 | |
Eric Paris | eadcabc | 2012-08-24 15:59:14 -0400 | [diff] [blame] | 569 | sbsec->flags |= SE_SBINITIALIZED; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply |
| 573 | * leave the flag untouched because sb_clone_mnt_opts might be handing |
| 574 | * us a superblock that needs the flag to be cleared. |
| 575 | */ |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 576 | if (selinux_is_sblabel_mnt(sb)) |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 577 | sbsec->flags |= SBLABEL_MNT; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 578 | else |
| 579 | sbsec->flags &= ~SBLABEL_MNT; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | /* Initialize the root inode. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 582 | rc = inode_doinit_with_dentry(root_inode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | /* Initialize any other inodes associated with the superblock, e.g. |
| 585 | inodes created prior to initial policy load or inodes created |
| 586 | during get_sb by a pseudo filesystem that directly |
| 587 | populates itself. */ |
| 588 | spin_lock(&sbsec->isec_lock); |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 589 | while (!list_empty(&sbsec->isec_head)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | struct inode_security_struct *isec = |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 591 | list_first_entry(&sbsec->isec_head, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 592 | struct inode_security_struct, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | struct inode *inode = isec->inode; |
Stephen Smalley | 923190d | 2014-10-06 16:32:52 -0400 | [diff] [blame] | 594 | list_del_init(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | spin_unlock(&sbsec->isec_lock); |
| 596 | inode = igrab(inode); |
| 597 | if (inode) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 598 | if (!IS_PRIVATE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | inode_doinit(inode); |
| 600 | iput(inode); |
| 601 | } |
| 602 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | spin_unlock(&sbsec->isec_lock); |
| 605 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 606 | return rc; |
| 607 | } |
| 608 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 609 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
| 610 | u32 old_sid, u32 new_sid) |
| 611 | { |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 612 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
| 613 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 614 | /* check if the old mount command had the same options */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 615 | if (sbsec->flags & SE_SBINITIALIZED) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 616 | if (!(sbsec->flags & flag) || |
| 617 | (old_sid != new_sid)) |
| 618 | return 1; |
| 619 | |
| 620 | /* check if we were passed the same options twice, |
| 621 | * aka someone passed context=a,context=b |
| 622 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 623 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 624 | if (mnt_flags & flag) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 625 | return 1; |
| 626 | return 0; |
| 627 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 628 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 629 | static int parse_sid(struct super_block *sb, const char *s, u32 *sid) |
| 630 | { |
| 631 | int rc = security_context_str_to_sid(&selinux_state, s, |
| 632 | sid, GFP_KERNEL); |
| 633 | if (rc) |
| 634 | pr_warn("SELinux: security_context_str_to_sid" |
| 635 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 636 | s, sb->s_id, sb->s_type->name, rc); |
| 637 | return rc; |
| 638 | } |
| 639 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 640 | /* |
| 641 | * Allow filesystems with binary mount data to explicitly set mount point |
| 642 | * labeling information. |
| 643 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 644 | static int selinux_set_mnt_opts(struct super_block *sb, |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 645 | void *mnt_opts, |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 646 | unsigned long kern_flags, |
| 647 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 648 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 649 | const struct cred *cred = current_cred(); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 650 | struct superblock_security_struct *sbsec = sb->s_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 651 | struct dentry *root = sbsec->sb->s_root; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 652 | struct selinux_mnt_opts *opts = mnt_opts; |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 653 | struct inode_security_struct *root_isec; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 654 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
| 655 | u32 defcontext_sid = 0; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 656 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 657 | |
| 658 | mutex_lock(&sbsec->lock); |
| 659 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 660 | if (!selinux_state.initialized) { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 661 | if (!opts) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 662 | /* Defer initialization until selinux_complete_init, |
| 663 | after the initial policy is loaded and the security |
| 664 | server is ready to handle calls. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 665 | goto out; |
| 666 | } |
| 667 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 668 | pr_warn("SELinux: Unable to set superblock options " |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 669 | "before the security server is initialized\n"); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 670 | goto out; |
| 671 | } |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 672 | if (kern_flags && !set_kern_flags) { |
| 673 | /* Specifying internal flags without providing a place to |
| 674 | * place the results is not allowed */ |
| 675 | rc = -EINVAL; |
| 676 | goto out; |
| 677 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 678 | |
| 679 | /* |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 680 | * Binary mount data FS will come through this function twice. Once |
| 681 | * from an explicit call and once from the generic calls from the vfs. |
| 682 | * Since the generic VFS calls will not contain any security mount data |
| 683 | * we need to skip the double mount verification. |
| 684 | * |
| 685 | * This does open a hole in which we will not notice if the first |
| 686 | * mount using this sb set explict options and a second mount using |
| 687 | * this sb does not set any security options. (The first options |
| 688 | * will be used for both mounts) |
| 689 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 690 | 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] | 691 | && !opts) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 692 | goto out; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 693 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 694 | root_isec = backing_inode_security_novalidate(root); |
| 695 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 696 | /* |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 697 | * parse the mount options, check if they are valid sids. |
| 698 | * also check if someone is trying to mount the same sb more |
| 699 | * than once with different security options. |
| 700 | */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 701 | if (opts) { |
| 702 | if (opts->fscontext) { |
| 703 | rc = parse_sid(sb, opts->fscontext, &fscontext_sid); |
| 704 | if (rc) |
| 705 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 706 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, |
| 707 | fscontext_sid)) |
| 708 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 709 | sbsec->flags |= FSCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 710 | } |
| 711 | if (opts->context) { |
| 712 | rc = parse_sid(sb, opts->context, &context_sid); |
| 713 | if (rc) |
| 714 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 715 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, |
| 716 | context_sid)) |
| 717 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 718 | sbsec->flags |= CONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 719 | } |
| 720 | if (opts->rootcontext) { |
| 721 | rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); |
| 722 | if (rc) |
| 723 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 724 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, |
| 725 | rootcontext_sid)) |
| 726 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 727 | sbsec->flags |= ROOTCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 728 | } |
| 729 | if (opts->defcontext) { |
| 730 | rc = parse_sid(sb, opts->defcontext, &defcontext_sid); |
| 731 | if (rc) |
| 732 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 733 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, |
| 734 | defcontext_sid)) |
| 735 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 736 | sbsec->flags |= DEFCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 740 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 741 | /* previously mounted with options, but not on this attempt? */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 742 | if ((sbsec->flags & SE_MNTMASK) && !opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 743 | goto out_double_mount; |
| 744 | rc = 0; |
| 745 | goto out; |
| 746 | } |
| 747 | |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 748 | if (strcmp(sb->s_type->name, "proc") == 0) |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 749 | sbsec->flags |= SE_SBPROC | SE_SBGENFS; |
| 750 | |
Stephen Smalley | 8e01472 | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 751 | if (!strcmp(sb->s_type->name, "debugfs") || |
Jeff Vander Stoep | 6a39118 | 2017-06-20 09:35:33 -0700 | [diff] [blame] | 752 | !strcmp(sb->s_type->name, "tracefs") || |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 753 | !strcmp(sb->s_type->name, "pstore")) |
| 754 | sbsec->flags |= SE_SBGENFS; |
| 755 | |
| 756 | if (!strcmp(sb->s_type->name, "sysfs") || |
Antonio Murdaca | 901ef84 | 2017-02-09 17:02:42 +0100 | [diff] [blame] | 757 | !strcmp(sb->s_type->name, "cgroup") || |
| 758 | !strcmp(sb->s_type->name, "cgroup2")) |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 759 | sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 760 | |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 761 | if (!sbsec->behavior) { |
| 762 | /* |
| 763 | * Determine the labeling behavior to use for this |
| 764 | * filesystem type. |
| 765 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 766 | rc = security_fs_use(&selinux_state, sb); |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 767 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 768 | pr_warn("%s: security_fs_use(%s) returned %d\n", |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 769 | __func__, sb->s_type->name, rc); |
| 770 | goto out; |
| 771 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 772 | } |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 773 | |
| 774 | /* |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 775 | * If this is a user namespace mount and the filesystem type is not |
| 776 | * explicitly whitelisted, then no contexts are allowed on the command |
| 777 | * line and security labels must be ignored. |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 778 | */ |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 779 | if (sb->s_user_ns != &init_user_ns && |
| 780 | strcmp(sb->s_type->name, "tmpfs") && |
| 781 | strcmp(sb->s_type->name, "ramfs") && |
| 782 | strcmp(sb->s_type->name, "devpts")) { |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 783 | if (context_sid || fscontext_sid || rootcontext_sid || |
| 784 | defcontext_sid) { |
| 785 | rc = -EACCES; |
| 786 | goto out; |
| 787 | } |
| 788 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 789 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 790 | rc = security_transition_sid(&selinux_state, |
| 791 | current_sid(), |
| 792 | current_sid(), |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 793 | SECCLASS_FILE, NULL, |
| 794 | &sbsec->mntpoint_sid); |
| 795 | if (rc) |
| 796 | goto out; |
| 797 | } |
| 798 | goto out_set_opts; |
| 799 | } |
| 800 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 801 | /* sets the context of the superblock for the fs being mounted. */ |
| 802 | if (fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 803 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 804 | if (rc) |
| 805 | goto out; |
| 806 | |
| 807 | sbsec->sid = fscontext_sid; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Switch to using mount point labeling behavior. |
| 812 | * sets the label used on all file below the mountpoint, and will set |
| 813 | * the superblock context if not already set. |
| 814 | */ |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 815 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { |
| 816 | sbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 817 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 818 | } |
| 819 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 820 | if (context_sid) { |
| 821 | if (!fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 822 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
| 823 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 824 | if (rc) |
| 825 | goto out; |
| 826 | sbsec->sid = context_sid; |
| 827 | } else { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 828 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
| 829 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 830 | if (rc) |
| 831 | goto out; |
| 832 | } |
| 833 | if (!rootcontext_sid) |
| 834 | rootcontext_sid = context_sid; |
| 835 | |
| 836 | sbsec->mntpoint_sid = context_sid; |
| 837 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
| 838 | } |
| 839 | |
| 840 | if (rootcontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 841 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
| 842 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 843 | if (rc) |
| 844 | goto out; |
| 845 | |
| 846 | root_isec->sid = rootcontext_sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 847 | root_isec->initialized = LABEL_INITIALIZED; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | if (defcontext_sid) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 851 | if (sbsec->behavior != SECURITY_FS_USE_XATTR && |
| 852 | sbsec->behavior != SECURITY_FS_USE_NATIVE) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 853 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 854 | pr_warn("SELinux: defcontext option is " |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 855 | "invalid for this filesystem type\n"); |
| 856 | goto out; |
| 857 | } |
| 858 | |
| 859 | if (defcontext_sid != sbsec->def_sid) { |
| 860 | rc = may_context_mount_inode_relabel(defcontext_sid, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 861 | sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 862 | if (rc) |
| 863 | goto out; |
| 864 | } |
| 865 | |
| 866 | sbsec->def_sid = defcontext_sid; |
| 867 | } |
| 868 | |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 869 | out_set_opts: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 870 | rc = sb_finish_set_opts(sb); |
| 871 | out: |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 872 | mutex_unlock(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 874 | out_double_mount: |
| 875 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 876 | pr_warn("SELinux: mount invalid. Same superblock, different " |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 877 | "security settings for (dev %s, type %s)\n", sb->s_id, |
| 878 | sb->s_type->name); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 879 | goto out; |
| 880 | } |
| 881 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 882 | static int selinux_cmp_sb_context(const struct super_block *oldsb, |
| 883 | const struct super_block *newsb) |
| 884 | { |
| 885 | struct superblock_security_struct *old = oldsb->s_security; |
| 886 | struct superblock_security_struct *new = newsb->s_security; |
| 887 | char oldflags = old->flags & SE_MNTMASK; |
| 888 | char newflags = new->flags & SE_MNTMASK; |
| 889 | |
| 890 | if (oldflags != newflags) |
| 891 | goto mismatch; |
| 892 | if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) |
| 893 | goto mismatch; |
| 894 | if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) |
| 895 | goto mismatch; |
| 896 | if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) |
| 897 | goto mismatch; |
| 898 | if (oldflags & ROOTCONTEXT_MNT) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 899 | struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); |
| 900 | struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 901 | if (oldroot->sid != newroot->sid) |
| 902 | goto mismatch; |
| 903 | } |
| 904 | return 0; |
| 905 | mismatch: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 906 | pr_warn("SELinux: mount invalid. Same superblock, " |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 907 | "different security settings for (dev %s, " |
| 908 | "type %s)\n", newsb->s_id, newsb->s_type->name); |
| 909 | return -EBUSY; |
| 910 | } |
| 911 | |
| 912 | static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 913 | struct super_block *newsb, |
| 914 | unsigned long kern_flags, |
| 915 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 916 | { |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 917 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 918 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
| 919 | struct superblock_security_struct *newsbsec = newsb->s_security; |
| 920 | |
| 921 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 922 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| 923 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); |
| 924 | |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 925 | /* |
| 926 | * 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] | 927 | * mount options. thus we can safely deal with this superblock later |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 928 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 929 | if (!selinux_state.initialized) |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 930 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 931 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 932 | /* |
| 933 | * Specifying internal flags without providing a place to |
| 934 | * place the results is not allowed. |
| 935 | */ |
| 936 | if (kern_flags && !set_kern_flags) |
| 937 | return -EINVAL; |
| 938 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 939 | /* 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] | 940 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 941 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 942 | /* if fs is reusing a sb, make sure that the contexts match */ |
J. Bruce Fields | 3815a24 | 2019-03-05 16:17:58 -0500 | [diff] [blame] | 943 | if (newsbsec->flags & SE_SBINITIALIZED) { |
| 944 | if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) |
| 945 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 946 | return selinux_cmp_sb_context(oldsb, newsb); |
J. Bruce Fields | 3815a24 | 2019-03-05 16:17:58 -0500 | [diff] [blame] | 947 | } |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 948 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 949 | mutex_lock(&newsbsec->lock); |
| 950 | |
| 951 | newsbsec->flags = oldsbsec->flags; |
| 952 | |
| 953 | newsbsec->sid = oldsbsec->sid; |
| 954 | newsbsec->def_sid = oldsbsec->def_sid; |
| 955 | newsbsec->behavior = oldsbsec->behavior; |
| 956 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 957 | if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && |
| 958 | !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 959 | rc = security_fs_use(&selinux_state, newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 960 | if (rc) |
| 961 | goto out; |
| 962 | } |
| 963 | |
| 964 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { |
| 965 | newsbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 966 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 967 | } |
| 968 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 969 | if (set_context) { |
| 970 | u32 sid = oldsbsec->mntpoint_sid; |
| 971 | |
| 972 | if (!set_fscontext) |
| 973 | newsbsec->sid = sid; |
| 974 | if (!set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 975 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 976 | newisec->sid = sid; |
| 977 | } |
| 978 | newsbsec->mntpoint_sid = sid; |
| 979 | } |
| 980 | if (set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 981 | const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); |
| 982 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 983 | |
| 984 | newisec->sid = oldisec->sid; |
| 985 | } |
| 986 | |
| 987 | sb_finish_set_opts(newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 988 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 989 | mutex_unlock(&newsbsec->lock); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 990 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 991 | } |
| 992 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 993 | static int selinux_add_opt(int token, const char *s, void **mnt_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 994 | { |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 995 | struct selinux_mnt_opts *opts = *mnt_opts; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 996 | |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 997 | if (token == Opt_seclabel) /* eaten and completely ignored */ |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 998 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 999 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1000 | if (!opts) { |
| 1001 | opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); |
| 1002 | if (!opts) |
| 1003 | return -ENOMEM; |
| 1004 | *mnt_opts = opts; |
| 1005 | } |
| 1006 | if (!s) |
| 1007 | return -ENOMEM; |
| 1008 | switch (token) { |
| 1009 | case Opt_context: |
| 1010 | if (opts->context || opts->defcontext) |
| 1011 | goto Einval; |
| 1012 | opts->context = s; |
| 1013 | break; |
| 1014 | case Opt_fscontext: |
| 1015 | if (opts->fscontext) |
| 1016 | goto Einval; |
| 1017 | opts->fscontext = s; |
| 1018 | break; |
| 1019 | case Opt_rootcontext: |
| 1020 | if (opts->rootcontext) |
| 1021 | goto Einval; |
| 1022 | opts->rootcontext = s; |
| 1023 | break; |
| 1024 | case Opt_defcontext: |
| 1025 | if (opts->context || opts->defcontext) |
| 1026 | goto Einval; |
| 1027 | opts->defcontext = s; |
| 1028 | break; |
| 1029 | } |
| 1030 | return 0; |
| 1031 | Einval: |
| 1032 | pr_warn(SEL_MOUNT_FAIL_MSG); |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1033 | return -EINVAL; |
| 1034 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1035 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1036 | static int selinux_add_mnt_opt(const char *option, const char *val, int len, |
| 1037 | void **mnt_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1038 | { |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1039 | int token = Opt_error; |
| 1040 | int rc, i; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1041 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1042 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 1043 | if (strcmp(option, tokens[i].name) == 0) { |
| 1044 | token = tokens[i].opt; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1045 | break; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1049 | if (token == Opt_error) |
| 1050 | return -EINVAL; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 1051 | |
Gen Zhang | e2e0e09 | 2019-06-12 21:28:21 +0800 | [diff] [blame] | 1052 | if (token != Opt_seclabel) { |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1053 | val = kmemdup_nul(val, len, GFP_KERNEL); |
Gen Zhang | e2e0e09 | 2019-06-12 21:28:21 +0800 | [diff] [blame] | 1054 | if (!val) { |
| 1055 | rc = -ENOMEM; |
| 1056 | goto free_opt; |
| 1057 | } |
| 1058 | } |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1059 | rc = selinux_add_opt(token, val, mnt_opts); |
| 1060 | if (unlikely(rc)) { |
| 1061 | kfree(val); |
Gen Zhang | e2e0e09 | 2019-06-12 21:28:21 +0800 | [diff] [blame] | 1062 | goto free_opt; |
| 1063 | } |
| 1064 | return rc; |
| 1065 | |
| 1066 | free_opt: |
| 1067 | if (*mnt_opts) { |
| 1068 | selinux_free_mnt_opts(*mnt_opts); |
| 1069 | *mnt_opts = NULL; |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1070 | } |
| 1071 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1074 | static int show_sid(struct seq_file *m, u32 sid) |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1075 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1076 | char *context = NULL; |
| 1077 | u32 len; |
| 1078 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1080 | rc = security_sid_to_context(&selinux_state, sid, |
| 1081 | &context, &len); |
| 1082 | if (!rc) { |
| 1083 | bool has_comma = context && strchr(context, ','); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 1085 | seq_putc(m, '='); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1086 | if (has_comma) |
| 1087 | seq_putc(m, '\"'); |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1088 | seq_escape(m, context, "\"\n\\"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1089 | if (has_comma) |
| 1090 | seq_putc(m, '\"'); |
| 1091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | kfree(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | return rc; |
| 1094 | } |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1095 | |
| 1096 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1097 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1098 | struct superblock_security_struct *sbsec = sb->s_security; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1099 | int rc; |
| 1100 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1101 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 1102 | return 0; |
| 1103 | |
| 1104 | if (!selinux_state.initialized) |
| 1105 | return 0; |
| 1106 | |
| 1107 | if (sbsec->flags & FSCONTEXT_MNT) { |
| 1108 | seq_putc(m, ','); |
| 1109 | seq_puts(m, FSCONTEXT_STR); |
| 1110 | rc = show_sid(m, sbsec->sid); |
| 1111 | if (rc) |
| 1112 | return rc; |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1113 | } |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1114 | if (sbsec->flags & CONTEXT_MNT) { |
| 1115 | seq_putc(m, ','); |
| 1116 | seq_puts(m, CONTEXT_STR); |
| 1117 | rc = show_sid(m, sbsec->mntpoint_sid); |
| 1118 | if (rc) |
| 1119 | return rc; |
| 1120 | } |
| 1121 | if (sbsec->flags & DEFCONTEXT_MNT) { |
| 1122 | seq_putc(m, ','); |
| 1123 | seq_puts(m, DEFCONTEXT_STR); |
| 1124 | rc = show_sid(m, sbsec->def_sid); |
| 1125 | if (rc) |
| 1126 | return rc; |
| 1127 | } |
| 1128 | if (sbsec->flags & ROOTCONTEXT_MNT) { |
| 1129 | struct dentry *root = sbsec->sb->s_root; |
| 1130 | struct inode_security_struct *isec = backing_inode_security(root); |
| 1131 | seq_putc(m, ','); |
| 1132 | seq_puts(m, ROOTCONTEXT_STR); |
| 1133 | rc = show_sid(m, isec->sid); |
| 1134 | if (rc) |
| 1135 | return rc; |
| 1136 | } |
| 1137 | if (sbsec->flags & SBLABEL_MNT) { |
| 1138 | seq_putc(m, ','); |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 1139 | seq_puts(m, SECLABEL_STR); |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1140 | } |
| 1141 | return 0; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1142 | } |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | static inline u16 inode_mode_to_security_class(umode_t mode) |
| 1145 | { |
| 1146 | switch (mode & S_IFMT) { |
| 1147 | case S_IFSOCK: |
| 1148 | return SECCLASS_SOCK_FILE; |
| 1149 | case S_IFLNK: |
| 1150 | return SECCLASS_LNK_FILE; |
| 1151 | case S_IFREG: |
| 1152 | return SECCLASS_FILE; |
| 1153 | case S_IFBLK: |
| 1154 | return SECCLASS_BLK_FILE; |
| 1155 | case S_IFDIR: |
| 1156 | return SECCLASS_DIR; |
| 1157 | case S_IFCHR: |
| 1158 | return SECCLASS_CHR_FILE; |
| 1159 | case S_IFIFO: |
| 1160 | return SECCLASS_FIFO_FILE; |
| 1161 | |
| 1162 | } |
| 1163 | |
| 1164 | return SECCLASS_FILE; |
| 1165 | } |
| 1166 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1167 | static inline int default_protocol_stream(int protocol) |
| 1168 | { |
| 1169 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); |
| 1170 | } |
| 1171 | |
| 1172 | static inline int default_protocol_dgram(int protocol) |
| 1173 | { |
| 1174 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); |
| 1175 | } |
| 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
| 1178 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1179 | int extsockclass = selinux_policycap_extsockclass(); |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | switch (family) { |
| 1182 | case PF_UNIX: |
| 1183 | switch (type) { |
| 1184 | case SOCK_STREAM: |
| 1185 | case SOCK_SEQPACKET: |
| 1186 | return SECCLASS_UNIX_STREAM_SOCKET; |
| 1187 | case SOCK_DGRAM: |
Luis Ressel | 2a764b5 | 2017-07-25 15:13:41 -0400 | [diff] [blame] | 1188 | case SOCK_RAW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | return SECCLASS_UNIX_DGRAM_SOCKET; |
| 1190 | } |
| 1191 | break; |
| 1192 | case PF_INET: |
| 1193 | case PF_INET6: |
| 1194 | switch (type) { |
| 1195 | case SOCK_STREAM: |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1196 | case SOCK_SEQPACKET: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1197 | if (default_protocol_stream(protocol)) |
| 1198 | return SECCLASS_TCP_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1199 | else if (extsockclass && protocol == IPPROTO_SCTP) |
| 1200 | return SECCLASS_SCTP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1201 | else |
| 1202 | return SECCLASS_RAWIP_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | case SOCK_DGRAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1204 | if (default_protocol_dgram(protocol)) |
| 1205 | return SECCLASS_UDP_SOCKET; |
Stephen Smalley | ef37979 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 1206 | else if (extsockclass && (protocol == IPPROTO_ICMP || |
| 1207 | protocol == IPPROTO_ICMPV6)) |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1208 | return SECCLASS_ICMP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1209 | else |
| 1210 | return SECCLASS_RAWIP_SOCKET; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 1211 | case SOCK_DCCP: |
| 1212 | return SECCLASS_DCCP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1213 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | return SECCLASS_RAWIP_SOCKET; |
| 1215 | } |
| 1216 | break; |
| 1217 | case PF_NETLINK: |
| 1218 | switch (protocol) { |
| 1219 | case NETLINK_ROUTE: |
| 1220 | return SECCLASS_NETLINK_ROUTE_SOCKET; |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1221 | case NETLINK_SOCK_DIAG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
| 1223 | case NETLINK_NFLOG: |
| 1224 | return SECCLASS_NETLINK_NFLOG_SOCKET; |
| 1225 | case NETLINK_XFRM: |
| 1226 | return SECCLASS_NETLINK_XFRM_SOCKET; |
| 1227 | case NETLINK_SELINUX: |
| 1228 | return SECCLASS_NETLINK_SELINUX_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1229 | case NETLINK_ISCSI: |
| 1230 | return SECCLASS_NETLINK_ISCSI_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | case NETLINK_AUDIT: |
| 1232 | return SECCLASS_NETLINK_AUDIT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1233 | case NETLINK_FIB_LOOKUP: |
| 1234 | return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; |
| 1235 | case NETLINK_CONNECTOR: |
| 1236 | return SECCLASS_NETLINK_CONNECTOR_SOCKET; |
| 1237 | case NETLINK_NETFILTER: |
| 1238 | return SECCLASS_NETLINK_NETFILTER_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | case NETLINK_DNRTMSG: |
| 1240 | return SECCLASS_NETLINK_DNRT_SOCKET; |
James Morris | 0c9b794 | 2005-04-16 15:24:13 -0700 | [diff] [blame] | 1241 | case NETLINK_KOBJECT_UEVENT: |
| 1242 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1243 | case NETLINK_GENERIC: |
| 1244 | return SECCLASS_NETLINK_GENERIC_SOCKET; |
| 1245 | case NETLINK_SCSITRANSPORT: |
| 1246 | return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; |
| 1247 | case NETLINK_RDMA: |
| 1248 | return SECCLASS_NETLINK_RDMA_SOCKET; |
| 1249 | case NETLINK_CRYPTO: |
| 1250 | return SECCLASS_NETLINK_CRYPTO_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | default: |
| 1252 | return SECCLASS_NETLINK_SOCKET; |
| 1253 | } |
| 1254 | case PF_PACKET: |
| 1255 | return SECCLASS_PACKET_SOCKET; |
| 1256 | case PF_KEY: |
| 1257 | return SECCLASS_KEY_SOCKET; |
Christopher J. PeBenito | 3e3ff15 | 2006-06-09 00:25:03 -0700 | [diff] [blame] | 1258 | case PF_APPLETALK: |
| 1259 | return SECCLASS_APPLETALK_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1262 | if (extsockclass) { |
| 1263 | switch (family) { |
| 1264 | case PF_AX25: |
| 1265 | return SECCLASS_AX25_SOCKET; |
| 1266 | case PF_IPX: |
| 1267 | return SECCLASS_IPX_SOCKET; |
| 1268 | case PF_NETROM: |
| 1269 | return SECCLASS_NETROM_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1270 | case PF_ATMPVC: |
| 1271 | return SECCLASS_ATMPVC_SOCKET; |
| 1272 | case PF_X25: |
| 1273 | return SECCLASS_X25_SOCKET; |
| 1274 | case PF_ROSE: |
| 1275 | return SECCLASS_ROSE_SOCKET; |
| 1276 | case PF_DECnet: |
| 1277 | return SECCLASS_DECNET_SOCKET; |
| 1278 | case PF_ATMSVC: |
| 1279 | return SECCLASS_ATMSVC_SOCKET; |
| 1280 | case PF_RDS: |
| 1281 | return SECCLASS_RDS_SOCKET; |
| 1282 | case PF_IRDA: |
| 1283 | return SECCLASS_IRDA_SOCKET; |
| 1284 | case PF_PPPOX: |
| 1285 | return SECCLASS_PPPOX_SOCKET; |
| 1286 | case PF_LLC: |
| 1287 | return SECCLASS_LLC_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1288 | case PF_CAN: |
| 1289 | return SECCLASS_CAN_SOCKET; |
| 1290 | case PF_TIPC: |
| 1291 | return SECCLASS_TIPC_SOCKET; |
| 1292 | case PF_BLUETOOTH: |
| 1293 | return SECCLASS_BLUETOOTH_SOCKET; |
| 1294 | case PF_IUCV: |
| 1295 | return SECCLASS_IUCV_SOCKET; |
| 1296 | case PF_RXRPC: |
| 1297 | return SECCLASS_RXRPC_SOCKET; |
| 1298 | case PF_ISDN: |
| 1299 | return SECCLASS_ISDN_SOCKET; |
| 1300 | case PF_PHONET: |
| 1301 | return SECCLASS_PHONET_SOCKET; |
| 1302 | case PF_IEEE802154: |
| 1303 | return SECCLASS_IEEE802154_SOCKET; |
| 1304 | case PF_CAIF: |
| 1305 | return SECCLASS_CAIF_SOCKET; |
| 1306 | case PF_ALG: |
| 1307 | return SECCLASS_ALG_SOCKET; |
| 1308 | case PF_NFC: |
| 1309 | return SECCLASS_NFC_SOCKET; |
| 1310 | case PF_VSOCK: |
| 1311 | return SECCLASS_VSOCK_SOCKET; |
| 1312 | case PF_KCM: |
| 1313 | return SECCLASS_KCM_SOCKET; |
| 1314 | case PF_QIPCRTR: |
| 1315 | return SECCLASS_QIPCRTR_SOCKET; |
Linus Torvalds | 3051bf3 | 2017-02-22 10:15:09 -0800 | [diff] [blame] | 1316 | case PF_SMC: |
| 1317 | return SECCLASS_SMC_SOCKET; |
Björn Töpel | 68e8b84 | 2018-05-02 13:01:22 +0200 | [diff] [blame] | 1318 | case PF_XDP: |
| 1319 | return SECCLASS_XDP_SOCKET; |
| 1320 | #if PF_MAX > 45 |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1321 | #error New address family defined, please update this function. |
| 1322 | #endif |
| 1323 | } |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | return SECCLASS_SOCKET; |
| 1327 | } |
| 1328 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1329 | static int selinux_genfs_get_sid(struct dentry *dentry, |
| 1330 | u16 tclass, |
| 1331 | u16 flags, |
| 1332 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1334 | int rc; |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 1335 | struct super_block *sb = dentry->d_sb; |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1336 | char *buffer, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1338 | buffer = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | if (!buffer) |
| 1340 | return -ENOMEM; |
| 1341 | |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1342 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
| 1343 | if (IS_ERR(path)) |
| 1344 | rc = PTR_ERR(path); |
| 1345 | else { |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1346 | if (flags & SE_SBPROC) { |
| 1347 | /* each process gets a /proc/PID/ entry. Strip off the |
| 1348 | * PID part to get a valid selinux labeling. |
| 1349 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ |
| 1350 | while (path[1] >= '0' && path[1] <= '9') { |
| 1351 | path[1] = '/'; |
| 1352 | path++; |
| 1353 | } |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1354 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1355 | rc = security_genfs_sid(&selinux_state, sb->s_type->name, |
| 1356 | path, tclass, sid); |
Stephen Smalley | 7bb185e | 2018-09-04 16:51:36 -0400 | [diff] [blame] | 1357 | if (rc == -ENOENT) { |
| 1358 | /* No match in policy, mark as unlabeled. */ |
| 1359 | *sid = SECINITSID_UNLABELED; |
| 1360 | rc = 0; |
| 1361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | free_page((unsigned long)buffer); |
| 1364 | return rc; |
| 1365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1367 | static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry, |
| 1368 | u32 def_sid, u32 *sid) |
| 1369 | { |
| 1370 | #define INITCONTEXTLEN 255 |
| 1371 | char *context; |
| 1372 | unsigned int len; |
| 1373 | int rc; |
| 1374 | |
| 1375 | len = INITCONTEXTLEN; |
| 1376 | context = kmalloc(len + 1, GFP_NOFS); |
| 1377 | if (!context) |
| 1378 | return -ENOMEM; |
| 1379 | |
| 1380 | context[len] = '\0'; |
| 1381 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); |
| 1382 | if (rc == -ERANGE) { |
| 1383 | kfree(context); |
| 1384 | |
| 1385 | /* Need a larger buffer. Query for the right size. */ |
| 1386 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); |
| 1387 | if (rc < 0) |
| 1388 | return rc; |
| 1389 | |
| 1390 | len = rc; |
| 1391 | context = kmalloc(len + 1, GFP_NOFS); |
| 1392 | if (!context) |
| 1393 | return -ENOMEM; |
| 1394 | |
| 1395 | context[len] = '\0'; |
| 1396 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, |
| 1397 | context, len); |
| 1398 | } |
| 1399 | if (rc < 0) { |
| 1400 | kfree(context); |
| 1401 | if (rc != -ENODATA) { |
| 1402 | pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n", |
| 1403 | __func__, -rc, inode->i_sb->s_id, inode->i_ino); |
| 1404 | return rc; |
| 1405 | } |
| 1406 | *sid = def_sid; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | rc = security_context_to_sid_default(&selinux_state, context, rc, sid, |
| 1411 | def_sid, GFP_NOFS); |
| 1412 | if (rc) { |
| 1413 | char *dev = inode->i_sb->s_id; |
| 1414 | unsigned long ino = inode->i_ino; |
| 1415 | |
| 1416 | if (rc == -EINVAL) { |
| 1417 | pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n", |
| 1418 | ino, dev, context); |
| 1419 | } else { |
| 1420 | pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n", |
| 1421 | __func__, context, -rc, dev, ino); |
| 1422 | } |
| 1423 | } |
| 1424 | kfree(context); |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | /* The inode's security attributes must be initialized before first use. */ |
| 1429 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1430 | { |
| 1431 | struct superblock_security_struct *sbsec = NULL; |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 1432 | struct inode_security_struct *isec = selinux_inode(inode); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1433 | u32 task_sid, sid = 0; |
| 1434 | u16 sclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1438 | if (isec->initialized == LABEL_INITIALIZED) |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1439 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1441 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1442 | if (isec->initialized == LABEL_INITIALIZED) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1443 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1445 | if (isec->sclass == SECCLASS_FILE) |
| 1446 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 1447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | sbsec = inode->i_sb->s_security; |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1449 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | /* Defer initialization until selinux_complete_init, |
| 1451 | after the initial policy is loaded and the security |
| 1452 | server is ready to handle calls. */ |
| 1453 | spin_lock(&sbsec->isec_lock); |
| 1454 | if (list_empty(&isec->list)) |
| 1455 | list_add(&isec->list, &sbsec->isec_head); |
| 1456 | spin_unlock(&sbsec->isec_lock); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1457 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1460 | sclass = isec->sclass; |
| 1461 | task_sid = isec->task_sid; |
| 1462 | sid = isec->sid; |
| 1463 | isec->initialized = LABEL_PENDING; |
| 1464 | spin_unlock(&isec->lock); |
| 1465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | switch (sbsec->behavior) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 1467 | case SECURITY_FS_USE_NATIVE: |
| 1468 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | case SECURITY_FS_USE_XATTR: |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1470 | if (!(inode->i_opflags & IOP_XATTR)) { |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1471 | sid = sbsec->def_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | break; |
| 1473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | /* Need a dentry, since the xattr API requires one. |
| 1475 | Life would be simpler if we could just pass the inode. */ |
| 1476 | if (opt_dentry) { |
| 1477 | /* Called from d_instantiate or d_splice_alias. */ |
| 1478 | dentry = dget(opt_dentry); |
| 1479 | } else { |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1480 | /* |
| 1481 | * Called from selinux_complete_init, try to find a dentry. |
| 1482 | * Some filesystems really want a connected one, so try |
| 1483 | * that first. We could split SECURITY_FS_USE_XATTR in |
| 1484 | * two, depending upon that... |
| 1485 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1487 | if (!dentry) |
| 1488 | dentry = d_find_any_alias(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | } |
| 1490 | if (!dentry) { |
Eric Paris | df7f54c | 2009-03-09 14:35:58 -0400 | [diff] [blame] | 1491 | /* |
| 1492 | * this is can be hit on boot when a file is accessed |
| 1493 | * before the policy is loaded. When we load policy we |
| 1494 | * may find inodes that have no dentry on the |
| 1495 | * sbsec->isec_head list. No reason to complain as these |
| 1496 | * will get fixed up the next time we go through |
| 1497 | * inode_doinit with a dentry, before these inodes could |
| 1498 | * be used again by userspace. |
| 1499 | */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1500 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1503 | rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid, |
| 1504 | &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | dput(dentry); |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1506 | if (rc) |
| 1507 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | break; |
| 1509 | case SECURITY_FS_USE_TASK: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1510 | sid = task_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | break; |
| 1512 | case SECURITY_FS_USE_TRANS: |
| 1513 | /* Default to the fs SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1514 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
| 1516 | /* Try to obtain a transition SID. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1517 | rc = security_transition_sid(&selinux_state, task_sid, sid, |
| 1518 | sclass, NULL, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | if (rc) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1520 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | break; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1522 | case SECURITY_FS_USE_MNTPOINT: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1523 | sid = sbsec->mntpoint_sid; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1524 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | default: |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1526 | /* Default to the fs superblock SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1527 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1529 | if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1530 | /* We must have a dentry to determine the label on |
| 1531 | * procfs inodes */ |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1532 | if (opt_dentry) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1533 | /* Called from d_instantiate or |
| 1534 | * d_splice_alias. */ |
| 1535 | dentry = dget(opt_dentry); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1536 | } else { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1537 | /* Called from selinux_complete_init, try to |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1538 | * find a dentry. Some filesystems really want |
| 1539 | * a connected one, so try that first. |
| 1540 | */ |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1541 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1542 | if (!dentry) |
| 1543 | dentry = d_find_any_alias(inode); |
| 1544 | } |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1545 | /* |
| 1546 | * This can be hit on boot when a file is accessed |
| 1547 | * before the policy is loaded. When we load policy we |
| 1548 | * may find inodes that have no dentry on the |
| 1549 | * sbsec->isec_head list. No reason to complain as |
| 1550 | * these will get fixed up the next time we go through |
| 1551 | * inode_doinit() with a dentry, before these inodes |
| 1552 | * could be used again by userspace. |
| 1553 | */ |
| 1554 | if (!dentry) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1555 | goto out; |
| 1556 | rc = selinux_genfs_get_sid(dentry, sclass, |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1557 | sbsec->flags, &sid); |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1558 | if (rc) { |
| 1559 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1560 | goto out; |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | if ((sbsec->flags & SE_SBGENFS_XATTR) && |
| 1564 | (inode->i_opflags & IOP_XATTR)) { |
| 1565 | rc = inode_doinit_use_xattr(inode, dentry, |
| 1566 | sid, &sid); |
| 1567 | if (rc) { |
| 1568 | dput(dentry); |
| 1569 | goto out; |
| 1570 | } |
| 1571 | } |
| 1572 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | } |
| 1574 | break; |
| 1575 | } |
| 1576 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1577 | out: |
| 1578 | spin_lock(&isec->lock); |
| 1579 | if (isec->initialized == LABEL_PENDING) { |
| 1580 | if (!sid || rc) { |
| 1581 | isec->initialized = LABEL_INVALID; |
| 1582 | goto out_unlock; |
| 1583 | } |
| 1584 | |
| 1585 | isec->initialized = LABEL_INITIALIZED; |
| 1586 | isec->sid = sid; |
| 1587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1589 | out_unlock: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1590 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | return rc; |
| 1592 | } |
| 1593 | |
| 1594 | /* Convert a Linux signal to an access vector. */ |
| 1595 | static inline u32 signal_to_av(int sig) |
| 1596 | { |
| 1597 | u32 perm = 0; |
| 1598 | |
| 1599 | switch (sig) { |
| 1600 | case SIGCHLD: |
| 1601 | /* Commonly granted from child to parent. */ |
| 1602 | perm = PROCESS__SIGCHLD; |
| 1603 | break; |
| 1604 | case SIGKILL: |
| 1605 | /* Cannot be caught or ignored */ |
| 1606 | perm = PROCESS__SIGKILL; |
| 1607 | break; |
| 1608 | case SIGSTOP: |
| 1609 | /* Cannot be caught or ignored */ |
| 1610 | perm = PROCESS__SIGSTOP; |
| 1611 | break; |
| 1612 | default: |
| 1613 | /* All other signals. */ |
| 1614 | perm = PROCESS__SIGNAL; |
| 1615 | break; |
| 1616 | } |
| 1617 | |
| 1618 | return perm; |
| 1619 | } |
| 1620 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1621 | #if CAP_LAST_CAP > 63 |
| 1622 | #error Fix SELinux to handle capabilities > 63. |
| 1623 | #endif |
| 1624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | /* Check whether a task is allowed to use a capability. */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1626 | static int cred_has_capability(const struct cred *cred, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 1627 | int cap, unsigned int opts, bool initns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1629 | struct common_audit_data ad; |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1630 | struct av_decision avd; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1631 | u16 sclass; |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 1632 | u32 sid = cred_sid(cred); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1633 | u32 av = CAP_TO_MASK(cap); |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1634 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1636 | ad.type = LSM_AUDIT_DATA_CAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | ad.u.cap = cap; |
| 1638 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1639 | switch (CAP_TO_INDEX(cap)) { |
| 1640 | case 0: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1641 | sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1642 | break; |
| 1643 | case 1: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1644 | sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1645 | break; |
| 1646 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1647 | pr_err("SELinux: out of range capability %d\n", cap); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1648 | BUG(); |
Eric Paris | a35c6c83 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1649 | return -EINVAL; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1650 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1651 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1652 | rc = avc_has_perm_noaudit(&selinux_state, |
| 1653 | sid, sid, sclass, av, 0, &avd); |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 1654 | if (!(opts & CAP_OPT_NOAUDIT)) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1655 | int rc2 = avc_audit(&selinux_state, |
| 1656 | sid, sid, sclass, av, &avd, rc, &ad, 0); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1657 | if (rc2) |
| 1658 | return rc2; |
| 1659 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1660 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | /* Check whether a task has a particular permission to an inode. |
| 1664 | The 'adp' parameter is optional and allows other audit |
| 1665 | data to be passed (e.g. the dentry). */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1666 | static int inode_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | struct inode *inode, |
| 1668 | u32 perms, |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1669 | struct common_audit_data *adp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1672 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 1674 | validate_creds(cred); |
| 1675 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1676 | if (unlikely(IS_PRIVATE(inode))) |
Stephen Smalley | bbaca6c | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 1677 | return 0; |
| 1678 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1679 | sid = cred_sid(cred); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 1680 | isec = selinux_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1682 | return avc_has_perm(&selinux_state, |
| 1683 | sid, isec->sid, isec->sclass, perms, adp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1687 | the dentry to help the auditing code to more easily generate the |
| 1688 | pathname if needed. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1689 | static inline int dentry_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | struct dentry *dentry, |
| 1691 | u32 av) |
| 1692 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1693 | struct inode *inode = d_backing_inode(dentry); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1694 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1695 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1696 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1697 | ad.u.dentry = dentry; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1698 | __inode_security_revalidate(inode, dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1699 | return inode_has_perm(cred, inode, av, &ad); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1703 | the path to help the auditing code to more easily generate the |
| 1704 | pathname if needed. */ |
| 1705 | static inline int path_has_perm(const struct cred *cred, |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1706 | const struct path *path, |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1707 | u32 av) |
| 1708 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1709 | struct inode *inode = d_backing_inode(path->dentry); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1710 | struct common_audit_data ad; |
| 1711 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1712 | ad.type = LSM_AUDIT_DATA_PATH; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1713 | ad.u.path = *path; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1714 | __inode_security_revalidate(inode, path->dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1715 | return inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1718 | /* Same as path_has_perm, but uses the inode from the file struct. */ |
| 1719 | static inline int file_path_has_perm(const struct cred *cred, |
| 1720 | struct file *file, |
| 1721 | u32 av) |
| 1722 | { |
| 1723 | struct common_audit_data ad; |
| 1724 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1725 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1726 | ad.u.file = file; |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1727 | return inode_has_perm(cred, file_inode(file), av, &ad); |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1730 | #ifdef CONFIG_BPF_SYSCALL |
| 1731 | static int bpf_fd_pass(struct file *file, u32 sid); |
| 1732 | #endif |
| 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | /* Check whether a task can use an open file descriptor to |
| 1735 | access an inode in a given way. Check access to the |
| 1736 | descriptor itself, and then use dentry_has_perm to |
| 1737 | check a particular permission to the file. |
| 1738 | Access to the descriptor is implicitly granted if it |
| 1739 | has the same SID as the process. If av is zero, then |
| 1740 | access to the file is not checked, e.g. for cases |
| 1741 | where only the descriptor is affected like seek. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1742 | static int file_has_perm(const struct cred *cred, |
| 1743 | struct file *file, |
| 1744 | u32 av) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | { |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 1746 | struct file_security_struct *fsec = selinux_file(file); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1747 | struct inode *inode = file_inode(file); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1748 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1749 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | int rc; |
| 1751 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1752 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1753 | ad.u.file = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1755 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1756 | rc = avc_has_perm(&selinux_state, |
| 1757 | sid, fsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | SECCLASS_FD, |
| 1759 | FD__USE, |
| 1760 | &ad); |
| 1761 | if (rc) |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1762 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1765 | #ifdef CONFIG_BPF_SYSCALL |
| 1766 | rc = bpf_fd_pass(file, cred_sid(cred)); |
| 1767 | if (rc) |
| 1768 | return rc; |
| 1769 | #endif |
| 1770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | /* av is zero if only checking access to the descriptor. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1772 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | if (av) |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1774 | rc = inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1776 | out: |
| 1777 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1780 | /* |
| 1781 | * Determine the label for an inode that might be unioned. |
| 1782 | */ |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1783 | static int |
| 1784 | selinux_determine_inode_label(const struct task_security_struct *tsec, |
| 1785 | struct inode *dir, |
| 1786 | const struct qstr *name, u16 tclass, |
| 1787 | u32 *_new_isid) |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1788 | { |
| 1789 | const struct superblock_security_struct *sbsec = dir->i_sb->s_security; |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1790 | |
| 1791 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 1792 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { |
| 1793 | *_new_isid = sbsec->mntpoint_sid; |
| 1794 | } else if ((sbsec->flags & SBLABEL_MNT) && |
| 1795 | tsec->create_sid) { |
| 1796 | *_new_isid = tsec->create_sid; |
| 1797 | } else { |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 1798 | const struct inode_security_struct *dsec = inode_security(dir); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1799 | return security_transition_sid(&selinux_state, tsec->sid, |
| 1800 | dsec->sid, tclass, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1801 | name, _new_isid); |
| 1802 | } |
| 1803 | |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | /* Check whether a task can create a file. */ |
| 1808 | static int may_create(struct inode *dir, |
| 1809 | struct dentry *dentry, |
| 1810 | u16 tclass) |
| 1811 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 1812 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | struct inode_security_struct *dsec; |
| 1814 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1815 | u32 sid, newsid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1816 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | int rc; |
| 1818 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1819 | dsec = inode_security(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | sbsec = dir->i_sb->s_security; |
| 1821 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1822 | sid = tsec->sid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1823 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1824 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1825 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1827 | rc = avc_has_perm(&selinux_state, |
| 1828 | sid, dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | DIR__ADD_NAME | DIR__SEARCH, |
| 1830 | &ad); |
| 1831 | if (rc) |
| 1832 | return rc; |
| 1833 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 1834 | rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir, |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1835 | &dentry->d_name, tclass, &newsid); |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1836 | if (rc) |
| 1837 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1839 | rc = avc_has_perm(&selinux_state, |
| 1840 | sid, newsid, tclass, FILE__CREATE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | if (rc) |
| 1842 | return rc; |
| 1843 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1844 | return avc_has_perm(&selinux_state, |
| 1845 | newsid, sbsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | SECCLASS_FILESYSTEM, |
| 1847 | FILESYSTEM__ASSOCIATE, &ad); |
| 1848 | } |
| 1849 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1850 | #define MAY_LINK 0 |
| 1851 | #define MAY_UNLINK 1 |
| 1852 | #define MAY_RMDIR 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
| 1854 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ |
| 1855 | static int may_link(struct inode *dir, |
| 1856 | struct dentry *dentry, |
| 1857 | int kind) |
| 1858 | |
| 1859 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | struct inode_security_struct *dsec, *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1861 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1862 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | u32 av; |
| 1864 | int rc; |
| 1865 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1866 | dsec = inode_security(dir); |
| 1867 | isec = backing_inode_security(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1869 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1870 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
| 1872 | av = DIR__SEARCH; |
| 1873 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1874 | rc = avc_has_perm(&selinux_state, |
| 1875 | sid, dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | if (rc) |
| 1877 | return rc; |
| 1878 | |
| 1879 | switch (kind) { |
| 1880 | case MAY_LINK: |
| 1881 | av = FILE__LINK; |
| 1882 | break; |
| 1883 | case MAY_UNLINK: |
| 1884 | av = FILE__UNLINK; |
| 1885 | break; |
| 1886 | case MAY_RMDIR: |
| 1887 | av = DIR__RMDIR; |
| 1888 | break; |
| 1889 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1890 | pr_warn("SELinux: %s: unrecognized kind %d\n", |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1891 | __func__, kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | return 0; |
| 1893 | } |
| 1894 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1895 | rc = avc_has_perm(&selinux_state, |
| 1896 | sid, isec->sid, isec->sclass, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | return rc; |
| 1898 | } |
| 1899 | |
| 1900 | static inline int may_rename(struct inode *old_dir, |
| 1901 | struct dentry *old_dentry, |
| 1902 | struct inode *new_dir, |
| 1903 | struct dentry *new_dentry) |
| 1904 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1906 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1907 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | u32 av; |
| 1909 | int old_is_dir, new_is_dir; |
| 1910 | int rc; |
| 1911 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1912 | old_dsec = inode_security(old_dir); |
| 1913 | old_isec = backing_inode_security(old_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1914 | old_is_dir = d_is_dir(old_dentry); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1915 | new_dsec = inode_security(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1917 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1919 | ad.u.dentry = old_dentry; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1920 | rc = avc_has_perm(&selinux_state, |
| 1921 | sid, old_dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
| 1923 | if (rc) |
| 1924 | return rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1925 | rc = avc_has_perm(&selinux_state, |
| 1926 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | old_isec->sclass, FILE__RENAME, &ad); |
| 1928 | if (rc) |
| 1929 | return rc; |
| 1930 | if (old_is_dir && new_dir != old_dir) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1931 | rc = avc_has_perm(&selinux_state, |
| 1932 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | old_isec->sclass, DIR__REPARENT, &ad); |
| 1934 | if (rc) |
| 1935 | return rc; |
| 1936 | } |
| 1937 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1938 | ad.u.dentry = new_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | av = DIR__ADD_NAME | DIR__SEARCH; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1940 | if (d_is_positive(new_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | av |= DIR__REMOVE_NAME; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1942 | rc = avc_has_perm(&selinux_state, |
| 1943 | sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | if (rc) |
| 1945 | return rc; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1946 | if (d_is_positive(new_dentry)) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1947 | new_isec = backing_inode_security(new_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1948 | new_is_dir = d_is_dir(new_dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1949 | rc = avc_has_perm(&selinux_state, |
| 1950 | sid, new_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | new_isec->sclass, |
| 1952 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); |
| 1953 | if (rc) |
| 1954 | return rc; |
| 1955 | } |
| 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | /* Check whether a task can perform a filesystem operation. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1961 | static int superblock_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | struct super_block *sb, |
| 1963 | u32 perms, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1964 | struct common_audit_data *ad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | struct superblock_security_struct *sbsec; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1967 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | sbsec = sb->s_security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1970 | return avc_has_perm(&selinux_state, |
| 1971 | sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /* Convert a Linux mode and permission mask to an access vector. */ |
| 1975 | static inline u32 file_mask_to_av(int mode, int mask) |
| 1976 | { |
| 1977 | u32 av = 0; |
| 1978 | |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 1979 | if (!S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | if (mask & MAY_EXEC) |
| 1981 | av |= FILE__EXECUTE; |
| 1982 | if (mask & MAY_READ) |
| 1983 | av |= FILE__READ; |
| 1984 | |
| 1985 | if (mask & MAY_APPEND) |
| 1986 | av |= FILE__APPEND; |
| 1987 | else if (mask & MAY_WRITE) |
| 1988 | av |= FILE__WRITE; |
| 1989 | |
| 1990 | } else { |
| 1991 | if (mask & MAY_EXEC) |
| 1992 | av |= DIR__SEARCH; |
| 1993 | if (mask & MAY_WRITE) |
| 1994 | av |= DIR__WRITE; |
| 1995 | if (mask & MAY_READ) |
| 1996 | av |= DIR__READ; |
| 1997 | } |
| 1998 | |
| 1999 | return av; |
| 2000 | } |
| 2001 | |
| 2002 | /* Convert a Linux file to an access vector. */ |
| 2003 | static inline u32 file_to_av(struct file *file) |
| 2004 | { |
| 2005 | u32 av = 0; |
| 2006 | |
| 2007 | if (file->f_mode & FMODE_READ) |
| 2008 | av |= FILE__READ; |
| 2009 | if (file->f_mode & FMODE_WRITE) { |
| 2010 | if (file->f_flags & O_APPEND) |
| 2011 | av |= FILE__APPEND; |
| 2012 | else |
| 2013 | av |= FILE__WRITE; |
| 2014 | } |
Stephen Smalley | 0794c66 | 2008-03-17 08:55:18 -0400 | [diff] [blame] | 2015 | if (!av) { |
| 2016 | /* |
| 2017 | * Special file opened with flags 3 for ioctl-only use. |
| 2018 | */ |
| 2019 | av = FILE__IOCTL; |
| 2020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
| 2022 | return av; |
| 2023 | } |
| 2024 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2025 | /* |
| 2026 | * Convert a file to an access vector and include the correct open |
| 2027 | * open permission. |
| 2028 | */ |
| 2029 | static inline u32 open_file_to_av(struct file *file) |
| 2030 | { |
| 2031 | u32 av = file_to_av(file); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 2032 | struct inode *inode = file_inode(file); |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2033 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2034 | if (selinux_policycap_openperm() && |
| 2035 | inode->i_sb->s_magic != SOCKFS_MAGIC) |
Eric Paris | 49b7b8d | 2010-07-23 11:44:09 -0400 | [diff] [blame] | 2036 | av |= FILE__OPEN; |
| 2037 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2038 | return av; |
| 2039 | } |
| 2040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | /* Hook functions begin here. */ |
| 2042 | |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2043 | static int selinux_binder_set_context_mgr(struct task_struct *mgr) |
| 2044 | { |
| 2045 | u32 mysid = current_sid(); |
| 2046 | u32 mgrsid = task_sid(mgr); |
| 2047 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2048 | return avc_has_perm(&selinux_state, |
| 2049 | mysid, mgrsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2050 | BINDER__SET_CONTEXT_MGR, NULL); |
| 2051 | } |
| 2052 | |
| 2053 | static int selinux_binder_transaction(struct task_struct *from, |
| 2054 | struct task_struct *to) |
| 2055 | { |
| 2056 | u32 mysid = current_sid(); |
| 2057 | u32 fromsid = task_sid(from); |
| 2058 | u32 tosid = task_sid(to); |
| 2059 | int rc; |
| 2060 | |
| 2061 | if (mysid != fromsid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2062 | rc = avc_has_perm(&selinux_state, |
| 2063 | mysid, fromsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2064 | BINDER__IMPERSONATE, NULL); |
| 2065 | if (rc) |
| 2066 | return rc; |
| 2067 | } |
| 2068 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2069 | return avc_has_perm(&selinux_state, |
| 2070 | fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2071 | NULL); |
| 2072 | } |
| 2073 | |
| 2074 | static int selinux_binder_transfer_binder(struct task_struct *from, |
| 2075 | struct task_struct *to) |
| 2076 | { |
| 2077 | u32 fromsid = task_sid(from); |
| 2078 | u32 tosid = task_sid(to); |
| 2079 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2080 | return avc_has_perm(&selinux_state, |
| 2081 | fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2082 | NULL); |
| 2083 | } |
| 2084 | |
| 2085 | static int selinux_binder_transfer_file(struct task_struct *from, |
| 2086 | struct task_struct *to, |
| 2087 | struct file *file) |
| 2088 | { |
| 2089 | u32 sid = task_sid(to); |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 2090 | struct file_security_struct *fsec = selinux_file(file); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2091 | struct dentry *dentry = file->f_path.dentry; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2092 | struct inode_security_struct *isec; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2093 | struct common_audit_data ad; |
| 2094 | int rc; |
| 2095 | |
| 2096 | ad.type = LSM_AUDIT_DATA_PATH; |
| 2097 | ad.u.path = file->f_path; |
| 2098 | |
| 2099 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2100 | rc = avc_has_perm(&selinux_state, |
| 2101 | sid, fsec->sid, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2102 | SECCLASS_FD, |
| 2103 | FD__USE, |
| 2104 | &ad); |
| 2105 | if (rc) |
| 2106 | return rc; |
| 2107 | } |
| 2108 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 2109 | #ifdef CONFIG_BPF_SYSCALL |
| 2110 | rc = bpf_fd_pass(file, sid); |
| 2111 | if (rc) |
| 2112 | return rc; |
| 2113 | #endif |
| 2114 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2115 | if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2116 | return 0; |
| 2117 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2118 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2119 | return avc_has_perm(&selinux_state, |
| 2120 | sid, isec->sid, isec->sclass, file_to_av(file), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2121 | &ad); |
| 2122 | } |
| 2123 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 2124 | static int selinux_ptrace_access_check(struct task_struct *child, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2125 | unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2127 | u32 sid = current_sid(); |
| 2128 | u32 csid = task_sid(child); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 2129 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2130 | if (mode & PTRACE_MODE_READ) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2131 | return avc_has_perm(&selinux_state, |
| 2132 | sid, csid, SECCLASS_FILE, FILE__READ, NULL); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2133 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2134 | return avc_has_perm(&selinux_state, |
| 2135 | sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | static int selinux_ptrace_traceme(struct task_struct *parent) |
| 2139 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2140 | return avc_has_perm(&selinux_state, |
| 2141 | task_sid(parent), current_sid(), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2142 | PROCESS__PTRACE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2146 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2148 | return avc_has_perm(&selinux_state, |
| 2149 | current_sid(), task_sid(target), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2150 | PROCESS__GETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2153 | static int selinux_capset(struct cred *new, const struct cred *old, |
| 2154 | const kernel_cap_t *effective, |
| 2155 | const kernel_cap_t *inheritable, |
| 2156 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2158 | return avc_has_perm(&selinux_state, |
| 2159 | cred_sid(old), cred_sid(new), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2160 | PROCESS__SETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
James Morris | 5626d3e | 2009-01-30 10:05:06 +1100 | [diff] [blame] | 2163 | /* |
| 2164 | * (This comment used to live with the selinux_task_setuid hook, |
| 2165 | * which was removed). |
| 2166 | * |
| 2167 | * Since setuid only affects the current process, and since the SELinux |
| 2168 | * controls are not based on the Linux identity attributes, SELinux does not |
| 2169 | * need to control this operation. However, SELinux does control the use of |
| 2170 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. |
| 2171 | */ |
| 2172 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2173 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 2174 | int cap, unsigned int opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | { |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 2176 | return cred_has_capability(cred, cap, opts, ns == &init_user_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| 2180 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2181 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | int rc = 0; |
| 2183 | |
| 2184 | if (!sb) |
| 2185 | return 0; |
| 2186 | |
| 2187 | switch (cmds) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2188 | case Q_SYNC: |
| 2189 | case Q_QUOTAON: |
| 2190 | case Q_QUOTAOFF: |
| 2191 | case Q_SETINFO: |
| 2192 | case Q_SETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2193 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2194 | break; |
| 2195 | case Q_GETFMT: |
| 2196 | case Q_GETINFO: |
| 2197 | case Q_GETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2198 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2199 | break; |
| 2200 | default: |
| 2201 | rc = 0; /* let the kernel handle invalid cmds */ |
| 2202 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | } |
| 2204 | return rc; |
| 2205 | } |
| 2206 | |
| 2207 | static int selinux_quota_on(struct dentry *dentry) |
| 2208 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2209 | const struct cred *cred = current_cred(); |
| 2210 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2211 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 2214 | static int selinux_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2217 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
| 2218 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2219 | return avc_has_perm(&selinux_state, |
| 2220 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2221 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2222 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
| 2223 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ |
| 2224 | /* Set level of messages printed to console */ |
| 2225 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2226 | return avc_has_perm(&selinux_state, |
| 2227 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2228 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, |
| 2229 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2231 | /* All other syslog types */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2232 | return avc_has_perm(&selinux_state, |
| 2233 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2234 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * Check that a process has enough memory to allocate a new virtual |
| 2239 | * mapping. 0 means there is enough memory for the allocation to |
| 2240 | * succeed and -ENOMEM implies there is not. |
| 2241 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | * Do not audit the selinux permission check, as this is applied to all |
| 2243 | * processes that allocate mappings. |
| 2244 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2245 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | { |
| 2247 | int rc, cap_sys_admin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2249 | rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 2250 | CAP_OPT_NOAUDIT, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | if (rc == 0) |
| 2252 | cap_sys_admin = 1; |
| 2253 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2254 | return cap_sys_admin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /* binprm security operations */ |
| 2258 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2259 | static u32 ptrace_parent_sid(void) |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2260 | { |
| 2261 | u32 sid = 0; |
| 2262 | struct task_struct *tracer; |
| 2263 | |
| 2264 | rcu_read_lock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2265 | tracer = ptrace_parent(current); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2266 | if (tracer) |
| 2267 | sid = task_sid(tracer); |
| 2268 | rcu_read_unlock(); |
| 2269 | |
| 2270 | return sid; |
| 2271 | } |
| 2272 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2273 | static int check_nnp_nosuid(const struct linux_binprm *bprm, |
| 2274 | const struct task_security_struct *old_tsec, |
| 2275 | const struct task_security_struct *new_tsec) |
| 2276 | { |
| 2277 | int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); |
Andy Lutomirski | 380cf5b | 2016-06-23 16:41:05 -0500 | [diff] [blame] | 2278 | int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2279 | int rc; |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2280 | u32 av; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2281 | |
| 2282 | if (!nnp && !nosuid) |
| 2283 | return 0; /* neither NNP nor nosuid */ |
| 2284 | |
| 2285 | if (new_tsec->sid == old_tsec->sid) |
| 2286 | return 0; /* No change in credentials */ |
| 2287 | |
| 2288 | /* |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2289 | * If the policy enables the nnp_nosuid_transition policy capability, |
| 2290 | * then we permit transitions under NNP or nosuid if the |
| 2291 | * policy allows the corresponding permission between |
| 2292 | * the old and new contexts. |
| 2293 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2294 | if (selinux_policycap_nnp_nosuid_transition()) { |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2295 | av = 0; |
| 2296 | if (nnp) |
| 2297 | av |= PROCESS2__NNP_TRANSITION; |
| 2298 | if (nosuid) |
| 2299 | av |= PROCESS2__NOSUID_TRANSITION; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2300 | rc = avc_has_perm(&selinux_state, |
| 2301 | old_tsec->sid, new_tsec->sid, |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2302 | SECCLASS_PROCESS2, av, NULL); |
| 2303 | if (!rc) |
| 2304 | return 0; |
| 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | * We also permit NNP or nosuid transitions to bounded SIDs, |
| 2309 | * i.e. SIDs that are guaranteed to only be allowed a subset |
| 2310 | * of the permissions of the current SID. |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2311 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2312 | rc = security_bounded_transition(&selinux_state, old_tsec->sid, |
| 2313 | new_tsec->sid); |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2314 | if (!rc) |
| 2315 | return 0; |
| 2316 | |
| 2317 | /* |
| 2318 | * On failure, preserve the errno values for NNP vs nosuid. |
| 2319 | * NNP: Operation not permitted for caller. |
| 2320 | * nosuid: Permission denied to file. |
| 2321 | */ |
| 2322 | if (nnp) |
| 2323 | return -EPERM; |
| 2324 | return -EACCES; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2325 | } |
| 2326 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2327 | static int selinux_bprm_set_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2329 | const struct task_security_struct *old_tsec; |
| 2330 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | struct inode_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2332 | struct common_audit_data ad; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2333 | struct inode *inode = file_inode(bprm->file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | int rc; |
| 2335 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2336 | /* SELinux context only depends on initial program or script and not |
| 2337 | * the script interpreter */ |
Kees Cook | ddb4a14 | 2017-07-18 15:25:23 -0700 | [diff] [blame] | 2338 | if (bprm->called_set_creds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | return 0; |
| 2340 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2341 | old_tsec = selinux_cred(current_cred()); |
| 2342 | new_tsec = selinux_cred(bprm->cred); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2343 | isec = inode_security(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
| 2345 | /* Default to the current task SID. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2346 | new_tsec->sid = old_tsec->sid; |
| 2347 | new_tsec->osid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
Michael LeMay | 28eba5b | 2006-06-27 02:53:42 -0700 | [diff] [blame] | 2349 | /* Reset fs, key, and sock SIDs on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2350 | new_tsec->create_sid = 0; |
| 2351 | new_tsec->keycreate_sid = 0; |
| 2352 | new_tsec->sockcreate_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2354 | if (old_tsec->exec_sid) { |
| 2355 | new_tsec->sid = old_tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | /* Reset exec SID on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2357 | new_tsec->exec_sid = 0; |
Andy Lutomirski | 259e5e6 | 2012-04-12 16:47:50 -0500 | [diff] [blame] | 2358 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2359 | /* Fail on NNP or nosuid if not an allowed transition. */ |
| 2360 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2361 | if (rc) |
| 2362 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | } else { |
| 2364 | /* Check for a default transition on this program. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2365 | rc = security_transition_sid(&selinux_state, old_tsec->sid, |
| 2366 | isec->sid, SECCLASS_PROCESS, NULL, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2367 | &new_tsec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | if (rc) |
| 2369 | return rc; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2370 | |
| 2371 | /* |
| 2372 | * Fallback to old SID on NNP or nosuid if not an allowed |
| 2373 | * transition. |
| 2374 | */ |
| 2375 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2376 | if (rc) |
| 2377 | new_tsec->sid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 2380 | ad.type = LSM_AUDIT_DATA_FILE; |
| 2381 | ad.u.file = bprm->file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2383 | if (new_tsec->sid == old_tsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2384 | rc = avc_has_perm(&selinux_state, |
| 2385 | old_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
| 2387 | if (rc) |
| 2388 | return rc; |
| 2389 | } else { |
| 2390 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2391 | rc = avc_has_perm(&selinux_state, |
| 2392 | old_tsec->sid, new_tsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
| 2394 | if (rc) |
| 2395 | return rc; |
| 2396 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2397 | rc = avc_has_perm(&selinux_state, |
| 2398 | new_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
| 2400 | if (rc) |
| 2401 | return rc; |
| 2402 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2403 | /* Check for shared state */ |
| 2404 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2405 | rc = avc_has_perm(&selinux_state, |
| 2406 | old_tsec->sid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2407 | SECCLASS_PROCESS, PROCESS__SHARE, |
| 2408 | NULL); |
| 2409 | if (rc) |
| 2410 | return -EPERM; |
| 2411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2413 | /* Make sure that anyone attempting to ptrace over a task that |
| 2414 | * changes its SID has the appropriate permit */ |
Eric W. Biederman | 9227dd2 | 2017-01-23 17:26:31 +1300 | [diff] [blame] | 2415 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2416 | u32 ptsid = ptrace_parent_sid(); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2417 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2418 | rc = avc_has_perm(&selinux_state, |
| 2419 | ptsid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2420 | SECCLASS_PROCESS, |
| 2421 | PROCESS__PTRACE, NULL); |
| 2422 | if (rc) |
| 2423 | return -EPERM; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | /* Clear any possibly unsafe personality bits on exec: */ |
| 2428 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | /* Enable secure mode for SIDs transitions unless |
| 2431 | the noatsecure permission is granted between |
| 2432 | the two SIDs, i.e. ahp returns 0. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2433 | rc = avc_has_perm(&selinux_state, |
| 2434 | old_tsec->sid, new_tsec->sid, |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2435 | SECCLASS_PROCESS, PROCESS__NOATSECURE, |
| 2436 | NULL); |
| 2437 | bprm->secureexec |= !!rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2440 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2443 | static int match_file(const void *p, struct file *file, unsigned fd) |
| 2444 | { |
| 2445 | return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; |
| 2446 | } |
| 2447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | /* Derived from fs/exec.c:flush_old_files. */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2449 | static inline void flush_unauthorized_files(const struct cred *cred, |
| 2450 | struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | struct file *file, *devnull = NULL; |
Stephen Smalley | b20c812 | 2006-09-25 23:32:03 -0700 | [diff] [blame] | 2453 | struct tty_struct *tty; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2454 | int drop_tty = 0; |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2455 | unsigned n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2457 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | if (tty) { |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2459 | spin_lock(&tty->files_lock); |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2460 | if (!list_empty(&tty->tty_files)) { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2461 | struct tty_file_private *file_priv; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | /* Revalidate access to controlling tty. |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2464 | Use file_path_has_perm on the tty path directly |
| 2465 | rather than using file_has_perm, as this particular |
| 2466 | open file may belong to another process and we are |
| 2467 | only interested in the inode-based check here. */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2468 | file_priv = list_first_entry(&tty->tty_files, |
| 2469 | struct tty_file_private, list); |
| 2470 | file = file_priv->file; |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2471 | if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2472 | drop_tty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2474 | spin_unlock(&tty->files_lock); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 2475 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | } |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 2477 | /* Reset controlling tty. */ |
| 2478 | if (drop_tty) |
| 2479 | no_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | |
| 2481 | /* Revalidate access to inherited open files. */ |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2482 | n = iterate_fd(files, 0, match_file, cred); |
| 2483 | if (!n) /* none found? */ |
| 2484 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2486 | devnull = dentry_open(&selinux_null, O_RDWR, cred); |
Al Viro | 45525b2 | 2012-10-16 13:30:07 -0400 | [diff] [blame] | 2487 | if (IS_ERR(devnull)) |
| 2488 | devnull = NULL; |
| 2489 | /* replace all the matching ones with this */ |
| 2490 | do { |
| 2491 | replace_fd(n - 1, devnull, 0); |
| 2492 | } while ((n = iterate_fd(files, n, match_file, cred)) != 0); |
| 2493 | if (devnull) |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2494 | fput(devnull); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2498 | * Prepare a process for imminent new credential changes due to exec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2500 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) |
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 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | struct rlimit *rlim, *initrlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | int rc, i; |
| 2505 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2506 | new_tsec = selinux_cred(bprm->cred); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2507 | if (new_tsec->sid == new_tsec->osid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | return; |
| 2509 | |
| 2510 | /* Close files for which the new task SID is not authorized. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2511 | flush_unauthorized_files(bprm->cred, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2513 | /* Always clear parent death signal on SID transitions. */ |
| 2514 | current->pdeath_signal = 0; |
| 2515 | |
| 2516 | /* Check whether the new SID can inherit resource limits from the old |
| 2517 | * SID. If not, reset all soft limits to the lower of the current |
| 2518 | * task's hard limit and the init task's soft limit. |
| 2519 | * |
| 2520 | * Note that the setting of hard limits (even to lower them) can be |
| 2521 | * controlled by the setrlimit check. The inclusion of the init task's |
| 2522 | * soft limit into the computation is to avoid resetting soft limits |
| 2523 | * higher than the default soft limit for cases where the default is |
| 2524 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. |
| 2525 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2526 | rc = avc_has_perm(&selinux_state, |
| 2527 | new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2528 | PROCESS__RLIMITINH, NULL); |
| 2529 | if (rc) { |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2530 | /* protect against do_prlimit() */ |
| 2531 | task_lock(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2532 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 2533 | rlim = current->signal->rlim + i; |
| 2534 | initrlim = init_task.signal->rlim + i; |
| 2535 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); |
| 2536 | } |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2537 | task_unlock(current); |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2538 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) |
| 2539 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * Clean up the process immediately after the installation of new credentials |
| 2545 | * due to exec |
| 2546 | */ |
| 2547 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2548 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2549 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2550 | struct itimerval itimer; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2551 | u32 osid, sid; |
| 2552 | int rc, i; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2553 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2554 | osid = tsec->osid; |
| 2555 | sid = tsec->sid; |
| 2556 | |
| 2557 | if (sid == osid) |
| 2558 | return; |
| 2559 | |
| 2560 | /* Check whether the new SID can inherit signal state from the old SID. |
| 2561 | * If not, clear itimers to avoid subsequent signal generation and |
| 2562 | * flush and unblock signals. |
| 2563 | * |
| 2564 | * This must occur _after_ the task SID has been updated so that any |
| 2565 | * kill done after the flush will be checked against the new SID. |
| 2566 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2567 | rc = avc_has_perm(&selinux_state, |
| 2568 | osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | if (rc) { |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2570 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { |
| 2571 | memset(&itimer, 0, sizeof itimer); |
| 2572 | for (i = 0; i < 3; i++) |
| 2573 | do_setitimer(i, &itimer, NULL); |
| 2574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2576 | if (!fatal_signal_pending(current)) { |
| 2577 | flush_sigqueue(¤t->pending); |
| 2578 | flush_sigqueue(¤t->signal->shared_pending); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2579 | flush_signal_handlers(current, 1); |
| 2580 | sigemptyset(¤t->blocked); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2581 | recalc_sigpending(); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | spin_unlock_irq(¤t->sighand->siglock); |
| 2584 | } |
| 2585 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2586 | /* Wake up the parent if it is waiting so that it can recheck |
| 2587 | * wait permission to the new task SID. */ |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2588 | read_lock(&tasklist_lock); |
Oleg Nesterov | 0b7570e | 2009-09-23 15:56:46 -0700 | [diff] [blame] | 2589 | __wake_up_parent(current, current->real_parent); |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2590 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | /* superblock security operations */ |
| 2594 | |
| 2595 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2596 | { |
| 2597 | return superblock_alloc_security(sb); |
| 2598 | } |
| 2599 | |
| 2600 | static void selinux_sb_free_security(struct super_block *sb) |
| 2601 | { |
| 2602 | superblock_free_security(sb); |
| 2603 | } |
| 2604 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2605 | static inline int opt_len(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | { |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2607 | bool open_quote = false; |
| 2608 | int len; |
| 2609 | char c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2611 | for (len = 0; (c = s[len]) != '\0'; len++) { |
| 2612 | if (c == '"') |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2613 | open_quote = !open_quote; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2614 | if (c == ',' && !open_quote) |
| 2615 | break; |
| 2616 | } |
| 2617 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2620 | static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2621 | { |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2622 | char *from = options; |
| 2623 | char *to = options; |
| 2624 | bool first = true; |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2625 | int rc; |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2626 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2627 | while (1) { |
| 2628 | int len = opt_len(from); |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2629 | int token; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2630 | char *arg = NULL; |
| 2631 | |
| 2632 | token = match_opt_prefix(from, len, &arg); |
| 2633 | |
| 2634 | if (token != Opt_error) { |
| 2635 | char *p, *q; |
| 2636 | |
| 2637 | /* strip quotes */ |
| 2638 | if (arg) { |
| 2639 | for (p = q = arg; p < from + len; p++) { |
| 2640 | char c = *p; |
| 2641 | if (c != '"') |
| 2642 | *q++ = c; |
| 2643 | } |
| 2644 | arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2645 | if (!arg) { |
| 2646 | rc = -ENOMEM; |
| 2647 | goto free_opt; |
| 2648 | } |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2649 | } |
| 2650 | rc = selinux_add_opt(token, arg, mnt_opts); |
| 2651 | if (unlikely(rc)) { |
| 2652 | kfree(arg); |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2653 | goto free_opt; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2654 | } |
| 2655 | } else { |
| 2656 | if (!first) { // copy with preceding comma |
| 2657 | from--; |
| 2658 | len++; |
| 2659 | } |
| 2660 | if (to != from) |
| 2661 | memmove(to, from, len); |
| 2662 | to += len; |
| 2663 | first = false; |
| 2664 | } |
| 2665 | if (!from[len]) |
| 2666 | break; |
| 2667 | from += len + 1; |
| 2668 | } |
| 2669 | *to = '\0'; |
| 2670 | return 0; |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2671 | |
| 2672 | free_opt: |
| 2673 | if (*mnt_opts) { |
| 2674 | selinux_free_mnt_opts(*mnt_opts); |
| 2675 | *mnt_opts = NULL; |
| 2676 | } |
| 2677 | return rc; |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2678 | } |
| 2679 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2680 | static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2681 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2682 | struct selinux_mnt_opts *opts = mnt_opts; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2683 | struct superblock_security_struct *sbsec = sb->s_security; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2684 | u32 sid; |
| 2685 | int rc; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2686 | |
| 2687 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 2688 | return 0; |
| 2689 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2690 | if (!opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2691 | return 0; |
| 2692 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2693 | if (opts->fscontext) { |
| 2694 | rc = parse_sid(sb, opts->fscontext, &sid); |
| 2695 | if (rc) |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2696 | return rc; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2697 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) |
| 2698 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2699 | } |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2700 | if (opts->context) { |
| 2701 | rc = parse_sid(sb, opts->context, &sid); |
| 2702 | if (rc) |
| 2703 | return rc; |
| 2704 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) |
| 2705 | goto out_bad_option; |
| 2706 | } |
| 2707 | if (opts->rootcontext) { |
| 2708 | struct inode_security_struct *root_isec; |
| 2709 | root_isec = backing_inode_security(sb->s_root); |
| 2710 | rc = parse_sid(sb, opts->rootcontext, &sid); |
| 2711 | if (rc) |
| 2712 | return rc; |
| 2713 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) |
| 2714 | goto out_bad_option; |
| 2715 | } |
| 2716 | if (opts->defcontext) { |
| 2717 | rc = parse_sid(sb, opts->defcontext, &sid); |
| 2718 | if (rc) |
| 2719 | return rc; |
| 2720 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) |
| 2721 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2722 | } |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2723 | return 0; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2724 | |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2725 | out_bad_option: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 2726 | pr_warn("SELinux: unable to change security options " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 2727 | "during remount (dev %s, type=%s)\n", sb->s_id, |
| 2728 | sb->s_type->name); |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2729 | return -EINVAL; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2730 | } |
| 2731 | |
Al Viro | a10d7c2 | 2018-12-05 11:58:35 -0500 | [diff] [blame] | 2732 | static int selinux_sb_kern_mount(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2734 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2735 | struct common_audit_data ad; |
James Morris | 7419224 | 2008-12-19 11:41:10 +1100 | [diff] [blame] | 2736 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2737 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2738 | ad.u.dentry = sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2739 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2742 | static int selinux_sb_statfs(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2744 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2745 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2747 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2748 | ad.u.dentry = dentry->d_sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2749 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2752 | static int selinux_mount(const char *dev_name, |
Al Viro | 8a04c43 | 2016-03-25 14:52:53 -0400 | [diff] [blame] | 2753 | const struct path *path, |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2754 | const char *type, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2755 | unsigned long flags, |
| 2756 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2758 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | |
| 2760 | if (flags & MS_REMOUNT) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 2761 | return superblock_has_perm(cred, path->dentry->d_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2762 | FILESYSTEM__REMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | else |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2764 | return path_has_perm(cred, path, FILE__MOUNTON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | static int selinux_umount(struct vfsmount *mnt, int flags) |
| 2768 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2769 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2771 | return superblock_has_perm(cred, mnt->mnt_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2772 | FILESYSTEM__UNMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 2775 | static int selinux_fs_context_dup(struct fs_context *fc, |
| 2776 | struct fs_context *src_fc) |
| 2777 | { |
| 2778 | const struct selinux_mnt_opts *src = src_fc->security; |
| 2779 | struct selinux_mnt_opts *opts; |
| 2780 | |
| 2781 | if (!src) |
| 2782 | return 0; |
| 2783 | |
| 2784 | fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); |
| 2785 | if (!fc->security) |
| 2786 | return -ENOMEM; |
| 2787 | |
| 2788 | opts = fc->security; |
| 2789 | |
| 2790 | if (src->fscontext) { |
| 2791 | opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL); |
| 2792 | if (!opts->fscontext) |
| 2793 | return -ENOMEM; |
| 2794 | } |
| 2795 | if (src->context) { |
| 2796 | opts->context = kstrdup(src->context, GFP_KERNEL); |
| 2797 | if (!opts->context) |
| 2798 | return -ENOMEM; |
| 2799 | } |
| 2800 | if (src->rootcontext) { |
| 2801 | opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL); |
| 2802 | if (!opts->rootcontext) |
| 2803 | return -ENOMEM; |
| 2804 | } |
| 2805 | if (src->defcontext) { |
| 2806 | opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL); |
| 2807 | if (!opts->defcontext) |
| 2808 | return -ENOMEM; |
| 2809 | } |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2813 | static const struct fs_parameter_spec selinux_param_specs[] = { |
| 2814 | fsparam_string(CONTEXT_STR, Opt_context), |
| 2815 | fsparam_string(DEFCONTEXT_STR, Opt_defcontext), |
| 2816 | fsparam_string(FSCONTEXT_STR, Opt_fscontext), |
| 2817 | fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext), |
| 2818 | fsparam_flag (SECLABEL_STR, Opt_seclabel), |
| 2819 | {} |
| 2820 | }; |
| 2821 | |
| 2822 | static const struct fs_parameter_description selinux_fs_parameters = { |
| 2823 | .name = "SELinux", |
| 2824 | .specs = selinux_param_specs, |
| 2825 | }; |
| 2826 | |
| 2827 | static int selinux_fs_context_parse_param(struct fs_context *fc, |
| 2828 | struct fs_parameter *param) |
| 2829 | { |
| 2830 | struct fs_parse_result result; |
| 2831 | int opt, rc; |
| 2832 | |
| 2833 | opt = fs_parse(fc, &selinux_fs_parameters, param, &result); |
| 2834 | if (opt < 0) |
| 2835 | return opt; |
| 2836 | |
| 2837 | rc = selinux_add_opt(opt, param->string, &fc->security); |
| 2838 | if (!rc) { |
| 2839 | param->string = NULL; |
| 2840 | rc = 1; |
| 2841 | } |
| 2842 | return rc; |
| 2843 | } |
| 2844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | /* inode security operations */ |
| 2846 | |
| 2847 | static int selinux_inode_alloc_security(struct inode *inode) |
| 2848 | { |
| 2849 | return inode_alloc_security(inode); |
| 2850 | } |
| 2851 | |
| 2852 | static void selinux_inode_free_security(struct inode *inode) |
| 2853 | { |
| 2854 | inode_free_security(inode); |
| 2855 | } |
| 2856 | |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2857 | static int selinux_dentry_init_security(struct dentry *dentry, int mode, |
Al Viro | 4f3ccd7 | 2016-07-20 16:06:15 -0400 | [diff] [blame] | 2858 | const struct qstr *name, void **ctx, |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2859 | u32 *ctxlen) |
| 2860 | { |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2861 | u32 newsid; |
| 2862 | int rc; |
| 2863 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2864 | rc = selinux_determine_inode_label(selinux_cred(current_cred()), |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 2865 | d_inode(dentry->d_parent), name, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2866 | inode_mode_to_security_class(mode), |
| 2867 | &newsid); |
| 2868 | if (rc) |
| 2869 | return rc; |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2870 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2871 | return security_sid_to_context(&selinux_state, newsid, (char **)ctx, |
| 2872 | ctxlen); |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2873 | } |
| 2874 | |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2875 | static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, |
| 2876 | struct qstr *name, |
| 2877 | const struct cred *old, |
| 2878 | struct cred *new) |
| 2879 | { |
| 2880 | u32 newsid; |
| 2881 | int rc; |
| 2882 | struct task_security_struct *tsec; |
| 2883 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2884 | rc = selinux_determine_inode_label(selinux_cred(old), |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2885 | d_inode(dentry->d_parent), name, |
| 2886 | inode_mode_to_security_class(mode), |
| 2887 | &newsid); |
| 2888 | if (rc) |
| 2889 | return rc; |
| 2890 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2891 | tsec = selinux_cred(new); |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2892 | tsec->create_sid = newsid; |
| 2893 | return 0; |
| 2894 | } |
| 2895 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2896 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2897 | const struct qstr *qstr, |
| 2898 | const char **name, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2899 | void **value, size_t *len) |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2900 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2901 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2902 | struct superblock_security_struct *sbsec; |
Corentin LABBE | c0d4f46 | 2017-10-04 20:32:17 +0200 | [diff] [blame] | 2903 | u32 newsid, clen; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2904 | int rc; |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2905 | char *context; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2906 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2907 | sbsec = dir->i_sb->s_security; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2908 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2909 | newsid = tsec->create_sid; |
| 2910 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2911 | rc = selinux_determine_inode_label(selinux_cred(current_cred()), |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2912 | dir, qstr, |
| 2913 | inode_mode_to_security_class(inode->i_mode), |
| 2914 | &newsid); |
| 2915 | if (rc) |
| 2916 | return rc; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2917 | |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2918 | /* Possibly defer initialization to selinux_complete_init. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 2919 | if (sbsec->flags & SE_SBINITIALIZED) { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 2920 | struct inode_security_struct *isec = selinux_inode(inode); |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2921 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2922 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2923 | isec->initialized = LABEL_INITIALIZED; |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2924 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2925 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2926 | if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) |
Stephen Smalley | 25a74f3 | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 2927 | return -EOPNOTSUPP; |
| 2928 | |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2929 | if (name) |
| 2930 | *name = XATTR_SELINUX_SUFFIX; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2931 | |
| 2932 | if (value && len) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2933 | rc = security_sid_to_context_force(&selinux_state, newsid, |
| 2934 | &context, &clen); |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2935 | if (rc) |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2936 | return rc; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2937 | *value = context; |
| 2938 | *len = clen; |
| 2939 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2940 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2941 | return 0; |
| 2942 | } |
| 2943 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2944 | 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] | 2945 | { |
| 2946 | return may_create(dir, dentry, SECCLASS_FILE); |
| 2947 | } |
| 2948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2950 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | return may_link(dir, old_dentry, MAY_LINK); |
| 2952 | } |
| 2953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 2955 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | return may_link(dir, dentry, MAY_UNLINK); |
| 2957 | } |
| 2958 | |
| 2959 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) |
| 2960 | { |
| 2961 | return may_create(dir, dentry, SECCLASS_LNK_FILE); |
| 2962 | } |
| 2963 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2964 | 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] | 2965 | { |
| 2966 | return may_create(dir, dentry, SECCLASS_DIR); |
| 2967 | } |
| 2968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 2970 | { |
| 2971 | return may_link(dir, dentry, MAY_RMDIR); |
| 2972 | } |
| 2973 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2974 | 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] | 2975 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
| 2977 | } |
| 2978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | 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] | 2980 | struct inode *new_inode, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | { |
| 2982 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2983 | } |
| 2984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | static int selinux_inode_readlink(struct dentry *dentry) |
| 2986 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2987 | const struct cred *cred = current_cred(); |
| 2988 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2989 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2992 | static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, |
| 2993 | bool rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2995 | const struct cred *cred = current_cred(); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2996 | struct common_audit_data ad; |
| 2997 | struct inode_security_struct *isec; |
| 2998 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 3000 | validate_creds(cred); |
| 3001 | |
| 3002 | ad.type = LSM_AUDIT_DATA_DENTRY; |
| 3003 | ad.u.dentry = dentry; |
| 3004 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3005 | isec = inode_security_rcu(inode, rcu); |
| 3006 | if (IS_ERR(isec)) |
| 3007 | return PTR_ERR(isec); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 3008 | |
Stephen Smalley | e46e01e | 2018-12-12 10:10:56 -0500 | [diff] [blame] | 3009 | return avc_has_perm(&selinux_state, |
| 3010 | sid, isec->sid, isec->sclass, FILE__READ, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3013 | static noinline int audit_inode_permission(struct inode *inode, |
| 3014 | u32 perms, u32 audited, u32 denied, |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 3015 | int result, |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3016 | unsigned flags) |
| 3017 | { |
| 3018 | struct common_audit_data ad; |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 3019 | struct inode_security_struct *isec = selinux_inode(inode); |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3020 | int rc; |
| 3021 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3022 | ad.type = LSM_AUDIT_DATA_INODE; |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3023 | ad.u.inode = inode; |
| 3024 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3025 | rc = slow_avc_audit(&selinux_state, |
| 3026 | current_sid(), isec->sid, isec->sclass, perms, |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 3027 | audited, denied, result, &ad, flags); |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3028 | if (rc) |
| 3029 | return rc; |
| 3030 | return 0; |
| 3031 | } |
| 3032 | |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 3033 | static int selinux_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3035 | const struct cred *cred = current_cred(); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3036 | u32 perms; |
| 3037 | bool from_access; |
Al Viro | cf1dd1d | 2011-06-20 19:44:08 -0400 | [diff] [blame] | 3038 | unsigned flags = mask & MAY_NOT_BLOCK; |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3039 | struct inode_security_struct *isec; |
| 3040 | u32 sid; |
| 3041 | struct av_decision avd; |
| 3042 | int rc, rc2; |
| 3043 | u32 audited, denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3045 | from_access = mask & MAY_ACCESS; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 3046 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
| 3047 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3048 | /* No permission to check. Existence test. */ |
| 3049 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3052 | validate_creds(cred); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3053 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3054 | if (unlikely(IS_PRIVATE(inode))) |
| 3055 | return 0; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3056 | |
| 3057 | perms = file_mask_to_av(inode->i_mode, mask); |
| 3058 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3059 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3060 | isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); |
| 3061 | if (IS_ERR(isec)) |
| 3062 | return PTR_ERR(isec); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3063 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3064 | rc = avc_has_perm_noaudit(&selinux_state, |
Stephen Smalley | 3a28cff | 2018-12-12 10:10:55 -0500 | [diff] [blame] | 3065 | sid, isec->sid, isec->sclass, perms, |
| 3066 | (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0, |
| 3067 | &avd); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3068 | audited = avc_audit_required(perms, &avd, rc, |
| 3069 | from_access ? FILE__AUDIT_ACCESS : 0, |
| 3070 | &denied); |
| 3071 | if (likely(!audited)) |
| 3072 | return rc; |
| 3073 | |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 3074 | rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3075 | if (rc2) |
| 3076 | return rc2; |
| 3077 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 3081 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3082 | const struct cred *cred = current_cred(); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3083 | struct inode *inode = d_backing_inode(dentry); |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3084 | unsigned int ia_valid = iattr->ia_valid; |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3085 | __u32 av = FILE__WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3087 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
| 3088 | if (ia_valid & ATTR_FORCE) { |
| 3089 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | |
| 3090 | ATTR_FORCE); |
| 3091 | if (!ia_valid) |
| 3092 | return 0; |
| 3093 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3095 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
| 3096 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3097 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3099 | if (selinux_policycap_openperm() && |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3100 | inode->i_sb->s_magic != SOCKFS_MAGIC && |
| 3101 | (ia_valid & ATTR_SIZE) && |
| 3102 | !(ia_valid & ATTR_FILE)) |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3103 | av |= FILE__OPEN; |
| 3104 | |
| 3105 | return dentry_has_perm(cred, dentry, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3108 | static int selinux_inode_getattr(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | { |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3110 | return path_has_perm(current_cred(), path, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | } |
| 3112 | |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3113 | static bool has_cap_mac_admin(bool audit) |
| 3114 | { |
| 3115 | const struct cred *cred = current_cred(); |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3116 | unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT; |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3117 | |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3118 | if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts)) |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3119 | return false; |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3120 | if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true)) |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3121 | return false; |
| 3122 | return true; |
| 3123 | } |
| 3124 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3125 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
| 3126 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3128 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3129 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | struct superblock_security_struct *sbsec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3131 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3132 | u32 newsid, sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | int rc = 0; |
| 3134 | |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3135 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3136 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 3137 | if (rc) |
| 3138 | return rc; |
| 3139 | |
| 3140 | /* Not an attribute we recognize, so just check the |
| 3141 | ordinary setattr permission. */ |
| 3142 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
| 3145 | sbsec = inode->i_sb->s_security; |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 3146 | if (!(sbsec->flags & SBLABEL_MNT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | return -EOPNOTSUPP; |
| 3148 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 3149 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | return -EPERM; |
| 3151 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3152 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 3153 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3155 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3156 | rc = avc_has_perm(&selinux_state, |
| 3157 | sid, isec->sid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | FILE__RELABELFROM, &ad); |
| 3159 | if (rc) |
| 3160 | return rc; |
| 3161 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3162 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3163 | GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3164 | if (rc == -EINVAL) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3165 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3166 | struct audit_buffer *ab; |
| 3167 | size_t audit_size; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3168 | |
| 3169 | /* We strip a nul only if it is at the end, otherwise the |
| 3170 | * context contains a nul and we should audit that */ |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3171 | if (value) { |
Colin Ian King | add2437 | 2017-10-14 13:46:55 +0100 | [diff] [blame] | 3172 | const char *str = value; |
| 3173 | |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3174 | if (str[size - 1] == '\0') |
| 3175 | audit_size = size - 1; |
| 3176 | else |
| 3177 | audit_size = size; |
| 3178 | } else { |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3179 | audit_size = 0; |
| 3180 | } |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 3181 | ab = audit_log_start(audit_context(), |
| 3182 | GFP_ATOMIC, AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3183 | audit_log_format(ab, "op=setxattr invalid_context="); |
| 3184 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 3185 | audit_log_end(ab); |
| 3186 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3187 | return rc; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3188 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3189 | rc = security_context_to_sid_force(&selinux_state, value, |
| 3190 | size, &newsid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | if (rc) |
| 3193 | return rc; |
| 3194 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3195 | rc = avc_has_perm(&selinux_state, |
| 3196 | sid, newsid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | FILE__RELABELTO, &ad); |
| 3198 | if (rc) |
| 3199 | return rc; |
| 3200 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3201 | rc = security_validate_transition(&selinux_state, isec->sid, newsid, |
| 3202 | sid, isec->sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | if (rc) |
| 3204 | return rc; |
| 3205 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3206 | return avc_has_perm(&selinux_state, |
| 3207 | newsid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | sbsec->sid, |
| 3209 | SECCLASS_FILESYSTEM, |
| 3210 | FILESYSTEM__ASSOCIATE, |
| 3211 | &ad); |
| 3212 | } |
| 3213 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3214 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 3215 | const void *value, size_t size, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3216 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3218 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3219 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | u32 newsid; |
| 3221 | int rc; |
| 3222 | |
| 3223 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3224 | /* Not an attribute we recognize, so nothing to do. */ |
| 3225 | return; |
| 3226 | } |
| 3227 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3228 | rc = security_context_to_sid_force(&selinux_state, value, size, |
| 3229 | &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 3231 | pr_err("SELinux: unable to map context to SID" |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3232 | "for (%s, %lu), rc=%d\n", |
| 3233 | inode->i_sb->s_id, inode->i_ino, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | return; |
| 3235 | } |
| 3236 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3237 | isec = backing_inode_security(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3238 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3239 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3241 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3242 | spin_unlock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | return; |
| 3245 | } |
| 3246 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3247 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3249 | const struct cred *cred = current_cred(); |
| 3250 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3251 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3254 | static int selinux_inode_listxattr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3256 | const struct cred *cred = current_cred(); |
| 3257 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3258 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3261 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | { |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3263 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3264 | int rc = cap_inode_removexattr(dentry, name); |
| 3265 | if (rc) |
| 3266 | return rc; |
| 3267 | |
| 3268 | /* Not an attribute we recognize, so just check the |
| 3269 | ordinary setattr permission. */ |
| 3270 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
| 3273 | /* No one is allowed to remove a SELinux security label. |
| 3274 | You can change the label, but all data must be labeled. */ |
| 3275 | return -EACCES; |
| 3276 | } |
| 3277 | |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3278 | /* |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3279 | * Copy the inode security context value to the user. |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3280 | * |
| 3281 | * Permission check is handled by selinux_inode_getxattr hook. |
| 3282 | */ |
Andreas Gruenbacher | ea861df | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3283 | 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] | 3284 | { |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3285 | u32 size; |
| 3286 | int error; |
| 3287 | char *context = NULL; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3288 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 3290 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3291 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3293 | /* |
| 3294 | * If the caller has CAP_MAC_ADMIN, then get the raw context |
| 3295 | * value even if it is not defined by current policy; otherwise, |
| 3296 | * use the in-core value under current policy. |
| 3297 | * Use the non-auditing forms of the permission checks since |
| 3298 | * getxattr may be called by unprivileged processes commonly |
| 3299 | * and lack of permission just means that we fall back to the |
| 3300 | * in-core context value, not a denial. |
| 3301 | */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3302 | isec = inode_security(inode); |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3303 | if (has_cap_mac_admin(false)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3304 | error = security_sid_to_context_force(&selinux_state, |
| 3305 | isec->sid, &context, |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3306 | &size); |
| 3307 | else |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3308 | error = security_sid_to_context(&selinux_state, isec->sid, |
| 3309 | &context, &size); |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3310 | if (error) |
| 3311 | return error; |
| 3312 | error = size; |
| 3313 | if (alloc) { |
| 3314 | *buffer = context; |
| 3315 | goto out_nofree; |
| 3316 | } |
| 3317 | kfree(context); |
| 3318 | out_nofree: |
| 3319 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3323 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | { |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 3325 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Ondrej Mosnacek | 53e0c2a | 2018-12-21 21:18:53 +0100 | [diff] [blame] | 3326 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | u32 newsid; |
| 3328 | int rc; |
| 3329 | |
| 3330 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3331 | return -EOPNOTSUPP; |
| 3332 | |
Ondrej Mosnacek | 53e0c2a | 2018-12-21 21:18:53 +0100 | [diff] [blame] | 3333 | if (!(sbsec->flags & SBLABEL_MNT)) |
| 3334 | return -EOPNOTSUPP; |
| 3335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | if (!value || !size) |
| 3337 | return -EACCES; |
| 3338 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3339 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3340 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | if (rc) |
| 3342 | return rc; |
| 3343 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3344 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3345 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3347 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3348 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | return 0; |
| 3350 | } |
| 3351 | |
| 3352 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) |
| 3353 | { |
| 3354 | const int len = sizeof(XATTR_NAME_SELINUX); |
| 3355 | if (buffer && len <= buffer_size) |
| 3356 | memcpy(buffer, XATTR_NAME_SELINUX, len); |
| 3357 | return len; |
| 3358 | } |
| 3359 | |
Andreas Gruenbacher | d6335d7 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3360 | static void selinux_inode_getsecid(struct inode *inode, u32 *secid) |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3361 | { |
Andreas Gruenbacher | e817c2f | 2016-02-18 12:04:08 +0100 | [diff] [blame] | 3362 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3363 | *secid = isec->sid; |
| 3364 | } |
| 3365 | |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 3366 | static int selinux_inode_copy_up(struct dentry *src, struct cred **new) |
| 3367 | { |
| 3368 | u32 sid; |
| 3369 | struct task_security_struct *tsec; |
| 3370 | struct cred *new_creds = *new; |
| 3371 | |
| 3372 | if (new_creds == NULL) { |
| 3373 | new_creds = prepare_creds(); |
| 3374 | if (!new_creds) |
| 3375 | return -ENOMEM; |
| 3376 | } |
| 3377 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3378 | tsec = selinux_cred(new_creds); |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 3379 | /* Get label from overlay inode and set it in create_sid */ |
| 3380 | selinux_inode_getsecid(d_inode(src), &sid); |
| 3381 | tsec->create_sid = sid; |
| 3382 | *new = new_creds; |
| 3383 | return 0; |
| 3384 | } |
| 3385 | |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 3386 | static int selinux_inode_copy_up_xattr(const char *name) |
| 3387 | { |
| 3388 | /* The copy_up hook above sets the initial context on an inode, but we |
| 3389 | * don't then want to overwrite it by blindly copying all the lower |
| 3390 | * xattrs up. Instead, we have to filter out SELinux-related xattrs. |
| 3391 | */ |
| 3392 | if (strcmp(name, XATTR_NAME_SELINUX) == 0) |
| 3393 | return 1; /* Discard */ |
| 3394 | /* |
| 3395 | * Any other attribute apart from SELINUX is not claimed, supported |
| 3396 | * by selinux. |
| 3397 | */ |
| 3398 | return -EOPNOTSUPP; |
| 3399 | } |
| 3400 | |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3401 | /* kernfs node operations */ |
| 3402 | |
YueHaibing | c72c4cd | 2019-03-22 22:04:00 +0800 | [diff] [blame] | 3403 | static int selinux_kernfs_init_security(struct kernfs_node *kn_dir, |
| 3404 | struct kernfs_node *kn) |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3405 | { |
| 3406 | const struct task_security_struct *tsec = current_security(); |
| 3407 | u32 parent_sid, newsid, clen; |
| 3408 | int rc; |
| 3409 | char *context; |
| 3410 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 3411 | rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3412 | if (rc == -ENODATA) |
| 3413 | return 0; |
| 3414 | else if (rc < 0) |
| 3415 | return rc; |
| 3416 | |
| 3417 | clen = (u32)rc; |
| 3418 | context = kmalloc(clen, GFP_KERNEL); |
| 3419 | if (!context) |
| 3420 | return -ENOMEM; |
| 3421 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 3422 | rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3423 | if (rc < 0) { |
| 3424 | kfree(context); |
| 3425 | return rc; |
| 3426 | } |
| 3427 | |
| 3428 | rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid, |
| 3429 | GFP_KERNEL); |
| 3430 | kfree(context); |
| 3431 | if (rc) |
| 3432 | return rc; |
| 3433 | |
| 3434 | if (tsec->create_sid) { |
| 3435 | newsid = tsec->create_sid; |
| 3436 | } else { |
| 3437 | u16 secclass = inode_mode_to_security_class(kn->mode); |
| 3438 | struct qstr q; |
| 3439 | |
| 3440 | q.name = kn->name; |
| 3441 | q.hash_len = hashlen_string(kn_dir, kn->name); |
| 3442 | |
| 3443 | rc = security_transition_sid(&selinux_state, tsec->sid, |
| 3444 | parent_sid, secclass, &q, |
| 3445 | &newsid); |
| 3446 | if (rc) |
| 3447 | return rc; |
| 3448 | } |
| 3449 | |
| 3450 | rc = security_sid_to_context_force(&selinux_state, newsid, |
| 3451 | &context, &clen); |
| 3452 | if (rc) |
| 3453 | return rc; |
| 3454 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 3455 | rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen, |
| 3456 | XATTR_CREATE); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3457 | kfree(context); |
| 3458 | return rc; |
| 3459 | } |
| 3460 | |
| 3461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | /* file security operations */ |
| 3463 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3464 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3466 | const struct cred *cred = current_cred(); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3467 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
| 3470 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) |
| 3471 | mask |= MAY_APPEND; |
| 3472 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3473 | return file_has_perm(cred, file, |
| 3474 | file_mask_to_av(inode->i_mode, mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3477 | static int selinux_file_permission(struct file *file, int mask) |
| 3478 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3479 | struct inode *inode = file_inode(file); |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3480 | struct file_security_struct *fsec = selinux_file(file); |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3481 | struct inode_security_struct *isec; |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3482 | u32 sid = current_sid(); |
| 3483 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3484 | if (!mask) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3485 | /* No permission to check. Existence test. */ |
| 3486 | return 0; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3487 | |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3488 | isec = inode_security(inode); |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3489 | if (sid == fsec->sid && fsec->isid == isec->sid && |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3490 | fsec->pseqno == avc_policy_seqno(&selinux_state)) |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 3491 | /* No change since file_open check. */ |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3492 | return 0; |
| 3493 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3494 | return selinux_revalidate_file_permission(file, mask); |
| 3495 | } |
| 3496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | static int selinux_file_alloc_security(struct file *file) |
| 3498 | { |
| 3499 | return file_alloc_security(file); |
| 3500 | } |
| 3501 | |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3502 | /* |
| 3503 | * Check whether a task has the ioctl permission and cmd |
| 3504 | * operation to an inode. |
| 3505 | */ |
Geliang Tang | 1d2a168 | 2015-10-21 17:44:27 -0400 | [diff] [blame] | 3506 | 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] | 3507 | u32 requested, u16 cmd) |
| 3508 | { |
| 3509 | struct common_audit_data ad; |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3510 | struct file_security_struct *fsec = selinux_file(file); |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3511 | struct inode *inode = file_inode(file); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3512 | struct inode_security_struct *isec; |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3513 | struct lsm_ioctlop_audit ioctl; |
| 3514 | u32 ssid = cred_sid(cred); |
| 3515 | int rc; |
| 3516 | u8 driver = cmd >> 8; |
| 3517 | u8 xperm = cmd & 0xff; |
| 3518 | |
| 3519 | ad.type = LSM_AUDIT_DATA_IOCTL_OP; |
| 3520 | ad.u.op = &ioctl; |
| 3521 | ad.u.op->cmd = cmd; |
| 3522 | ad.u.op->path = file->f_path; |
| 3523 | |
| 3524 | if (ssid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3525 | rc = avc_has_perm(&selinux_state, |
| 3526 | ssid, fsec->sid, |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3527 | SECCLASS_FD, |
| 3528 | FD__USE, |
| 3529 | &ad); |
| 3530 | if (rc) |
| 3531 | goto out; |
| 3532 | } |
| 3533 | |
| 3534 | if (unlikely(IS_PRIVATE(inode))) |
| 3535 | return 0; |
| 3536 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3537 | isec = inode_security(inode); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3538 | rc = avc_has_extended_perms(&selinux_state, |
| 3539 | ssid, isec->sid, isec->sclass, |
| 3540 | requested, driver, xperm, &ad); |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3541 | out: |
| 3542 | return rc; |
| 3543 | } |
| 3544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, |
| 3546 | unsigned long arg) |
| 3547 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3548 | const struct cred *cred = current_cred(); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3549 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3551 | switch (cmd) { |
| 3552 | case FIONREAD: |
| 3553 | /* fall through */ |
| 3554 | case FIBMAP: |
| 3555 | /* fall through */ |
| 3556 | case FIGETBSZ: |
| 3557 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3558 | case FS_IOC_GETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3559 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3560 | case FS_IOC_GETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3561 | error = file_has_perm(cred, file, FILE__GETATTR); |
| 3562 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3564 | case FS_IOC_SETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3565 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3566 | case FS_IOC_SETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3567 | error = file_has_perm(cred, file, FILE__SETATTR); |
| 3568 | break; |
| 3569 | |
| 3570 | /* sys_ioctl() checks */ |
| 3571 | case FIONBIO: |
| 3572 | /* fall through */ |
| 3573 | case FIOASYNC: |
| 3574 | error = file_has_perm(cred, file, 0); |
| 3575 | break; |
| 3576 | |
| 3577 | case KDSKBENT: |
| 3578 | case KDSKBSENT: |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3579 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3580 | CAP_OPT_NONE, true); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3581 | break; |
| 3582 | |
| 3583 | /* default case assumes that the command will go |
| 3584 | * to the file's ioctl() function. |
| 3585 | */ |
| 3586 | default: |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3587 | error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3588 | } |
| 3589 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3592 | static int default_noexec; |
| 3593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
| 3595 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3596 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3597 | u32 sid = cred_sid(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3598 | int rc = 0; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3599 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3600 | if (default_noexec && |
Stephen Smalley | 892e8ca | 2015-07-10 09:40:59 -0400 | [diff] [blame] | 3601 | (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || |
| 3602 | (!shared && (prot & PROT_WRITE)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | /* |
| 3604 | * We are making executable an anonymous mapping or a |
| 3605 | * private file mapping that will also be writable. |
| 3606 | * This has an additional check. |
| 3607 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3608 | rc = avc_has_perm(&selinux_state, |
| 3609 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3610 | PROCESS__EXECMEM, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | if (rc) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3612 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | |
| 3615 | if (file) { |
| 3616 | /* read access is always possible with a mapping */ |
| 3617 | u32 av = FILE__READ; |
| 3618 | |
| 3619 | /* write access only matters if the mapping is shared */ |
| 3620 | if (shared && (prot & PROT_WRITE)) |
| 3621 | av |= FILE__WRITE; |
| 3622 | |
| 3623 | if (prot & PROT_EXEC) |
| 3624 | av |= FILE__EXECUTE; |
| 3625 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3626 | return file_has_perm(cred, file, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3628 | |
| 3629 | error: |
| 3630 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3633 | static int selinux_mmap_addr(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 3635 | int rc = 0; |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3636 | |
| 3637 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
| 3638 | u32 sid = current_sid(); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3639 | rc = avc_has_perm(&selinux_state, |
| 3640 | sid, sid, SECCLASS_MEMPROTECT, |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3641 | MEMPROTECT__MMAP_ZERO, NULL); |
| 3642 | } |
| 3643 | |
| 3644 | return rc; |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3647 | static int selinux_mmap_file(struct file *file, unsigned long reqprot, |
| 3648 | unsigned long prot, unsigned long flags) |
| 3649 | { |
Stephen Smalley | 3ba4bf5 | 2017-05-05 09:14:48 -0400 | [diff] [blame] | 3650 | struct common_audit_data ad; |
| 3651 | int rc; |
| 3652 | |
| 3653 | if (file) { |
| 3654 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3655 | ad.u.file = file; |
| 3656 | rc = inode_has_perm(current_cred(), file_inode(file), |
| 3657 | FILE__MAP, &ad); |
| 3658 | if (rc) |
| 3659 | return rc; |
| 3660 | } |
| 3661 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3662 | if (selinux_state.checkreqprot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | prot = reqprot; |
| 3664 | |
| 3665 | return file_map_prot_check(file, prot, |
| 3666 | (flags & MAP_TYPE) == MAP_SHARED); |
| 3667 | } |
| 3668 | |
| 3669 | static int selinux_file_mprotect(struct vm_area_struct *vma, |
| 3670 | unsigned long reqprot, |
| 3671 | unsigned long prot) |
| 3672 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3673 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3674 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3676 | if (selinux_state.checkreqprot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3677 | prot = reqprot; |
| 3678 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3679 | if (default_noexec && |
| 3680 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { |
James Morris | d541bbe | 2009-01-29 12:19:51 +1100 | [diff] [blame] | 3681 | int rc = 0; |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3682 | if (vma->vm_start >= vma->vm_mm->start_brk && |
| 3683 | vma->vm_end <= vma->vm_mm->brk) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3684 | rc = avc_has_perm(&selinux_state, |
| 3685 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3686 | PROCESS__EXECHEAP, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3687 | } else if (!vma->vm_file && |
Stephen Smalley | c2316db | 2016-04-08 13:55:03 -0400 | [diff] [blame] | 3688 | ((vma->vm_start <= vma->vm_mm->start_stack && |
| 3689 | vma->vm_end >= vma->vm_mm->start_stack) || |
Andy Lutomirski | d17af50 | 2016-09-30 10:58:58 -0700 | [diff] [blame] | 3690 | vma_is_stack_for_current(vma))) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3691 | rc = avc_has_perm(&selinux_state, |
| 3692 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3693 | PROCESS__EXECSTACK, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3694 | } else if (vma->vm_file && vma->anon_vma) { |
| 3695 | /* |
| 3696 | * We are making executable a file mapping that has |
| 3697 | * had some COW done. Since pages might have been |
| 3698 | * written, check ability to execute the possibly |
| 3699 | * modified content. This typically should only |
| 3700 | * occur for text relocations. |
| 3701 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3702 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3703 | } |
Lorenzo Hernandez GarcÃa-Hierro | 6b99219 | 2005-06-25 14:54:34 -0700 | [diff] [blame] | 3704 | if (rc) |
| 3705 | return rc; |
| 3706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | |
| 3708 | return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); |
| 3709 | } |
| 3710 | |
| 3711 | static int selinux_file_lock(struct file *file, unsigned int cmd) |
| 3712 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3713 | const struct cred *cred = current_cred(); |
| 3714 | |
| 3715 | return file_has_perm(cred, file, FILE__LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | static int selinux_file_fcntl(struct file *file, unsigned int cmd, |
| 3719 | unsigned long arg) |
| 3720 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3721 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | int err = 0; |
| 3723 | |
| 3724 | switch (cmd) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3725 | case F_SETFL: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3726 | if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3727 | err = file_has_perm(cred, file, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3729 | } |
| 3730 | /* fall through */ |
| 3731 | case F_SETOWN: |
| 3732 | case F_SETSIG: |
| 3733 | case F_GETFL: |
| 3734 | case F_GETOWN: |
| 3735 | case F_GETSIG: |
Cyrill Gorcunov | 1d151c3 | 2012-07-30 14:43:00 -0700 | [diff] [blame] | 3736 | case F_GETOWNER_UIDS: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3737 | /* Just check FD__USE permission */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3738 | err = file_has_perm(cred, file, 0); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3739 | break; |
| 3740 | case F_GETLK: |
| 3741 | case F_SETLK: |
| 3742 | case F_SETLKW: |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 3743 | case F_OFD_GETLK: |
| 3744 | case F_OFD_SETLK: |
| 3745 | case F_OFD_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | #if BITS_PER_LONG == 32 |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3747 | case F_GETLK64: |
| 3748 | case F_SETLK64: |
| 3749 | case F_SETLKW64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | #endif |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3751 | err = file_has_perm(cred, file, FILE__LOCK); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3752 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | return err; |
| 3756 | } |
| 3757 | |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 3758 | static void selinux_file_set_fowner(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | struct file_security_struct *fsec; |
| 3761 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3762 | fsec = selinux_file(file); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3763 | fsec->fown_sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | static int selinux_file_send_sigiotask(struct task_struct *tsk, |
| 3767 | struct fown_struct *fown, int signum) |
| 3768 | { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3769 | struct file *file; |
Stephen Smalley | 65c90bc | 2009-05-04 15:43:18 -0400 | [diff] [blame] | 3770 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | struct file_security_struct *fsec; |
| 3773 | |
| 3774 | /* struct fown_struct is never outside the context of a struct file */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3775 | file = container_of(fown, struct file, f_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3777 | fsec = selinux_file(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | |
| 3779 | if (!signum) |
| 3780 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| 3781 | else |
| 3782 | perm = signal_to_av(signum); |
| 3783 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3784 | return avc_has_perm(&selinux_state, |
| 3785 | fsec->fown_sid, sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | SECCLASS_PROCESS, perm, NULL); |
| 3787 | } |
| 3788 | |
| 3789 | static int selinux_file_receive(struct file *file) |
| 3790 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3791 | const struct cred *cred = current_cred(); |
| 3792 | |
| 3793 | return file_has_perm(cred, file, file_to_av(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3796 | static int selinux_file_open(struct file *file) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3797 | { |
| 3798 | struct file_security_struct *fsec; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3799 | struct inode_security_struct *isec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3800 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3801 | fsec = selinux_file(file); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3802 | isec = inode_security(file_inode(file)); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3803 | /* |
| 3804 | * Save inode label and policy sequence number |
| 3805 | * at open-time so that selinux_file_permission |
| 3806 | * can determine whether revalidation is necessary. |
| 3807 | * Task label is already saved in the file security |
| 3808 | * struct as its SID. |
| 3809 | */ |
| 3810 | fsec->isid = isec->sid; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3811 | fsec->pseqno = avc_policy_seqno(&selinux_state); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3812 | /* |
| 3813 | * Since the inode label or policy seqno may have changed |
| 3814 | * between the selinux_inode_permission check and the saving |
| 3815 | * of state above, recheck that access is still permitted. |
| 3816 | * Otherwise, access might never be revalidated against the |
| 3817 | * new inode label or new policy. |
| 3818 | * This check is not redundant - do not remove. |
| 3819 | */ |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3820 | 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] | 3821 | } |
| 3822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | /* task security operations */ |
| 3824 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 3825 | static int selinux_task_alloc(struct task_struct *task, |
| 3826 | unsigned long clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3828 | u32 sid = current_sid(); |
| 3829 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3830 | return avc_has_perm(&selinux_state, |
| 3831 | sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | } |
| 3833 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3834 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3835 | * prepare a new set of credentials for modification |
| 3836 | */ |
| 3837 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3838 | gfp_t gfp) |
| 3839 | { |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 3840 | const struct task_security_struct *old_tsec = selinux_cred(old); |
| 3841 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3842 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 3843 | *tsec = *old_tsec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3844 | return 0; |
| 3845 | } |
| 3846 | |
| 3847 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3848 | * transfer the SELinux data to a blank set of creds |
| 3849 | */ |
| 3850 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3851 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3852 | const struct task_security_struct *old_tsec = selinux_cred(old); |
| 3853 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3854 | |
| 3855 | *tsec = *old_tsec; |
| 3856 | } |
| 3857 | |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 3858 | static void selinux_cred_getsecid(const struct cred *c, u32 *secid) |
| 3859 | { |
| 3860 | *secid = cred_sid(c); |
| 3861 | } |
| 3862 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3863 | /* |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3864 | * set the security data for a kernel service |
| 3865 | * - all the creation contexts are set to unlabelled |
| 3866 | */ |
| 3867 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3868 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3869 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3870 | u32 sid = current_sid(); |
| 3871 | int ret; |
| 3872 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3873 | ret = avc_has_perm(&selinux_state, |
| 3874 | sid, secid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3875 | SECCLASS_KERNEL_SERVICE, |
| 3876 | KERNEL_SERVICE__USE_AS_OVERRIDE, |
| 3877 | NULL); |
| 3878 | if (ret == 0) { |
| 3879 | tsec->sid = secid; |
| 3880 | tsec->create_sid = 0; |
| 3881 | tsec->keycreate_sid = 0; |
| 3882 | tsec->sockcreate_sid = 0; |
| 3883 | } |
| 3884 | return ret; |
| 3885 | } |
| 3886 | |
| 3887 | /* |
| 3888 | * set the file creation context in a security record to the same as the |
| 3889 | * objective context of the specified inode |
| 3890 | */ |
| 3891 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3892 | { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3893 | struct inode_security_struct *isec = inode_security(inode); |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3894 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3895 | u32 sid = current_sid(); |
| 3896 | int ret; |
| 3897 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3898 | ret = avc_has_perm(&selinux_state, |
| 3899 | sid, isec->sid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3900 | SECCLASS_KERNEL_SERVICE, |
| 3901 | KERNEL_SERVICE__CREATE_FILES_AS, |
| 3902 | NULL); |
| 3903 | |
| 3904 | if (ret == 0) |
| 3905 | tsec->create_sid = isec->sid; |
David Howells | ef57471 | 2010-02-26 01:56:16 +0000 | [diff] [blame] | 3906 | return ret; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3907 | } |
| 3908 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3909 | static int selinux_kernel_module_request(char *kmod_name) |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3910 | { |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3911 | struct common_audit_data ad; |
| 3912 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3913 | ad.type = LSM_AUDIT_DATA_KMOD; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3914 | ad.u.kmod_name = kmod_name; |
| 3915 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3916 | return avc_has_perm(&selinux_state, |
| 3917 | current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3918 | SYSTEM__MODULE_REQUEST, &ad); |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3919 | } |
| 3920 | |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3921 | static int selinux_kernel_module_from_file(struct file *file) |
| 3922 | { |
| 3923 | struct common_audit_data ad; |
| 3924 | struct inode_security_struct *isec; |
| 3925 | struct file_security_struct *fsec; |
| 3926 | u32 sid = current_sid(); |
| 3927 | int rc; |
| 3928 | |
| 3929 | /* init_module */ |
| 3930 | if (file == NULL) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3931 | return avc_has_perm(&selinux_state, |
| 3932 | sid, sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3933 | SYSTEM__MODULE_LOAD, NULL); |
| 3934 | |
| 3935 | /* finit_module */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3936 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 3937 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3938 | ad.u.file = file; |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3939 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3940 | fsec = selinux_file(file); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3941 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3942 | rc = avc_has_perm(&selinux_state, |
| 3943 | sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3944 | if (rc) |
| 3945 | return rc; |
| 3946 | } |
| 3947 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3948 | isec = inode_security(file_inode(file)); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3949 | return avc_has_perm(&selinux_state, |
| 3950 | sid, isec->sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3951 | SYSTEM__MODULE_LOAD, &ad); |
| 3952 | } |
| 3953 | |
| 3954 | static int selinux_kernel_read_file(struct file *file, |
| 3955 | enum kernel_read_file_id id) |
| 3956 | { |
| 3957 | int rc = 0; |
| 3958 | |
| 3959 | switch (id) { |
| 3960 | case READING_MODULE: |
| 3961 | rc = selinux_kernel_module_from_file(file); |
| 3962 | break; |
| 3963 | default: |
| 3964 | break; |
| 3965 | } |
| 3966 | |
| 3967 | return rc; |
| 3968 | } |
| 3969 | |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 3970 | static int selinux_kernel_load_data(enum kernel_load_data_id id) |
| 3971 | { |
| 3972 | int rc = 0; |
| 3973 | |
| 3974 | switch (id) { |
| 3975 | case LOADING_MODULE: |
| 3976 | rc = selinux_kernel_module_from_file(NULL); |
| 3977 | default: |
| 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | return rc; |
| 3982 | } |
| 3983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) |
| 3985 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3986 | return avc_has_perm(&selinux_state, |
| 3987 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3988 | PROCESS__SETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | static int selinux_task_getpgid(struct task_struct *p) |
| 3992 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3993 | return avc_has_perm(&selinux_state, |
| 3994 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3995 | PROCESS__GETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | static int selinux_task_getsid(struct task_struct *p) |
| 3999 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4000 | return avc_has_perm(&selinux_state, |
| 4001 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4002 | PROCESS__GETSESSION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 4005 | static void selinux_task_getsecid(struct task_struct *p, u32 *secid) |
| 4006 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4007 | *secid = task_sid(p); |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | static int selinux_task_setnice(struct task_struct *p, int nice) |
| 4011 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4012 | return avc_has_perm(&selinux_state, |
| 4013 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4014 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 4017 | static int selinux_task_setioprio(struct task_struct *p, int ioprio) |
| 4018 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4019 | return avc_has_perm(&selinux_state, |
| 4020 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4021 | PROCESS__SETSCHED, NULL); |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 4022 | } |
| 4023 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 4024 | static int selinux_task_getioprio(struct task_struct *p) |
| 4025 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4026 | return avc_has_perm(&selinux_state, |
| 4027 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4028 | PROCESS__GETSCHED, NULL); |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
Corentin LABBE | 4298555 | 2017-10-04 20:32:18 +0200 | [diff] [blame] | 4031 | static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, |
| 4032 | unsigned int flags) |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 4033 | { |
| 4034 | u32 av = 0; |
| 4035 | |
Stephen Smalley | 84e6885 | 2017-02-28 09:35:08 -0500 | [diff] [blame] | 4036 | if (!flags) |
| 4037 | return 0; |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 4038 | if (flags & LSM_PRLIMIT_WRITE) |
| 4039 | av |= PROCESS__SETRLIMIT; |
| 4040 | if (flags & LSM_PRLIMIT_READ) |
| 4041 | av |= PROCESS__GETRLIMIT; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4042 | return avc_has_perm(&selinux_state, |
| 4043 | cred_sid(cred), cred_sid(tcred), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 4044 | SECCLASS_PROCESS, av, NULL); |
| 4045 | } |
| 4046 | |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 4047 | static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, |
| 4048 | struct rlimit *new_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | { |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 4050 | struct rlimit *old_rlim = p->signal->rlim + resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | |
| 4052 | /* Control the ability to change the hard limit (whether |
| 4053 | lowering or raising it), so that the hard limit can |
| 4054 | later be used as a safe reset point for the soft limit |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4055 | upon context transitions. See selinux_bprm_committing_creds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | if (old_rlim->rlim_max != new_rlim->rlim_max) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4057 | return avc_has_perm(&selinux_state, |
| 4058 | current_sid(), task_sid(p), |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4059 | SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | |
| 4061 | return 0; |
| 4062 | } |
| 4063 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 4064 | static int selinux_task_setscheduler(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4066 | return avc_has_perm(&selinux_state, |
| 4067 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4068 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | static int selinux_task_getscheduler(struct task_struct *p) |
| 4072 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4073 | return avc_has_perm(&selinux_state, |
| 4074 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4075 | PROCESS__GETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | } |
| 4077 | |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 4078 | static int selinux_task_movememory(struct task_struct *p) |
| 4079 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4080 | return avc_has_perm(&selinux_state, |
| 4081 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4082 | PROCESS__SETSCHED, NULL); |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Eric W. Biederman | ae7795b | 2018-09-25 11:27:20 +0200 | [diff] [blame] | 4085 | 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] | 4086 | int sig, const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | { |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4088 | u32 secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | if (!sig) |
| 4092 | perm = PROCESS__SIGNULL; /* null signal; existence test */ |
| 4093 | else |
| 4094 | perm = signal_to_av(sig); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4095 | if (!cred) |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4096 | secid = current_sid(); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4097 | else |
| 4098 | secid = cred_sid(cred); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4099 | return avc_has_perm(&selinux_state, |
| 4100 | secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | static void selinux_task_to_inode(struct task_struct *p, |
| 4104 | struct inode *inode) |
| 4105 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 4106 | struct inode_security_struct *isec = selinux_inode(inode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4107 | u32 sid = task_sid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4109 | spin_lock(&isec->lock); |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 4110 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4111 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4112 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4113 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4117 | static int selinux_parse_skb_ipv4(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4118 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | { |
| 4120 | int offset, ihlen, ret = -EINVAL; |
| 4121 | struct iphdr _iph, *ih; |
| 4122 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4123 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); |
| 4125 | if (ih == NULL) |
| 4126 | goto out; |
| 4127 | |
| 4128 | ihlen = ih->ihl * 4; |
| 4129 | if (ihlen < sizeof(_iph)) |
| 4130 | goto out; |
| 4131 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4132 | ad->u.net->v4info.saddr = ih->saddr; |
| 4133 | ad->u.net->v4info.daddr = ih->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | ret = 0; |
| 4135 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4136 | if (proto) |
| 4137 | *proto = ih->protocol; |
| 4138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | switch (ih->protocol) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4140 | case IPPROTO_TCP: { |
| 4141 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4143 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4144 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | |
| 4146 | offset += ihlen; |
| 4147 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4148 | if (th == NULL) |
| 4149 | break; |
| 4150 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4151 | ad->u.net->sport = th->source; |
| 4152 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4156 | case IPPROTO_UDP: { |
| 4157 | struct udphdr _udph, *uh; |
| 4158 | |
| 4159 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4160 | break; |
| 4161 | |
| 4162 | offset += ihlen; |
| 4163 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4164 | if (uh == NULL) |
| 4165 | break; |
| 4166 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4167 | ad->u.net->sport = uh->source; |
| 4168 | ad->u.net->dport = uh->dest; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4169 | break; |
| 4170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4172 | case IPPROTO_DCCP: { |
| 4173 | struct dccp_hdr _dccph, *dh; |
| 4174 | |
| 4175 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4176 | break; |
| 4177 | |
| 4178 | offset += ihlen; |
| 4179 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4180 | if (dh == NULL) |
| 4181 | break; |
| 4182 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4183 | ad->u.net->sport = dh->dccph_sport; |
| 4184 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4185 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4186 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4187 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4188 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4189 | case IPPROTO_SCTP: { |
| 4190 | struct sctphdr _sctph, *sh; |
| 4191 | |
| 4192 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4193 | break; |
| 4194 | |
| 4195 | offset += ihlen; |
| 4196 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4197 | if (sh == NULL) |
| 4198 | break; |
| 4199 | |
| 4200 | ad->u.net->sport = sh->source; |
| 4201 | ad->u.net->dport = sh->dest; |
| 4202 | break; |
| 4203 | } |
| 4204 | #endif |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4205 | default: |
| 4206 | break; |
| 4207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | out: |
| 4209 | return ret; |
| 4210 | } |
| 4211 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4212 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
| 4214 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4215 | static int selinux_parse_skb_ipv6(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4216 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | { |
| 4218 | u8 nexthdr; |
| 4219 | int ret = -EINVAL, offset; |
| 4220 | struct ipv6hdr _ipv6h, *ip6; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4221 | __be16 frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4223 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 4225 | if (ip6 == NULL) |
| 4226 | goto out; |
| 4227 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4228 | ad->u.net->v6info.saddr = ip6->saddr; |
| 4229 | ad->u.net->v6info.daddr = ip6->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | ret = 0; |
| 4231 | |
| 4232 | nexthdr = ip6->nexthdr; |
| 4233 | offset += sizeof(_ipv6h); |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4234 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | if (offset < 0) |
| 4236 | goto out; |
| 4237 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4238 | if (proto) |
| 4239 | *proto = nexthdr; |
| 4240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | switch (nexthdr) { |
| 4242 | case IPPROTO_TCP: { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4243 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | |
| 4245 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4246 | if (th == NULL) |
| 4247 | break; |
| 4248 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4249 | ad->u.net->sport = th->source; |
| 4250 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | break; |
| 4252 | } |
| 4253 | |
| 4254 | case IPPROTO_UDP: { |
| 4255 | struct udphdr _udph, *uh; |
| 4256 | |
| 4257 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4258 | if (uh == NULL) |
| 4259 | break; |
| 4260 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4261 | ad->u.net->sport = uh->source; |
| 4262 | ad->u.net->dport = uh->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | break; |
| 4264 | } |
| 4265 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4266 | case IPPROTO_DCCP: { |
| 4267 | struct dccp_hdr _dccph, *dh; |
| 4268 | |
| 4269 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4270 | if (dh == NULL) |
| 4271 | break; |
| 4272 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4273 | ad->u.net->sport = dh->dccph_sport; |
| 4274 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4275 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4276 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4277 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4278 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4279 | case IPPROTO_SCTP: { |
| 4280 | struct sctphdr _sctph, *sh; |
| 4281 | |
| 4282 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4283 | if (sh == NULL) |
| 4284 | break; |
| 4285 | |
| 4286 | ad->u.net->sport = sh->source; |
| 4287 | ad->u.net->dport = sh->dest; |
| 4288 | break; |
| 4289 | } |
| 4290 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | /* includes fragments */ |
| 4292 | default: |
| 4293 | break; |
| 4294 | } |
| 4295 | out: |
| 4296 | return ret; |
| 4297 | } |
| 4298 | |
| 4299 | #endif /* IPV6 */ |
| 4300 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4301 | 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] | 4302 | char **_addrp, int src, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | { |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4304 | char *addrp; |
| 4305 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4307 | switch (ad->u.net->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | case PF_INET: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4309 | ret = selinux_parse_skb_ipv4(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4310 | if (ret) |
| 4311 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4312 | addrp = (char *)(src ? &ad->u.net->v4info.saddr : |
| 4313 | &ad->u.net->v4info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4314 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4316 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | case PF_INET6: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4318 | ret = selinux_parse_skb_ipv6(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4319 | if (ret) |
| 4320 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4321 | addrp = (char *)(src ? &ad->u.net->v6info.saddr : |
| 4322 | &ad->u.net->v6info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4323 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | #endif /* IPV6 */ |
| 4325 | default: |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4326 | addrp = NULL; |
| 4327 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | } |
| 4329 | |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4330 | parse_error: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4331 | pr_warn( |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4332 | "SELinux: failure in selinux_parse_skb()," |
| 4333 | " unable to parse packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | return ret; |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4335 | |
| 4336 | okay: |
| 4337 | if (_addrp) |
| 4338 | *_addrp = addrp; |
| 4339 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | } |
| 4341 | |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4342 | /** |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4343 | * selinux_skb_peerlbl_sid - Determine the peer label of a packet |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4344 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4345 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4346 | * @sid: the packet's peer label SID |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4347 | * |
| 4348 | * Description: |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4349 | * Check the various different forms of network peer labeling and determine |
| 4350 | * the peer label/SID for the packet; most of the magic actually occurs in |
| 4351 | * the security server function security_net_peersid_cmp(). The function |
| 4352 | * returns zero if the value in @sid is valid (although it may be SECSID_NULL) |
| 4353 | * or -EACCES if @sid is invalid due to inconsistencies with the different |
| 4354 | * peer labels. |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4355 | * |
| 4356 | */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4357 | 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] | 4358 | { |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4359 | int err; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4360 | u32 xfrm_sid; |
| 4361 | u32 nlbl_sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4362 | u32 nlbl_type; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4363 | |
Paul Moore | 817eff7 | 2013-12-10 14:57:54 -0500 | [diff] [blame] | 4364 | err = selinux_xfrm_skb_sid(skb, &xfrm_sid); |
Paul Moore | bed4d7e | 2013-07-23 17:38:40 -0400 | [diff] [blame] | 4365 | if (unlikely(err)) |
| 4366 | return -EACCES; |
| 4367 | err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); |
| 4368 | if (unlikely(err)) |
| 4369 | return -EACCES; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4370 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4371 | err = security_net_peersid_resolve(&selinux_state, nlbl_sid, |
| 4372 | nlbl_type, xfrm_sid, sid); |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4373 | if (unlikely(err)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4374 | pr_warn( |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4375 | "SELinux: failure in selinux_skb_peerlbl_sid()," |
| 4376 | " unable to determine packet's peer label\n"); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4377 | return -EACCES; |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4378 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4379 | |
| 4380 | return 0; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4381 | } |
| 4382 | |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4383 | /** |
| 4384 | * selinux_conn_sid - Determine the child socket label for a connection |
| 4385 | * @sk_sid: the parent socket's SID |
| 4386 | * @skb_sid: the packet's SID |
| 4387 | * @conn_sid: the resulting connection SID |
| 4388 | * |
| 4389 | * If @skb_sid is valid then the user:role:type information from @sk_sid is |
| 4390 | * combined with the MLS information from @skb_sid in order to create |
| 4391 | * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy |
| 4392 | * of @sk_sid. Returns zero on success, negative values on failure. |
| 4393 | * |
| 4394 | */ |
| 4395 | static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) |
| 4396 | { |
| 4397 | int err = 0; |
| 4398 | |
| 4399 | if (skb_sid != SECSID_NULL) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4400 | err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, |
| 4401 | conn_sid); |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4402 | else |
| 4403 | *conn_sid = sk_sid; |
| 4404 | |
| 4405 | return err; |
| 4406 | } |
| 4407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | /* socket security operations */ |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4409 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4410 | static int socket_sockcreate_sid(const struct task_security_struct *tsec, |
| 4411 | u16 secclass, u32 *socksid) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4412 | { |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4413 | if (tsec->sockcreate_sid > SECSID_NULL) { |
| 4414 | *socksid = tsec->sockcreate_sid; |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4418 | return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, |
| 4419 | secclass, NULL, socksid); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4420 | } |
| 4421 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4422 | static int sock_has_perm(struct sock *sk, u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4424 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4425 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4426 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4428 | if (sksec->sid == SECINITSID_KERNEL) |
| 4429 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4431 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4432 | ad.u.net = &net; |
| 4433 | ad.u.net->sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4435 | return avc_has_perm(&selinux_state, |
| 4436 | current_sid(), sksec->sid, sksec->sclass, perms, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4437 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | static int selinux_socket_create(int family, int type, |
| 4441 | int protocol, int kern) |
| 4442 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 4443 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4444 | u32 newsid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4445 | u16 secclass; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4446 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | |
| 4448 | if (kern) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4449 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4451 | secclass = socket_type_to_security_class(family, type, protocol); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4452 | rc = socket_sockcreate_sid(tsec, secclass, &newsid); |
| 4453 | if (rc) |
| 4454 | return rc; |
| 4455 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4456 | return avc_has_perm(&selinux_state, |
| 4457 | tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | } |
| 4459 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4460 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 4461 | int type, int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 4463 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4464 | struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4465 | struct sk_security_struct *sksec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4466 | u16 sclass = socket_type_to_security_class(family, type, protocol); |
| 4467 | u32 sid = SECINITSID_KERNEL; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4468 | int err = 0; |
| 4469 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4470 | if (!kern) { |
| 4471 | err = socket_sockcreate_sid(tsec, sclass, &sid); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4472 | if (err) |
| 4473 | return err; |
| 4474 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4475 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4476 | isec->sclass = sclass; |
| 4477 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4478 | isec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4480 | if (sock->sk) { |
| 4481 | sksec = sock->sk->sk_security; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4482 | sksec->sclass = sclass; |
| 4483 | sksec->sid = sid; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4484 | /* Allows detection of the first association on this socket */ |
| 4485 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4486 | sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; |
| 4487 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4488 | err = selinux_netlbl_socket_post_create(sock->sk, family); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4489 | } |
| 4490 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4491 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | } |
| 4493 | |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 4494 | static int selinux_socket_socketpair(struct socket *socka, |
| 4495 | struct socket *sockb) |
| 4496 | { |
| 4497 | struct sk_security_struct *sksec_a = socka->sk->sk_security; |
| 4498 | struct sk_security_struct *sksec_b = sockb->sk->sk_security; |
| 4499 | |
| 4500 | sksec_a->peer_sid = sksec_b->sid; |
| 4501 | sksec_b->peer_sid = sksec_a->sid; |
| 4502 | |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | /* Range of port numbers used to automatically bind. |
| 4507 | Need to determine whether we should perform a name_bind |
| 4508 | permission check between the socket and the port number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | |
| 4510 | static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4511 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4512 | struct sock *sk = sock->sk; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4513 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | u16 family; |
| 4515 | int err; |
| 4516 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4517 | err = sock_has_perm(sk, SOCKET__BIND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | if (err) |
| 4519 | goto out; |
| 4520 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4521 | /* 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] | 4522 | family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | if (family == PF_INET || family == PF_INET6) { |
| 4524 | char *addrp; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4525 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4526 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | struct sockaddr_in *addr4 = NULL; |
| 4528 | struct sockaddr_in6 *addr6 = NULL; |
Tetsuo Handa | c750e69 | 2019-04-12 19:59:34 +0900 | [diff] [blame] | 4529 | u16 family_sa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | unsigned short snum; |
James Morris | e399f98 | 2008-06-12 01:39:58 +1000 | [diff] [blame] | 4531 | u32 sid, node_perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4533 | /* |
| 4534 | * sctp_bindx(3) calls via selinux_sctp_bind_connect() |
| 4535 | * that validates multiple binding addresses. Because of this |
| 4536 | * need to check address->sa_family as it is possible to have |
| 4537 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4538 | */ |
Tetsuo Handa | c750e69 | 2019-04-12 19:59:34 +0900 | [diff] [blame] | 4539 | if (addrlen < offsetofend(struct sockaddr, sa_family)) |
| 4540 | return -EINVAL; |
| 4541 | family_sa = address->sa_family; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4542 | switch (family_sa) { |
| 4543 | case AF_UNSPEC: |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4544 | case AF_INET: |
| 4545 | if (addrlen < sizeof(struct sockaddr_in)) |
| 4546 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | addr4 = (struct sockaddr_in *)address; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4548 | if (family_sa == AF_UNSPEC) { |
| 4549 | /* see __inet_bind(), we only want to allow |
| 4550 | * AF_UNSPEC if the address is INADDR_ANY |
| 4551 | */ |
| 4552 | if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) |
| 4553 | goto err_af; |
| 4554 | family_sa = AF_INET; |
| 4555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | snum = ntohs(addr4->sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | addrp = (char *)&addr4->sin_addr.s_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4558 | break; |
| 4559 | case AF_INET6: |
| 4560 | if (addrlen < SIN6_LEN_RFC2133) |
| 4561 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | addr6 = (struct sockaddr_in6 *)address; |
| 4563 | snum = ntohs(addr6->sin6_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | addrp = (char *)&addr6->sin6_addr.s6_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4565 | break; |
| 4566 | default: |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4567 | goto err_af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4570 | ad.type = LSM_AUDIT_DATA_NET; |
| 4571 | ad.u.net = &net; |
| 4572 | ad.u.net->sport = htons(snum); |
| 4573 | ad.u.net->family = family_sa; |
| 4574 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4575 | if (snum) { |
| 4576 | int low, high; |
| 4577 | |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 4578 | inet_get_local_port_range(sock_net(sk), &low, &high); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4579 | |
Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 4580 | if (snum < max(inet_prot_sock(sock_net(sk)), low) || |
| 4581 | snum > high) { |
Paul Moore | 3e112172 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4582 | err = sel_netport_sid(sk->sk_protocol, |
| 4583 | snum, &sid); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4584 | if (err) |
| 4585 | goto out; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4586 | err = avc_has_perm(&selinux_state, |
| 4587 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4588 | sksec->sclass, |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4589 | SOCKET__NAME_BIND, &ad); |
| 4590 | if (err) |
| 4591 | goto out; |
| 4592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4594 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4595 | switch (sksec->sclass) { |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4596 | case SECCLASS_TCP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | node_perm = TCP_SOCKET__NODE_BIND; |
| 4598 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4599 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4600 | case SECCLASS_UDP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | node_perm = UDP_SOCKET__NODE_BIND; |
| 4602 | break; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4603 | |
| 4604 | case SECCLASS_DCCP_SOCKET: |
| 4605 | node_perm = DCCP_SOCKET__NODE_BIND; |
| 4606 | break; |
| 4607 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4608 | case SECCLASS_SCTP_SOCKET: |
| 4609 | node_perm = SCTP_SOCKET__NODE_BIND; |
| 4610 | break; |
| 4611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | default: |
| 4613 | node_perm = RAWIP_SOCKET__NODE_BIND; |
| 4614 | break; |
| 4615 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4616 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4617 | err = sel_netnode_sid(addrp, family_sa, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | if (err) |
| 4619 | goto out; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4620 | |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4621 | if (family_sa == AF_INET) |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4622 | ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | else |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4624 | ad.u.net->v6info.saddr = addr6->sin6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4626 | err = avc_has_perm(&selinux_state, |
| 4627 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4628 | sksec->sclass, node_perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | if (err) |
| 4630 | goto out; |
| 4631 | } |
| 4632 | out: |
| 4633 | return err; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4634 | err_af: |
| 4635 | /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ |
| 4636 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4637 | return -EINVAL; |
| 4638 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4641 | /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) |
Kees Cook | d61330c | 2019-02-17 14:08:36 -0800 | [diff] [blame] | 4642 | * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4643 | */ |
| 4644 | static int selinux_socket_connect_helper(struct socket *sock, |
| 4645 | struct sockaddr *address, int addrlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4647 | struct sock *sk = sock->sk; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4648 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | int err; |
| 4650 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4651 | err = sock_has_perm(sk, SOCKET__CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | if (err) |
| 4653 | return err; |
Paolo Abeni | 05174c9 | 2019-05-10 19:12:33 +0200 | [diff] [blame] | 4654 | if (addrlen < offsetofend(struct sockaddr, sa_family)) |
| 4655 | return -EINVAL; |
| 4656 | |
| 4657 | /* connect(AF_UNSPEC) has special handling, as it is a documented |
| 4658 | * way to disconnect the socket |
| 4659 | */ |
| 4660 | if (address->sa_family == AF_UNSPEC) |
| 4661 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | |
| 4663 | /* |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4664 | * If a TCP, DCCP or SCTP socket, check name_connect permission |
| 4665 | * for the port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4667 | if (sksec->sclass == SECCLASS_TCP_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4668 | sksec->sclass == SECCLASS_DCCP_SOCKET || |
| 4669 | sksec->sclass == SECCLASS_SCTP_SOCKET) { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4670 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4671 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | struct sockaddr_in *addr4 = NULL; |
| 4673 | struct sockaddr_in6 *addr6 = NULL; |
| 4674 | unsigned short snum; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4675 | u32 sid, perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4677 | /* sctp_connectx(3) calls via selinux_sctp_bind_connect() |
| 4678 | * that validates multiple connect addresses. Because of this |
| 4679 | * need to check address->sa_family as it is possible to have |
| 4680 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4681 | */ |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4682 | switch (address->sa_family) { |
| 4683 | case AF_INET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | addr4 = (struct sockaddr_in *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4685 | if (addrlen < sizeof(struct sockaddr_in)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | return -EINVAL; |
| 4687 | snum = ntohs(addr4->sin_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4688 | break; |
| 4689 | case AF_INET6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | addr6 = (struct sockaddr_in6 *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4691 | if (addrlen < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | return -EINVAL; |
| 4693 | snum = ntohs(addr6->sin6_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4694 | break; |
| 4695 | default: |
| 4696 | /* Note that SCTP services expect -EINVAL, whereas |
| 4697 | * others expect -EAFNOSUPPORT. |
| 4698 | */ |
| 4699 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4700 | return -EINVAL; |
| 4701 | else |
| 4702 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
Paul Moore | 3e112172 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4705 | err = sel_netport_sid(sk->sk_protocol, snum, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4707 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4709 | switch (sksec->sclass) { |
| 4710 | case SECCLASS_TCP_SOCKET: |
| 4711 | perm = TCP_SOCKET__NAME_CONNECT; |
| 4712 | break; |
| 4713 | case SECCLASS_DCCP_SOCKET: |
| 4714 | perm = DCCP_SOCKET__NAME_CONNECT; |
| 4715 | break; |
| 4716 | case SECCLASS_SCTP_SOCKET: |
| 4717 | perm = SCTP_SOCKET__NAME_CONNECT; |
| 4718 | break; |
| 4719 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4720 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4721 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4722 | ad.u.net = &net; |
| 4723 | ad.u.net->dport = htons(snum); |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4724 | ad.u.net->family = address->sa_family; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4725 | err = avc_has_perm(&selinux_state, |
| 4726 | sksec->sid, sid, sksec->sclass, perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4728 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | } |
| 4730 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4731 | return 0; |
| 4732 | } |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4733 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4734 | /* Supports connect(2), see comments in selinux_socket_connect_helper() */ |
| 4735 | static int selinux_socket_connect(struct socket *sock, |
| 4736 | struct sockaddr *address, int addrlen) |
| 4737 | { |
| 4738 | int err; |
| 4739 | struct sock *sk = sock->sk; |
| 4740 | |
| 4741 | err = selinux_socket_connect_helper(sock, address, addrlen); |
| 4742 | if (err) |
| 4743 | return err; |
| 4744 | |
| 4745 | return selinux_netlbl_socket_connect(sk, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | static int selinux_socket_listen(struct socket *sock, int backlog) |
| 4749 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4750 | return sock_has_perm(sock->sk, SOCKET__LISTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | static int selinux_socket_accept(struct socket *sock, struct socket *newsock) |
| 4754 | { |
| 4755 | int err; |
| 4756 | struct inode_security_struct *isec; |
| 4757 | struct inode_security_struct *newisec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4758 | u16 sclass; |
| 4759 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4761 | err = sock_has_perm(sock->sk, SOCKET__ACCEPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | if (err) |
| 4763 | return err; |
| 4764 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4765 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4766 | spin_lock(&isec->lock); |
| 4767 | sclass = isec->sclass; |
| 4768 | sid = isec->sid; |
| 4769 | spin_unlock(&isec->lock); |
| 4770 | |
| 4771 | newisec = inode_security_novalidate(SOCK_INODE(newsock)); |
| 4772 | newisec->sclass = sclass; |
| 4773 | newisec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4774 | newisec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | |
| 4776 | return 0; |
| 4777 | } |
| 4778 | |
| 4779 | static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4780 | int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4782 | return sock_has_perm(sock->sk, SOCKET__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 4786 | int size, int flags) |
| 4787 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4788 | return sock_has_perm(sock->sk, SOCKET__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | static int selinux_socket_getsockname(struct socket *sock) |
| 4792 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4793 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | static int selinux_socket_getpeername(struct socket *sock) |
| 4797 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4798 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4801 | static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | { |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4803 | int err; |
| 4804 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4805 | err = sock_has_perm(sock->sk, SOCKET__SETOPT); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4806 | if (err) |
| 4807 | return err; |
| 4808 | |
| 4809 | return selinux_netlbl_socket_setsockopt(sock, level, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | static int selinux_socket_getsockopt(struct socket *sock, int level, |
| 4813 | int optname) |
| 4814 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4815 | return sock_has_perm(sock->sk, SOCKET__GETOPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | static int selinux_socket_shutdown(struct socket *sock, int how) |
| 4819 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4820 | return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4823 | static int selinux_socket_unix_stream_connect(struct sock *sock, |
| 4824 | struct sock *other, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | struct sock *newsk) |
| 4826 | { |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4827 | struct sk_security_struct *sksec_sock = sock->sk_security; |
| 4828 | struct sk_security_struct *sksec_other = other->sk_security; |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4829 | struct sk_security_struct *sksec_new = newsk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4830 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4831 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | int err; |
| 4833 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4834 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4835 | ad.u.net = &net; |
| 4836 | ad.u.net->sk = other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4838 | err = avc_has_perm(&selinux_state, |
| 4839 | sksec_sock->sid, sksec_other->sid, |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4840 | sksec_other->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | UNIX_STREAM_SOCKET__CONNECTTO, &ad); |
| 4842 | if (err) |
| 4843 | return err; |
| 4844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | /* server child socket */ |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4846 | sksec_new->peer_sid = sksec_sock->sid; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4847 | err = security_sid_mls_copy(&selinux_state, sksec_other->sid, |
| 4848 | sksec_sock->sid, &sksec_new->sid); |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4849 | if (err) |
| 4850 | return err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4851 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4852 | /* connecting socket */ |
| 4853 | sksec_sock->peer_sid = sksec_new->sid; |
| 4854 | |
| 4855 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | static int selinux_socket_unix_may_send(struct socket *sock, |
| 4859 | struct socket *other) |
| 4860 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4861 | struct sk_security_struct *ssec = sock->sk->sk_security; |
| 4862 | struct sk_security_struct *osec = other->sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4863 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4864 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4866 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4867 | ad.u.net = &net; |
| 4868 | ad.u.net->sk = other->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4870 | return avc_has_perm(&selinux_state, |
| 4871 | ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4872 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | } |
| 4874 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4875 | static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, |
| 4876 | char *addrp, u16 family, u32 peer_sid, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4877 | struct common_audit_data *ad) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4878 | { |
| 4879 | int err; |
| 4880 | u32 if_sid; |
| 4881 | u32 node_sid; |
| 4882 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4883 | err = sel_netif_sid(ns, ifindex, &if_sid); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4884 | if (err) |
| 4885 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4886 | err = avc_has_perm(&selinux_state, |
| 4887 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4888 | SECCLASS_NETIF, NETIF__INGRESS, ad); |
| 4889 | if (err) |
| 4890 | return err; |
| 4891 | |
| 4892 | err = sel_netnode_sid(addrp, family, &node_sid); |
| 4893 | if (err) |
| 4894 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4895 | return avc_has_perm(&selinux_state, |
| 4896 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4897 | SECCLASS_NODE, NODE__RECVFROM, ad); |
| 4898 | } |
| 4899 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4900 | 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] | 4901 | u16 family) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4902 | { |
Paul Moore | 277d342 | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 4903 | int err = 0; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4904 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4905 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4906 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4907 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4908 | char *addrp; |
| 4909 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4910 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4911 | ad.u.net = &net; |
| 4912 | ad.u.net->netif = skb->skb_iif; |
| 4913 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4914 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
| 4915 | if (err) |
| 4916 | return err; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4917 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4918 | if (selinux_secmark_enabled()) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4919 | err = avc_has_perm(&selinux_state, |
| 4920 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4921 | PACKET__RECV, &ad); |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4922 | if (err) |
| 4923 | return err; |
| 4924 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4925 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4926 | err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); |
| 4927 | if (err) |
| 4928 | return err; |
| 4929 | err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4930 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4931 | return err; |
| 4932 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4933 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4934 | static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 4935 | { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4936 | int err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4937 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4938 | u16 family = sk->sk_family; |
| 4939 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4940 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4941 | struct lsm_network_audit net = {0,}; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4942 | char *addrp; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4943 | u8 secmark_active; |
| 4944 | u8 peerlbl_active; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4945 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4946 | if (family != PF_INET && family != PF_INET6) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4947 | return 0; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4948 | |
| 4949 | /* Handle mapped IPv4 packets arriving via IPv6 sockets */ |
Al Viro | 87fcd70 | 2006-12-04 22:00:55 +0000 | [diff] [blame] | 4950 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4951 | family = PF_INET; |
| 4952 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4953 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4954 | * to the selinux_sock_rcv_skb_compat() function to deal with the |
| 4955 | * special handling. We do this in an attempt to keep this function |
| 4956 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4957 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4958 | return selinux_sock_rcv_skb_compat(sk, skb, family); |
| 4959 | |
| 4960 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 4961 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4962 | if (!secmark_active && !peerlbl_active) |
| 4963 | return 0; |
| 4964 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4965 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4966 | ad.u.net = &net; |
| 4967 | ad.u.net->netif = skb->skb_iif; |
| 4968 | ad.u.net->family = family; |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4969 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4970 | if (err) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4971 | return err; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4972 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4973 | if (peerlbl_active) { |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4974 | u32 peer_sid; |
| 4975 | |
| 4976 | err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); |
| 4977 | if (err) |
| 4978 | return err; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4979 | err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, |
| 4980 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4981 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 4982 | selinux_netlbl_err(skb, family, err, 0); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4983 | return err; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4984 | } |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4985 | err = avc_has_perm(&selinux_state, |
| 4986 | sk_sid, peer_sid, SECCLASS_PEER, |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4987 | PEER__RECV, &ad); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4988 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 4989 | selinux_netlbl_err(skb, family, err, 0); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4990 | return err; |
| 4991 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4992 | } |
| 4993 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4994 | if (secmark_active) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4995 | err = avc_has_perm(&selinux_state, |
| 4996 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4997 | PACKET__RECV, &ad); |
| 4998 | if (err) |
| 4999 | return err; |
| 5000 | } |
| 5001 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 5002 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | } |
| 5004 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5005 | static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, |
| 5006 | int __user *optlen, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | { |
| 5008 | int err = 0; |
| 5009 | char *scontext; |
| 5010 | u32 scontext_len; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5011 | struct sk_security_struct *sksec = sock->sk->sk_security; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 5012 | u32 peer_sid = SECSID_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5014 | if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5015 | sksec->sclass == SECCLASS_TCP_SOCKET || |
| 5016 | sksec->sclass == SECCLASS_SCTP_SOCKET) |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5017 | peer_sid = sksec->peer_sid; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5018 | if (peer_sid == SECSID_NULL) |
| 5019 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5021 | err = security_sid_to_context(&selinux_state, peer_sid, &scontext, |
| 5022 | &scontext_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | if (err) |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5024 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | |
| 5026 | if (scontext_len > len) { |
| 5027 | err = -ERANGE; |
| 5028 | goto out_len; |
| 5029 | } |
| 5030 | |
| 5031 | if (copy_to_user(optval, scontext, scontext_len)) |
| 5032 | err = -EFAULT; |
| 5033 | |
| 5034 | out_len: |
| 5035 | if (put_user(scontext_len, optlen)) |
| 5036 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | kfree(scontext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | return err; |
| 5039 | } |
| 5040 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5041 | 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] | 5042 | { |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5043 | u32 peer_secid = SECSID_NULL; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5044 | u16 family; |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 5045 | struct inode_security_struct *isec; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 5046 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5047 | if (skb && skb->protocol == htons(ETH_P_IP)) |
| 5048 | family = PF_INET; |
| 5049 | else if (skb && skb->protocol == htons(ETH_P_IPV6)) |
| 5050 | family = PF_INET6; |
| 5051 | else if (sock) |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5052 | family = sock->sk->sk_family; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5053 | else |
| 5054 | goto out; |
| 5055 | |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 5056 | if (sock && family == PF_UNIX) { |
| 5057 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
| 5058 | peer_secid = isec->sid; |
| 5059 | } else if (skb) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5060 | selinux_skb_peerlbl_sid(skb, family, &peer_secid); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5061 | |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5062 | out: |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5063 | *secid = peer_secid; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5064 | if (peer_secid == SECSID_NULL) |
| 5065 | return -EINVAL; |
| 5066 | return 0; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5067 | } |
| 5068 | |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 5069 | 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] | 5070 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5071 | struct sk_security_struct *sksec; |
| 5072 | |
| 5073 | sksec = kzalloc(sizeof(*sksec), priority); |
| 5074 | if (!sksec) |
| 5075 | return -ENOMEM; |
| 5076 | |
| 5077 | sksec->peer_sid = SECINITSID_UNLABELED; |
| 5078 | sksec->sid = SECINITSID_UNLABELED; |
Stephen Smalley | 5dee25d | 2015-07-10 17:19:57 -0400 | [diff] [blame] | 5079 | sksec->sclass = SECCLASS_SOCKET; |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5080 | selinux_netlbl_sk_security_reset(sksec); |
| 5081 | sk->sk_security = sksec; |
| 5082 | |
| 5083 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | static void selinux_sk_free_security(struct sock *sk) |
| 5087 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5088 | struct sk_security_struct *sksec = sk->sk_security; |
| 5089 | |
| 5090 | sk->sk_security = NULL; |
| 5091 | selinux_netlbl_sk_security_free(sksec); |
| 5092 | kfree(sksec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | } |
| 5094 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5095 | static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) |
| 5096 | { |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5097 | struct sk_security_struct *sksec = sk->sk_security; |
| 5098 | struct sk_security_struct *newsksec = newsk->sk_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5099 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5100 | newsksec->sid = sksec->sid; |
| 5101 | newsksec->peer_sid = sksec->peer_sid; |
| 5102 | newsksec->sclass = sksec->sclass; |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5103 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5104 | selinux_netlbl_sk_security_reset(newsksec); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5105 | } |
| 5106 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5107 | static void selinux_sk_getsecid(struct sock *sk, u32 *secid) |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5108 | { |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5109 | if (!sk) |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5110 | *secid = SECINITSID_ANY_SOCKET; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5111 | else { |
| 5112 | struct sk_security_struct *sksec = sk->sk_security; |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5113 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5114 | *secid = sksec->sid; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5115 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5116 | } |
| 5117 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5118 | static void selinux_sock_graft(struct sock *sk, struct socket *parent) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5119 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 5120 | struct inode_security_struct *isec = |
| 5121 | inode_security_novalidate(SOCK_INODE(parent)); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5122 | struct sk_security_struct *sksec = sk->sk_security; |
| 5123 | |
Paul Moore | 2873ead | 2014-07-28 10:42:48 -0400 | [diff] [blame] | 5124 | if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || |
| 5125 | sk->sk_family == PF_UNIX) |
David Woodhouse | 2148ccc | 2006-09-29 15:50:25 -0700 | [diff] [blame] | 5126 | isec->sid = sksec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5127 | sksec->sclass = isec->sclass; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5128 | } |
| 5129 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5130 | /* Called whenever SCTP receives an INIT chunk. This happens when an incoming |
| 5131 | * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association |
| 5132 | * already present). |
| 5133 | */ |
| 5134 | static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, |
| 5135 | struct sk_buff *skb) |
| 5136 | { |
| 5137 | struct sk_security_struct *sksec = ep->base.sk->sk_security; |
| 5138 | struct common_audit_data ad; |
| 5139 | struct lsm_network_audit net = {0,}; |
| 5140 | u8 peerlbl_active; |
| 5141 | u32 peer_sid = SECINITSID_UNLABELED; |
| 5142 | u32 conn_sid; |
| 5143 | int err = 0; |
| 5144 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5145 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5146 | return 0; |
| 5147 | |
| 5148 | peerlbl_active = selinux_peerlbl_enabled(); |
| 5149 | |
| 5150 | if (peerlbl_active) { |
| 5151 | /* This will return peer_sid = SECSID_NULL if there are |
| 5152 | * no peer labels, see security_net_peersid_resolve(). |
| 5153 | */ |
| 5154 | err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, |
| 5155 | &peer_sid); |
| 5156 | if (err) |
| 5157 | return err; |
| 5158 | |
| 5159 | if (peer_sid == SECSID_NULL) |
| 5160 | peer_sid = SECINITSID_UNLABELED; |
| 5161 | } |
| 5162 | |
| 5163 | if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { |
| 5164 | sksec->sctp_assoc_state = SCTP_ASSOC_SET; |
| 5165 | |
| 5166 | /* Here as first association on socket. As the peer SID |
| 5167 | * was allowed by peer recv (and the netif/node checks), |
| 5168 | * then it is approved by policy and used as the primary |
| 5169 | * peer SID for getpeercon(3). |
| 5170 | */ |
| 5171 | sksec->peer_sid = peer_sid; |
| 5172 | } else if (sksec->peer_sid != peer_sid) { |
| 5173 | /* Other association peer SIDs are checked to enforce |
| 5174 | * consistency among the peer SIDs. |
| 5175 | */ |
| 5176 | ad.type = LSM_AUDIT_DATA_NET; |
| 5177 | ad.u.net = &net; |
| 5178 | ad.u.net->sk = ep->base.sk; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5179 | err = avc_has_perm(&selinux_state, |
| 5180 | sksec->peer_sid, peer_sid, sksec->sclass, |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5181 | SCTP_SOCKET__ASSOCIATION, &ad); |
| 5182 | if (err) |
| 5183 | return err; |
| 5184 | } |
| 5185 | |
| 5186 | /* Compute the MLS component for the connection and store |
| 5187 | * the information in ep. This will be used by SCTP TCP type |
| 5188 | * sockets and peeled off connections as they cause a new |
| 5189 | * socket to be generated. selinux_sctp_sk_clone() will then |
| 5190 | * plug this into the new socket. |
| 5191 | */ |
| 5192 | err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); |
| 5193 | if (err) |
| 5194 | return err; |
| 5195 | |
| 5196 | ep->secid = conn_sid; |
| 5197 | ep->peer_secid = peer_sid; |
| 5198 | |
| 5199 | /* Set any NetLabel labels including CIPSO/CALIPSO options. */ |
| 5200 | return selinux_netlbl_sctp_assoc_request(ep, skb); |
| 5201 | } |
| 5202 | |
| 5203 | /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting |
| 5204 | * based on their @optname. |
| 5205 | */ |
| 5206 | static int selinux_sctp_bind_connect(struct sock *sk, int optname, |
| 5207 | struct sockaddr *address, |
| 5208 | int addrlen) |
| 5209 | { |
| 5210 | int len, err = 0, walk_size = 0; |
| 5211 | void *addr_buf; |
| 5212 | struct sockaddr *addr; |
| 5213 | struct socket *sock; |
| 5214 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5215 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5216 | return 0; |
| 5217 | |
| 5218 | /* Process one or more addresses that may be IPv4 or IPv6 */ |
| 5219 | sock = sk->sk_socket; |
| 5220 | addr_buf = address; |
| 5221 | |
| 5222 | while (walk_size < addrlen) { |
Ondrej Mosnacek | c138325 | 2018-11-13 16:16:08 +0100 | [diff] [blame] | 5223 | if (walk_size + sizeof(sa_family_t) > addrlen) |
| 5224 | return -EINVAL; |
| 5225 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5226 | addr = addr_buf; |
| 5227 | switch (addr->sa_family) { |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5228 | case AF_UNSPEC: |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5229 | case AF_INET: |
| 5230 | len = sizeof(struct sockaddr_in); |
| 5231 | break; |
| 5232 | case AF_INET6: |
| 5233 | len = sizeof(struct sockaddr_in6); |
| 5234 | break; |
| 5235 | default: |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5236 | return -EINVAL; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
Xin Long | 292c997 | 2019-03-09 00:07:34 +0800 | [diff] [blame] | 5239 | if (walk_size + len > addrlen) |
| 5240 | return -EINVAL; |
| 5241 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5242 | err = -EINVAL; |
| 5243 | switch (optname) { |
| 5244 | /* Bind checks */ |
| 5245 | case SCTP_PRIMARY_ADDR: |
| 5246 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 5247 | case SCTP_SOCKOPT_BINDX_ADD: |
| 5248 | err = selinux_socket_bind(sock, addr, len); |
| 5249 | break; |
| 5250 | /* Connect checks */ |
| 5251 | case SCTP_SOCKOPT_CONNECTX: |
| 5252 | case SCTP_PARAM_SET_PRIMARY: |
| 5253 | case SCTP_PARAM_ADD_IP: |
| 5254 | case SCTP_SENDMSG_CONNECT: |
| 5255 | err = selinux_socket_connect_helper(sock, addr, len); |
| 5256 | if (err) |
| 5257 | return err; |
| 5258 | |
| 5259 | /* As selinux_sctp_bind_connect() is called by the |
| 5260 | * SCTP protocol layer, the socket is already locked, |
| 5261 | * therefore selinux_netlbl_socket_connect_locked() is |
| 5262 | * is called here. The situations handled are: |
| 5263 | * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), |
| 5264 | * whenever a new IP address is added or when a new |
| 5265 | * primary address is selected. |
| 5266 | * Note that an SCTP connect(2) call happens before |
| 5267 | * the SCTP protocol layer and is handled via |
| 5268 | * selinux_socket_connect(). |
| 5269 | */ |
| 5270 | err = selinux_netlbl_socket_connect_locked(sk, addr); |
| 5271 | break; |
| 5272 | } |
| 5273 | |
| 5274 | if (err) |
| 5275 | return err; |
| 5276 | |
| 5277 | addr_buf += len; |
| 5278 | walk_size += len; |
| 5279 | } |
| 5280 | |
| 5281 | return 0; |
| 5282 | } |
| 5283 | |
| 5284 | /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ |
| 5285 | static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, |
| 5286 | struct sock *newsk) |
| 5287 | { |
| 5288 | struct sk_security_struct *sksec = sk->sk_security; |
| 5289 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5290 | |
| 5291 | /* If policy does not support SECCLASS_SCTP_SOCKET then call |
| 5292 | * the non-sctp clone version. |
| 5293 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5294 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5295 | return selinux_sk_clone_security(sk, newsk); |
| 5296 | |
| 5297 | newsksec->sid = ep->secid; |
| 5298 | newsksec->peer_sid = ep->peer_secid; |
| 5299 | newsksec->sclass = sksec->sclass; |
| 5300 | selinux_netlbl_sctp_sk_clone(sk, newsk); |
| 5301 | } |
| 5302 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5303 | static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 5304 | struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5305 | { |
| 5306 | struct sk_security_struct *sksec = sk->sk_security; |
| 5307 | int err; |
Paul Moore | 0b1f24e | 2013-12-03 11:39:13 -0500 | [diff] [blame] | 5308 | u16 family = req->rsk_ops->family; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5309 | u32 connsid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5310 | u32 peersid; |
| 5311 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5312 | err = selinux_skb_peerlbl_sid(skb, family, &peersid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5313 | if (err) |
| 5314 | return err; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5315 | err = selinux_conn_sid(sksec->sid, peersid, &connsid); |
| 5316 | if (err) |
| 5317 | return err; |
| 5318 | req->secid = connsid; |
| 5319 | req->peer_secid = peersid; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 5320 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5321 | return selinux_netlbl_inet_conn_request(req, family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5322 | } |
| 5323 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5324 | static void selinux_inet_csk_clone(struct sock *newsk, |
| 5325 | const struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5326 | { |
| 5327 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5328 | |
| 5329 | newsksec->sid = req->secid; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5330 | newsksec->peer_sid = req->peer_secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5331 | /* NOTE: Ideally, we should also get the isec->sid for the |
| 5332 | new socket in sync, but we don't have the isec available yet. |
| 5333 | So we will wait until sock_graft to do it, by which |
| 5334 | time it will have been created and available. */ |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5335 | |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 5336 | /* We don't need to take any sort of lock here as we are the only |
| 5337 | * thread with access to newsksec */ |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5338 | selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5339 | } |
| 5340 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 5341 | 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] | 5342 | { |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5343 | u16 family = sk->sk_family; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5344 | struct sk_security_struct *sksec = sk->sk_security; |
| 5345 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5346 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 5347 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 5348 | family = PF_INET; |
| 5349 | |
| 5350 | selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5351 | } |
| 5352 | |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5353 | static int selinux_secmark_relabel_packet(u32 sid) |
| 5354 | { |
| 5355 | const struct task_security_struct *__tsec; |
| 5356 | u32 tsid; |
| 5357 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 5358 | __tsec = selinux_cred(current_cred()); |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5359 | tsid = __tsec->sid; |
| 5360 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5361 | return avc_has_perm(&selinux_state, |
| 5362 | tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, |
| 5363 | NULL); |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | static void selinux_secmark_refcount_inc(void) |
| 5367 | { |
| 5368 | atomic_inc(&selinux_secmark_refcount); |
| 5369 | } |
| 5370 | |
| 5371 | static void selinux_secmark_refcount_dec(void) |
| 5372 | { |
| 5373 | atomic_dec(&selinux_secmark_refcount); |
| 5374 | } |
| 5375 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5376 | static void selinux_req_classify_flow(const struct request_sock *req, |
| 5377 | struct flowi *fl) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5378 | { |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 5379 | fl->flowi_secid = req->secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5380 | } |
| 5381 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5382 | static int selinux_tun_dev_alloc_security(void **security) |
| 5383 | { |
| 5384 | struct tun_security_struct *tunsec; |
| 5385 | |
| 5386 | tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); |
| 5387 | if (!tunsec) |
| 5388 | return -ENOMEM; |
| 5389 | tunsec->sid = current_sid(); |
| 5390 | |
| 5391 | *security = tunsec; |
| 5392 | return 0; |
| 5393 | } |
| 5394 | |
| 5395 | static void selinux_tun_dev_free_security(void *security) |
| 5396 | { |
| 5397 | kfree(security); |
| 5398 | } |
| 5399 | |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5400 | static int selinux_tun_dev_create(void) |
| 5401 | { |
| 5402 | u32 sid = current_sid(); |
| 5403 | |
| 5404 | /* we aren't taking into account the "sockcreate" SID since the socket |
| 5405 | * that is being created here is not a socket in the traditional sense, |
| 5406 | * instead it is a private sock, accessible only to the kernel, and |
| 5407 | * representing a wide range of network traffic spanning multiple |
| 5408 | * connections unlike traditional sockets - check the TUN driver to |
| 5409 | * get a better understanding of why this socket is special */ |
| 5410 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5411 | return avc_has_perm(&selinux_state, |
| 5412 | sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5413 | NULL); |
| 5414 | } |
| 5415 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5416 | static int selinux_tun_dev_attach_queue(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5417 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5418 | struct tun_security_struct *tunsec = security; |
| 5419 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5420 | return avc_has_perm(&selinux_state, |
| 5421 | current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5422 | TUN_SOCKET__ATTACH_QUEUE, NULL); |
| 5423 | } |
| 5424 | |
| 5425 | static int selinux_tun_dev_attach(struct sock *sk, void *security) |
| 5426 | { |
| 5427 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5428 | struct sk_security_struct *sksec = sk->sk_security; |
| 5429 | |
| 5430 | /* we don't currently perform any NetLabel based labeling here and it |
| 5431 | * isn't clear that we would want to do so anyway; while we could apply |
| 5432 | * labeling without the support of the TUN user the resulting labeled |
| 5433 | * traffic from the other end of the connection would almost certainly |
| 5434 | * cause confusion to the TUN user that had no idea network labeling |
| 5435 | * protocols were being used */ |
| 5436 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5437 | sksec->sid = tunsec->sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5438 | sksec->sclass = SECCLASS_TUN_SOCKET; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5439 | |
| 5440 | return 0; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5441 | } |
| 5442 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5443 | static int selinux_tun_dev_open(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5444 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5445 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5446 | u32 sid = current_sid(); |
| 5447 | int err; |
| 5448 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5449 | err = avc_has_perm(&selinux_state, |
| 5450 | sid, tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5451 | TUN_SOCKET__RELABELFROM, NULL); |
| 5452 | if (err) |
| 5453 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5454 | err = avc_has_perm(&selinux_state, |
| 5455 | sid, sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5456 | TUN_SOCKET__RELABELTO, NULL); |
| 5457 | if (err) |
| 5458 | return err; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5459 | tunsec->sid = sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5460 | |
| 5461 | return 0; |
| 5462 | } |
| 5463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) |
| 5465 | { |
| 5466 | int err = 0; |
| 5467 | u32 perm; |
| 5468 | struct nlmsghdr *nlh; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5469 | struct sk_security_struct *sksec = sk->sk_security; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5470 | |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 5471 | if (skb->len < NLMSG_HDRLEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | err = -EINVAL; |
| 5473 | goto out; |
| 5474 | } |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 5475 | nlh = nlmsg_hdr(skb); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5476 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5477 | err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | if (err) { |
| 5479 | if (err == -EINVAL) { |
Vladis Dronov | 7631994 | 2015-12-24 11:09:41 -0500 | [diff] [blame] | 5480 | pr_warn_ratelimited("SELinux: unrecognized netlink" |
| 5481 | " message: protocol=%hu nlmsg_type=%hu sclass=%s" |
| 5482 | " pig=%d comm=%s\n", |
Marek Milkovic | cded3ff | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 5483 | sk->sk_protocol, nlh->nlmsg_type, |
Vladis Dronov | 7631994 | 2015-12-24 11:09:41 -0500 | [diff] [blame] | 5484 | secclass_map[sksec->sclass - 1].name, |
| 5485 | task_pid_nr(current), current->comm); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 5486 | if (!enforcing_enabled(&selinux_state) || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5487 | security_get_allow_unknown(&selinux_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | err = 0; |
| 5489 | } |
| 5490 | |
| 5491 | /* Ignore */ |
| 5492 | if (err == -ENOENT) |
| 5493 | err = 0; |
| 5494 | goto out; |
| 5495 | } |
| 5496 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5497 | err = sock_has_perm(sk, perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | out: |
| 5499 | return err; |
| 5500 | } |
| 5501 | |
| 5502 | #ifdef CONFIG_NETFILTER |
| 5503 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5504 | static unsigned int selinux_ip_forward(struct sk_buff *skb, |
| 5505 | const struct net_device *indev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5506 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5508 | int err; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5509 | char *addrp; |
| 5510 | u32 peer_sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5511 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5512 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5513 | u8 secmark_active; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5514 | u8 netlbl_active; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5515 | u8 peerlbl_active; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5516 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5517 | if (!selinux_policycap_netpeer()) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5518 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5519 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5520 | secmark_active = selinux_secmark_enabled(); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5521 | netlbl_active = netlbl_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5522 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5523 | if (!secmark_active && !peerlbl_active) |
| 5524 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5525 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5526 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) |
| 5527 | return NF_DROP; |
| 5528 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5529 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5530 | ad.u.net = &net; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5531 | ad.u.net->netif = indev->ifindex; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5532 | ad.u.net->family = family; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5533 | if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) |
| 5534 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5536 | if (peerlbl_active) { |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5537 | err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, |
| 5538 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5539 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 5540 | selinux_netlbl_err(skb, family, err, 1); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5541 | return NF_DROP; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5542 | } |
| 5543 | } |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5544 | |
| 5545 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5546 | if (avc_has_perm(&selinux_state, |
| 5547 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5548 | SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) |
| 5549 | return NF_DROP; |
| 5550 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5551 | if (netlbl_active) |
| 5552 | /* we do this in the FORWARD path and not the POST_ROUTING |
| 5553 | * path because we want to make sure we apply the necessary |
| 5554 | * labeling before IPsec is applied so we can leverage AH |
| 5555 | * protection */ |
| 5556 | if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) |
| 5557 | return NF_DROP; |
| 5558 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5559 | return NF_ACCEPT; |
| 5560 | } |
| 5561 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5562 | static unsigned int selinux_ipv4_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5563 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5564 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5565 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5566 | return selinux_ip_forward(skb, state->in, PF_INET); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5567 | } |
| 5568 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5569 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5570 | static unsigned int selinux_ipv6_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5571 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5572 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5573 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5574 | return selinux_ip_forward(skb, state->in, PF_INET6); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5575 | } |
| 5576 | #endif /* IPV6 */ |
| 5577 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5578 | static unsigned int selinux_ip_output(struct sk_buff *skb, |
| 5579 | u16 family) |
| 5580 | { |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5581 | struct sock *sk; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5582 | u32 sid; |
| 5583 | |
| 5584 | if (!netlbl_enabled()) |
| 5585 | return NF_ACCEPT; |
| 5586 | |
| 5587 | /* we do this in the LOCAL_OUT path and not the POST_ROUTING path |
| 5588 | * because we want to make sure we apply the necessary labeling |
| 5589 | * before IPsec is applied so we can leverage AH protection */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5590 | sk = skb->sk; |
| 5591 | if (sk) { |
| 5592 | struct sk_security_struct *sksec; |
| 5593 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5594 | if (sk_listener(sk)) |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5595 | /* if the socket is the listening state then this |
| 5596 | * packet is a SYN-ACK packet which means it needs to |
| 5597 | * be labeled based on the connection/request_sock and |
| 5598 | * not the parent socket. unfortunately, we can't |
| 5599 | * lookup the request_sock yet as it isn't queued on |
| 5600 | * the parent socket until after the SYN-ACK is sent. |
| 5601 | * the "solution" is to simply pass the packet as-is |
| 5602 | * as any IP option based labeling should be copied |
| 5603 | * from the initial connection request (in the IP |
| 5604 | * layer). it is far from ideal, but until we get a |
| 5605 | * security label in the packet itself this is the |
| 5606 | * best we can do. */ |
| 5607 | return NF_ACCEPT; |
| 5608 | |
| 5609 | /* standard practice, label using the parent socket */ |
| 5610 | sksec = sk->sk_security; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5611 | sid = sksec->sid; |
| 5612 | } else |
| 5613 | sid = SECINITSID_KERNEL; |
| 5614 | if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) |
| 5615 | return NF_DROP; |
| 5616 | |
| 5617 | return NF_ACCEPT; |
| 5618 | } |
| 5619 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5620 | static unsigned int selinux_ipv4_output(void *priv, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5621 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5622 | const struct nf_hook_state *state) |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5623 | { |
| 5624 | return selinux_ip_output(skb, PF_INET); |
| 5625 | } |
| 5626 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5627 | #if IS_ENABLED(CONFIG_IPV6) |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 5628 | static unsigned int selinux_ipv6_output(void *priv, |
| 5629 | struct sk_buff *skb, |
| 5630 | const struct nf_hook_state *state) |
| 5631 | { |
| 5632 | return selinux_ip_output(skb, PF_INET6); |
| 5633 | } |
| 5634 | #endif /* IPV6 */ |
| 5635 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5636 | static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, |
| 5637 | int ifindex, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5638 | u16 family) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5639 | { |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5640 | struct sock *sk = skb_to_full_sk(skb); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5641 | struct sk_security_struct *sksec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5642 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5643 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5644 | char *addrp; |
| 5645 | u8 proto; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5646 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5647 | if (sk == NULL) |
| 5648 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5649 | sksec = sk->sk_security; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5650 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5651 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5652 | ad.u.net = &net; |
| 5653 | ad.u.net->netif = ifindex; |
| 5654 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5655 | if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) |
| 5656 | return NF_DROP; |
| 5657 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 5658 | if (selinux_secmark_enabled()) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5659 | if (avc_has_perm(&selinux_state, |
| 5660 | sksec->sid, skb->secmark, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5661 | SECCLASS_PACKET, PACKET__SEND, &ad)) |
Eric Paris | 2fe66ec | 2010-11-23 06:28:08 +0000 | [diff] [blame] | 5662 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5663 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 5664 | if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) |
| 5665 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5666 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5667 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | } |
| 5669 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5670 | static unsigned int selinux_ip_postroute(struct sk_buff *skb, |
| 5671 | const struct net_device *outdev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5672 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5674 | u32 secmark_perm; |
| 5675 | u32 peer_sid; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5676 | int ifindex = outdev->ifindex; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5677 | struct sock *sk; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5678 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5679 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5680 | char *addrp; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5681 | u8 secmark_active; |
| 5682 | u8 peerlbl_active; |
| 5683 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5684 | /* If any sort of compatibility mode is enabled then handoff processing |
| 5685 | * to the selinux_ip_postroute_compat() function to deal with the |
| 5686 | * special handling. We do this in an attempt to keep this function |
| 5687 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5688 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5689 | return selinux_ip_postroute_compat(skb, ifindex, family); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5690 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5691 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5692 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5693 | if (!secmark_active && !peerlbl_active) |
| 5694 | return NF_ACCEPT; |
| 5695 | |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5696 | sk = skb_to_full_sk(skb); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5697 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5698 | #ifdef CONFIG_XFRM |
| 5699 | /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec |
| 5700 | * packet transformation so allow the packet to pass without any checks |
| 5701 | * since we'll have another chance to perform access control checks |
| 5702 | * when the packet is on it's final way out. |
| 5703 | * NOTE: there appear to be some IPv6 multicast cases where skb->dst |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5704 | * is NULL, in this case go ahead and apply access control. |
| 5705 | * NOTE: if this is a local socket (skb->sk != NULL) that is in the |
| 5706 | * TCP listening state we cannot wait until the XFRM processing |
| 5707 | * is done as we will miss out on the SA label if we do; |
| 5708 | * unfortunately, this means more work, but it is only once per |
| 5709 | * connection. */ |
| 5710 | if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5711 | !(sk && sk_listener(sk))) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5712 | return NF_ACCEPT; |
| 5713 | #endif |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5714 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5715 | if (sk == NULL) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5716 | /* Without an associated socket the packet is either coming |
| 5717 | * from the kernel or it is being forwarded; check the packet |
| 5718 | * to determine which and if the packet is being forwarded |
| 5719 | * query the packet directly to determine the security label. */ |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5720 | if (skb->skb_iif) { |
| 5721 | secmark_perm = PACKET__FORWARD_OUT; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5722 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5723 | return NF_DROP; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5724 | } else { |
| 5725 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5726 | peer_sid = SECINITSID_KERNEL; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5727 | } |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5728 | } else if (sk_listener(sk)) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5729 | /* Locally generated packet but the associated socket is in the |
| 5730 | * listening state which means this is a SYN-ACK packet. In |
| 5731 | * this particular case the correct security label is assigned |
| 5732 | * to the connection/request_sock but unfortunately we can't |
| 5733 | * query the request_sock as it isn't queued on the parent |
| 5734 | * socket until after the SYN-ACK packet is sent; the only |
| 5735 | * viable choice is to regenerate the label like we do in |
| 5736 | * selinux_inet_conn_request(). See also selinux_ip_output() |
| 5737 | * for similar problems. */ |
| 5738 | u32 skb_sid; |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5739 | struct sk_security_struct *sksec; |
| 5740 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5741 | sksec = sk->sk_security; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5742 | if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) |
| 5743 | return NF_DROP; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5744 | /* At this point, if the returned skb peerlbl is SECSID_NULL |
| 5745 | * and the packet has been through at least one XFRM |
| 5746 | * transformation then we must be dealing with the "final" |
| 5747 | * form of labeled IPsec packet; since we've already applied |
| 5748 | * all of our access controls on this packet we can safely |
| 5749 | * pass the packet. */ |
| 5750 | if (skb_sid == SECSID_NULL) { |
| 5751 | switch (family) { |
| 5752 | case PF_INET: |
| 5753 | if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) |
| 5754 | return NF_ACCEPT; |
| 5755 | break; |
| 5756 | case PF_INET6: |
| 5757 | if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) |
| 5758 | return NF_ACCEPT; |
Paul Moore | a7a91a1 | 2014-09-03 10:51:59 -0400 | [diff] [blame] | 5759 | break; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5760 | default: |
| 5761 | return NF_DROP_ERR(-ECONNREFUSED); |
| 5762 | } |
| 5763 | } |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5764 | if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) |
| 5765 | return NF_DROP; |
| 5766 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5767 | } else { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5768 | /* Locally generated packet, fetch the security label from the |
| 5769 | * associated socket. */ |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5770 | struct sk_security_struct *sksec = sk->sk_security; |
| 5771 | peer_sid = sksec->sid; |
| 5772 | secmark_perm = PACKET__SEND; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5773 | } |
| 5774 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5775 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5776 | ad.u.net = &net; |
| 5777 | ad.u.net->netif = ifindex; |
| 5778 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5779 | if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5780 | return NF_DROP; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5781 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5782 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5783 | if (avc_has_perm(&selinux_state, |
| 5784 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5785 | SECCLASS_PACKET, secmark_perm, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5786 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5787 | |
| 5788 | if (peerlbl_active) { |
| 5789 | u32 if_sid; |
| 5790 | u32 node_sid; |
| 5791 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5792 | if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5793 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5794 | if (avc_has_perm(&selinux_state, |
| 5795 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5796 | SECCLASS_NETIF, NETIF__EGRESS, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5797 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5798 | |
| 5799 | if (sel_netnode_sid(addrp, family, &node_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5800 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5801 | if (avc_has_perm(&selinux_state, |
| 5802 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5803 | SECCLASS_NODE, NODE__SENDTO, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5804 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | return NF_ACCEPT; |
| 5808 | } |
| 5809 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5810 | static unsigned int selinux_ipv4_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5811 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5812 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5813 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5814 | return selinux_ip_postroute(skb, state->out, PF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5817 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5818 | static unsigned int selinux_ipv6_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5819 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5820 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5822 | return selinux_ip_postroute(skb, state->out, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | #endif /* IPV6 */ |
| 5825 | |
| 5826 | #endif /* CONFIG_NETFILTER */ |
| 5827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 5829 | { |
Stephen Smalley | 941fc5b | 2009-10-01 14:48:23 -0400 | [diff] [blame] | 5830 | return selinux_nlmsg_perm(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | } |
| 5832 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5833 | static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | isec->sclass = sclass; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5836 | isec->sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | } |
| 5838 | |
| 5839 | static int msg_msg_alloc_security(struct msg_msg *msg) |
| 5840 | { |
| 5841 | struct msg_security_struct *msec; |
| 5842 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5843 | msec = selinux_msg_msg(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | msec->sid = SECINITSID_UNLABELED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | |
| 5846 | return 0; |
| 5847 | } |
| 5848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5850 | u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5853 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5854 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 5856 | isec = selinux_ipc(ipc_perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5858 | ad.type = LSM_AUDIT_DATA_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | ad.u.ipc_id = ipc_perms->key; |
| 5860 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5861 | return avc_has_perm(&selinux_state, |
| 5862 | sid, isec->sid, isec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | } |
| 5864 | |
| 5865 | static int selinux_msg_msg_alloc_security(struct msg_msg *msg) |
| 5866 | { |
| 5867 | return msg_msg_alloc_security(msg); |
| 5868 | } |
| 5869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | /* message queue security operations */ |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5871 | static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5874 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5875 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | int rc; |
| 5877 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5878 | isec = selinux_ipc(msq); |
| 5879 | ipc_init_security(isec, SECCLASS_MSGQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5881 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5882 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5884 | rc = avc_has_perm(&selinux_state, |
| 5885 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | MSGQ__CREATE, &ad); |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5887 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | } |
| 5889 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5890 | 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] | 5891 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5893 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5894 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 5896 | isec = selinux_ipc(msq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5898 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5899 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5901 | return avc_has_perm(&selinux_state, |
| 5902 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | MSGQ__ASSOCIATE, &ad); |
| 5904 | } |
| 5905 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5906 | 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] | 5907 | { |
| 5908 | int err; |
| 5909 | int perms; |
| 5910 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5911 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | case IPC_INFO: |
| 5913 | case MSG_INFO: |
| 5914 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5915 | return avc_has_perm(&selinux_state, |
| 5916 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5917 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | case IPC_STAT: |
| 5919 | case MSG_STAT: |
Davidlohr Bueso | 23c8cec | 2018-04-10 16:35:30 -0700 | [diff] [blame] | 5920 | case MSG_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; |
| 5922 | break; |
| 5923 | case IPC_SET: |
| 5924 | perms = MSGQ__SETATTR; |
| 5925 | break; |
| 5926 | case IPC_RMID: |
| 5927 | perms = MSGQ__DESTROY; |
| 5928 | break; |
| 5929 | default: |
| 5930 | return 0; |
| 5931 | } |
| 5932 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5933 | err = ipc_has_perm(msq, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | return err; |
| 5935 | } |
| 5936 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5937 | 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] | 5938 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | struct ipc_security_struct *isec; |
| 5940 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5941 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5942 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | int rc; |
| 5944 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 5945 | isec = selinux_ipc(msq); |
| 5946 | msec = selinux_msg_msg(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | |
| 5948 | /* |
| 5949 | * First time through, need to assign label to the message |
| 5950 | */ |
| 5951 | if (msec->sid == SECINITSID_UNLABELED) { |
| 5952 | /* |
| 5953 | * Compute new sid based on current process and |
| 5954 | * message queue this message will be stored in |
| 5955 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5956 | rc = security_transition_sid(&selinux_state, sid, isec->sid, |
| 5957 | SECCLASS_MSG, NULL, &msec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | if (rc) |
| 5959 | return rc; |
| 5960 | } |
| 5961 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5962 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5963 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | |
| 5965 | /* Can this process write to the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5966 | rc = avc_has_perm(&selinux_state, |
| 5967 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | MSGQ__WRITE, &ad); |
| 5969 | if (!rc) |
| 5970 | /* Can this process send the message */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5971 | rc = avc_has_perm(&selinux_state, |
| 5972 | sid, msec->sid, SECCLASS_MSG, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5973 | MSG__SEND, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | if (!rc) |
| 5975 | /* Can the message be put in the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5976 | rc = avc_has_perm(&selinux_state, |
| 5977 | msec->sid, isec->sid, SECCLASS_MSGQ, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5978 | MSGQ__ENQUEUE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | |
| 5980 | return rc; |
| 5981 | } |
| 5982 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5983 | 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] | 5984 | struct task_struct *target, |
| 5985 | long type, int mode) |
| 5986 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | struct ipc_security_struct *isec; |
| 5988 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5989 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5990 | u32 sid = task_sid(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | int rc; |
| 5992 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 5993 | isec = selinux_ipc(msq); |
| 5994 | msec = selinux_msg_msg(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5996 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5997 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5999 | rc = avc_has_perm(&selinux_state, |
| 6000 | sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | SECCLASS_MSGQ, MSGQ__READ, &ad); |
| 6002 | if (!rc) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6003 | rc = avc_has_perm(&selinux_state, |
| 6004 | sid, msec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | SECCLASS_MSG, MSG__RECEIVE, &ad); |
| 6006 | return rc; |
| 6007 | } |
| 6008 | |
| 6009 | /* Shared Memory security operations */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6010 | static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) |
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 | int rc; |
| 6016 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6017 | isec = selinux_ipc(shp); |
| 6018 | ipc_init_security(isec, SECCLASS_SHM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6020 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6021 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6023 | rc = avc_has_perm(&selinux_state, |
| 6024 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | SHM__CREATE, &ad); |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6026 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | } |
| 6028 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6029 | static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6032 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6033 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6035 | isec = selinux_ipc(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6036 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6037 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6038 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6040 | return avc_has_perm(&selinux_state, |
| 6041 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | SHM__ASSOCIATE, &ad); |
| 6043 | } |
| 6044 | |
| 6045 | /* Note, at this point, shp is locked down */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6046 | static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | { |
| 6048 | int perms; |
| 6049 | int err; |
| 6050 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6051 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | case IPC_INFO: |
| 6053 | case SHM_INFO: |
| 6054 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6055 | return avc_has_perm(&selinux_state, |
| 6056 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6057 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | case IPC_STAT: |
| 6059 | case SHM_STAT: |
Davidlohr Bueso | c21a697 | 2018-04-10 16:35:23 -0700 | [diff] [blame] | 6060 | case SHM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | perms = SHM__GETATTR | SHM__ASSOCIATE; |
| 6062 | break; |
| 6063 | case IPC_SET: |
| 6064 | perms = SHM__SETATTR; |
| 6065 | break; |
| 6066 | case SHM_LOCK: |
| 6067 | case SHM_UNLOCK: |
| 6068 | perms = SHM__LOCK; |
| 6069 | break; |
| 6070 | case IPC_RMID: |
| 6071 | perms = SHM__DESTROY; |
| 6072 | break; |
| 6073 | default: |
| 6074 | return 0; |
| 6075 | } |
| 6076 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6077 | err = ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | return err; |
| 6079 | } |
| 6080 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6081 | static int selinux_shm_shmat(struct kern_ipc_perm *shp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | char __user *shmaddr, int shmflg) |
| 6083 | { |
| 6084 | u32 perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | |
| 6086 | if (shmflg & SHM_RDONLY) |
| 6087 | perms = SHM__READ; |
| 6088 | else |
| 6089 | perms = SHM__READ | SHM__WRITE; |
| 6090 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6091 | return ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | /* Semaphore security operations */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6095 | static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6098 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6099 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | int rc; |
| 6101 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6102 | isec = selinux_ipc(sma); |
| 6103 | ipc_init_security(isec, SECCLASS_SEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6105 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6106 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6108 | rc = avc_has_perm(&selinux_state, |
| 6109 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | SEM__CREATE, &ad); |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6111 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | } |
| 6113 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6114 | static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6117 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6118 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6119 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6120 | isec = selinux_ipc(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6122 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6123 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6125 | return avc_has_perm(&selinux_state, |
| 6126 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | SEM__ASSOCIATE, &ad); |
| 6128 | } |
| 6129 | |
| 6130 | /* Note, at this point, sma is locked down */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6131 | static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | { |
| 6133 | int err; |
| 6134 | u32 perms; |
| 6135 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6136 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | case IPC_INFO: |
| 6138 | case SEM_INFO: |
| 6139 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6140 | return avc_has_perm(&selinux_state, |
| 6141 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6142 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | case GETPID: |
| 6144 | case GETNCNT: |
| 6145 | case GETZCNT: |
| 6146 | perms = SEM__GETATTR; |
| 6147 | break; |
| 6148 | case GETVAL: |
| 6149 | case GETALL: |
| 6150 | perms = SEM__READ; |
| 6151 | break; |
| 6152 | case SETVAL: |
| 6153 | case SETALL: |
| 6154 | perms = SEM__WRITE; |
| 6155 | break; |
| 6156 | case IPC_RMID: |
| 6157 | perms = SEM__DESTROY; |
| 6158 | break; |
| 6159 | case IPC_SET: |
| 6160 | perms = SEM__SETATTR; |
| 6161 | break; |
| 6162 | case IPC_STAT: |
| 6163 | case SEM_STAT: |
Davidlohr Bueso | a280d6d | 2018-04-10 16:35:26 -0700 | [diff] [blame] | 6164 | case SEM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | perms = SEM__GETATTR | SEM__ASSOCIATE; |
| 6166 | break; |
| 6167 | default: |
| 6168 | return 0; |
| 6169 | } |
| 6170 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6171 | err = ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | return err; |
| 6173 | } |
| 6174 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6175 | static int selinux_sem_semop(struct kern_ipc_perm *sma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | struct sembuf *sops, unsigned nsops, int alter) |
| 6177 | { |
| 6178 | u32 perms; |
| 6179 | |
| 6180 | if (alter) |
| 6181 | perms = SEM__READ | SEM__WRITE; |
| 6182 | else |
| 6183 | perms = SEM__READ; |
| 6184 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6185 | return ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) |
| 6189 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | u32 av = 0; |
| 6191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | av = 0; |
| 6193 | if (flag & S_IRUGO) |
| 6194 | av |= IPC__UNIX_READ; |
| 6195 | if (flag & S_IWUGO) |
| 6196 | av |= IPC__UNIX_WRITE; |
| 6197 | |
| 6198 | if (av == 0) |
| 6199 | return 0; |
| 6200 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 6201 | return ipc_has_perm(ipcp, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | } |
| 6203 | |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 6204 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 6205 | { |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6206 | struct ipc_security_struct *isec = selinux_ipc(ipcp); |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 6207 | *secid = isec->sid; |
| 6208 | } |
| 6209 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6210 | static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | { |
| 6212 | if (inode) |
| 6213 | inode_doinit_with_dentry(inode, dentry); |
| 6214 | } |
| 6215 | |
| 6216 | static int selinux_getprocattr(struct task_struct *p, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6217 | char *name, char **value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6219 | const struct task_security_struct *__tsec; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 6220 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | int error; |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6222 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6224 | rcu_read_lock(); |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 6225 | __tsec = selinux_cred(__task_cred(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6227 | if (current != p) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6228 | error = avc_has_perm(&selinux_state, |
| 6229 | current_sid(), __tsec->sid, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6230 | SECCLASS_PROCESS, PROCESS__GETATTR, NULL); |
| 6231 | if (error) |
| 6232 | goto bad; |
| 6233 | } |
| 6234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | if (!strcmp(name, "current")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6236 | sid = __tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | else if (!strcmp(name, "prev")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6238 | sid = __tsec->osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | else if (!strcmp(name, "exec")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6240 | sid = __tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | else if (!strcmp(name, "fscreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6242 | sid = __tsec->create_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6243 | else if (!strcmp(name, "keycreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6244 | sid = __tsec->keycreate_sid; |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6245 | else if (!strcmp(name, "sockcreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6246 | sid = __tsec->sockcreate_sid; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6247 | else { |
| 6248 | error = -EINVAL; |
| 6249 | goto bad; |
| 6250 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6251 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | |
| 6253 | if (!sid) |
| 6254 | return 0; |
| 6255 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6256 | error = security_sid_to_context(&selinux_state, sid, value, &len); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6257 | if (error) |
| 6258 | return error; |
| 6259 | return len; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6260 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6261 | bad: |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6262 | rcu_read_unlock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6263 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | } |
| 6265 | |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6266 | static int selinux_setprocattr(const char *name, void *value, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | { |
| 6268 | struct task_security_struct *tsec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6269 | struct cred *new; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6270 | u32 mysid = current_sid(), sid = 0, ptsid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | int error; |
| 6272 | char *str = value; |
| 6273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | /* |
| 6275 | * Basic control over ability to set these attributes at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | */ |
| 6277 | if (!strcmp(name, "exec")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6278 | error = avc_has_perm(&selinux_state, |
| 6279 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6280 | PROCESS__SETEXEC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | else if (!strcmp(name, "fscreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6282 | error = avc_has_perm(&selinux_state, |
| 6283 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6284 | PROCESS__SETFSCREATE, NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6285 | else if (!strcmp(name, "keycreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6286 | error = avc_has_perm(&selinux_state, |
| 6287 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6288 | PROCESS__SETKEYCREATE, NULL); |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6289 | else if (!strcmp(name, "sockcreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6290 | error = avc_has_perm(&selinux_state, |
| 6291 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6292 | PROCESS__SETSOCKCREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | else if (!strcmp(name, "current")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6294 | error = avc_has_perm(&selinux_state, |
| 6295 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6296 | PROCESS__SETCURRENT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | else |
| 6298 | error = -EINVAL; |
| 6299 | if (error) |
| 6300 | return error; |
| 6301 | |
| 6302 | /* Obtain a SID for the context, if one was specified. */ |
Stephen Smalley | a050a57 | 2017-01-31 11:54:04 -0500 | [diff] [blame] | 6303 | if (size && str[0] && str[0] != '\n') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | if (str[size-1] == '\n') { |
| 6305 | str[size-1] = 0; |
| 6306 | size--; |
| 6307 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6308 | error = security_context_to_sid(&selinux_state, value, size, |
| 6309 | &sid, GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6310 | if (error == -EINVAL && !strcmp(name, "fscreate")) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6311 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6312 | struct audit_buffer *ab; |
| 6313 | size_t audit_size; |
| 6314 | |
| 6315 | /* We strip a nul only if it is at the end, otherwise the |
| 6316 | * context contains a nul and we should audit that */ |
| 6317 | if (str[size - 1] == '\0') |
| 6318 | audit_size = size - 1; |
| 6319 | else |
| 6320 | audit_size = size; |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 6321 | ab = audit_log_start(audit_context(), |
| 6322 | GFP_ATOMIC, |
| 6323 | AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6324 | audit_log_format(ab, "op=fscreate invalid_context="); |
| 6325 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 6326 | audit_log_end(ab); |
| 6327 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6328 | return error; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6329 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6330 | error = security_context_to_sid_force( |
| 6331 | &selinux_state, |
| 6332 | value, size, &sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | if (error) |
| 6335 | return error; |
| 6336 | } |
| 6337 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6338 | new = prepare_creds(); |
| 6339 | if (!new) |
| 6340 | return -ENOMEM; |
| 6341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | /* Permission checking based on the specified context is |
| 6343 | performed during the actual operation (execve, |
| 6344 | open/mkdir/...), when we know the full context of the |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6345 | operation. See selinux_bprm_set_creds for the execve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | checks and may_create for the file creation checks. The |
| 6347 | operation will then fail if the context is not permitted. */ |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 6348 | tsec = selinux_cred(new); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6349 | if (!strcmp(name, "exec")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | tsec->exec_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6351 | } else if (!strcmp(name, "fscreate")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | tsec->create_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6353 | } else if (!strcmp(name, "keycreate")) { |
Ondrej Mosnacek | 464c258 | 2019-06-12 10:12:26 +0200 | [diff] [blame] | 6354 | if (sid) { |
| 6355 | error = avc_has_perm(&selinux_state, mysid, sid, |
| 6356 | SECCLASS_KEY, KEY__CREATE, NULL); |
| 6357 | if (error) |
| 6358 | goto abort_change; |
| 6359 | } |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6360 | tsec->keycreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6361 | } else if (!strcmp(name, "sockcreate")) { |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6362 | tsec->sockcreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6363 | } else if (!strcmp(name, "current")) { |
| 6364 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | if (sid == 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6366 | goto abort_change; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 6367 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6368 | /* Only allow single threaded processes to change context */ |
| 6369 | error = -EPERM; |
Oleg Nesterov | 5bb459b | 2009-07-10 03:48:23 +0200 | [diff] [blame] | 6370 | if (!current_is_single_threaded()) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6371 | error = security_bounded_transition(&selinux_state, |
| 6372 | tsec->sid, sid); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6373 | if (error) |
| 6374 | goto abort_change; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | |
| 6377 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6378 | error = avc_has_perm(&selinux_state, |
| 6379 | tsec->sid, sid, SECCLASS_PROCESS, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6380 | PROCESS__DYNTRANSITION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6382 | goto abort_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
| 6384 | /* Check for ptracing, and update the task SID if ok. |
| 6385 | Otherwise, leave SID unchanged and fail. */ |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6386 | ptsid = ptrace_parent_sid(); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 6387 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6388 | error = avc_has_perm(&selinux_state, |
| 6389 | ptsid, sid, SECCLASS_PROCESS, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6390 | PROCESS__PTRACE, NULL); |
| 6391 | if (error) |
| 6392 | goto abort_change; |
| 6393 | } |
| 6394 | |
| 6395 | tsec->sid = sid; |
| 6396 | } else { |
| 6397 | error = -EINVAL; |
| 6398 | goto abort_change; |
| 6399 | } |
| 6400 | |
| 6401 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | return size; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6403 | |
| 6404 | abort_change: |
| 6405 | abort_creds(new); |
| 6406 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | } |
| 6408 | |
David Quigley | 746df9b | 2013-05-22 12:50:35 -0400 | [diff] [blame] | 6409 | static int selinux_ismaclabel(const char *name) |
| 6410 | { |
| 6411 | return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); |
| 6412 | } |
| 6413 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6414 | static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 6415 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6416 | return security_sid_to_context(&selinux_state, secid, |
| 6417 | secdata, seclen); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6418 | } |
| 6419 | |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 6420 | 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] | 6421 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6422 | return security_context_to_sid(&selinux_state, secdata, seclen, |
| 6423 | secid, GFP_KERNEL); |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6426 | static void selinux_release_secctx(char *secdata, u32 seclen) |
| 6427 | { |
Paul Moore | 088999e | 2007-08-01 11:12:58 -0400 | [diff] [blame] | 6428 | kfree(secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6429 | } |
| 6430 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6431 | static void selinux_inode_invalidate_secctx(struct inode *inode) |
| 6432 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 6433 | struct inode_security_struct *isec = selinux_inode(inode); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6434 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6435 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6436 | isec->initialized = LABEL_INVALID; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6437 | spin_unlock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6438 | } |
| 6439 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 6440 | /* |
| 6441 | * called with inode->i_mutex locked |
| 6442 | */ |
| 6443 | static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 6444 | { |
Ondrej Mosnacek | 53e0c2a | 2018-12-21 21:18:53 +0100 | [diff] [blame] | 6445 | int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 6446 | ctx, ctxlen, 0); |
| 6447 | /* Do not return error when suppressing label (SBLABEL_MNT not set). */ |
| 6448 | return rc == -EOPNOTSUPP ? 0 : rc; |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 6449 | } |
| 6450 | |
| 6451 | /* |
| 6452 | * called with inode->i_mutex locked |
| 6453 | */ |
| 6454 | static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 6455 | { |
| 6456 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); |
| 6457 | } |
| 6458 | |
| 6459 | static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 6460 | { |
| 6461 | int len = 0; |
| 6462 | len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 6463 | ctx, true); |
| 6464 | if (len < 0) |
| 6465 | return len; |
| 6466 | *ctxlen = len; |
| 6467 | return 0; |
| 6468 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6469 | #ifdef CONFIG_KEYS |
| 6470 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6471 | static int selinux_key_alloc(struct key *k, const struct cred *cred, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 6472 | unsigned long flags) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6473 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6474 | const struct task_security_struct *tsec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6475 | struct key_security_struct *ksec; |
| 6476 | |
| 6477 | ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); |
| 6478 | if (!ksec) |
| 6479 | return -ENOMEM; |
| 6480 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 6481 | tsec = selinux_cred(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6482 | if (tsec->keycreate_sid) |
| 6483 | ksec->sid = tsec->keycreate_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6484 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6485 | ksec->sid = tsec->sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6486 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6487 | k->security = ksec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6488 | return 0; |
| 6489 | } |
| 6490 | |
| 6491 | static void selinux_key_free(struct key *k) |
| 6492 | { |
| 6493 | struct key_security_struct *ksec = k->security; |
| 6494 | |
| 6495 | k->security = NULL; |
| 6496 | kfree(ksec); |
| 6497 | } |
| 6498 | |
| 6499 | static int selinux_key_permission(key_ref_t key_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6500 | const struct cred *cred, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 6501 | unsigned perm) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6502 | { |
| 6503 | struct key *key; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6504 | struct key_security_struct *ksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6505 | u32 sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6506 | |
| 6507 | /* if no specific permissions are requested, we skip the |
| 6508 | permission check. No serious, additional covert channels |
| 6509 | appear to be created. */ |
| 6510 | if (perm == 0) |
| 6511 | return 0; |
| 6512 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6513 | sid = cred_sid(cred); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6514 | |
| 6515 | key = key_ref_to_ptr(key_ref); |
| 6516 | ksec = key->security; |
| 6517 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6518 | return avc_has_perm(&selinux_state, |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 6519 | sid, ksec->sid, SECCLASS_KEY, perm, NULL); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6520 | } |
| 6521 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6522 | static int selinux_key_getsecurity(struct key *key, char **_buffer) |
| 6523 | { |
| 6524 | struct key_security_struct *ksec = key->security; |
| 6525 | char *context = NULL; |
| 6526 | unsigned len; |
| 6527 | int rc; |
| 6528 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6529 | rc = security_sid_to_context(&selinux_state, ksec->sid, |
| 6530 | &context, &len); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6531 | if (!rc) |
| 6532 | rc = len; |
| 6533 | *_buffer = context; |
| 6534 | return rc; |
| 6535 | } |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6536 | #endif |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6537 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6538 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6539 | static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) |
| 6540 | { |
| 6541 | struct common_audit_data ad; |
| 6542 | int err; |
| 6543 | u32 sid = 0; |
| 6544 | struct ib_security_struct *sec = ib_sec; |
| 6545 | struct lsm_ibpkey_audit ibpkey; |
| 6546 | |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 6547 | err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6548 | if (err) |
| 6549 | return err; |
| 6550 | |
| 6551 | ad.type = LSM_AUDIT_DATA_IBPKEY; |
| 6552 | ibpkey.subnet_prefix = subnet_prefix; |
| 6553 | ibpkey.pkey = pkey_val; |
| 6554 | ad.u.ibpkey = &ibpkey; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6555 | return avc_has_perm(&selinux_state, |
| 6556 | sec->sid, sid, |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6557 | SECCLASS_INFINIBAND_PKEY, |
| 6558 | INFINIBAND_PKEY__ACCESS, &ad); |
| 6559 | } |
| 6560 | |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6561 | static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, |
| 6562 | u8 port_num) |
| 6563 | { |
| 6564 | struct common_audit_data ad; |
| 6565 | int err; |
| 6566 | u32 sid = 0; |
| 6567 | struct ib_security_struct *sec = ib_sec; |
| 6568 | struct lsm_ibendport_audit ibendport; |
| 6569 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6570 | err = security_ib_endport_sid(&selinux_state, dev_name, port_num, |
| 6571 | &sid); |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6572 | |
| 6573 | if (err) |
| 6574 | return err; |
| 6575 | |
| 6576 | ad.type = LSM_AUDIT_DATA_IBENDPORT; |
| 6577 | strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); |
| 6578 | ibendport.port = port_num; |
| 6579 | ad.u.ibendport = &ibendport; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6580 | return avc_has_perm(&selinux_state, |
| 6581 | sec->sid, sid, |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6582 | SECCLASS_INFINIBAND_ENDPORT, |
| 6583 | INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); |
| 6584 | } |
| 6585 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6586 | static int selinux_ib_alloc_security(void **ib_sec) |
| 6587 | { |
| 6588 | struct ib_security_struct *sec; |
| 6589 | |
| 6590 | sec = kzalloc(sizeof(*sec), GFP_KERNEL); |
| 6591 | if (!sec) |
| 6592 | return -ENOMEM; |
| 6593 | sec->sid = current_sid(); |
| 6594 | |
| 6595 | *ib_sec = sec; |
| 6596 | return 0; |
| 6597 | } |
| 6598 | |
| 6599 | static void selinux_ib_free_security(void *ib_sec) |
| 6600 | { |
| 6601 | kfree(ib_sec); |
| 6602 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6603 | #endif |
| 6604 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6605 | #ifdef CONFIG_BPF_SYSCALL |
| 6606 | static int selinux_bpf(int cmd, union bpf_attr *attr, |
| 6607 | unsigned int size) |
| 6608 | { |
| 6609 | u32 sid = current_sid(); |
| 6610 | int ret; |
| 6611 | |
| 6612 | switch (cmd) { |
| 6613 | case BPF_MAP_CREATE: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6614 | ret = avc_has_perm(&selinux_state, |
| 6615 | sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6616 | NULL); |
| 6617 | break; |
| 6618 | case BPF_PROG_LOAD: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6619 | ret = avc_has_perm(&selinux_state, |
| 6620 | sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6621 | NULL); |
| 6622 | break; |
| 6623 | default: |
| 6624 | ret = 0; |
| 6625 | break; |
| 6626 | } |
| 6627 | |
| 6628 | return ret; |
| 6629 | } |
| 6630 | |
| 6631 | static u32 bpf_map_fmode_to_av(fmode_t fmode) |
| 6632 | { |
| 6633 | u32 av = 0; |
| 6634 | |
| 6635 | if (fmode & FMODE_READ) |
| 6636 | av |= BPF__MAP_READ; |
| 6637 | if (fmode & FMODE_WRITE) |
| 6638 | av |= BPF__MAP_WRITE; |
| 6639 | return av; |
| 6640 | } |
| 6641 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6642 | /* This function will check the file pass through unix socket or binder to see |
| 6643 | * if it is a bpf related object. And apply correspinding checks on the bpf |
| 6644 | * object based on the type. The bpf maps and programs, not like other files and |
| 6645 | * socket, are using a shared anonymous inode inside the kernel as their inode. |
| 6646 | * So checking that inode cannot identify if the process have privilege to |
| 6647 | * access the bpf object and that's why we have to add this additional check in |
| 6648 | * selinux_file_receive and selinux_binder_transfer_files. |
| 6649 | */ |
| 6650 | static int bpf_fd_pass(struct file *file, u32 sid) |
| 6651 | { |
| 6652 | struct bpf_security_struct *bpfsec; |
| 6653 | struct bpf_prog *prog; |
| 6654 | struct bpf_map *map; |
| 6655 | int ret; |
| 6656 | |
| 6657 | if (file->f_op == &bpf_map_fops) { |
| 6658 | map = file->private_data; |
| 6659 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6660 | ret = avc_has_perm(&selinux_state, |
| 6661 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6662 | bpf_map_fmode_to_av(file->f_mode), NULL); |
| 6663 | if (ret) |
| 6664 | return ret; |
| 6665 | } else if (file->f_op == &bpf_prog_fops) { |
| 6666 | prog = file->private_data; |
| 6667 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6668 | ret = avc_has_perm(&selinux_state, |
| 6669 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6670 | BPF__PROG_RUN, NULL); |
| 6671 | if (ret) |
| 6672 | return ret; |
| 6673 | } |
| 6674 | return 0; |
| 6675 | } |
| 6676 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6677 | static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) |
| 6678 | { |
| 6679 | u32 sid = current_sid(); |
| 6680 | struct bpf_security_struct *bpfsec; |
| 6681 | |
| 6682 | bpfsec = map->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_map_fmode_to_av(fmode), NULL); |
| 6686 | } |
| 6687 | |
| 6688 | static int selinux_bpf_prog(struct bpf_prog *prog) |
| 6689 | { |
| 6690 | u32 sid = current_sid(); |
| 6691 | struct bpf_security_struct *bpfsec; |
| 6692 | |
| 6693 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6694 | return avc_has_perm(&selinux_state, |
| 6695 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6696 | BPF__PROG_RUN, NULL); |
| 6697 | } |
| 6698 | |
| 6699 | static int selinux_bpf_map_alloc(struct bpf_map *map) |
| 6700 | { |
| 6701 | struct bpf_security_struct *bpfsec; |
| 6702 | |
| 6703 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6704 | if (!bpfsec) |
| 6705 | return -ENOMEM; |
| 6706 | |
| 6707 | bpfsec->sid = current_sid(); |
| 6708 | map->security = bpfsec; |
| 6709 | |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
| 6713 | static void selinux_bpf_map_free(struct bpf_map *map) |
| 6714 | { |
| 6715 | struct bpf_security_struct *bpfsec = map->security; |
| 6716 | |
| 6717 | map->security = NULL; |
| 6718 | kfree(bpfsec); |
| 6719 | } |
| 6720 | |
| 6721 | static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) |
| 6722 | { |
| 6723 | struct bpf_security_struct *bpfsec; |
| 6724 | |
| 6725 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6726 | if (!bpfsec) |
| 6727 | return -ENOMEM; |
| 6728 | |
| 6729 | bpfsec->sid = current_sid(); |
| 6730 | aux->security = bpfsec; |
| 6731 | |
| 6732 | return 0; |
| 6733 | } |
| 6734 | |
| 6735 | static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) |
| 6736 | { |
| 6737 | struct bpf_security_struct *bpfsec = aux->security; |
| 6738 | |
| 6739 | aux->security = NULL; |
| 6740 | kfree(bpfsec); |
| 6741 | } |
| 6742 | #endif |
| 6743 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 6744 | struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { |
| 6745 | .lbs_cred = sizeof(struct task_security_struct), |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame] | 6746 | .lbs_file = sizeof(struct file_security_struct), |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 6747 | .lbs_inode = sizeof(struct inode_security_struct), |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6748 | .lbs_ipc = sizeof(struct ipc_security_struct), |
| 6749 | .lbs_msg_msg = sizeof(struct msg_security_struct), |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 6750 | }; |
| 6751 | |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 6752 | static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6753 | LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), |
| 6754 | LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), |
| 6755 | LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), |
| 6756 | LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6757 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6758 | LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), |
| 6759 | LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), |
| 6760 | LSM_HOOK_INIT(capget, selinux_capget), |
| 6761 | LSM_HOOK_INIT(capset, selinux_capset), |
| 6762 | LSM_HOOK_INIT(capable, selinux_capable), |
| 6763 | LSM_HOOK_INIT(quotactl, selinux_quotactl), |
| 6764 | LSM_HOOK_INIT(quota_on, selinux_quota_on), |
| 6765 | LSM_HOOK_INIT(syslog, selinux_syslog), |
| 6766 | LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 6767 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6768 | LSM_HOOK_INIT(netlink_send, selinux_netlink_send), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6770 | LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), |
| 6771 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), |
| 6772 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 6774 | LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 6775 | LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), |
| 6776 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6777 | LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), |
| 6778 | LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 6779 | LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 6780 | LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6781 | LSM_HOOK_INIT(sb_remount, selinux_sb_remount), |
| 6782 | LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), |
| 6783 | LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), |
| 6784 | LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), |
| 6785 | LSM_HOOK_INIT(sb_mount, selinux_mount), |
| 6786 | LSM_HOOK_INIT(sb_umount, selinux_umount), |
| 6787 | LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), |
| 6788 | LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 6789 | LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6791 | LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 6792 | LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 6793 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6794 | LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), |
| 6795 | LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), |
| 6796 | LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), |
| 6797 | LSM_HOOK_INIT(inode_create, selinux_inode_create), |
| 6798 | LSM_HOOK_INIT(inode_link, selinux_inode_link), |
| 6799 | LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), |
| 6800 | LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), |
| 6801 | LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), |
| 6802 | LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), |
| 6803 | LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), |
| 6804 | LSM_HOOK_INIT(inode_rename, selinux_inode_rename), |
| 6805 | LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), |
| 6806 | LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), |
| 6807 | LSM_HOOK_INIT(inode_permission, selinux_inode_permission), |
| 6808 | LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), |
| 6809 | LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), |
| 6810 | LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), |
| 6811 | LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), |
| 6812 | LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), |
| 6813 | LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), |
| 6814 | LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), |
| 6815 | LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), |
| 6816 | LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), |
| 6817 | LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), |
| 6818 | LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 6819 | LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 6820 | LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 6822 | LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security), |
| 6823 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6824 | LSM_HOOK_INIT(file_permission, selinux_file_permission), |
| 6825 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6826 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), |
| 6827 | LSM_HOOK_INIT(mmap_file, selinux_mmap_file), |
| 6828 | LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), |
| 6829 | LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), |
| 6830 | LSM_HOOK_INIT(file_lock, selinux_file_lock), |
| 6831 | LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), |
| 6832 | LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), |
| 6833 | LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), |
| 6834 | LSM_HOOK_INIT(file_receive, selinux_file_receive), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6836 | LSM_HOOK_INIT(file_open, selinux_file_open), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 6838 | LSM_HOOK_INIT(task_alloc, selinux_task_alloc), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6839 | LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), |
| 6840 | LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 6841 | LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6842 | LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), |
| 6843 | LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), |
| 6844 | LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 6845 | LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 6846 | LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6847 | LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), |
| 6848 | LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), |
| 6849 | LSM_HOOK_INIT(task_getsid, selinux_task_getsid), |
| 6850 | LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), |
| 6851 | LSM_HOOK_INIT(task_setnice, selinux_task_setnice), |
| 6852 | LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), |
| 6853 | LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 6854 | LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6855 | LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), |
| 6856 | LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), |
| 6857 | LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), |
| 6858 | LSM_HOOK_INIT(task_movememory, selinux_task_movememory), |
| 6859 | LSM_HOOK_INIT(task_kill, selinux_task_kill), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6860 | LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 6861 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6862 | LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), |
| 6863 | LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6865 | LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), |
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(msg_queue_alloc_security, |
| 6868 | selinux_msg_queue_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6869 | LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), |
| 6870 | LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), |
| 6871 | LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), |
| 6872 | LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6874 | LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6875 | LSM_HOOK_INIT(shm_associate, selinux_shm_associate), |
| 6876 | LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), |
| 6877 | LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6879 | LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6880 | LSM_HOOK_INIT(sem_associate, selinux_sem_associate), |
| 6881 | LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), |
| 6882 | LSM_HOOK_INIT(sem_semop, selinux_sem_semop), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6884 | LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6886 | LSM_HOOK_INIT(getprocattr, selinux_getprocattr), |
| 6887 | LSM_HOOK_INIT(setprocattr, selinux_setprocattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6889 | LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), |
| 6890 | LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), |
| 6891 | LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), |
| 6892 | LSM_HOOK_INIT(release_secctx, selinux_release_secctx), |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6893 | LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6894 | LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), |
| 6895 | LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), |
| 6896 | LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6897 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6898 | LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), |
| 6899 | LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6900 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6901 | LSM_HOOK_INIT(socket_create, selinux_socket_create), |
| 6902 | LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 6903 | LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6904 | LSM_HOOK_INIT(socket_bind, selinux_socket_bind), |
| 6905 | LSM_HOOK_INIT(socket_connect, selinux_socket_connect), |
| 6906 | LSM_HOOK_INIT(socket_listen, selinux_socket_listen), |
| 6907 | LSM_HOOK_INIT(socket_accept, selinux_socket_accept), |
| 6908 | LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), |
| 6909 | LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), |
| 6910 | LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), |
| 6911 | LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), |
| 6912 | LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), |
| 6913 | LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), |
| 6914 | LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), |
| 6915 | LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), |
| 6916 | LSM_HOOK_INIT(socket_getpeersec_stream, |
| 6917 | selinux_socket_getpeersec_stream), |
| 6918 | LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), |
| 6919 | LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), |
| 6920 | LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), |
| 6921 | LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), |
| 6922 | LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), |
| 6923 | LSM_HOOK_INIT(sock_graft, selinux_sock_graft), |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 6924 | LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), |
| 6925 | LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), |
| 6926 | LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6927 | LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), |
| 6928 | LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), |
| 6929 | LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), |
| 6930 | LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), |
| 6931 | LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), |
| 6932 | LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), |
| 6933 | LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), |
| 6934 | LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), |
| 6935 | LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), |
| 6936 | LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), |
| 6937 | LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), |
| 6938 | LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), |
| 6939 | LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6940 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6941 | LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6942 | LSM_HOOK_INIT(ib_endport_manage_subnet, |
| 6943 | selinux_ib_endport_manage_subnet), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6944 | LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), |
| 6945 | LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), |
| 6946 | #endif |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 6947 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6948 | LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), |
| 6949 | LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), |
| 6950 | LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), |
| 6951 | LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), |
| 6952 | LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), |
| 6953 | LSM_HOOK_INIT(xfrm_state_alloc_acquire, |
| 6954 | selinux_xfrm_state_alloc_acquire), |
| 6955 | LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), |
| 6956 | LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), |
| 6957 | LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), |
| 6958 | LSM_HOOK_INIT(xfrm_state_pol_flow_match, |
| 6959 | selinux_xfrm_state_pol_flow_match), |
| 6960 | LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6962 | |
| 6963 | #ifdef CONFIG_KEYS |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6964 | LSM_HOOK_INIT(key_alloc, selinux_key_alloc), |
| 6965 | LSM_HOOK_INIT(key_free, selinux_key_free), |
| 6966 | LSM_HOOK_INIT(key_permission, selinux_key_permission), |
| 6967 | LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6968 | #endif |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6969 | |
| 6970 | #ifdef CONFIG_AUDIT |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6971 | LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), |
| 6972 | LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), |
| 6973 | LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), |
| 6974 | LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6975 | #endif |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6976 | |
| 6977 | #ifdef CONFIG_BPF_SYSCALL |
| 6978 | LSM_HOOK_INIT(bpf, selinux_bpf), |
| 6979 | LSM_HOOK_INIT(bpf_map, selinux_bpf_map), |
| 6980 | LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), |
| 6981 | LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), |
| 6982 | LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), |
| 6983 | LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), |
| 6984 | LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), |
| 6985 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | }; |
| 6987 | |
| 6988 | static __init int selinux_init(void) |
| 6989 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6990 | pr_info("SELinux: Initializing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6991 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6992 | memset(&selinux_state, 0, sizeof(selinux_state)); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 6993 | enforcing_set(&selinux_state, selinux_enforcing_boot); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6994 | selinux_state.checkreqprot = selinux_checkreqprot_boot; |
| 6995 | selinux_ss_init(&selinux_state.ss); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6996 | selinux_avc_init(&selinux_state.avc); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | /* Set the security state for the initial task. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6999 | cred_init_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 7001 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 7002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | avc_init(); |
| 7004 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7005 | avtab_cache_init(); |
| 7006 | |
| 7007 | ebitmap_cache_init(); |
| 7008 | |
| 7009 | hashtab_cache_init(); |
| 7010 | |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 7011 | security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 7013 | if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) |
| 7014 | panic("SELinux: Unable to register AVC netcache callback\n"); |
| 7015 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 7016 | if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) |
| 7017 | panic("SELinux: Unable to register AVC LSM notifier callback\n"); |
| 7018 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7019 | if (selinux_enforcing_boot) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7020 | pr_debug("SELinux: Starting in enforcing mode\n"); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 7021 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7022 | pr_debug("SELinux: Starting in permissive mode\n"); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 7023 | |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 7024 | fs_validate_description(&selinux_fs_parameters); |
| 7025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | return 0; |
| 7027 | } |
| 7028 | |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7029 | static void delayed_superblock_init(struct super_block *sb, void *unused) |
| 7030 | { |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 7031 | selinux_set_mnt_opts(sb, NULL, 0, NULL); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7032 | } |
| 7033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | void selinux_complete_init(void) |
| 7035 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7036 | pr_debug("SELinux: Completing initialization.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | |
| 7038 | /* Set up any superblocks initialized prior to the policy load. */ |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7039 | pr_debug("SELinux: Setting up existing superblocks.\n"); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7040 | iterate_supers(delayed_superblock_init, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | /* SELinux requires early initialization in order to label |
| 7044 | all processes and objects when they are created. */ |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7045 | DEFINE_LSM(selinux) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 7046 | .name = "selinux", |
Kees Cook | 14bd99c | 2018-09-19 19:57:06 -0700 | [diff] [blame] | 7047 | .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
Kees Cook | c5459b8 | 2018-09-13 22:28:48 -0700 | [diff] [blame] | 7048 | .enabled = &selinux_enabled, |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 7049 | .blobs = &selinux_blob_sizes, |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7050 | .init = selinux_init, |
| 7051 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7053 | #if defined(CONFIG_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | |
Florian Westphal | 591bb27 | 2017-07-26 11:40:52 +0200 | [diff] [blame] | 7055 | static const struct nf_hook_ops selinux_nf_ops[] = { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7056 | { |
| 7057 | .hook = selinux_ipv4_postroute, |
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_POST_ROUTING, |
| 7060 | .priority = NF_IP_PRI_SELINUX_LAST, |
| 7061 | }, |
| 7062 | { |
| 7063 | .hook = selinux_ipv4_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7064 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7065 | .hooknum = NF_INET_FORWARD, |
| 7066 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7067 | }, |
| 7068 | { |
| 7069 | .hook = selinux_ipv4_output, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7070 | .pf = NFPROTO_IPV4, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7071 | .hooknum = NF_INET_LOCAL_OUT, |
| 7072 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7073 | }, |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 7074 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7075 | { |
| 7076 | .hook = selinux_ipv6_postroute, |
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_POST_ROUTING, |
| 7079 | .priority = NF_IP6_PRI_SELINUX_LAST, |
| 7080 | }, |
| 7081 | { |
| 7082 | .hook = selinux_ipv6_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7083 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7084 | .hooknum = NF_INET_FORWARD, |
| 7085 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7086 | }, |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 7087 | { |
| 7088 | .hook = selinux_ipv6_output, |
| 7089 | .pf = NFPROTO_IPV6, |
| 7090 | .hooknum = NF_INET_LOCAL_OUT, |
| 7091 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 7092 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | #endif /* IPV6 */ |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7094 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7096 | static int __net_init selinux_nf_register(struct net *net) |
| 7097 | { |
| 7098 | return nf_register_net_hooks(net, selinux_nf_ops, |
| 7099 | ARRAY_SIZE(selinux_nf_ops)); |
| 7100 | } |
| 7101 | |
| 7102 | static void __net_exit selinux_nf_unregister(struct net *net) |
| 7103 | { |
| 7104 | nf_unregister_net_hooks(net, selinux_nf_ops, |
| 7105 | ARRAY_SIZE(selinux_nf_ops)); |
| 7106 | } |
| 7107 | |
| 7108 | static struct pernet_operations selinux_net_ops = { |
| 7109 | .init = selinux_nf_register, |
| 7110 | .exit = selinux_nf_unregister, |
| 7111 | }; |
| 7112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7113 | static int __init selinux_nf_ip_init(void) |
| 7114 | { |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7115 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | |
| 7117 | if (!selinux_enabled) |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7118 | return 0; |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7119 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7120 | pr_debug("SELinux: Registering netfilter hooks\n"); |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7121 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7122 | err = register_pernet_subsys(&selinux_net_ops); |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 7123 | if (err) |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7124 | panic("SELinux: register_pernet_subsys: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7126 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | __initcall(selinux_nf_ip_init); |
| 7129 | |
| 7130 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7131 | static void selinux_nf_ip_exit(void) |
| 7132 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7133 | pr_debug("SELinux: Unregistering netfilter hooks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7135 | unregister_pernet_subsys(&selinux_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | } |
| 7137 | #endif |
| 7138 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7139 | #else /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | |
| 7141 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7142 | #define selinux_nf_ip_exit() |
| 7143 | #endif |
| 7144 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7145 | #endif /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | |
| 7147 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7148 | int selinux_disable(struct selinux_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7150 | if (state->initialized) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7151 | /* Not permitted after initial policy load. */ |
| 7152 | return -EINVAL; |
| 7153 | } |
| 7154 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7155 | if (state->disabled) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | /* Only do this once. */ |
| 7157 | return -EINVAL; |
| 7158 | } |
| 7159 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7160 | state->disabled = 1; |
| 7161 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7162 | pr_info("SELinux: Disabled at runtime.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 7164 | selinux_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7165 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 7166 | security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | |
Eric Paris | af8ff04 | 2009-09-20 21:23:01 -0400 | [diff] [blame] | 7168 | /* Try to destroy the avc node cache */ |
| 7169 | avc_disable(); |
| 7170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | /* Unregister netfilter hooks. */ |
| 7172 | selinux_nf_ip_exit(); |
| 7173 | |
| 7174 | /* Unregister selinuxfs. */ |
| 7175 | exit_sel_fs(); |
| 7176 | |
| 7177 | return 0; |
| 7178 | } |
| 7179 | #endif |