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> |
Scott Branden | b89999d0 | 2020-10-02 10:38:15 -0700 | [diff] [blame] | 27 | #include <linux/kernel_read_file.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 28 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/errno.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 30 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 31 | #include <linux/sched/task.h> |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 32 | #include <linux/lsm_hooks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/xattr.h> |
| 34 | #include <linux/capability.h> |
| 35 | #include <linux/unistd.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mman.h> |
| 38 | #include <linux/slab.h> |
| 39 | #include <linux/pagemap.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/syscalls.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 44 | #include <linux/dcache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 46 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/namei.h> |
| 48 | #include <linux/mount.h> |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 49 | #include <linux/fs_context.h> |
| 50 | #include <linux/fs_parser.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/netfilter_ipv4.h> |
| 52 | #include <linux/netfilter_ipv6.h> |
| 53 | #include <linux/tty.h> |
| 54 | #include <net/icmp.h> |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 55 | #include <net/ip.h> /* for local_port_range[] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 57 | #include <net/inet_connection_sock.h> |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 58 | #include <net/net_namespace.h> |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 59 | #include <net/netlabel.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 60 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <asm/ioctls.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 62 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/bitops.h> |
| 64 | #include <linux/interrupt.h> |
| 65 | #include <linux/netdevice.h> /* for network interface checks */ |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 66 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/tcp.h> |
| 68 | #include <linux/udp.h> |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 69 | #include <linux/dccp.h> |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 70 | #include <linux/sctp.h> |
| 71 | #include <net/sctp/structs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/quota.h> |
| 73 | #include <linux/un.h> /* for Unix socket types */ |
| 74 | #include <net/af_unix.h> /* for Unix socket types */ |
| 75 | #include <linux/parser.h> |
| 76 | #include <linux/nfs_mount.h> |
| 77 | #include <net/ipv6.h> |
| 78 | #include <linux/hugetlb.h> |
| 79 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <linux/audit.h> |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 81 | #include <linux/string.h> |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 82 | #include <linux/mutex.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 83 | #include <linux/posix-timers.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 84 | #include <linux/syslog.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 85 | #include <linux/user_namespace.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 86 | #include <linux/export.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 87 | #include <linux/msg.h> |
| 88 | #include <linux/shm.h> |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 89 | #include <linux/bpf.h> |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 90 | #include <linux/kernfs.h> |
| 91 | #include <linux/stringhash.h> /* for hashlen_string() */ |
David Howells | e262e32d | 2018-11-01 23:07:23 +0000 | [diff] [blame] | 92 | #include <uapi/linux/mount.h> |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 93 | #include <linux/fsnotify.h> |
| 94 | #include <linux/fanotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | #include "avc.h" |
| 97 | #include "objsec.h" |
| 98 | #include "netif.h" |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 99 | #include "netnode.h" |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 100 | #include "netport.h" |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 101 | #include "ibpkey.h" |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 102 | #include "xfrm.h" |
Paul Moore | c60475b | 2007-02-28 15:14:23 -0500 | [diff] [blame] | 103 | #include "netlabel.h" |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 104 | #include "audit.h" |
James Morris | 7b98a58 | 2011-08-30 12:52:32 +1000 | [diff] [blame] | 105 | #include "avc_ss.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 107 | struct selinux_state selinux_state; |
| 108 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 109 | /* SECMARK reference count */ |
James Morris | 56a4ca9 | 2011-08-17 11:08:43 +1000 | [diff] [blame] | 110 | static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
Stephen Smalley | 6c5a682 | 2019-12-17 09:15:10 -0500 | [diff] [blame] | 113 | static int selinux_enforcing_boot __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | static int __init enforcing_setup(char *str) |
| 116 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 117 | unsigned long enforcing; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 118 | if (!kstrtoul(str, 0, &enforcing)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 119 | selinux_enforcing_boot = enforcing ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return 1; |
| 121 | } |
| 122 | __setup("enforcing=", enforcing_setup); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 123 | #else |
| 124 | #define selinux_enforcing_boot 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #endif |
| 126 | |
Stephen Smalley | 6c5a682 | 2019-12-17 09:15:10 -0500 | [diff] [blame] | 127 | int selinux_enabled_boot __initdata = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | static int __init selinux_enabled_setup(char *str) |
| 130 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 131 | unsigned long enabled; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 132 | if (!kstrtoul(str, 0, &enabled)) |
Stephen Smalley | 6c5a682 | 2019-12-17 09:15:10 -0500 | [diff] [blame] | 133 | selinux_enabled_boot = enabled ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return 1; |
| 135 | } |
| 136 | __setup("selinux=", selinux_enabled_setup); |
| 137 | #endif |
| 138 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 139 | static unsigned int selinux_checkreqprot_boot = |
| 140 | CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 141 | |
| 142 | static int __init checkreqprot_setup(char *str) |
| 143 | { |
| 144 | unsigned long checkreqprot; |
| 145 | |
Stephen Smalley | e9c38f9 | 2020-01-08 11:24:47 -0500 | [diff] [blame] | 146 | if (!kstrtoul(str, 0, &checkreqprot)) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 147 | selinux_checkreqprot_boot = checkreqprot ? 1 : 0; |
Stephen Smalley | e9c38f9 | 2020-01-08 11:24:47 -0500 | [diff] [blame] | 148 | if (checkreqprot) |
| 149 | pr_warn("SELinux: checkreqprot set to 1 via kernel parameter. This is deprecated and will be rejected in a future kernel release.\n"); |
| 150 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | __setup("checkreqprot=", checkreqprot_setup); |
| 154 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 155 | /** |
| 156 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled |
| 157 | * |
| 158 | * Description: |
| 159 | * This function checks the SECMARK reference counter to see if any SECMARK |
| 160 | * targets are currently configured, if the reference counter is greater than |
| 161 | * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 162 | * enabled, false (0) if SECMARK is disabled. If the always_check_network |
| 163 | * policy capability is enabled, SECMARK is always considered enabled. |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 164 | * |
| 165 | */ |
| 166 | static int selinux_secmark_enabled(void) |
| 167 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 168 | return (selinux_policycap_alwaysnetwork() || |
| 169 | atomic_read(&selinux_secmark_refcount)); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled |
| 174 | * |
| 175 | * Description: |
| 176 | * This function checks if NetLabel or labeled IPSEC is enabled. Returns true |
| 177 | * (1) if any are enabled or false (0) if neither are enabled. If the |
| 178 | * always_check_network policy capability is enabled, peer labeling |
| 179 | * is always considered enabled. |
| 180 | * |
| 181 | */ |
| 182 | static int selinux_peerlbl_enabled(void) |
| 183 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 184 | return (selinux_policycap_alwaysnetwork() || |
| 185 | netlbl_enabled() || selinux_xfrm_enabled()); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 188 | static int selinux_netcache_avc_callback(u32 event) |
| 189 | { |
| 190 | if (event == AVC_CALLBACK_RESET) { |
| 191 | sel_netif_flush(); |
| 192 | sel_netnode_flush(); |
| 193 | sel_netport_flush(); |
| 194 | synchronize_net(); |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 199 | static int selinux_lsm_notifier_avc_callback(u32 event) |
| 200 | { |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 201 | if (event == AVC_CALLBACK_RESET) { |
| 202 | sel_ib_pkey_flush(); |
Janne Karhunen | 42df744 | 2019-06-14 15:20:14 +0300 | [diff] [blame] | 203 | call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 204 | } |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 209 | /* |
| 210 | * initialise the security for the init task |
| 211 | */ |
| 212 | static void cred_init_security(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 214 | struct cred *cred = (struct cred *) current->real_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct task_security_struct *tsec; |
| 216 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 217 | tsec = selinux_cred(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 218 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 221 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 222 | * get the security ID of a set of credentials |
| 223 | */ |
| 224 | static inline u32 cred_sid(const struct cred *cred) |
| 225 | { |
| 226 | const struct task_security_struct *tsec; |
| 227 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 228 | tsec = selinux_cred(cred); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 229 | return tsec->sid; |
| 230 | } |
| 231 | |
| 232 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 233 | * get the objective security ID of a task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 234 | */ |
| 235 | static inline u32 task_sid(const struct task_struct *task) |
| 236 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 237 | u32 sid; |
| 238 | |
| 239 | rcu_read_lock(); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 240 | sid = cred_sid(__task_cred(task)); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 241 | rcu_read_unlock(); |
| 242 | return sid; |
| 243 | } |
| 244 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 245 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); |
| 246 | |
| 247 | /* |
| 248 | * Try reloading inode security labels that have been marked as invalid. The |
| 249 | * @may_sleep parameter indicates when sleeping and thus reloading labels is |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 250 | * allowed; when set to false, returns -ECHILD when the label is |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 251 | * 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] | 252 | */ |
| 253 | static int __inode_security_revalidate(struct inode *inode, |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 254 | struct dentry *dentry, |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 255 | bool may_sleep) |
| 256 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 257 | struct inode_security_struct *isec = selinux_inode(inode); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 258 | |
| 259 | might_sleep_if(may_sleep); |
| 260 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 261 | if (selinux_initialized(&selinux_state) && |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 262 | isec->initialized != LABEL_INITIALIZED) { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 263 | if (!may_sleep) |
| 264 | return -ECHILD; |
| 265 | |
| 266 | /* |
| 267 | * Try reloading the inode security label. This will fail if |
| 268 | * @opt_dentry is NULL and no dentry for this inode can be |
| 269 | * found; in that case, continue using the old label. |
| 270 | */ |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 271 | inode_doinit_with_dentry(inode, dentry); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 276 | static struct inode_security_struct *inode_security_novalidate(struct inode *inode) |
| 277 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 278 | return selinux_inode(inode); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) |
| 282 | { |
| 283 | int error; |
| 284 | |
| 285 | error = __inode_security_revalidate(inode, NULL, !rcu); |
| 286 | if (error) |
| 287 | return ERR_PTR(error); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 288 | return selinux_inode(inode); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 291 | /* |
| 292 | * Get the security label of an inode. |
| 293 | */ |
| 294 | static struct inode_security_struct *inode_security(struct inode *inode) |
| 295 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 296 | __inode_security_revalidate(inode, NULL, true); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 297 | return selinux_inode(inode); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 300 | static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) |
| 301 | { |
| 302 | struct inode *inode = d_backing_inode(dentry); |
| 303 | |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 304 | return selinux_inode(inode); |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 307 | /* |
| 308 | * Get the security label of a dentry's backing inode. |
| 309 | */ |
| 310 | static struct inode_security_struct *backing_inode_security(struct dentry *dentry) |
| 311 | { |
| 312 | struct inode *inode = d_backing_inode(dentry); |
| 313 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 314 | __inode_security_revalidate(inode, dentry, true); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 315 | return selinux_inode(inode); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 316 | } |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | static void inode_free_security(struct inode *inode) |
| 319 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 320 | struct inode_security_struct *isec = selinux_inode(inode); |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 321 | struct superblock_security_struct *sbsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 323 | if (!isec) |
| 324 | return; |
| 325 | sbsec = inode->i_sb->s_security; |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 326 | /* |
| 327 | * As not all inode security structures are in a list, we check for |
| 328 | * empty list outside of the lock to make sure that we won't waste |
| 329 | * time taking a lock doing nothing. |
| 330 | * |
| 331 | * The list_del_init() function can be safely called more than once. |
| 332 | * It should not be possible for this function to be called with |
| 333 | * concurrent list_add(), but for better safety against future changes |
| 334 | * in the code, we use list_empty_careful() here. |
| 335 | */ |
| 336 | if (!list_empty_careful(&isec->list)) { |
| 337 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | list_del_init(&isec->list); |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 339 | spin_unlock(&sbsec->isec_lock); |
| 340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | static void superblock_free_security(struct super_block *sb) |
| 344 | { |
| 345 | struct superblock_security_struct *sbsec = sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | sb->s_security = NULL; |
| 347 | kfree(sbsec); |
| 348 | } |
| 349 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 350 | struct selinux_mnt_opts { |
| 351 | const char *fscontext, *context, *rootcontext, *defcontext; |
| 352 | }; |
| 353 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 354 | static void selinux_free_mnt_opts(void *mnt_opts) |
| 355 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 356 | struct selinux_mnt_opts *opts = mnt_opts; |
| 357 | kfree(opts->fscontext); |
| 358 | kfree(opts->context); |
| 359 | kfree(opts->rootcontext); |
| 360 | kfree(opts->defcontext); |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 361 | kfree(opts); |
| 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | enum { |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 365 | Opt_error = -1, |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 366 | Opt_context = 0, |
| 367 | Opt_defcontext = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | Opt_fscontext = 2, |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 369 | Opt_rootcontext = 3, |
| 370 | Opt_seclabel = 4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 373 | #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] | 374 | static struct { |
| 375 | const char *name; |
| 376 | int len; |
| 377 | int opt; |
| 378 | bool has_arg; |
| 379 | } tokens[] = { |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 380 | A(context, true), |
| 381 | A(fscontext, true), |
| 382 | A(defcontext, true), |
| 383 | A(rootcontext, true), |
| 384 | A(seclabel, false), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | }; |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 386 | #undef A |
| 387 | |
| 388 | static int match_opt_prefix(char *s, int l, char **arg) |
| 389 | { |
| 390 | int i; |
| 391 | |
| 392 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 393 | size_t len = tokens[i].len; |
| 394 | if (len > l || memcmp(s, tokens[i].name, len)) |
| 395 | continue; |
| 396 | if (tokens[i].has_arg) { |
| 397 | if (len == l || s[len] != '=') |
| 398 | continue; |
| 399 | *arg = s + len + 1; |
| 400 | } else if (len != l) |
| 401 | continue; |
| 402 | return tokens[i].opt; |
| 403 | } |
| 404 | return Opt_error; |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" |
| 408 | |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 409 | static int may_context_mount_sb_relabel(u32 sid, |
| 410 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 411 | const struct cred *cred) |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 412 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 413 | const struct task_security_struct *tsec = selinux_cred(cred); |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 414 | int rc; |
| 415 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 416 | rc = avc_has_perm(&selinux_state, |
| 417 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 418 | FILESYSTEM__RELABELFROM, NULL); |
| 419 | if (rc) |
| 420 | return rc; |
| 421 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 422 | rc = avc_has_perm(&selinux_state, |
| 423 | tsec->sid, sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 424 | FILESYSTEM__RELABELTO, NULL); |
| 425 | return rc; |
| 426 | } |
| 427 | |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 428 | static int may_context_mount_inode_relabel(u32 sid, |
| 429 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 430 | const struct cred *cred) |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 431 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 432 | const struct task_security_struct *tsec = selinux_cred(cred); |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 433 | int rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 434 | rc = avc_has_perm(&selinux_state, |
| 435 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 436 | FILESYSTEM__RELABELFROM, NULL); |
| 437 | if (rc) |
| 438 | return rc; |
| 439 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 440 | rc = avc_has_perm(&selinux_state, |
| 441 | sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 442 | FILESYSTEM__ASSOCIATE, NULL); |
| 443 | return rc; |
| 444 | } |
| 445 | |
Ondrej Mosnacek | a83d6dd | 2018-12-21 21:18:52 +0100 | [diff] [blame] | 446 | static int selinux_is_genfs_special_handling(struct super_block *sb) |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 447 | { |
Ondrej Mosnacek | a83d6dd | 2018-12-21 21:18:52 +0100 | [diff] [blame] | 448 | /* Special handling. Genfs but also in-core setxattr handler */ |
| 449 | return !strcmp(sb->s_type->name, "sysfs") || |
Mark Salyzyn | d5f3a5f | 2015-02-04 11:34:30 -0500 | [diff] [blame] | 450 | !strcmp(sb->s_type->name, "pstore") || |
| 451 | !strcmp(sb->s_type->name, "debugfs") || |
Yongqin Liu | a2c7c6f | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 452 | !strcmp(sb->s_type->name, "tracefs") || |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 453 | !strcmp(sb->s_type->name, "rootfs") || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 454 | (selinux_policycap_cgroupseclabel() && |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 455 | (!strcmp(sb->s_type->name, "cgroup") || |
| 456 | !strcmp(sb->s_type->name, "cgroup2"))); |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Ondrej Mosnacek | a83d6dd | 2018-12-21 21:18:52 +0100 | [diff] [blame] | 459 | static int selinux_is_sblabel_mnt(struct super_block *sb) |
| 460 | { |
| 461 | struct superblock_security_struct *sbsec = sb->s_security; |
| 462 | |
| 463 | /* |
| 464 | * IMPORTANT: Double-check logic in this function when adding a new |
| 465 | * SECURITY_FS_USE_* definition! |
| 466 | */ |
| 467 | BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7); |
| 468 | |
| 469 | switch (sbsec->behavior) { |
| 470 | case SECURITY_FS_USE_XATTR: |
| 471 | case SECURITY_FS_USE_TRANS: |
| 472 | case SECURITY_FS_USE_TASK: |
| 473 | case SECURITY_FS_USE_NATIVE: |
| 474 | return 1; |
| 475 | |
| 476 | case SECURITY_FS_USE_GENFS: |
| 477 | return selinux_is_genfs_special_handling(sb); |
| 478 | |
| 479 | /* Never allow relabeling on context mounts */ |
| 480 | case SECURITY_FS_USE_MNTPOINT: |
| 481 | case SECURITY_FS_USE_NONE: |
| 482 | default: |
| 483 | return 0; |
| 484 | } |
| 485 | } |
| 486 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 487 | static int sb_finish_set_opts(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | struct superblock_security_struct *sbsec = sb->s_security; |
| 490 | struct dentry *root = sb->s_root; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 491 | struct inode *root_inode = d_backing_inode(root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | int rc = 0; |
| 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 495 | /* Make sure that the xattr handler exists and that no |
| 496 | error other than -ENODATA is returned by getxattr on |
| 497 | the root directory. -ENODATA is ok, as this may be |
| 498 | the first boot of the SELinux kernel before we have |
| 499 | assigned xattr values to the filesystem. */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 500 | if (!(root_inode->i_opflags & IOP_XATTR)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 501 | pr_warn("SELinux: (dev %s, type %s) has no " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 502 | "xattr support\n", sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | rc = -EOPNOTSUPP; |
| 504 | goto out; |
| 505 | } |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 506 | |
| 507 | rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | if (rc < 0 && rc != -ENODATA) { |
| 509 | if (rc == -EOPNOTSUPP) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 510 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 511 | "%s) has no security xattr handler\n", |
| 512 | sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 514 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 515 | "%s) getxattr errno %d\n", sb->s_id, |
| 516 | sb->s_type->name, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | goto out; |
| 518 | } |
| 519 | } |
| 520 | |
Eric Paris | eadcabc | 2012-08-24 15:59:14 -0400 | [diff] [blame] | 521 | sbsec->flags |= SE_SBINITIALIZED; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply |
| 525 | * leave the flag untouched because sb_clone_mnt_opts might be handing |
| 526 | * us a superblock that needs the flag to be cleared. |
| 527 | */ |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 528 | if (selinux_is_sblabel_mnt(sb)) |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 529 | sbsec->flags |= SBLABEL_MNT; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 530 | else |
| 531 | sbsec->flags &= ~SBLABEL_MNT; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | /* Initialize the root inode. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 534 | rc = inode_doinit_with_dentry(root_inode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | /* Initialize any other inodes associated with the superblock, e.g. |
| 537 | inodes created prior to initial policy load or inodes created |
| 538 | during get_sb by a pseudo filesystem that directly |
| 539 | populates itself. */ |
| 540 | spin_lock(&sbsec->isec_lock); |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 541 | while (!list_empty(&sbsec->isec_head)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | struct inode_security_struct *isec = |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 543 | list_first_entry(&sbsec->isec_head, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 544 | struct inode_security_struct, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | struct inode *inode = isec->inode; |
Stephen Smalley | 923190d | 2014-10-06 16:32:52 -0400 | [diff] [blame] | 546 | list_del_init(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | spin_unlock(&sbsec->isec_lock); |
| 548 | inode = igrab(inode); |
| 549 | if (inode) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 550 | if (!IS_PRIVATE(inode)) |
Paul Moore | cb89e24 | 2020-01-10 16:32:10 -0500 | [diff] [blame] | 551 | inode_doinit_with_dentry(inode, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | iput(inode); |
| 553 | } |
| 554 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | spin_unlock(&sbsec->isec_lock); |
| 557 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 558 | return rc; |
| 559 | } |
| 560 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 561 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
| 562 | u32 old_sid, u32 new_sid) |
| 563 | { |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 564 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
| 565 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 566 | /* check if the old mount command had the same options */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 567 | if (sbsec->flags & SE_SBINITIALIZED) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 568 | if (!(sbsec->flags & flag) || |
| 569 | (old_sid != new_sid)) |
| 570 | return 1; |
| 571 | |
| 572 | /* check if we were passed the same options twice, |
| 573 | * aka someone passed context=a,context=b |
| 574 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 575 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 576 | if (mnt_flags & flag) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 577 | return 1; |
| 578 | return 0; |
| 579 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 580 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 581 | static int parse_sid(struct super_block *sb, const char *s, u32 *sid) |
| 582 | { |
| 583 | int rc = security_context_str_to_sid(&selinux_state, s, |
| 584 | sid, GFP_KERNEL); |
| 585 | if (rc) |
| 586 | pr_warn("SELinux: security_context_str_to_sid" |
| 587 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 588 | s, sb->s_id, sb->s_type->name, rc); |
| 589 | return rc; |
| 590 | } |
| 591 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 592 | /* |
| 593 | * Allow filesystems with binary mount data to explicitly set mount point |
| 594 | * labeling information. |
| 595 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 596 | static int selinux_set_mnt_opts(struct super_block *sb, |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 597 | void *mnt_opts, |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 598 | unsigned long kern_flags, |
| 599 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 600 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 601 | const struct cred *cred = current_cred(); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 602 | struct superblock_security_struct *sbsec = sb->s_security; |
Ondrej Mosnacek | b159e86 | 2020-11-04 13:01:10 +0100 | [diff] [blame] | 603 | struct dentry *root = sb->s_root; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 604 | struct selinux_mnt_opts *opts = mnt_opts; |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 605 | struct inode_security_struct *root_isec; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 606 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
| 607 | u32 defcontext_sid = 0; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 608 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 609 | |
| 610 | mutex_lock(&sbsec->lock); |
| 611 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 612 | if (!selinux_initialized(&selinux_state)) { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 613 | if (!opts) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 614 | /* Defer initialization until selinux_complete_init, |
| 615 | after the initial policy is loaded and the security |
| 616 | server is ready to handle calls. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 617 | goto out; |
| 618 | } |
| 619 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 620 | pr_warn("SELinux: Unable to set superblock options " |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 621 | "before the security server is initialized\n"); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 622 | goto out; |
| 623 | } |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 624 | if (kern_flags && !set_kern_flags) { |
| 625 | /* Specifying internal flags without providing a place to |
| 626 | * place the results is not allowed */ |
| 627 | rc = -EINVAL; |
| 628 | goto out; |
| 629 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 630 | |
| 631 | /* |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 632 | * Binary mount data FS will come through this function twice. Once |
| 633 | * from an explicit call and once from the generic calls from the vfs. |
| 634 | * Since the generic VFS calls will not contain any security mount data |
| 635 | * we need to skip the double mount verification. |
| 636 | * |
| 637 | * This does open a hole in which we will not notice if the first |
| 638 | * mount using this sb set explict options and a second mount using |
| 639 | * this sb does not set any security options. (The first options |
| 640 | * will be used for both mounts) |
| 641 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 642 | 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] | 643 | && !opts) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 644 | goto out; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 645 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 646 | root_isec = backing_inode_security_novalidate(root); |
| 647 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 648 | /* |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 649 | * parse the mount options, check if they are valid sids. |
| 650 | * also check if someone is trying to mount the same sb more |
| 651 | * than once with different security options. |
| 652 | */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 653 | if (opts) { |
| 654 | if (opts->fscontext) { |
| 655 | rc = parse_sid(sb, opts->fscontext, &fscontext_sid); |
| 656 | if (rc) |
| 657 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 658 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, |
| 659 | fscontext_sid)) |
| 660 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 661 | sbsec->flags |= FSCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 662 | } |
| 663 | if (opts->context) { |
| 664 | rc = parse_sid(sb, opts->context, &context_sid); |
| 665 | if (rc) |
| 666 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 667 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, |
| 668 | context_sid)) |
| 669 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 670 | sbsec->flags |= CONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 671 | } |
| 672 | if (opts->rootcontext) { |
| 673 | rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); |
| 674 | if (rc) |
| 675 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 676 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, |
| 677 | rootcontext_sid)) |
| 678 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 679 | sbsec->flags |= ROOTCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 680 | } |
| 681 | if (opts->defcontext) { |
| 682 | rc = parse_sid(sb, opts->defcontext, &defcontext_sid); |
| 683 | if (rc) |
| 684 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 685 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, |
| 686 | defcontext_sid)) |
| 687 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 688 | sbsec->flags |= DEFCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 692 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 693 | /* previously mounted with options, but not on this attempt? */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 694 | if ((sbsec->flags & SE_MNTMASK) && !opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 695 | goto out_double_mount; |
| 696 | rc = 0; |
| 697 | goto out; |
| 698 | } |
| 699 | |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 700 | if (strcmp(sb->s_type->name, "proc") == 0) |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 701 | sbsec->flags |= SE_SBPROC | SE_SBGENFS; |
| 702 | |
Stephen Smalley | 8e01472 | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 703 | if (!strcmp(sb->s_type->name, "debugfs") || |
Jeff Vander Stoep | 6a39118 | 2017-06-20 09:35:33 -0700 | [diff] [blame] | 704 | !strcmp(sb->s_type->name, "tracefs") || |
Hridya Valsaraju | a20456a | 2020-02-01 17:46:23 -0800 | [diff] [blame] | 705 | !strcmp(sb->s_type->name, "binder") || |
Connor O'Brien | 4ca54d3 | 2020-02-07 10:01:49 -0800 | [diff] [blame] | 706 | !strcmp(sb->s_type->name, "bpf") || |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 707 | !strcmp(sb->s_type->name, "pstore")) |
| 708 | sbsec->flags |= SE_SBGENFS; |
| 709 | |
| 710 | if (!strcmp(sb->s_type->name, "sysfs") || |
Antonio Murdaca | 901ef84 | 2017-02-09 17:02:42 +0100 | [diff] [blame] | 711 | !strcmp(sb->s_type->name, "cgroup") || |
| 712 | !strcmp(sb->s_type->name, "cgroup2")) |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 713 | sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 714 | |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 715 | if (!sbsec->behavior) { |
| 716 | /* |
| 717 | * Determine the labeling behavior to use for this |
| 718 | * filesystem type. |
| 719 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 720 | rc = security_fs_use(&selinux_state, sb); |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 721 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 722 | pr_warn("%s: security_fs_use(%s) returned %d\n", |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 723 | __func__, sb->s_type->name, rc); |
| 724 | goto out; |
| 725 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 726 | } |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 727 | |
| 728 | /* |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 729 | * If this is a user namespace mount and the filesystem type is not |
| 730 | * explicitly whitelisted, then no contexts are allowed on the command |
| 731 | * line and security labels must be ignored. |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 732 | */ |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 733 | if (sb->s_user_ns != &init_user_ns && |
| 734 | strcmp(sb->s_type->name, "tmpfs") && |
| 735 | strcmp(sb->s_type->name, "ramfs") && |
| 736 | strcmp(sb->s_type->name, "devpts")) { |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 737 | if (context_sid || fscontext_sid || rootcontext_sid || |
| 738 | defcontext_sid) { |
| 739 | rc = -EACCES; |
| 740 | goto out; |
| 741 | } |
| 742 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 743 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 744 | rc = security_transition_sid(&selinux_state, |
| 745 | current_sid(), |
| 746 | current_sid(), |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 747 | SECCLASS_FILE, NULL, |
| 748 | &sbsec->mntpoint_sid); |
| 749 | if (rc) |
| 750 | goto out; |
| 751 | } |
| 752 | goto out_set_opts; |
| 753 | } |
| 754 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 755 | /* sets the context of the superblock for the fs being mounted. */ |
| 756 | if (fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 757 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 758 | if (rc) |
| 759 | goto out; |
| 760 | |
| 761 | sbsec->sid = fscontext_sid; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * Switch to using mount point labeling behavior. |
| 766 | * sets the label used on all file below the mountpoint, and will set |
| 767 | * the superblock context if not already set. |
| 768 | */ |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 769 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { |
| 770 | sbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 771 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 772 | } |
| 773 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 774 | if (context_sid) { |
| 775 | if (!fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 776 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
| 777 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 778 | if (rc) |
| 779 | goto out; |
| 780 | sbsec->sid = context_sid; |
| 781 | } else { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 782 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
| 783 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 784 | if (rc) |
| 785 | goto out; |
| 786 | } |
| 787 | if (!rootcontext_sid) |
| 788 | rootcontext_sid = context_sid; |
| 789 | |
| 790 | sbsec->mntpoint_sid = context_sid; |
| 791 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
| 792 | } |
| 793 | |
| 794 | if (rootcontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 795 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
| 796 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 797 | if (rc) |
| 798 | goto out; |
| 799 | |
| 800 | root_isec->sid = rootcontext_sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 801 | root_isec->initialized = LABEL_INITIALIZED; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | if (defcontext_sid) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 805 | if (sbsec->behavior != SECURITY_FS_USE_XATTR && |
| 806 | sbsec->behavior != SECURITY_FS_USE_NATIVE) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 807 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 808 | pr_warn("SELinux: defcontext option is " |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 809 | "invalid for this filesystem type\n"); |
| 810 | goto out; |
| 811 | } |
| 812 | |
| 813 | if (defcontext_sid != sbsec->def_sid) { |
| 814 | rc = may_context_mount_inode_relabel(defcontext_sid, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 815 | sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 816 | if (rc) |
| 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | sbsec->def_sid = defcontext_sid; |
| 821 | } |
| 822 | |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 823 | out_set_opts: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 824 | rc = sb_finish_set_opts(sb); |
| 825 | out: |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 826 | mutex_unlock(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 828 | out_double_mount: |
| 829 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 830 | pr_warn("SELinux: mount invalid. Same superblock, different " |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 831 | "security settings for (dev %s, type %s)\n", sb->s_id, |
| 832 | sb->s_type->name); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 833 | goto out; |
| 834 | } |
| 835 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 836 | static int selinux_cmp_sb_context(const struct super_block *oldsb, |
| 837 | const struct super_block *newsb) |
| 838 | { |
| 839 | struct superblock_security_struct *old = oldsb->s_security; |
| 840 | struct superblock_security_struct *new = newsb->s_security; |
| 841 | char oldflags = old->flags & SE_MNTMASK; |
| 842 | char newflags = new->flags & SE_MNTMASK; |
| 843 | |
| 844 | if (oldflags != newflags) |
| 845 | goto mismatch; |
| 846 | if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) |
| 847 | goto mismatch; |
| 848 | if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) |
| 849 | goto mismatch; |
| 850 | if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) |
| 851 | goto mismatch; |
| 852 | if (oldflags & ROOTCONTEXT_MNT) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 853 | struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); |
| 854 | struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 855 | if (oldroot->sid != newroot->sid) |
| 856 | goto mismatch; |
| 857 | } |
| 858 | return 0; |
| 859 | mismatch: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 860 | pr_warn("SELinux: mount invalid. Same superblock, " |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 861 | "different security settings for (dev %s, " |
| 862 | "type %s)\n", newsb->s_id, newsb->s_type->name); |
| 863 | return -EBUSY; |
| 864 | } |
| 865 | |
| 866 | static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 867 | struct super_block *newsb, |
| 868 | unsigned long kern_flags, |
| 869 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 870 | { |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 871 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 872 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
| 873 | struct superblock_security_struct *newsbsec = newsb->s_security; |
| 874 | |
| 875 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 876 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| 877 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); |
| 878 | |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 879 | /* |
| 880 | * 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] | 881 | * mount options. thus we can safely deal with this superblock later |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 882 | */ |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 883 | if (!selinux_initialized(&selinux_state)) |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 884 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 885 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 886 | /* |
| 887 | * Specifying internal flags without providing a place to |
| 888 | * place the results is not allowed. |
| 889 | */ |
| 890 | if (kern_flags && !set_kern_flags) |
| 891 | return -EINVAL; |
| 892 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 893 | /* 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] | 894 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 895 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 896 | /* 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] | 897 | if (newsbsec->flags & SE_SBINITIALIZED) { |
| 898 | if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) |
| 899 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 900 | return selinux_cmp_sb_context(oldsb, newsb); |
J. Bruce Fields | 3815a24 | 2019-03-05 16:17:58 -0500 | [diff] [blame] | 901 | } |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 902 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 903 | mutex_lock(&newsbsec->lock); |
| 904 | |
| 905 | newsbsec->flags = oldsbsec->flags; |
| 906 | |
| 907 | newsbsec->sid = oldsbsec->sid; |
| 908 | newsbsec->def_sid = oldsbsec->def_sid; |
| 909 | newsbsec->behavior = oldsbsec->behavior; |
| 910 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 911 | if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && |
| 912 | !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 913 | rc = security_fs_use(&selinux_state, newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 914 | if (rc) |
| 915 | goto out; |
| 916 | } |
| 917 | |
| 918 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { |
| 919 | newsbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 920 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 921 | } |
| 922 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 923 | if (set_context) { |
| 924 | u32 sid = oldsbsec->mntpoint_sid; |
| 925 | |
| 926 | if (!set_fscontext) |
| 927 | newsbsec->sid = sid; |
| 928 | if (!set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 929 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 930 | newisec->sid = sid; |
| 931 | } |
| 932 | newsbsec->mntpoint_sid = sid; |
| 933 | } |
| 934 | if (set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 935 | const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); |
| 936 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 937 | |
| 938 | newisec->sid = oldisec->sid; |
| 939 | } |
| 940 | |
| 941 | sb_finish_set_opts(newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 942 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 943 | mutex_unlock(&newsbsec->lock); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 944 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 945 | } |
| 946 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 947 | 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] | 948 | { |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 949 | struct selinux_mnt_opts *opts = *mnt_opts; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 950 | |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 951 | if (token == Opt_seclabel) /* eaten and completely ignored */ |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 952 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 953 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 954 | if (!opts) { |
| 955 | opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); |
| 956 | if (!opts) |
| 957 | return -ENOMEM; |
| 958 | *mnt_opts = opts; |
| 959 | } |
| 960 | if (!s) |
| 961 | return -ENOMEM; |
| 962 | switch (token) { |
| 963 | case Opt_context: |
| 964 | if (opts->context || opts->defcontext) |
| 965 | goto Einval; |
| 966 | opts->context = s; |
| 967 | break; |
| 968 | case Opt_fscontext: |
| 969 | if (opts->fscontext) |
| 970 | goto Einval; |
| 971 | opts->fscontext = s; |
| 972 | break; |
| 973 | case Opt_rootcontext: |
| 974 | if (opts->rootcontext) |
| 975 | goto Einval; |
| 976 | opts->rootcontext = s; |
| 977 | break; |
| 978 | case Opt_defcontext: |
| 979 | if (opts->context || opts->defcontext) |
| 980 | goto Einval; |
| 981 | opts->defcontext = s; |
| 982 | break; |
| 983 | } |
| 984 | return 0; |
| 985 | Einval: |
| 986 | pr_warn(SEL_MOUNT_FAIL_MSG); |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 987 | return -EINVAL; |
| 988 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 989 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 990 | static int selinux_add_mnt_opt(const char *option, const char *val, int len, |
| 991 | void **mnt_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 992 | { |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 993 | int token = Opt_error; |
| 994 | int rc, i; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 995 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 996 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 997 | if (strcmp(option, tokens[i].name) == 0) { |
| 998 | token = tokens[i].opt; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 999 | break; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1003 | if (token == Opt_error) |
| 1004 | return -EINVAL; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 1005 | |
Gen Zhang | e2e0e09 | 2019-06-12 21:28:21 +0800 | [diff] [blame] | 1006 | if (token != Opt_seclabel) { |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1007 | val = kmemdup_nul(val, len, GFP_KERNEL); |
Gen Zhang | e2e0e09 | 2019-06-12 21:28:21 +0800 | [diff] [blame] | 1008 | if (!val) { |
| 1009 | rc = -ENOMEM; |
| 1010 | goto free_opt; |
| 1011 | } |
| 1012 | } |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1013 | rc = selinux_add_opt(token, val, mnt_opts); |
| 1014 | if (unlikely(rc)) { |
| 1015 | kfree(val); |
Gen Zhang | e2e0e09 | 2019-06-12 21:28:21 +0800 | [diff] [blame] | 1016 | goto free_opt; |
| 1017 | } |
| 1018 | return rc; |
| 1019 | |
| 1020 | free_opt: |
| 1021 | if (*mnt_opts) { |
| 1022 | selinux_free_mnt_opts(*mnt_opts); |
| 1023 | *mnt_opts = NULL; |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1024 | } |
| 1025 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1028 | static int show_sid(struct seq_file *m, u32 sid) |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1029 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1030 | char *context = NULL; |
| 1031 | u32 len; |
| 1032 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1034 | rc = security_sid_to_context(&selinux_state, sid, |
| 1035 | &context, &len); |
| 1036 | if (!rc) { |
| 1037 | bool has_comma = context && strchr(context, ','); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 1039 | seq_putc(m, '='); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1040 | if (has_comma) |
| 1041 | seq_putc(m, '\"'); |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1042 | seq_escape(m, context, "\"\n\\"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1043 | if (has_comma) |
| 1044 | seq_putc(m, '\"'); |
| 1045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | kfree(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | return rc; |
| 1048 | } |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1049 | |
| 1050 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1051 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1052 | struct superblock_security_struct *sbsec = sb->s_security; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1053 | int rc; |
| 1054 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1055 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 1056 | return 0; |
| 1057 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 1058 | if (!selinux_initialized(&selinux_state)) |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1059 | return 0; |
| 1060 | |
| 1061 | if (sbsec->flags & FSCONTEXT_MNT) { |
| 1062 | seq_putc(m, ','); |
| 1063 | seq_puts(m, FSCONTEXT_STR); |
| 1064 | rc = show_sid(m, sbsec->sid); |
| 1065 | if (rc) |
| 1066 | return rc; |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1067 | } |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1068 | if (sbsec->flags & CONTEXT_MNT) { |
| 1069 | seq_putc(m, ','); |
| 1070 | seq_puts(m, CONTEXT_STR); |
| 1071 | rc = show_sid(m, sbsec->mntpoint_sid); |
| 1072 | if (rc) |
| 1073 | return rc; |
| 1074 | } |
| 1075 | if (sbsec->flags & DEFCONTEXT_MNT) { |
| 1076 | seq_putc(m, ','); |
| 1077 | seq_puts(m, DEFCONTEXT_STR); |
| 1078 | rc = show_sid(m, sbsec->def_sid); |
| 1079 | if (rc) |
| 1080 | return rc; |
| 1081 | } |
| 1082 | if (sbsec->flags & ROOTCONTEXT_MNT) { |
Ondrej Mosnacek | b159e86 | 2020-11-04 13:01:10 +0100 | [diff] [blame] | 1083 | struct dentry *root = sb->s_root; |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1084 | struct inode_security_struct *isec = backing_inode_security(root); |
| 1085 | seq_putc(m, ','); |
| 1086 | seq_puts(m, ROOTCONTEXT_STR); |
| 1087 | rc = show_sid(m, isec->sid); |
| 1088 | if (rc) |
| 1089 | return rc; |
| 1090 | } |
| 1091 | if (sbsec->flags & SBLABEL_MNT) { |
| 1092 | seq_putc(m, ','); |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 1093 | seq_puts(m, SECLABEL_STR); |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1094 | } |
| 1095 | return 0; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1096 | } |
| 1097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | static inline u16 inode_mode_to_security_class(umode_t mode) |
| 1099 | { |
| 1100 | switch (mode & S_IFMT) { |
| 1101 | case S_IFSOCK: |
| 1102 | return SECCLASS_SOCK_FILE; |
| 1103 | case S_IFLNK: |
| 1104 | return SECCLASS_LNK_FILE; |
| 1105 | case S_IFREG: |
| 1106 | return SECCLASS_FILE; |
| 1107 | case S_IFBLK: |
| 1108 | return SECCLASS_BLK_FILE; |
| 1109 | case S_IFDIR: |
| 1110 | return SECCLASS_DIR; |
| 1111 | case S_IFCHR: |
| 1112 | return SECCLASS_CHR_FILE; |
| 1113 | case S_IFIFO: |
| 1114 | return SECCLASS_FIFO_FILE; |
| 1115 | |
| 1116 | } |
| 1117 | |
| 1118 | return SECCLASS_FILE; |
| 1119 | } |
| 1120 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1121 | static inline int default_protocol_stream(int protocol) |
| 1122 | { |
Paolo Abeni | 95ca907 | 2020-12-16 12:55:27 +0100 | [diff] [blame^] | 1123 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP || |
| 1124 | protocol == IPPROTO_MPTCP); |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static inline int default_protocol_dgram(int protocol) |
| 1128 | { |
| 1129 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); |
| 1130 | } |
| 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
| 1133 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1134 | int extsockclass = selinux_policycap_extsockclass(); |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | switch (family) { |
| 1137 | case PF_UNIX: |
| 1138 | switch (type) { |
| 1139 | case SOCK_STREAM: |
| 1140 | case SOCK_SEQPACKET: |
| 1141 | return SECCLASS_UNIX_STREAM_SOCKET; |
| 1142 | case SOCK_DGRAM: |
Luis Ressel | 2a764b5 | 2017-07-25 15:13:41 -0400 | [diff] [blame] | 1143 | case SOCK_RAW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | return SECCLASS_UNIX_DGRAM_SOCKET; |
| 1145 | } |
| 1146 | break; |
| 1147 | case PF_INET: |
| 1148 | case PF_INET6: |
| 1149 | switch (type) { |
| 1150 | case SOCK_STREAM: |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1151 | case SOCK_SEQPACKET: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1152 | if (default_protocol_stream(protocol)) |
| 1153 | return SECCLASS_TCP_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1154 | else if (extsockclass && protocol == IPPROTO_SCTP) |
| 1155 | return SECCLASS_SCTP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1156 | else |
| 1157 | return SECCLASS_RAWIP_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | case SOCK_DGRAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1159 | if (default_protocol_dgram(protocol)) |
| 1160 | return SECCLASS_UDP_SOCKET; |
Stephen Smalley | ef37979 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 1161 | else if (extsockclass && (protocol == IPPROTO_ICMP || |
| 1162 | protocol == IPPROTO_ICMPV6)) |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1163 | return SECCLASS_ICMP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1164 | else |
| 1165 | return SECCLASS_RAWIP_SOCKET; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 1166 | case SOCK_DCCP: |
| 1167 | return SECCLASS_DCCP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1168 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | return SECCLASS_RAWIP_SOCKET; |
| 1170 | } |
| 1171 | break; |
| 1172 | case PF_NETLINK: |
| 1173 | switch (protocol) { |
| 1174 | case NETLINK_ROUTE: |
| 1175 | return SECCLASS_NETLINK_ROUTE_SOCKET; |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1176 | case NETLINK_SOCK_DIAG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
| 1178 | case NETLINK_NFLOG: |
| 1179 | return SECCLASS_NETLINK_NFLOG_SOCKET; |
| 1180 | case NETLINK_XFRM: |
| 1181 | return SECCLASS_NETLINK_XFRM_SOCKET; |
| 1182 | case NETLINK_SELINUX: |
| 1183 | return SECCLASS_NETLINK_SELINUX_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1184 | case NETLINK_ISCSI: |
| 1185 | return SECCLASS_NETLINK_ISCSI_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | case NETLINK_AUDIT: |
| 1187 | return SECCLASS_NETLINK_AUDIT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1188 | case NETLINK_FIB_LOOKUP: |
| 1189 | return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; |
| 1190 | case NETLINK_CONNECTOR: |
| 1191 | return SECCLASS_NETLINK_CONNECTOR_SOCKET; |
| 1192 | case NETLINK_NETFILTER: |
| 1193 | return SECCLASS_NETLINK_NETFILTER_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | case NETLINK_DNRTMSG: |
| 1195 | return SECCLASS_NETLINK_DNRT_SOCKET; |
James Morris | 0c9b794 | 2005-04-16 15:24:13 -0700 | [diff] [blame] | 1196 | case NETLINK_KOBJECT_UEVENT: |
| 1197 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1198 | case NETLINK_GENERIC: |
| 1199 | return SECCLASS_NETLINK_GENERIC_SOCKET; |
| 1200 | case NETLINK_SCSITRANSPORT: |
| 1201 | return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; |
| 1202 | case NETLINK_RDMA: |
| 1203 | return SECCLASS_NETLINK_RDMA_SOCKET; |
| 1204 | case NETLINK_CRYPTO: |
| 1205 | return SECCLASS_NETLINK_CRYPTO_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | default: |
| 1207 | return SECCLASS_NETLINK_SOCKET; |
| 1208 | } |
| 1209 | case PF_PACKET: |
| 1210 | return SECCLASS_PACKET_SOCKET; |
| 1211 | case PF_KEY: |
| 1212 | return SECCLASS_KEY_SOCKET; |
Christopher J. PeBenito | 3e3ff15 | 2006-06-09 00:25:03 -0700 | [diff] [blame] | 1213 | case PF_APPLETALK: |
| 1214 | return SECCLASS_APPLETALK_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1217 | if (extsockclass) { |
| 1218 | switch (family) { |
| 1219 | case PF_AX25: |
| 1220 | return SECCLASS_AX25_SOCKET; |
| 1221 | case PF_IPX: |
| 1222 | return SECCLASS_IPX_SOCKET; |
| 1223 | case PF_NETROM: |
| 1224 | return SECCLASS_NETROM_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1225 | case PF_ATMPVC: |
| 1226 | return SECCLASS_ATMPVC_SOCKET; |
| 1227 | case PF_X25: |
| 1228 | return SECCLASS_X25_SOCKET; |
| 1229 | case PF_ROSE: |
| 1230 | return SECCLASS_ROSE_SOCKET; |
| 1231 | case PF_DECnet: |
| 1232 | return SECCLASS_DECNET_SOCKET; |
| 1233 | case PF_ATMSVC: |
| 1234 | return SECCLASS_ATMSVC_SOCKET; |
| 1235 | case PF_RDS: |
| 1236 | return SECCLASS_RDS_SOCKET; |
| 1237 | case PF_IRDA: |
| 1238 | return SECCLASS_IRDA_SOCKET; |
| 1239 | case PF_PPPOX: |
| 1240 | return SECCLASS_PPPOX_SOCKET; |
| 1241 | case PF_LLC: |
| 1242 | return SECCLASS_LLC_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1243 | case PF_CAN: |
| 1244 | return SECCLASS_CAN_SOCKET; |
| 1245 | case PF_TIPC: |
| 1246 | return SECCLASS_TIPC_SOCKET; |
| 1247 | case PF_BLUETOOTH: |
| 1248 | return SECCLASS_BLUETOOTH_SOCKET; |
| 1249 | case PF_IUCV: |
| 1250 | return SECCLASS_IUCV_SOCKET; |
| 1251 | case PF_RXRPC: |
| 1252 | return SECCLASS_RXRPC_SOCKET; |
| 1253 | case PF_ISDN: |
| 1254 | return SECCLASS_ISDN_SOCKET; |
| 1255 | case PF_PHONET: |
| 1256 | return SECCLASS_PHONET_SOCKET; |
| 1257 | case PF_IEEE802154: |
| 1258 | return SECCLASS_IEEE802154_SOCKET; |
| 1259 | case PF_CAIF: |
| 1260 | return SECCLASS_CAIF_SOCKET; |
| 1261 | case PF_ALG: |
| 1262 | return SECCLASS_ALG_SOCKET; |
| 1263 | case PF_NFC: |
| 1264 | return SECCLASS_NFC_SOCKET; |
| 1265 | case PF_VSOCK: |
| 1266 | return SECCLASS_VSOCK_SOCKET; |
| 1267 | case PF_KCM: |
| 1268 | return SECCLASS_KCM_SOCKET; |
| 1269 | case PF_QIPCRTR: |
| 1270 | return SECCLASS_QIPCRTR_SOCKET; |
Linus Torvalds | 3051bf3 | 2017-02-22 10:15:09 -0800 | [diff] [blame] | 1271 | case PF_SMC: |
| 1272 | return SECCLASS_SMC_SOCKET; |
Björn Töpel | 68e8b84 | 2018-05-02 13:01:22 +0200 | [diff] [blame] | 1273 | case PF_XDP: |
| 1274 | return SECCLASS_XDP_SOCKET; |
| 1275 | #if PF_MAX > 45 |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1276 | #error New address family defined, please update this function. |
| 1277 | #endif |
| 1278 | } |
| 1279 | } |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | return SECCLASS_SOCKET; |
| 1282 | } |
| 1283 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1284 | static int selinux_genfs_get_sid(struct dentry *dentry, |
| 1285 | u16 tclass, |
| 1286 | u16 flags, |
| 1287 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1289 | int rc; |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 1290 | struct super_block *sb = dentry->d_sb; |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1291 | char *buffer, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1293 | buffer = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | if (!buffer) |
| 1295 | return -ENOMEM; |
| 1296 | |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1297 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
| 1298 | if (IS_ERR(path)) |
| 1299 | rc = PTR_ERR(path); |
| 1300 | else { |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1301 | if (flags & SE_SBPROC) { |
| 1302 | /* each process gets a /proc/PID/ entry. Strip off the |
| 1303 | * PID part to get a valid selinux labeling. |
| 1304 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ |
| 1305 | while (path[1] >= '0' && path[1] <= '9') { |
| 1306 | path[1] = '/'; |
| 1307 | path++; |
| 1308 | } |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1309 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1310 | rc = security_genfs_sid(&selinux_state, sb->s_type->name, |
| 1311 | path, tclass, sid); |
Stephen Smalley | 7bb185e | 2018-09-04 16:51:36 -0400 | [diff] [blame] | 1312 | if (rc == -ENOENT) { |
| 1313 | /* No match in policy, mark as unlabeled. */ |
| 1314 | *sid = SECINITSID_UNLABELED; |
| 1315 | rc = 0; |
| 1316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | free_page((unsigned long)buffer); |
| 1319 | return rc; |
| 1320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1322 | static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry, |
| 1323 | u32 def_sid, u32 *sid) |
| 1324 | { |
| 1325 | #define INITCONTEXTLEN 255 |
| 1326 | char *context; |
| 1327 | unsigned int len; |
| 1328 | int rc; |
| 1329 | |
| 1330 | len = INITCONTEXTLEN; |
| 1331 | context = kmalloc(len + 1, GFP_NOFS); |
| 1332 | if (!context) |
| 1333 | return -ENOMEM; |
| 1334 | |
| 1335 | context[len] = '\0'; |
| 1336 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); |
| 1337 | if (rc == -ERANGE) { |
| 1338 | kfree(context); |
| 1339 | |
| 1340 | /* Need a larger buffer. Query for the right size. */ |
| 1341 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); |
| 1342 | if (rc < 0) |
| 1343 | return rc; |
| 1344 | |
| 1345 | len = rc; |
| 1346 | context = kmalloc(len + 1, GFP_NOFS); |
| 1347 | if (!context) |
| 1348 | return -ENOMEM; |
| 1349 | |
| 1350 | context[len] = '\0'; |
| 1351 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, |
| 1352 | context, len); |
| 1353 | } |
| 1354 | if (rc < 0) { |
| 1355 | kfree(context); |
| 1356 | if (rc != -ENODATA) { |
| 1357 | pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n", |
| 1358 | __func__, -rc, inode->i_sb->s_id, inode->i_ino); |
| 1359 | return rc; |
| 1360 | } |
| 1361 | *sid = def_sid; |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | rc = security_context_to_sid_default(&selinux_state, context, rc, sid, |
| 1366 | def_sid, GFP_NOFS); |
| 1367 | if (rc) { |
| 1368 | char *dev = inode->i_sb->s_id; |
| 1369 | unsigned long ino = inode->i_ino; |
| 1370 | |
| 1371 | if (rc == -EINVAL) { |
| 1372 | 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", |
| 1373 | ino, dev, context); |
| 1374 | } else { |
| 1375 | pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n", |
| 1376 | __func__, context, -rc, dev, ino); |
| 1377 | } |
| 1378 | } |
| 1379 | kfree(context); |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | /* The inode's security attributes must be initialized before first use. */ |
| 1384 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1385 | { |
| 1386 | struct superblock_security_struct *sbsec = NULL; |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 1387 | struct inode_security_struct *isec = selinux_inode(inode); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1388 | u32 task_sid, sid = 0; |
| 1389 | u16 sclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1393 | if (isec->initialized == LABEL_INITIALIZED) |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1394 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1396 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1397 | if (isec->initialized == LABEL_INITIALIZED) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1398 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1400 | if (isec->sclass == SECCLASS_FILE) |
| 1401 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 1402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | sbsec = inode->i_sb->s_security; |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1404 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | /* Defer initialization until selinux_complete_init, |
| 1406 | after the initial policy is loaded and the security |
| 1407 | server is ready to handle calls. */ |
| 1408 | spin_lock(&sbsec->isec_lock); |
| 1409 | if (list_empty(&isec->list)) |
| 1410 | list_add(&isec->list, &sbsec->isec_head); |
| 1411 | spin_unlock(&sbsec->isec_lock); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1412 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1415 | sclass = isec->sclass; |
| 1416 | task_sid = isec->task_sid; |
| 1417 | sid = isec->sid; |
| 1418 | isec->initialized = LABEL_PENDING; |
| 1419 | spin_unlock(&isec->lock); |
| 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | switch (sbsec->behavior) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 1422 | case SECURITY_FS_USE_NATIVE: |
| 1423 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | case SECURITY_FS_USE_XATTR: |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1425 | if (!(inode->i_opflags & IOP_XATTR)) { |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1426 | sid = sbsec->def_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | break; |
| 1428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | /* Need a dentry, since the xattr API requires one. |
| 1430 | Life would be simpler if we could just pass the inode. */ |
| 1431 | if (opt_dentry) { |
| 1432 | /* Called from d_instantiate or d_splice_alias. */ |
| 1433 | dentry = dget(opt_dentry); |
| 1434 | } else { |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1435 | /* |
| 1436 | * Called from selinux_complete_init, try to find a dentry. |
| 1437 | * Some filesystems really want a connected one, so try |
| 1438 | * that first. We could split SECURITY_FS_USE_XATTR in |
| 1439 | * two, depending upon that... |
| 1440 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1442 | if (!dentry) |
| 1443 | dentry = d_find_any_alias(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | if (!dentry) { |
Eric Paris | df7f54c | 2009-03-09 14:35:58 -0400 | [diff] [blame] | 1446 | /* |
| 1447 | * this is can be hit on boot when a file is accessed |
| 1448 | * before the policy is loaded. When we load policy we |
| 1449 | * may find inodes that have no dentry on the |
| 1450 | * sbsec->isec_head list. No reason to complain as these |
| 1451 | * will get fixed up the next time we go through |
| 1452 | * inode_doinit with a dentry, before these inodes could |
| 1453 | * be used again by userspace. |
| 1454 | */ |
Paul Moore | 200ea5a | 2020-11-03 11:49:38 -0500 | [diff] [blame] | 1455 | goto out_invalid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1458 | rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid, |
| 1459 | &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | dput(dentry); |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1461 | if (rc) |
| 1462 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | case SECURITY_FS_USE_TASK: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1465 | sid = task_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | break; |
| 1467 | case SECURITY_FS_USE_TRANS: |
| 1468 | /* Default to the fs SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1469 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* Try to obtain a transition SID. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1472 | rc = security_transition_sid(&selinux_state, task_sid, sid, |
| 1473 | sclass, NULL, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | if (rc) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1475 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | break; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1477 | case SECURITY_FS_USE_MNTPOINT: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1478 | sid = sbsec->mntpoint_sid; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1479 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | default: |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1481 | /* Default to the fs superblock SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1482 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Christian Göttsche | 7470d0d1 | 2020-01-28 20:16:48 +0100 | [diff] [blame] | 1484 | if ((sbsec->flags & SE_SBGENFS) && |
| 1485 | (!S_ISLNK(inode->i_mode) || |
| 1486 | selinux_policycap_genfs_seclabel_symlinks())) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1487 | /* We must have a dentry to determine the label on |
| 1488 | * procfs inodes */ |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1489 | if (opt_dentry) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1490 | /* Called from d_instantiate or |
| 1491 | * d_splice_alias. */ |
| 1492 | dentry = dget(opt_dentry); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1493 | } else { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1494 | /* Called from selinux_complete_init, try to |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1495 | * find a dentry. Some filesystems really want |
| 1496 | * a connected one, so try that first. |
| 1497 | */ |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1498 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1499 | if (!dentry) |
| 1500 | dentry = d_find_any_alias(inode); |
| 1501 | } |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1502 | /* |
| 1503 | * This can be hit on boot when a file is accessed |
| 1504 | * before the policy is loaded. When we load policy we |
| 1505 | * may find inodes that have no dentry on the |
| 1506 | * sbsec->isec_head list. No reason to complain as |
| 1507 | * these will get fixed up the next time we go through |
| 1508 | * inode_doinit() with a dentry, before these inodes |
| 1509 | * could be used again by userspace. |
| 1510 | */ |
| 1511 | if (!dentry) |
Paul Moore | 200ea5a | 2020-11-03 11:49:38 -0500 | [diff] [blame] | 1512 | goto out_invalid; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1513 | rc = selinux_genfs_get_sid(dentry, sclass, |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1514 | sbsec->flags, &sid); |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1515 | if (rc) { |
| 1516 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1517 | goto out; |
Ondrej Mosnacek | b754026 | 2019-02-22 15:57:14 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | if ((sbsec->flags & SE_SBGENFS_XATTR) && |
| 1521 | (inode->i_opflags & IOP_XATTR)) { |
| 1522 | rc = inode_doinit_use_xattr(inode, dentry, |
| 1523 | sid, &sid); |
| 1524 | if (rc) { |
| 1525 | dput(dentry); |
| 1526 | goto out; |
| 1527 | } |
| 1528 | } |
| 1529 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | } |
| 1531 | break; |
| 1532 | } |
| 1533 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1534 | out: |
| 1535 | spin_lock(&isec->lock); |
| 1536 | if (isec->initialized == LABEL_PENDING) { |
Paul Moore | 200ea5a | 2020-11-03 11:49:38 -0500 | [diff] [blame] | 1537 | if (rc) { |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1538 | isec->initialized = LABEL_INVALID; |
| 1539 | goto out_unlock; |
| 1540 | } |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1541 | isec->initialized = LABEL_INITIALIZED; |
| 1542 | isec->sid = sid; |
| 1543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1545 | out_unlock: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1546 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | return rc; |
Paul Moore | 200ea5a | 2020-11-03 11:49:38 -0500 | [diff] [blame] | 1548 | |
| 1549 | out_invalid: |
| 1550 | spin_lock(&isec->lock); |
| 1551 | if (isec->initialized == LABEL_PENDING) { |
| 1552 | isec->initialized = LABEL_INVALID; |
| 1553 | isec->sid = sid; |
| 1554 | } |
| 1555 | spin_unlock(&isec->lock); |
| 1556 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* Convert a Linux signal to an access vector. */ |
| 1560 | static inline u32 signal_to_av(int sig) |
| 1561 | { |
| 1562 | u32 perm = 0; |
| 1563 | |
| 1564 | switch (sig) { |
| 1565 | case SIGCHLD: |
| 1566 | /* Commonly granted from child to parent. */ |
| 1567 | perm = PROCESS__SIGCHLD; |
| 1568 | break; |
| 1569 | case SIGKILL: |
| 1570 | /* Cannot be caught or ignored */ |
| 1571 | perm = PROCESS__SIGKILL; |
| 1572 | break; |
| 1573 | case SIGSTOP: |
| 1574 | /* Cannot be caught or ignored */ |
| 1575 | perm = PROCESS__SIGSTOP; |
| 1576 | break; |
| 1577 | default: |
| 1578 | /* All other signals. */ |
| 1579 | perm = PROCESS__SIGNAL; |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | return perm; |
| 1584 | } |
| 1585 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1586 | #if CAP_LAST_CAP > 63 |
| 1587 | #error Fix SELinux to handle capabilities > 63. |
| 1588 | #endif |
| 1589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | /* Check whether a task is allowed to use a capability. */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1591 | static int cred_has_capability(const struct cred *cred, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 1592 | int cap, unsigned int opts, bool initns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1594 | struct common_audit_data ad; |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1595 | struct av_decision avd; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1596 | u16 sclass; |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 1597 | u32 sid = cred_sid(cred); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1598 | u32 av = CAP_TO_MASK(cap); |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1599 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1601 | ad.type = LSM_AUDIT_DATA_CAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | ad.u.cap = cap; |
| 1603 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1604 | switch (CAP_TO_INDEX(cap)) { |
| 1605 | case 0: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1606 | sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1607 | break; |
| 1608 | case 1: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1609 | sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1610 | break; |
| 1611 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1612 | pr_err("SELinux: out of range capability %d\n", cap); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1613 | BUG(); |
Eric Paris | a35c6c83 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1614 | return -EINVAL; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1615 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1616 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1617 | rc = avc_has_perm_noaudit(&selinux_state, |
| 1618 | sid, sid, sclass, av, 0, &avd); |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 1619 | if (!(opts & CAP_OPT_NOAUDIT)) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1620 | int rc2 = avc_audit(&selinux_state, |
| 1621 | sid, sid, sclass, av, &avd, rc, &ad, 0); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1622 | if (rc2) |
| 1623 | return rc2; |
| 1624 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1625 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | /* Check whether a task has a particular permission to an inode. |
| 1629 | The 'adp' parameter is optional and allows other audit |
| 1630 | data to be passed (e.g. the dentry). */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1631 | static int inode_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | struct inode *inode, |
| 1633 | u32 perms, |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1634 | struct common_audit_data *adp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1637 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 1639 | validate_creds(cred); |
| 1640 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1641 | if (unlikely(IS_PRIVATE(inode))) |
Stephen Smalley | bbaca6c | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 1642 | return 0; |
| 1643 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1644 | sid = cred_sid(cred); |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 1645 | isec = selinux_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1647 | return avc_has_perm(&selinux_state, |
| 1648 | sid, isec->sid, isec->sclass, perms, adp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1652 | the dentry to help the auditing code to more easily generate the |
| 1653 | pathname if needed. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1654 | static inline int dentry_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | struct dentry *dentry, |
| 1656 | u32 av) |
| 1657 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1658 | struct inode *inode = d_backing_inode(dentry); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1659 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1660 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1661 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1662 | ad.u.dentry = dentry; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1663 | __inode_security_revalidate(inode, dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1664 | return inode_has_perm(cred, inode, av, &ad); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1668 | the path to help the auditing code to more easily generate the |
| 1669 | pathname if needed. */ |
| 1670 | static inline int path_has_perm(const struct cred *cred, |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1671 | const struct path *path, |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1672 | u32 av) |
| 1673 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1674 | struct inode *inode = d_backing_inode(path->dentry); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1675 | struct common_audit_data ad; |
| 1676 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1677 | ad.type = LSM_AUDIT_DATA_PATH; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1678 | ad.u.path = *path; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1679 | __inode_security_revalidate(inode, path->dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1680 | return inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1683 | /* Same as path_has_perm, but uses the inode from the file struct. */ |
| 1684 | static inline int file_path_has_perm(const struct cred *cred, |
| 1685 | struct file *file, |
| 1686 | u32 av) |
| 1687 | { |
| 1688 | struct common_audit_data ad; |
| 1689 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1690 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1691 | ad.u.file = file; |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1692 | return inode_has_perm(cred, file_inode(file), av, &ad); |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1695 | #ifdef CONFIG_BPF_SYSCALL |
| 1696 | static int bpf_fd_pass(struct file *file, u32 sid); |
| 1697 | #endif |
| 1698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | /* Check whether a task can use an open file descriptor to |
| 1700 | access an inode in a given way. Check access to the |
| 1701 | descriptor itself, and then use dentry_has_perm to |
| 1702 | check a particular permission to the file. |
| 1703 | Access to the descriptor is implicitly granted if it |
| 1704 | has the same SID as the process. If av is zero, then |
| 1705 | access to the file is not checked, e.g. for cases |
| 1706 | where only the descriptor is affected like seek. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1707 | static int file_has_perm(const struct cred *cred, |
| 1708 | struct file *file, |
| 1709 | u32 av) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | { |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 1711 | struct file_security_struct *fsec = selinux_file(file); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1712 | struct inode *inode = file_inode(file); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1713 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1714 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | int rc; |
| 1716 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1717 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1718 | ad.u.file = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1720 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1721 | rc = avc_has_perm(&selinux_state, |
| 1722 | sid, fsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | SECCLASS_FD, |
| 1724 | FD__USE, |
| 1725 | &ad); |
| 1726 | if (rc) |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1727 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1730 | #ifdef CONFIG_BPF_SYSCALL |
| 1731 | rc = bpf_fd_pass(file, cred_sid(cred)); |
| 1732 | if (rc) |
| 1733 | return rc; |
| 1734 | #endif |
| 1735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | /* av is zero if only checking access to the descriptor. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1737 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | if (av) |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1739 | rc = inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1741 | out: |
| 1742 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1745 | /* |
| 1746 | * Determine the label for an inode that might be unioned. |
| 1747 | */ |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1748 | static int |
| 1749 | selinux_determine_inode_label(const struct task_security_struct *tsec, |
| 1750 | struct inode *dir, |
| 1751 | const struct qstr *name, u16 tclass, |
| 1752 | u32 *_new_isid) |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1753 | { |
| 1754 | const struct superblock_security_struct *sbsec = dir->i_sb->s_security; |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1755 | |
| 1756 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 1757 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { |
| 1758 | *_new_isid = sbsec->mntpoint_sid; |
| 1759 | } else if ((sbsec->flags & SBLABEL_MNT) && |
| 1760 | tsec->create_sid) { |
| 1761 | *_new_isid = tsec->create_sid; |
| 1762 | } else { |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 1763 | const struct inode_security_struct *dsec = inode_security(dir); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1764 | return security_transition_sid(&selinux_state, tsec->sid, |
| 1765 | dsec->sid, tclass, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1766 | name, _new_isid); |
| 1767 | } |
| 1768 | |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | /* Check whether a task can create a file. */ |
| 1773 | static int may_create(struct inode *dir, |
| 1774 | struct dentry *dentry, |
| 1775 | u16 tclass) |
| 1776 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 1777 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | struct inode_security_struct *dsec; |
| 1779 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1780 | u32 sid, newsid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1781 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | int rc; |
| 1783 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1784 | dsec = inode_security(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | sbsec = dir->i_sb->s_security; |
| 1786 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1787 | sid = tsec->sid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1788 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1789 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1790 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1792 | rc = avc_has_perm(&selinux_state, |
| 1793 | sid, dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | DIR__ADD_NAME | DIR__SEARCH, |
| 1795 | &ad); |
| 1796 | if (rc) |
| 1797 | return rc; |
| 1798 | |
Yang Guo | 210a292 | 2019-12-12 10:02:24 +0800 | [diff] [blame] | 1799 | rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass, |
| 1800 | &newsid); |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1801 | if (rc) |
| 1802 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1804 | rc = avc_has_perm(&selinux_state, |
| 1805 | sid, newsid, tclass, FILE__CREATE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | if (rc) |
| 1807 | return rc; |
| 1808 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1809 | return avc_has_perm(&selinux_state, |
| 1810 | newsid, sbsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | SECCLASS_FILESYSTEM, |
| 1812 | FILESYSTEM__ASSOCIATE, &ad); |
| 1813 | } |
| 1814 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1815 | #define MAY_LINK 0 |
| 1816 | #define MAY_UNLINK 1 |
| 1817 | #define MAY_RMDIR 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | |
| 1819 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ |
| 1820 | static int may_link(struct inode *dir, |
| 1821 | struct dentry *dentry, |
| 1822 | int kind) |
| 1823 | |
| 1824 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | struct inode_security_struct *dsec, *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1826 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1827 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | u32 av; |
| 1829 | int rc; |
| 1830 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1831 | dsec = inode_security(dir); |
| 1832 | isec = backing_inode_security(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1834 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1835 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | av = DIR__SEARCH; |
| 1838 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1839 | rc = avc_has_perm(&selinux_state, |
| 1840 | sid, dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | if (rc) |
| 1842 | return rc; |
| 1843 | |
| 1844 | switch (kind) { |
| 1845 | case MAY_LINK: |
| 1846 | av = FILE__LINK; |
| 1847 | break; |
| 1848 | case MAY_UNLINK: |
| 1849 | av = FILE__UNLINK; |
| 1850 | break; |
| 1851 | case MAY_RMDIR: |
| 1852 | av = DIR__RMDIR; |
| 1853 | break; |
| 1854 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1855 | pr_warn("SELinux: %s: unrecognized kind %d\n", |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1856 | __func__, kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | return 0; |
| 1858 | } |
| 1859 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1860 | rc = avc_has_perm(&selinux_state, |
| 1861 | sid, isec->sid, isec->sclass, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | return rc; |
| 1863 | } |
| 1864 | |
| 1865 | static inline int may_rename(struct inode *old_dir, |
| 1866 | struct dentry *old_dentry, |
| 1867 | struct inode *new_dir, |
| 1868 | struct dentry *new_dentry) |
| 1869 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1871 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1872 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | u32 av; |
| 1874 | int old_is_dir, new_is_dir; |
| 1875 | int rc; |
| 1876 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1877 | old_dsec = inode_security(old_dir); |
| 1878 | old_isec = backing_inode_security(old_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1879 | old_is_dir = d_is_dir(old_dentry); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1880 | new_dsec = inode_security(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1882 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1884 | ad.u.dentry = old_dentry; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1885 | rc = avc_has_perm(&selinux_state, |
| 1886 | sid, old_dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
| 1888 | if (rc) |
| 1889 | return rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1890 | rc = avc_has_perm(&selinux_state, |
| 1891 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | old_isec->sclass, FILE__RENAME, &ad); |
| 1893 | if (rc) |
| 1894 | return rc; |
| 1895 | if (old_is_dir && new_dir != old_dir) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1896 | rc = avc_has_perm(&selinux_state, |
| 1897 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | old_isec->sclass, DIR__REPARENT, &ad); |
| 1899 | if (rc) |
| 1900 | return rc; |
| 1901 | } |
| 1902 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1903 | ad.u.dentry = new_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | av = DIR__ADD_NAME | DIR__SEARCH; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1905 | if (d_is_positive(new_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | av |= DIR__REMOVE_NAME; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1907 | rc = avc_has_perm(&selinux_state, |
| 1908 | sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | if (rc) |
| 1910 | return rc; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1911 | if (d_is_positive(new_dentry)) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1912 | new_isec = backing_inode_security(new_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1913 | new_is_dir = d_is_dir(new_dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1914 | rc = avc_has_perm(&selinux_state, |
| 1915 | sid, new_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | new_isec->sclass, |
| 1917 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); |
| 1918 | if (rc) |
| 1919 | return rc; |
| 1920 | } |
| 1921 | |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | /* Check whether a task can perform a filesystem operation. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1926 | static int superblock_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | struct super_block *sb, |
| 1928 | u32 perms, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1929 | struct common_audit_data *ad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | struct superblock_security_struct *sbsec; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1932 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | sbsec = sb->s_security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1935 | return avc_has_perm(&selinux_state, |
| 1936 | sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* Convert a Linux mode and permission mask to an access vector. */ |
| 1940 | static inline u32 file_mask_to_av(int mode, int mask) |
| 1941 | { |
| 1942 | u32 av = 0; |
| 1943 | |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 1944 | if (!S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | if (mask & MAY_EXEC) |
| 1946 | av |= FILE__EXECUTE; |
| 1947 | if (mask & MAY_READ) |
| 1948 | av |= FILE__READ; |
| 1949 | |
| 1950 | if (mask & MAY_APPEND) |
| 1951 | av |= FILE__APPEND; |
| 1952 | else if (mask & MAY_WRITE) |
| 1953 | av |= FILE__WRITE; |
| 1954 | |
| 1955 | } else { |
| 1956 | if (mask & MAY_EXEC) |
| 1957 | av |= DIR__SEARCH; |
| 1958 | if (mask & MAY_WRITE) |
| 1959 | av |= DIR__WRITE; |
| 1960 | if (mask & MAY_READ) |
| 1961 | av |= DIR__READ; |
| 1962 | } |
| 1963 | |
| 1964 | return av; |
| 1965 | } |
| 1966 | |
| 1967 | /* Convert a Linux file to an access vector. */ |
| 1968 | static inline u32 file_to_av(struct file *file) |
| 1969 | { |
| 1970 | u32 av = 0; |
| 1971 | |
| 1972 | if (file->f_mode & FMODE_READ) |
| 1973 | av |= FILE__READ; |
| 1974 | if (file->f_mode & FMODE_WRITE) { |
| 1975 | if (file->f_flags & O_APPEND) |
| 1976 | av |= FILE__APPEND; |
| 1977 | else |
| 1978 | av |= FILE__WRITE; |
| 1979 | } |
Stephen Smalley | 0794c66 | 2008-03-17 08:55:18 -0400 | [diff] [blame] | 1980 | if (!av) { |
| 1981 | /* |
| 1982 | * Special file opened with flags 3 for ioctl-only use. |
| 1983 | */ |
| 1984 | av = FILE__IOCTL; |
| 1985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
| 1987 | return av; |
| 1988 | } |
| 1989 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1990 | /* |
Randy Dunlap | c76a2f9 | 2020-08-07 09:51:34 -0700 | [diff] [blame] | 1991 | * Convert a file to an access vector and include the correct |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1992 | * open permission. |
| 1993 | */ |
| 1994 | static inline u32 open_file_to_av(struct file *file) |
| 1995 | { |
| 1996 | u32 av = file_to_av(file); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 1997 | struct inode *inode = file_inode(file); |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 1998 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1999 | if (selinux_policycap_openperm() && |
| 2000 | inode->i_sb->s_magic != SOCKFS_MAGIC) |
Eric Paris | 49b7b8d | 2010-07-23 11:44:09 -0400 | [diff] [blame] | 2001 | av |= FILE__OPEN; |
| 2002 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2003 | return av; |
| 2004 | } |
| 2005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | /* Hook functions begin here. */ |
| 2007 | |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2008 | static int selinux_binder_set_context_mgr(struct task_struct *mgr) |
| 2009 | { |
| 2010 | u32 mysid = current_sid(); |
| 2011 | u32 mgrsid = task_sid(mgr); |
| 2012 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2013 | return avc_has_perm(&selinux_state, |
| 2014 | mysid, mgrsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2015 | BINDER__SET_CONTEXT_MGR, NULL); |
| 2016 | } |
| 2017 | |
| 2018 | static int selinux_binder_transaction(struct task_struct *from, |
| 2019 | struct task_struct *to) |
| 2020 | { |
| 2021 | u32 mysid = current_sid(); |
| 2022 | u32 fromsid = task_sid(from); |
| 2023 | u32 tosid = task_sid(to); |
| 2024 | int rc; |
| 2025 | |
| 2026 | if (mysid != fromsid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2027 | rc = avc_has_perm(&selinux_state, |
| 2028 | mysid, fromsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2029 | BINDER__IMPERSONATE, NULL); |
| 2030 | if (rc) |
| 2031 | return rc; |
| 2032 | } |
| 2033 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2034 | return avc_has_perm(&selinux_state, |
| 2035 | fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2036 | NULL); |
| 2037 | } |
| 2038 | |
| 2039 | static int selinux_binder_transfer_binder(struct task_struct *from, |
| 2040 | struct task_struct *to) |
| 2041 | { |
| 2042 | u32 fromsid = task_sid(from); |
| 2043 | u32 tosid = task_sid(to); |
| 2044 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2045 | return avc_has_perm(&selinux_state, |
| 2046 | fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2047 | NULL); |
| 2048 | } |
| 2049 | |
| 2050 | static int selinux_binder_transfer_file(struct task_struct *from, |
| 2051 | struct task_struct *to, |
| 2052 | struct file *file) |
| 2053 | { |
| 2054 | u32 sid = task_sid(to); |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 2055 | struct file_security_struct *fsec = selinux_file(file); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2056 | struct dentry *dentry = file->f_path.dentry; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2057 | struct inode_security_struct *isec; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2058 | struct common_audit_data ad; |
| 2059 | int rc; |
| 2060 | |
| 2061 | ad.type = LSM_AUDIT_DATA_PATH; |
| 2062 | ad.u.path = file->f_path; |
| 2063 | |
| 2064 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2065 | rc = avc_has_perm(&selinux_state, |
| 2066 | sid, fsec->sid, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2067 | SECCLASS_FD, |
| 2068 | FD__USE, |
| 2069 | &ad); |
| 2070 | if (rc) |
| 2071 | return rc; |
| 2072 | } |
| 2073 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 2074 | #ifdef CONFIG_BPF_SYSCALL |
| 2075 | rc = bpf_fd_pass(file, sid); |
| 2076 | if (rc) |
| 2077 | return rc; |
| 2078 | #endif |
| 2079 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2080 | if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2081 | return 0; |
| 2082 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2083 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2084 | return avc_has_perm(&selinux_state, |
| 2085 | sid, isec->sid, isec->sclass, file_to_av(file), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2086 | &ad); |
| 2087 | } |
| 2088 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 2089 | static int selinux_ptrace_access_check(struct task_struct *child, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2090 | unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2092 | u32 sid = current_sid(); |
| 2093 | u32 csid = task_sid(child); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 2094 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2095 | if (mode & PTRACE_MODE_READ) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2096 | return avc_has_perm(&selinux_state, |
| 2097 | sid, csid, SECCLASS_FILE, FILE__READ, NULL); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2098 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2099 | return avc_has_perm(&selinux_state, |
| 2100 | sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | static int selinux_ptrace_traceme(struct task_struct *parent) |
| 2104 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2105 | return avc_has_perm(&selinux_state, |
| 2106 | task_sid(parent), current_sid(), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2107 | PROCESS__PTRACE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2111 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2113 | return avc_has_perm(&selinux_state, |
| 2114 | current_sid(), task_sid(target), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2115 | PROCESS__GETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2118 | static int selinux_capset(struct cred *new, const struct cred *old, |
| 2119 | const kernel_cap_t *effective, |
| 2120 | const kernel_cap_t *inheritable, |
| 2121 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2123 | return avc_has_perm(&selinux_state, |
| 2124 | cred_sid(old), cred_sid(new), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2125 | PROCESS__SETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
James Morris | 5626d3e | 2009-01-30 10:05:06 +1100 | [diff] [blame] | 2128 | /* |
| 2129 | * (This comment used to live with the selinux_task_setuid hook, |
| 2130 | * which was removed). |
| 2131 | * |
| 2132 | * Since setuid only affects the current process, and since the SELinux |
| 2133 | * controls are not based on the Linux identity attributes, SELinux does not |
| 2134 | * need to control this operation. However, SELinux does control the use of |
| 2135 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. |
| 2136 | */ |
| 2137 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2138 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 2139 | int cap, unsigned int opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | { |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 2141 | return cred_has_capability(cred, cap, opts, ns == &init_user_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| 2145 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2146 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | int rc = 0; |
| 2148 | |
| 2149 | if (!sb) |
| 2150 | return 0; |
| 2151 | |
| 2152 | switch (cmds) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2153 | case Q_SYNC: |
| 2154 | case Q_QUOTAON: |
| 2155 | case Q_QUOTAOFF: |
| 2156 | case Q_SETINFO: |
| 2157 | case Q_SETQUOTA: |
Richard Haines | e4cfa05 | 2020-02-20 15:32:34 +0000 | [diff] [blame] | 2158 | case Q_XQUOTAOFF: |
| 2159 | case Q_XQUOTAON: |
| 2160 | case Q_XSETQLIM: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2161 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2162 | break; |
| 2163 | case Q_GETFMT: |
| 2164 | case Q_GETINFO: |
| 2165 | case Q_GETQUOTA: |
Richard Haines | e4cfa05 | 2020-02-20 15:32:34 +0000 | [diff] [blame] | 2166 | case Q_XGETQUOTA: |
| 2167 | case Q_XGETQSTAT: |
| 2168 | case Q_XGETQSTATV: |
| 2169 | case Q_XGETNEXTQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2170 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2171 | break; |
| 2172 | default: |
| 2173 | rc = 0; /* let the kernel handle invalid cmds */ |
| 2174 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | return rc; |
| 2177 | } |
| 2178 | |
| 2179 | static int selinux_quota_on(struct dentry *dentry) |
| 2180 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2181 | const struct cred *cred = current_cred(); |
| 2182 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2183 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 2186 | static int selinux_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2189 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
| 2190 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2191 | return avc_has_perm(&selinux_state, |
| 2192 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2193 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2194 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
| 2195 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ |
| 2196 | /* Set level of messages printed to console */ |
| 2197 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2198 | return avc_has_perm(&selinux_state, |
| 2199 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2200 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, |
| 2201 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | } |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2203 | /* All other syslog types */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2204 | return avc_has_perm(&selinux_state, |
| 2205 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2206 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Check that a process has enough memory to allocate a new virtual |
| 2211 | * mapping. 0 means there is enough memory for the allocation to |
| 2212 | * succeed and -ENOMEM implies there is not. |
| 2213 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | * Do not audit the selinux permission check, as this is applied to all |
| 2215 | * processes that allocate mappings. |
| 2216 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2217 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
| 2219 | int rc, cap_sys_admin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2221 | rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 2222 | CAP_OPT_NOAUDIT, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | if (rc == 0) |
| 2224 | cap_sys_admin = 1; |
| 2225 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2226 | return cap_sys_admin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | /* binprm security operations */ |
| 2230 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2231 | static u32 ptrace_parent_sid(void) |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2232 | { |
| 2233 | u32 sid = 0; |
| 2234 | struct task_struct *tracer; |
| 2235 | |
| 2236 | rcu_read_lock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2237 | tracer = ptrace_parent(current); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2238 | if (tracer) |
| 2239 | sid = task_sid(tracer); |
| 2240 | rcu_read_unlock(); |
| 2241 | |
| 2242 | return sid; |
| 2243 | } |
| 2244 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2245 | static int check_nnp_nosuid(const struct linux_binprm *bprm, |
| 2246 | const struct task_security_struct *old_tsec, |
| 2247 | const struct task_security_struct *new_tsec) |
| 2248 | { |
| 2249 | int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); |
Andy Lutomirski | 380cf5b | 2016-06-23 16:41:05 -0500 | [diff] [blame] | 2250 | int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2251 | int rc; |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2252 | u32 av; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2253 | |
| 2254 | if (!nnp && !nosuid) |
| 2255 | return 0; /* neither NNP nor nosuid */ |
| 2256 | |
| 2257 | if (new_tsec->sid == old_tsec->sid) |
| 2258 | return 0; /* No change in credentials */ |
| 2259 | |
| 2260 | /* |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2261 | * If the policy enables the nnp_nosuid_transition policy capability, |
| 2262 | * then we permit transitions under NNP or nosuid if the |
| 2263 | * policy allows the corresponding permission between |
| 2264 | * the old and new contexts. |
| 2265 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2266 | if (selinux_policycap_nnp_nosuid_transition()) { |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2267 | av = 0; |
| 2268 | if (nnp) |
| 2269 | av |= PROCESS2__NNP_TRANSITION; |
| 2270 | if (nosuid) |
| 2271 | av |= PROCESS2__NOSUID_TRANSITION; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2272 | rc = avc_has_perm(&selinux_state, |
| 2273 | old_tsec->sid, new_tsec->sid, |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2274 | SECCLASS_PROCESS2, av, NULL); |
| 2275 | if (!rc) |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * We also permit NNP or nosuid transitions to bounded SIDs, |
| 2281 | * i.e. SIDs that are guaranteed to only be allowed a subset |
| 2282 | * of the permissions of the current SID. |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2283 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2284 | rc = security_bounded_transition(&selinux_state, old_tsec->sid, |
| 2285 | new_tsec->sid); |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2286 | if (!rc) |
| 2287 | return 0; |
| 2288 | |
| 2289 | /* |
| 2290 | * On failure, preserve the errno values for NNP vs nosuid. |
| 2291 | * NNP: Operation not permitted for caller. |
| 2292 | * nosuid: Permission denied to file. |
| 2293 | */ |
| 2294 | if (nnp) |
| 2295 | return -EPERM; |
| 2296 | return -EACCES; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
Eric W. Biederman | b8bff59 | 2020-03-22 15:46:24 -0500 | [diff] [blame] | 2299 | static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2301 | const struct task_security_struct *old_tsec; |
| 2302 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | struct inode_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2304 | struct common_audit_data ad; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2305 | struct inode *inode = file_inode(bprm->file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | int rc; |
| 2307 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2308 | /* SELinux context only depends on initial program or script and not |
| 2309 | * the script interpreter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2311 | old_tsec = selinux_cred(current_cred()); |
| 2312 | new_tsec = selinux_cred(bprm->cred); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2313 | isec = inode_security(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
| 2315 | /* Default to the current task SID. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2316 | new_tsec->sid = old_tsec->sid; |
| 2317 | new_tsec->osid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | |
Michael LeMay | 28eba5b | 2006-06-27 02:53:42 -0700 | [diff] [blame] | 2319 | /* Reset fs, key, and sock SIDs on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2320 | new_tsec->create_sid = 0; |
| 2321 | new_tsec->keycreate_sid = 0; |
| 2322 | new_tsec->sockcreate_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2324 | if (old_tsec->exec_sid) { |
| 2325 | new_tsec->sid = old_tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | /* Reset exec SID on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2327 | new_tsec->exec_sid = 0; |
Andy Lutomirski | 259e5e6 | 2012-04-12 16:47:50 -0500 | [diff] [blame] | 2328 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2329 | /* Fail on NNP or nosuid if not an allowed transition. */ |
| 2330 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2331 | if (rc) |
| 2332 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } else { |
| 2334 | /* Check for a default transition on this program. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2335 | rc = security_transition_sid(&selinux_state, old_tsec->sid, |
| 2336 | isec->sid, SECCLASS_PROCESS, NULL, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2337 | &new_tsec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | if (rc) |
| 2339 | return rc; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2340 | |
| 2341 | /* |
| 2342 | * Fallback to old SID on NNP or nosuid if not an allowed |
| 2343 | * transition. |
| 2344 | */ |
| 2345 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2346 | if (rc) |
| 2347 | new_tsec->sid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 2350 | ad.type = LSM_AUDIT_DATA_FILE; |
| 2351 | ad.u.file = bprm->file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2353 | if (new_tsec->sid == old_tsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2354 | rc = avc_has_perm(&selinux_state, |
| 2355 | old_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
| 2357 | if (rc) |
| 2358 | return rc; |
| 2359 | } else { |
| 2360 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2361 | rc = avc_has_perm(&selinux_state, |
| 2362 | old_tsec->sid, new_tsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
| 2364 | if (rc) |
| 2365 | return rc; |
| 2366 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2367 | rc = avc_has_perm(&selinux_state, |
| 2368 | new_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
| 2370 | if (rc) |
| 2371 | return rc; |
| 2372 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2373 | /* Check for shared state */ |
| 2374 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2375 | rc = avc_has_perm(&selinux_state, |
| 2376 | old_tsec->sid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2377 | SECCLASS_PROCESS, PROCESS__SHARE, |
| 2378 | NULL); |
| 2379 | if (rc) |
| 2380 | return -EPERM; |
| 2381 | } |
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 | /* Make sure that anyone attempting to ptrace over a task that |
| 2384 | * changes its SID has the appropriate permit */ |
Eric W. Biederman | 9227dd2 | 2017-01-23 17:26:31 +1300 | [diff] [blame] | 2385 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2386 | u32 ptsid = ptrace_parent_sid(); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2387 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2388 | rc = avc_has_perm(&selinux_state, |
| 2389 | ptsid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2390 | SECCLASS_PROCESS, |
| 2391 | PROCESS__PTRACE, NULL); |
| 2392 | if (rc) |
| 2393 | return -EPERM; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | /* Clear any possibly unsafe personality bits on exec: */ |
| 2398 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | /* Enable secure mode for SIDs transitions unless |
| 2401 | the noatsecure permission is granted between |
| 2402 | the two SIDs, i.e. ahp returns 0. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2403 | rc = avc_has_perm(&selinux_state, |
| 2404 | old_tsec->sid, new_tsec->sid, |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2405 | SECCLASS_PROCESS, PROCESS__NOATSECURE, |
| 2406 | NULL); |
| 2407 | bprm->secureexec |= !!rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2410 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2413 | static int match_file(const void *p, struct file *file, unsigned fd) |
| 2414 | { |
| 2415 | return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; |
| 2416 | } |
| 2417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | /* Derived from fs/exec.c:flush_old_files. */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2419 | static inline void flush_unauthorized_files(const struct cred *cred, |
| 2420 | struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | struct file *file, *devnull = NULL; |
Stephen Smalley | b20c812 | 2006-09-25 23:32:03 -0700 | [diff] [blame] | 2423 | struct tty_struct *tty; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2424 | int drop_tty = 0; |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2425 | unsigned n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2427 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | if (tty) { |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2429 | spin_lock(&tty->files_lock); |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2430 | if (!list_empty(&tty->tty_files)) { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2431 | struct tty_file_private *file_priv; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | /* Revalidate access to controlling tty. |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2434 | Use file_path_has_perm on the tty path directly |
| 2435 | rather than using file_has_perm, as this particular |
| 2436 | open file may belong to another process and we are |
| 2437 | only interested in the inode-based check here. */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2438 | file_priv = list_first_entry(&tty->tty_files, |
| 2439 | struct tty_file_private, list); |
| 2440 | file = file_priv->file; |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2441 | if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2442 | drop_tty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | } |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2444 | spin_unlock(&tty->files_lock); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 2445 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | } |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 2447 | /* Reset controlling tty. */ |
| 2448 | if (drop_tty) |
| 2449 | no_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | |
| 2451 | /* Revalidate access to inherited open files. */ |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2452 | n = iterate_fd(files, 0, match_file, cred); |
| 2453 | if (!n) /* none found? */ |
| 2454 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2456 | devnull = dentry_open(&selinux_null, O_RDWR, cred); |
Al Viro | 45525b2 | 2012-10-16 13:30:07 -0400 | [diff] [blame] | 2457 | if (IS_ERR(devnull)) |
| 2458 | devnull = NULL; |
| 2459 | /* replace all the matching ones with this */ |
| 2460 | do { |
| 2461 | replace_fd(n - 1, devnull, 0); |
| 2462 | } while ((n = iterate_fd(files, n, match_file, cred)) != 0); |
| 2463 | if (devnull) |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2464 | fput(devnull); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2468 | * Prepare a process for imminent new credential changes due to exec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2470 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2472 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | struct rlimit *rlim, *initrlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | int rc, i; |
| 2475 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2476 | new_tsec = selinux_cred(bprm->cred); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2477 | if (new_tsec->sid == new_tsec->osid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | return; |
| 2479 | |
| 2480 | /* Close files for which the new task SID is not authorized. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2481 | flush_unauthorized_files(bprm->cred, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2483 | /* Always clear parent death signal on SID transitions. */ |
| 2484 | current->pdeath_signal = 0; |
| 2485 | |
| 2486 | /* Check whether the new SID can inherit resource limits from the old |
| 2487 | * SID. If not, reset all soft limits to the lower of the current |
| 2488 | * task's hard limit and the init task's soft limit. |
| 2489 | * |
| 2490 | * Note that the setting of hard limits (even to lower them) can be |
| 2491 | * controlled by the setrlimit check. The inclusion of the init task's |
| 2492 | * soft limit into the computation is to avoid resetting soft limits |
| 2493 | * higher than the default soft limit for cases where the default is |
| 2494 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. |
| 2495 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2496 | rc = avc_has_perm(&selinux_state, |
| 2497 | new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2498 | PROCESS__RLIMITINH, NULL); |
| 2499 | if (rc) { |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2500 | /* protect against do_prlimit() */ |
| 2501 | task_lock(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2502 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 2503 | rlim = current->signal->rlim + i; |
| 2504 | initrlim = init_task.signal->rlim + i; |
| 2505 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); |
| 2506 | } |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2507 | task_unlock(current); |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2508 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) |
| 2509 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | /* |
| 2514 | * Clean up the process immediately after the installation of new credentials |
| 2515 | * due to exec |
| 2516 | */ |
| 2517 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2518 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2519 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2520 | u32 osid, sid; |
Arnd Bergmann | ddbc7d0 | 2019-10-25 21:37:43 +0200 | [diff] [blame] | 2521 | int rc; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2522 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2523 | osid = tsec->osid; |
| 2524 | sid = tsec->sid; |
| 2525 | |
| 2526 | if (sid == osid) |
| 2527 | return; |
| 2528 | |
| 2529 | /* Check whether the new SID can inherit signal state from the old SID. |
| 2530 | * If not, clear itimers to avoid subsequent signal generation and |
| 2531 | * flush and unblock signals. |
| 2532 | * |
| 2533 | * This must occur _after_ the task SID has been updated so that any |
| 2534 | * kill done after the flush will be checked against the new SID. |
| 2535 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2536 | rc = avc_has_perm(&selinux_state, |
| 2537 | osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | if (rc) { |
Arnd Bergmann | ddbc7d0 | 2019-10-25 21:37:43 +0200 | [diff] [blame] | 2539 | clear_itimer(); |
| 2540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2542 | if (!fatal_signal_pending(current)) { |
| 2543 | flush_sigqueue(¤t->pending); |
| 2544 | flush_sigqueue(¤t->signal->shared_pending); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2545 | flush_signal_handlers(current, 1); |
| 2546 | sigemptyset(¤t->blocked); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2547 | recalc_sigpending(); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | spin_unlock_irq(¤t->sighand->siglock); |
| 2550 | } |
| 2551 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2552 | /* Wake up the parent if it is waiting so that it can recheck |
| 2553 | * wait permission to the new task SID. */ |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2554 | read_lock(&tasklist_lock); |
Oleg Nesterov | 0b7570e | 2009-09-23 15:56:46 -0700 | [diff] [blame] | 2555 | __wake_up_parent(current, current->real_parent); |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2556 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | /* superblock security operations */ |
| 2560 | |
| 2561 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2562 | { |
Paul Moore | cb89e24 | 2020-01-10 16:32:10 -0500 | [diff] [blame] | 2563 | struct superblock_security_struct *sbsec; |
| 2564 | |
| 2565 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
| 2566 | if (!sbsec) |
| 2567 | return -ENOMEM; |
| 2568 | |
| 2569 | mutex_init(&sbsec->lock); |
| 2570 | INIT_LIST_HEAD(&sbsec->isec_head); |
| 2571 | spin_lock_init(&sbsec->isec_lock); |
Paul Moore | cb89e24 | 2020-01-10 16:32:10 -0500 | [diff] [blame] | 2572 | sbsec->sid = SECINITSID_UNLABELED; |
| 2573 | sbsec->def_sid = SECINITSID_FILE; |
| 2574 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
| 2575 | sb->s_security = sbsec; |
| 2576 | |
| 2577 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | static void selinux_sb_free_security(struct super_block *sb) |
| 2581 | { |
| 2582 | superblock_free_security(sb); |
| 2583 | } |
| 2584 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2585 | static inline int opt_len(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | { |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2587 | bool open_quote = false; |
| 2588 | int len; |
| 2589 | char c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2591 | for (len = 0; (c = s[len]) != '\0'; len++) { |
| 2592 | if (c == '"') |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2593 | open_quote = !open_quote; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2594 | if (c == ',' && !open_quote) |
| 2595 | break; |
| 2596 | } |
| 2597 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2600 | static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2601 | { |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2602 | char *from = options; |
| 2603 | char *to = options; |
| 2604 | bool first = true; |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2605 | int rc; |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2606 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2607 | while (1) { |
| 2608 | int len = opt_len(from); |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2609 | int token; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2610 | char *arg = NULL; |
| 2611 | |
| 2612 | token = match_opt_prefix(from, len, &arg); |
| 2613 | |
| 2614 | if (token != Opt_error) { |
| 2615 | char *p, *q; |
| 2616 | |
| 2617 | /* strip quotes */ |
| 2618 | if (arg) { |
| 2619 | for (p = q = arg; p < from + len; p++) { |
| 2620 | char c = *p; |
| 2621 | if (c != '"') |
| 2622 | *q++ = c; |
| 2623 | } |
| 2624 | arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2625 | if (!arg) { |
| 2626 | rc = -ENOMEM; |
| 2627 | goto free_opt; |
| 2628 | } |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2629 | } |
| 2630 | rc = selinux_add_opt(token, arg, mnt_opts); |
| 2631 | if (unlikely(rc)) { |
| 2632 | kfree(arg); |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2633 | goto free_opt; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2634 | } |
| 2635 | } else { |
| 2636 | if (!first) { // copy with preceding comma |
| 2637 | from--; |
| 2638 | len++; |
| 2639 | } |
| 2640 | if (to != from) |
| 2641 | memmove(to, from, len); |
| 2642 | to += len; |
| 2643 | first = false; |
| 2644 | } |
| 2645 | if (!from[len]) |
| 2646 | break; |
| 2647 | from += len + 1; |
| 2648 | } |
| 2649 | *to = '\0'; |
| 2650 | return 0; |
Gen Zhang | fec6375 | 2019-06-12 21:55:38 +0800 | [diff] [blame] | 2651 | |
| 2652 | free_opt: |
| 2653 | if (*mnt_opts) { |
| 2654 | selinux_free_mnt_opts(*mnt_opts); |
| 2655 | *mnt_opts = NULL; |
| 2656 | } |
| 2657 | return rc; |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2658 | } |
| 2659 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2660 | static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2661 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2662 | struct selinux_mnt_opts *opts = mnt_opts; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2663 | struct superblock_security_struct *sbsec = sb->s_security; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2664 | u32 sid; |
| 2665 | int rc; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2666 | |
| 2667 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 2668 | return 0; |
| 2669 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2670 | if (!opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2671 | return 0; |
| 2672 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2673 | if (opts->fscontext) { |
| 2674 | rc = parse_sid(sb, opts->fscontext, &sid); |
| 2675 | if (rc) |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2676 | return rc; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2677 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) |
| 2678 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2679 | } |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2680 | if (opts->context) { |
| 2681 | rc = parse_sid(sb, opts->context, &sid); |
| 2682 | if (rc) |
| 2683 | return rc; |
| 2684 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) |
| 2685 | goto out_bad_option; |
| 2686 | } |
| 2687 | if (opts->rootcontext) { |
| 2688 | struct inode_security_struct *root_isec; |
| 2689 | root_isec = backing_inode_security(sb->s_root); |
| 2690 | rc = parse_sid(sb, opts->rootcontext, &sid); |
| 2691 | if (rc) |
| 2692 | return rc; |
| 2693 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) |
| 2694 | goto out_bad_option; |
| 2695 | } |
| 2696 | if (opts->defcontext) { |
| 2697 | rc = parse_sid(sb, opts->defcontext, &sid); |
| 2698 | if (rc) |
| 2699 | return rc; |
| 2700 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) |
| 2701 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2702 | } |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2703 | return 0; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2704 | |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2705 | out_bad_option: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 2706 | pr_warn("SELinux: unable to change security options " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 2707 | "during remount (dev %s, type=%s)\n", sb->s_id, |
| 2708 | sb->s_type->name); |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2709 | return -EINVAL; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2710 | } |
| 2711 | |
Al Viro | a10d7c2 | 2018-12-05 11:58:35 -0500 | [diff] [blame] | 2712 | static int selinux_sb_kern_mount(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2714 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2715 | struct common_audit_data ad; |
James Morris | 7419224 | 2008-12-19 11:41:10 +1100 | [diff] [blame] | 2716 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2717 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2718 | ad.u.dentry = sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2719 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2722 | static int selinux_sb_statfs(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2724 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2725 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2727 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2728 | ad.u.dentry = dentry->d_sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2729 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | } |
| 2731 | |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2732 | static int selinux_mount(const char *dev_name, |
Al Viro | 8a04c43 | 2016-03-25 14:52:53 -0400 | [diff] [blame] | 2733 | const struct path *path, |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2734 | const char *type, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2735 | unsigned long flags, |
| 2736 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2738 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | |
| 2740 | if (flags & MS_REMOUNT) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 2741 | return superblock_has_perm(cred, path->dentry->d_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2742 | FILESYSTEM__REMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | else |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2744 | return path_has_perm(cred, path, FILE__MOUNTON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | } |
| 2746 | |
Stephen Smalley | 98aa003 | 2020-01-17 15:24:07 -0500 | [diff] [blame] | 2747 | static int selinux_move_mount(const struct path *from_path, |
| 2748 | const struct path *to_path) |
| 2749 | { |
| 2750 | const struct cred *cred = current_cred(); |
| 2751 | |
| 2752 | return path_has_perm(cred, to_path, FILE__MOUNTON); |
| 2753 | } |
| 2754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | static int selinux_umount(struct vfsmount *mnt, int flags) |
| 2756 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2757 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2759 | return superblock_has_perm(cred, mnt->mnt_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2760 | FILESYSTEM__UNMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
Al Viro | 0b52075 | 2018-12-23 16:02:47 -0500 | [diff] [blame] | 2763 | static int selinux_fs_context_dup(struct fs_context *fc, |
| 2764 | struct fs_context *src_fc) |
| 2765 | { |
| 2766 | const struct selinux_mnt_opts *src = src_fc->security; |
| 2767 | struct selinux_mnt_opts *opts; |
| 2768 | |
| 2769 | if (!src) |
| 2770 | return 0; |
| 2771 | |
| 2772 | fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); |
| 2773 | if (!fc->security) |
| 2774 | return -ENOMEM; |
| 2775 | |
| 2776 | opts = fc->security; |
| 2777 | |
| 2778 | if (src->fscontext) { |
| 2779 | opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL); |
| 2780 | if (!opts->fscontext) |
| 2781 | return -ENOMEM; |
| 2782 | } |
| 2783 | if (src->context) { |
| 2784 | opts->context = kstrdup(src->context, GFP_KERNEL); |
| 2785 | if (!opts->context) |
| 2786 | return -ENOMEM; |
| 2787 | } |
| 2788 | if (src->rootcontext) { |
| 2789 | opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL); |
| 2790 | if (!opts->rootcontext) |
| 2791 | return -ENOMEM; |
| 2792 | } |
| 2793 | if (src->defcontext) { |
| 2794 | opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL); |
| 2795 | if (!opts->defcontext) |
| 2796 | return -ENOMEM; |
| 2797 | } |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 2801 | static const struct fs_parameter_spec selinux_fs_parameters[] = { |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2802 | fsparam_string(CONTEXT_STR, Opt_context), |
| 2803 | fsparam_string(DEFCONTEXT_STR, Opt_defcontext), |
| 2804 | fsparam_string(FSCONTEXT_STR, Opt_fscontext), |
| 2805 | fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext), |
| 2806 | fsparam_flag (SECLABEL_STR, Opt_seclabel), |
| 2807 | {} |
| 2808 | }; |
| 2809 | |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2810 | static int selinux_fs_context_parse_param(struct fs_context *fc, |
| 2811 | struct fs_parameter *param) |
| 2812 | { |
| 2813 | struct fs_parse_result result; |
| 2814 | int opt, rc; |
| 2815 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 2816 | opt = fs_parse(fc, selinux_fs_parameters, param, &result); |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 2817 | if (opt < 0) |
| 2818 | return opt; |
| 2819 | |
| 2820 | rc = selinux_add_opt(opt, param->string, &fc->security); |
| 2821 | if (!rc) { |
| 2822 | param->string = NULL; |
| 2823 | rc = 1; |
| 2824 | } |
| 2825 | return rc; |
| 2826 | } |
| 2827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | /* inode security operations */ |
| 2829 | |
| 2830 | static int selinux_inode_alloc_security(struct inode *inode) |
| 2831 | { |
Paul Moore | cb89e24 | 2020-01-10 16:32:10 -0500 | [diff] [blame] | 2832 | struct inode_security_struct *isec = selinux_inode(inode); |
| 2833 | u32 sid = current_sid(); |
| 2834 | |
| 2835 | spin_lock_init(&isec->lock); |
| 2836 | INIT_LIST_HEAD(&isec->list); |
| 2837 | isec->inode = inode; |
| 2838 | isec->sid = SECINITSID_UNLABELED; |
| 2839 | isec->sclass = SECCLASS_FILE; |
| 2840 | isec->task_sid = sid; |
| 2841 | isec->initialized = LABEL_INVALID; |
| 2842 | |
| 2843 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | static void selinux_inode_free_security(struct inode *inode) |
| 2847 | { |
| 2848 | inode_free_security(inode); |
| 2849 | } |
| 2850 | |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2851 | static int selinux_dentry_init_security(struct dentry *dentry, int mode, |
Al Viro | 4f3ccd7 | 2016-07-20 16:06:15 -0400 | [diff] [blame] | 2852 | const struct qstr *name, void **ctx, |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2853 | u32 *ctxlen) |
| 2854 | { |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2855 | u32 newsid; |
| 2856 | int rc; |
| 2857 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2858 | rc = selinux_determine_inode_label(selinux_cred(current_cred()), |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 2859 | d_inode(dentry->d_parent), name, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2860 | inode_mode_to_security_class(mode), |
| 2861 | &newsid); |
| 2862 | if (rc) |
| 2863 | return rc; |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2864 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2865 | return security_sid_to_context(&selinux_state, newsid, (char **)ctx, |
| 2866 | ctxlen); |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2867 | } |
| 2868 | |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2869 | static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, |
| 2870 | struct qstr *name, |
| 2871 | const struct cred *old, |
| 2872 | struct cred *new) |
| 2873 | { |
| 2874 | u32 newsid; |
| 2875 | int rc; |
| 2876 | struct task_security_struct *tsec; |
| 2877 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2878 | rc = selinux_determine_inode_label(selinux_cred(old), |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2879 | d_inode(dentry->d_parent), name, |
| 2880 | inode_mode_to_security_class(mode), |
| 2881 | &newsid); |
| 2882 | if (rc) |
| 2883 | return rc; |
| 2884 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2885 | tsec = selinux_cred(new); |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2886 | tsec->create_sid = newsid; |
| 2887 | return 0; |
| 2888 | } |
| 2889 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2890 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2891 | const struct qstr *qstr, |
| 2892 | const char **name, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2893 | void **value, size_t *len) |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2894 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 2895 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2896 | struct superblock_security_struct *sbsec; |
Corentin LABBE | c0d4f46 | 2017-10-04 20:32:17 +0200 | [diff] [blame] | 2897 | u32 newsid, clen; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2898 | int rc; |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2899 | char *context; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2900 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2901 | sbsec = dir->i_sb->s_security; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2902 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2903 | newsid = tsec->create_sid; |
| 2904 | |
Yang Guo | 210a292 | 2019-12-12 10:02:24 +0800 | [diff] [blame] | 2905 | rc = selinux_determine_inode_label(tsec, dir, qstr, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2906 | inode_mode_to_security_class(inode->i_mode), |
| 2907 | &newsid); |
| 2908 | if (rc) |
| 2909 | return rc; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2910 | |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2911 | /* Possibly defer initialization to selinux_complete_init. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 2912 | if (sbsec->flags & SE_SBINITIALIZED) { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 2913 | struct inode_security_struct *isec = selinux_inode(inode); |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2914 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2915 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2916 | isec->initialized = LABEL_INITIALIZED; |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2917 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2918 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 2919 | if (!selinux_initialized(&selinux_state) || |
| 2920 | !(sbsec->flags & SBLABEL_MNT)) |
Stephen Smalley | 25a74f3 | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 2921 | return -EOPNOTSUPP; |
| 2922 | |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2923 | if (name) |
| 2924 | *name = XATTR_SELINUX_SUFFIX; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2925 | |
| 2926 | if (value && len) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2927 | rc = security_sid_to_context_force(&selinux_state, newsid, |
| 2928 | &context, &clen); |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2929 | if (rc) |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2930 | return rc; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2931 | *value = context; |
| 2932 | *len = clen; |
| 2933 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2934 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2935 | return 0; |
| 2936 | } |
| 2937 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2938 | 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] | 2939 | { |
| 2940 | return may_create(dir, dentry, SECCLASS_FILE); |
| 2941 | } |
| 2942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2944 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | return may_link(dir, old_dentry, MAY_LINK); |
| 2946 | } |
| 2947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 2949 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | return may_link(dir, dentry, MAY_UNLINK); |
| 2951 | } |
| 2952 | |
| 2953 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) |
| 2954 | { |
| 2955 | return may_create(dir, dentry, SECCLASS_LNK_FILE); |
| 2956 | } |
| 2957 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2958 | 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] | 2959 | { |
| 2960 | return may_create(dir, dentry, SECCLASS_DIR); |
| 2961 | } |
| 2962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 2964 | { |
| 2965 | return may_link(dir, dentry, MAY_RMDIR); |
| 2966 | } |
| 2967 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2968 | 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] | 2969 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
| 2971 | } |
| 2972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | 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] | 2974 | struct inode *new_inode, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | { |
| 2976 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2977 | } |
| 2978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | static int selinux_inode_readlink(struct dentry *dentry) |
| 2980 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2981 | const struct cred *cred = current_cred(); |
| 2982 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2983 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2986 | static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, |
| 2987 | bool rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2989 | const struct cred *cred = current_cred(); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2990 | struct common_audit_data ad; |
| 2991 | struct inode_security_struct *isec; |
| 2992 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2994 | validate_creds(cred); |
| 2995 | |
| 2996 | ad.type = LSM_AUDIT_DATA_DENTRY; |
| 2997 | ad.u.dentry = dentry; |
| 2998 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2999 | isec = inode_security_rcu(inode, rcu); |
| 3000 | if (IS_ERR(isec)) |
| 3001 | return PTR_ERR(isec); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 3002 | |
Stephen Smalley | 1a37079 | 2019-11-22 12:22:44 -0500 | [diff] [blame] | 3003 | return avc_has_perm_flags(&selinux_state, |
| 3004 | sid, isec->sid, isec->sclass, FILE__READ, &ad, |
| 3005 | rcu ? MAY_NOT_BLOCK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3008 | static noinline int audit_inode_permission(struct inode *inode, |
| 3009 | u32 perms, u32 audited, u32 denied, |
Stephen Smalley | 0188d5c | 2019-11-22 12:22:45 -0500 | [diff] [blame] | 3010 | int result) |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3011 | { |
| 3012 | struct common_audit_data ad; |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 3013 | struct inode_security_struct *isec = selinux_inode(inode); |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3014 | int rc; |
| 3015 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3016 | ad.type = LSM_AUDIT_DATA_INODE; |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3017 | ad.u.inode = inode; |
| 3018 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3019 | rc = slow_avc_audit(&selinux_state, |
| 3020 | current_sid(), isec->sid, isec->sclass, perms, |
Stephen Smalley | 0188d5c | 2019-11-22 12:22:45 -0500 | [diff] [blame] | 3021 | audited, denied, result, &ad); |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3022 | if (rc) |
| 3023 | return rc; |
| 3024 | return 0; |
| 3025 | } |
| 3026 | |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 3027 | static int selinux_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3029 | const struct cred *cred = current_cred(); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3030 | u32 perms; |
| 3031 | bool from_access; |
Stephen Smalley | 5298d0b | 2019-11-22 16:16:56 -0500 | [diff] [blame] | 3032 | bool no_block = mask & MAY_NOT_BLOCK; |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3033 | struct inode_security_struct *isec; |
| 3034 | u32 sid; |
| 3035 | struct av_decision avd; |
| 3036 | int rc, rc2; |
| 3037 | u32 audited, denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3039 | from_access = mask & MAY_ACCESS; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 3040 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
| 3041 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3042 | /* No permission to check. Existence test. */ |
| 3043 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3046 | validate_creds(cred); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3047 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3048 | if (unlikely(IS_PRIVATE(inode))) |
| 3049 | return 0; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 3050 | |
| 3051 | perms = file_mask_to_av(inode->i_mode, mask); |
| 3052 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3053 | sid = cred_sid(cred); |
Stephen Smalley | 5298d0b | 2019-11-22 16:16:56 -0500 | [diff] [blame] | 3054 | isec = inode_security_rcu(inode, no_block); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3055 | if (IS_ERR(isec)) |
| 3056 | return PTR_ERR(isec); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3057 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3058 | rc = avc_has_perm_noaudit(&selinux_state, |
Stephen Smalley | 3a28cff | 2018-12-12 10:10:55 -0500 | [diff] [blame] | 3059 | sid, isec->sid, isec->sclass, perms, |
Stephen Smalley | 5298d0b | 2019-11-22 16:16:56 -0500 | [diff] [blame] | 3060 | no_block ? AVC_NONBLOCKING : 0, |
Stephen Smalley | 3a28cff | 2018-12-12 10:10:55 -0500 | [diff] [blame] | 3061 | &avd); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3062 | audited = avc_audit_required(perms, &avd, rc, |
| 3063 | from_access ? FILE__AUDIT_ACCESS : 0, |
| 3064 | &denied); |
| 3065 | if (likely(!audited)) |
| 3066 | return rc; |
| 3067 | |
Stephen Smalley | 0188d5c | 2019-11-22 12:22:45 -0500 | [diff] [blame] | 3068 | /* fall back to ref-walk if we have to generate audit */ |
Stephen Smalley | 5298d0b | 2019-11-22 16:16:56 -0500 | [diff] [blame] | 3069 | if (no_block) |
Stephen Smalley | 0188d5c | 2019-11-22 12:22:45 -0500 | [diff] [blame] | 3070 | return -ECHILD; |
| 3071 | |
| 3072 | rc2 = audit_inode_permission(inode, perms, audited, denied, rc); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 3073 | if (rc2) |
| 3074 | return rc2; |
| 3075 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 3079 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3080 | const struct cred *cred = current_cred(); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3081 | struct inode *inode = d_backing_inode(dentry); |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3082 | unsigned int ia_valid = iattr->ia_valid; |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3083 | __u32 av = FILE__WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3085 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
| 3086 | if (ia_valid & ATTR_FORCE) { |
| 3087 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | |
| 3088 | ATTR_FORCE); |
| 3089 | if (!ia_valid) |
| 3090 | return 0; |
| 3091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3093 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
| 3094 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3095 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3097 | if (selinux_policycap_openperm() && |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3098 | inode->i_sb->s_magic != SOCKFS_MAGIC && |
| 3099 | (ia_valid & ATTR_SIZE) && |
| 3100 | !(ia_valid & ATTR_FILE)) |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3101 | av |= FILE__OPEN; |
| 3102 | |
| 3103 | return dentry_has_perm(cred, dentry, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3106 | static int selinux_inode_getattr(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | { |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3108 | return path_has_perm(current_cred(), path, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | } |
| 3110 | |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3111 | static bool has_cap_mac_admin(bool audit) |
| 3112 | { |
| 3113 | const struct cred *cred = current_cred(); |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3114 | unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT; |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3115 | |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3116 | if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts)) |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3117 | return false; |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3118 | if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true)) |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3119 | return false; |
| 3120 | return true; |
| 3121 | } |
| 3122 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3123 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
| 3124 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3126 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3127 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | struct superblock_security_struct *sbsec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3129 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3130 | u32 newsid, sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | int rc = 0; |
| 3132 | |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3133 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3134 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 3135 | if (rc) |
| 3136 | return rc; |
| 3137 | |
| 3138 | /* Not an attribute we recognize, so just check the |
| 3139 | ordinary setattr permission. */ |
| 3140 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 3143 | if (!selinux_initialized(&selinux_state)) |
Jonathan Lebon | 3e3e24b | 2019-09-12 09:30:07 -0400 | [diff] [blame] | 3144 | return (inode_owner_or_capable(inode) ? 0 : -EPERM); |
| 3145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | sbsec = inode->i_sb->s_security; |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 3147 | if (!(sbsec->flags & SBLABEL_MNT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | return -EOPNOTSUPP; |
| 3149 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 3150 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | return -EPERM; |
| 3152 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3153 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 3154 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3156 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3157 | rc = avc_has_perm(&selinux_state, |
| 3158 | sid, isec->sid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | FILE__RELABELFROM, &ad); |
| 3160 | if (rc) |
| 3161 | return rc; |
| 3162 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3163 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3164 | GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3165 | if (rc == -EINVAL) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3166 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3167 | struct audit_buffer *ab; |
| 3168 | size_t audit_size; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3169 | |
| 3170 | /* We strip a nul only if it is at the end, otherwise the |
| 3171 | * context contains a nul and we should audit that */ |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3172 | if (value) { |
Colin Ian King | add2437 | 2017-10-14 13:46:55 +0100 | [diff] [blame] | 3173 | const char *str = value; |
| 3174 | |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3175 | if (str[size - 1] == '\0') |
| 3176 | audit_size = size - 1; |
| 3177 | else |
| 3178 | audit_size = size; |
| 3179 | } else { |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3180 | audit_size = 0; |
| 3181 | } |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 3182 | ab = audit_log_start(audit_context(), |
| 3183 | GFP_ATOMIC, AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3184 | audit_log_format(ab, "op=setxattr invalid_context="); |
| 3185 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 3186 | audit_log_end(ab); |
| 3187 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3188 | return rc; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3189 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3190 | rc = security_context_to_sid_force(&selinux_state, value, |
| 3191 | size, &newsid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | if (rc) |
| 3194 | return rc; |
| 3195 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3196 | rc = avc_has_perm(&selinux_state, |
| 3197 | sid, newsid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | FILE__RELABELTO, &ad); |
| 3199 | if (rc) |
| 3200 | return rc; |
| 3201 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3202 | rc = security_validate_transition(&selinux_state, isec->sid, newsid, |
| 3203 | sid, isec->sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | if (rc) |
| 3205 | return rc; |
| 3206 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3207 | return avc_has_perm(&selinux_state, |
| 3208 | newsid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | sbsec->sid, |
| 3210 | SECCLASS_FILESYSTEM, |
| 3211 | FILESYSTEM__ASSOCIATE, |
| 3212 | &ad); |
| 3213 | } |
| 3214 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3215 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 3216 | const void *value, size_t size, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3217 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3219 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3220 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | u32 newsid; |
| 3222 | int rc; |
| 3223 | |
| 3224 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3225 | /* Not an attribute we recognize, so nothing to do. */ |
| 3226 | return; |
| 3227 | } |
| 3228 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 3229 | if (!selinux_initialized(&selinux_state)) { |
Jonathan Lebon | 3e3e24b | 2019-09-12 09:30:07 -0400 | [diff] [blame] | 3230 | /* If we haven't even been initialized, then we can't validate |
| 3231 | * against a policy, so leave the label as invalid. It may |
| 3232 | * resolve to a valid label on the next revalidation try if |
| 3233 | * we've since initialized. |
| 3234 | */ |
| 3235 | return; |
| 3236 | } |
| 3237 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3238 | rc = security_context_to_sid_force(&selinux_state, value, size, |
| 3239 | &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 3241 | pr_err("SELinux: unable to map context to SID" |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3242 | "for (%s, %lu), rc=%d\n", |
| 3243 | inode->i_sb->s_id, inode->i_ino, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | return; |
| 3245 | } |
| 3246 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3247 | isec = backing_inode_security(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3248 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3249 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3251 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3252 | spin_unlock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | return; |
| 3255 | } |
| 3256 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3257 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3259 | const struct cred *cred = current_cred(); |
| 3260 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3261 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3264 | static int selinux_inode_listxattr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3266 | const struct cred *cred = current_cred(); |
| 3267 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3268 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | } |
| 3270 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3271 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | { |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3273 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3274 | int rc = cap_inode_removexattr(dentry, name); |
| 3275 | if (rc) |
| 3276 | return rc; |
| 3277 | |
| 3278 | /* Not an attribute we recognize, so just check the |
| 3279 | ordinary setattr permission. */ |
| 3280 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | |
Stephen Smalley | 9530a3e | 2020-08-20 13:00:40 -0400 | [diff] [blame] | 3283 | if (!selinux_initialized(&selinux_state)) |
| 3284 | return 0; |
| 3285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | /* No one is allowed to remove a SELinux security label. |
| 3287 | You can change the label, but all data must be labeled. */ |
| 3288 | return -EACCES; |
| 3289 | } |
| 3290 | |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 3291 | static int selinux_path_notify(const struct path *path, u64 mask, |
| 3292 | unsigned int obj_type) |
| 3293 | { |
| 3294 | int ret; |
| 3295 | u32 perm; |
| 3296 | |
| 3297 | struct common_audit_data ad; |
| 3298 | |
| 3299 | ad.type = LSM_AUDIT_DATA_PATH; |
| 3300 | ad.u.path = *path; |
| 3301 | |
| 3302 | /* |
| 3303 | * Set permission needed based on the type of mark being set. |
| 3304 | * Performs an additional check for sb watches. |
| 3305 | */ |
| 3306 | switch (obj_type) { |
| 3307 | case FSNOTIFY_OBJ_TYPE_VFSMOUNT: |
| 3308 | perm = FILE__WATCH_MOUNT; |
| 3309 | break; |
| 3310 | case FSNOTIFY_OBJ_TYPE_SB: |
| 3311 | perm = FILE__WATCH_SB; |
| 3312 | ret = superblock_has_perm(current_cred(), path->dentry->d_sb, |
| 3313 | FILESYSTEM__WATCH, &ad); |
| 3314 | if (ret) |
| 3315 | return ret; |
| 3316 | break; |
| 3317 | case FSNOTIFY_OBJ_TYPE_INODE: |
| 3318 | perm = FILE__WATCH; |
| 3319 | break; |
| 3320 | default: |
| 3321 | return -EINVAL; |
| 3322 | } |
| 3323 | |
| 3324 | /* blocking watches require the file:watch_with_perm permission */ |
| 3325 | if (mask & (ALL_FSNOTIFY_PERM_EVENTS)) |
| 3326 | perm |= FILE__WATCH_WITH_PERM; |
| 3327 | |
| 3328 | /* watches on read-like events need the file:watch_reads permission */ |
| 3329 | if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE)) |
| 3330 | perm |= FILE__WATCH_READS; |
| 3331 | |
| 3332 | return path_has_perm(current_cred(), path, perm); |
| 3333 | } |
| 3334 | |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3335 | /* |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3336 | * Copy the inode security context value to the user. |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3337 | * |
| 3338 | * Permission check is handled by selinux_inode_getxattr hook. |
| 3339 | */ |
Andreas Gruenbacher | ea861df | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3340 | 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] | 3341 | { |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3342 | u32 size; |
| 3343 | int error; |
| 3344 | char *context = NULL; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3345 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | |
Jonathan Lebon | c8e2226 | 2020-05-28 10:39:40 -0400 | [diff] [blame] | 3347 | /* |
| 3348 | * If we're not initialized yet, then we can't validate contexts, so |
| 3349 | * just let vfs_getxattr fall back to using the on-disk xattr. |
| 3350 | */ |
| 3351 | if (!selinux_initialized(&selinux_state) || |
| 3352 | strcmp(name, XATTR_SELINUX_SUFFIX)) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 3353 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3355 | /* |
| 3356 | * If the caller has CAP_MAC_ADMIN, then get the raw context |
| 3357 | * value even if it is not defined by current policy; otherwise, |
| 3358 | * use the in-core value under current policy. |
| 3359 | * Use the non-auditing forms of the permission checks since |
| 3360 | * getxattr may be called by unprivileged processes commonly |
| 3361 | * and lack of permission just means that we fall back to the |
| 3362 | * in-core context value, not a denial. |
| 3363 | */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3364 | isec = inode_security(inode); |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3365 | if (has_cap_mac_admin(false)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3366 | error = security_sid_to_context_force(&selinux_state, |
| 3367 | isec->sid, &context, |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3368 | &size); |
| 3369 | else |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3370 | error = security_sid_to_context(&selinux_state, isec->sid, |
| 3371 | &context, &size); |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3372 | if (error) |
| 3373 | return error; |
| 3374 | error = size; |
| 3375 | if (alloc) { |
| 3376 | *buffer = context; |
| 3377 | goto out_nofree; |
| 3378 | } |
| 3379 | kfree(context); |
| 3380 | out_nofree: |
| 3381 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3385 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | { |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 3387 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Ondrej Mosnacek | 53e0c2a | 2018-12-21 21:18:53 +0100 | [diff] [blame] | 3388 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | u32 newsid; |
| 3390 | int rc; |
| 3391 | |
| 3392 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3393 | return -EOPNOTSUPP; |
| 3394 | |
Ondrej Mosnacek | 53e0c2a | 2018-12-21 21:18:53 +0100 | [diff] [blame] | 3395 | if (!(sbsec->flags & SBLABEL_MNT)) |
| 3396 | return -EOPNOTSUPP; |
| 3397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | if (!value || !size) |
| 3399 | return -EACCES; |
| 3400 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3401 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3402 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | if (rc) |
| 3404 | return rc; |
| 3405 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3406 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3407 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3409 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3410 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | return 0; |
| 3412 | } |
| 3413 | |
| 3414 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) |
| 3415 | { |
| 3416 | const int len = sizeof(XATTR_NAME_SELINUX); |
| 3417 | if (buffer && len <= buffer_size) |
| 3418 | memcpy(buffer, XATTR_NAME_SELINUX, len); |
| 3419 | return len; |
| 3420 | } |
| 3421 | |
Andreas Gruenbacher | d6335d7 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3422 | static void selinux_inode_getsecid(struct inode *inode, u32 *secid) |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3423 | { |
Andreas Gruenbacher | e817c2f | 2016-02-18 12:04:08 +0100 | [diff] [blame] | 3424 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3425 | *secid = isec->sid; |
| 3426 | } |
| 3427 | |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 3428 | static int selinux_inode_copy_up(struct dentry *src, struct cred **new) |
| 3429 | { |
| 3430 | u32 sid; |
| 3431 | struct task_security_struct *tsec; |
| 3432 | struct cred *new_creds = *new; |
| 3433 | |
| 3434 | if (new_creds == NULL) { |
| 3435 | new_creds = prepare_creds(); |
| 3436 | if (!new_creds) |
| 3437 | return -ENOMEM; |
| 3438 | } |
| 3439 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3440 | tsec = selinux_cred(new_creds); |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 3441 | /* Get label from overlay inode and set it in create_sid */ |
| 3442 | selinux_inode_getsecid(d_inode(src), &sid); |
| 3443 | tsec->create_sid = sid; |
| 3444 | *new = new_creds; |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 3448 | static int selinux_inode_copy_up_xattr(const char *name) |
| 3449 | { |
| 3450 | /* The copy_up hook above sets the initial context on an inode, but we |
| 3451 | * don't then want to overwrite it by blindly copying all the lower |
| 3452 | * xattrs up. Instead, we have to filter out SELinux-related xattrs. |
| 3453 | */ |
| 3454 | if (strcmp(name, XATTR_NAME_SELINUX) == 0) |
| 3455 | return 1; /* Discard */ |
| 3456 | /* |
| 3457 | * Any other attribute apart from SELINUX is not claimed, supported |
| 3458 | * by selinux. |
| 3459 | */ |
| 3460 | return -EOPNOTSUPP; |
| 3461 | } |
| 3462 | |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3463 | /* kernfs node operations */ |
| 3464 | |
YueHaibing | c72c4cd | 2019-03-22 22:04:00 +0800 | [diff] [blame] | 3465 | static int selinux_kernfs_init_security(struct kernfs_node *kn_dir, |
| 3466 | struct kernfs_node *kn) |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3467 | { |
Stephen Smalley | 169ce0c | 2019-09-04 10:32:48 -0400 | [diff] [blame] | 3468 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3469 | u32 parent_sid, newsid, clen; |
| 3470 | int rc; |
| 3471 | char *context; |
| 3472 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 3473 | rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3474 | if (rc == -ENODATA) |
| 3475 | return 0; |
| 3476 | else if (rc < 0) |
| 3477 | return rc; |
| 3478 | |
| 3479 | clen = (u32)rc; |
| 3480 | context = kmalloc(clen, GFP_KERNEL); |
| 3481 | if (!context) |
| 3482 | return -ENOMEM; |
| 3483 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 3484 | rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3485 | if (rc < 0) { |
| 3486 | kfree(context); |
| 3487 | return rc; |
| 3488 | } |
| 3489 | |
| 3490 | rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid, |
| 3491 | GFP_KERNEL); |
| 3492 | kfree(context); |
| 3493 | if (rc) |
| 3494 | return rc; |
| 3495 | |
| 3496 | if (tsec->create_sid) { |
| 3497 | newsid = tsec->create_sid; |
| 3498 | } else { |
| 3499 | u16 secclass = inode_mode_to_security_class(kn->mode); |
| 3500 | struct qstr q; |
| 3501 | |
| 3502 | q.name = kn->name; |
| 3503 | q.hash_len = hashlen_string(kn_dir, kn->name); |
| 3504 | |
| 3505 | rc = security_transition_sid(&selinux_state, tsec->sid, |
| 3506 | parent_sid, secclass, &q, |
| 3507 | &newsid); |
| 3508 | if (rc) |
| 3509 | return rc; |
| 3510 | } |
| 3511 | |
| 3512 | rc = security_sid_to_context_force(&selinux_state, newsid, |
| 3513 | &context, &clen); |
| 3514 | if (rc) |
| 3515 | return rc; |
| 3516 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 3517 | rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen, |
| 3518 | XATTR_CREATE); |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 3519 | kfree(context); |
| 3520 | return rc; |
| 3521 | } |
| 3522 | |
| 3523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | /* file security operations */ |
| 3525 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3526 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3528 | const struct cred *cred = current_cred(); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3529 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
| 3532 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) |
| 3533 | mask |= MAY_APPEND; |
| 3534 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3535 | return file_has_perm(cred, file, |
| 3536 | file_mask_to_av(inode->i_mode, mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3539 | static int selinux_file_permission(struct file *file, int mask) |
| 3540 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3541 | struct inode *inode = file_inode(file); |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3542 | struct file_security_struct *fsec = selinux_file(file); |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3543 | struct inode_security_struct *isec; |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3544 | u32 sid = current_sid(); |
| 3545 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3546 | if (!mask) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3547 | /* No permission to check. Existence test. */ |
| 3548 | return 0; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3549 | |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3550 | isec = inode_security(inode); |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3551 | if (sid == fsec->sid && fsec->isid == isec->sid && |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3552 | fsec->pseqno == avc_policy_seqno(&selinux_state)) |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 3553 | /* No change since file_open check. */ |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3554 | return 0; |
| 3555 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3556 | return selinux_revalidate_file_permission(file, mask); |
| 3557 | } |
| 3558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | static int selinux_file_alloc_security(struct file *file) |
| 3560 | { |
Paul Moore | cb89e24 | 2020-01-10 16:32:10 -0500 | [diff] [blame] | 3561 | struct file_security_struct *fsec = selinux_file(file); |
| 3562 | u32 sid = current_sid(); |
| 3563 | |
| 3564 | fsec->sid = sid; |
| 3565 | fsec->fown_sid = sid; |
| 3566 | |
| 3567 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | } |
| 3569 | |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3570 | /* |
| 3571 | * Check whether a task has the ioctl permission and cmd |
| 3572 | * operation to an inode. |
| 3573 | */ |
Geliang Tang | 1d2a168 | 2015-10-21 17:44:27 -0400 | [diff] [blame] | 3574 | 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] | 3575 | u32 requested, u16 cmd) |
| 3576 | { |
| 3577 | struct common_audit_data ad; |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3578 | struct file_security_struct *fsec = selinux_file(file); |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3579 | struct inode *inode = file_inode(file); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3580 | struct inode_security_struct *isec; |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3581 | struct lsm_ioctlop_audit ioctl; |
| 3582 | u32 ssid = cred_sid(cred); |
| 3583 | int rc; |
| 3584 | u8 driver = cmd >> 8; |
| 3585 | u8 xperm = cmd & 0xff; |
| 3586 | |
| 3587 | ad.type = LSM_AUDIT_DATA_IOCTL_OP; |
| 3588 | ad.u.op = &ioctl; |
| 3589 | ad.u.op->cmd = cmd; |
| 3590 | ad.u.op->path = file->f_path; |
| 3591 | |
| 3592 | if (ssid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3593 | rc = avc_has_perm(&selinux_state, |
| 3594 | ssid, fsec->sid, |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3595 | SECCLASS_FD, |
| 3596 | FD__USE, |
| 3597 | &ad); |
| 3598 | if (rc) |
| 3599 | goto out; |
| 3600 | } |
| 3601 | |
| 3602 | if (unlikely(IS_PRIVATE(inode))) |
| 3603 | return 0; |
| 3604 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3605 | isec = inode_security(inode); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3606 | rc = avc_has_extended_perms(&selinux_state, |
| 3607 | ssid, isec->sid, isec->sclass, |
| 3608 | requested, driver, xperm, &ad); |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3609 | out: |
| 3610 | return rc; |
| 3611 | } |
| 3612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, |
| 3614 | unsigned long arg) |
| 3615 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3616 | const struct cred *cred = current_cred(); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3617 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3619 | switch (cmd) { |
| 3620 | case FIONREAD: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3621 | case FIBMAP: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3622 | case FIGETBSZ: |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3623 | case FS_IOC_GETFLAGS: |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3624 | case FS_IOC_GETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3625 | error = file_has_perm(cred, file, FILE__GETATTR); |
| 3626 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3628 | case FS_IOC_SETFLAGS: |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3629 | case FS_IOC_SETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3630 | error = file_has_perm(cred, file, FILE__SETATTR); |
| 3631 | break; |
| 3632 | |
| 3633 | /* sys_ioctl() checks */ |
| 3634 | case FIONBIO: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3635 | case FIOASYNC: |
| 3636 | error = file_has_perm(cred, file, 0); |
| 3637 | break; |
| 3638 | |
| 3639 | case KDSKBENT: |
| 3640 | case KDSKBSENT: |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3641 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 3642 | CAP_OPT_NONE, true); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3643 | break; |
| 3644 | |
| 3645 | /* default case assumes that the command will go |
| 3646 | * to the file's ioctl() function. |
| 3647 | */ |
| 3648 | default: |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3649 | error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3650 | } |
| 3651 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | } |
| 3653 | |
Stephen Smalley | b78b7d5 | 2020-01-08 12:23:56 -0500 | [diff] [blame] | 3654 | static int default_noexec __ro_after_init; |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
| 3657 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3658 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3659 | u32 sid = cred_sid(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3660 | int rc = 0; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3661 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3662 | if (default_noexec && |
Stephen Smalley | 892e8ca | 2015-07-10 09:40:59 -0400 | [diff] [blame] | 3663 | (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || |
| 3664 | (!shared && (prot & PROT_WRITE)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | /* |
| 3666 | * We are making executable an anonymous mapping or a |
| 3667 | * private file mapping that will also be writable. |
| 3668 | * This has an additional check. |
| 3669 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3670 | rc = avc_has_perm(&selinux_state, |
| 3671 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3672 | PROCESS__EXECMEM, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | if (rc) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3674 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | |
| 3677 | if (file) { |
| 3678 | /* read access is always possible with a mapping */ |
| 3679 | u32 av = FILE__READ; |
| 3680 | |
| 3681 | /* write access only matters if the mapping is shared */ |
| 3682 | if (shared && (prot & PROT_WRITE)) |
| 3683 | av |= FILE__WRITE; |
| 3684 | |
| 3685 | if (prot & PROT_EXEC) |
| 3686 | av |= FILE__EXECUTE; |
| 3687 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3688 | return file_has_perm(cred, file, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3689 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3690 | |
| 3691 | error: |
| 3692 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3695 | static int selinux_mmap_addr(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 3697 | int rc = 0; |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3698 | |
| 3699 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
| 3700 | u32 sid = current_sid(); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3701 | rc = avc_has_perm(&selinux_state, |
| 3702 | sid, sid, SECCLASS_MEMPROTECT, |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3703 | MEMPROTECT__MMAP_ZERO, NULL); |
| 3704 | } |
| 3705 | |
| 3706 | return rc; |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3707 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3709 | static int selinux_mmap_file(struct file *file, unsigned long reqprot, |
| 3710 | unsigned long prot, unsigned long flags) |
| 3711 | { |
Stephen Smalley | 3ba4bf5 | 2017-05-05 09:14:48 -0400 | [diff] [blame] | 3712 | struct common_audit_data ad; |
| 3713 | int rc; |
| 3714 | |
| 3715 | if (file) { |
| 3716 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3717 | ad.u.file = file; |
| 3718 | rc = inode_has_perm(current_cred(), file_inode(file), |
| 3719 | FILE__MAP, &ad); |
| 3720 | if (rc) |
| 3721 | return rc; |
| 3722 | } |
| 3723 | |
Lakshmi Ramasubramanian | 8861d0a | 2020-09-14 10:31:57 -0700 | [diff] [blame] | 3724 | if (checkreqprot_get(&selinux_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | prot = reqprot; |
| 3726 | |
| 3727 | return file_map_prot_check(file, prot, |
| 3728 | (flags & MAP_TYPE) == MAP_SHARED); |
| 3729 | } |
| 3730 | |
| 3731 | static int selinux_file_mprotect(struct vm_area_struct *vma, |
| 3732 | unsigned long reqprot, |
| 3733 | unsigned long prot) |
| 3734 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3735 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3736 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | |
Lakshmi Ramasubramanian | 8861d0a | 2020-09-14 10:31:57 -0700 | [diff] [blame] | 3738 | if (checkreqprot_get(&selinux_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | prot = reqprot; |
| 3740 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3741 | if (default_noexec && |
| 3742 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { |
James Morris | d541bbe | 2009-01-29 12:19:51 +1100 | [diff] [blame] | 3743 | int rc = 0; |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3744 | if (vma->vm_start >= vma->vm_mm->start_brk && |
| 3745 | vma->vm_end <= vma->vm_mm->brk) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3746 | rc = avc_has_perm(&selinux_state, |
| 3747 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3748 | PROCESS__EXECHEAP, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3749 | } else if (!vma->vm_file && |
Stephen Smalley | c2316db | 2016-04-08 13:55:03 -0400 | [diff] [blame] | 3750 | ((vma->vm_start <= vma->vm_mm->start_stack && |
| 3751 | vma->vm_end >= vma->vm_mm->start_stack) || |
Andy Lutomirski | d17af50 | 2016-09-30 10:58:58 -0700 | [diff] [blame] | 3752 | vma_is_stack_for_current(vma))) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3753 | rc = avc_has_perm(&selinux_state, |
| 3754 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3755 | PROCESS__EXECSTACK, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3756 | } else if (vma->vm_file && vma->anon_vma) { |
| 3757 | /* |
| 3758 | * We are making executable a file mapping that has |
| 3759 | * had some COW done. Since pages might have been |
| 3760 | * written, check ability to execute the possibly |
| 3761 | * modified content. This typically should only |
| 3762 | * occur for text relocations. |
| 3763 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3764 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3765 | } |
Lorenzo Hernandez GarcÃa-Hierro | 6b99219 | 2005-06-25 14:54:34 -0700 | [diff] [blame] | 3766 | if (rc) |
| 3767 | return rc; |
| 3768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | |
| 3770 | return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); |
| 3771 | } |
| 3772 | |
| 3773 | static int selinux_file_lock(struct file *file, unsigned int cmd) |
| 3774 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3775 | const struct cred *cred = current_cred(); |
| 3776 | |
| 3777 | return file_has_perm(cred, file, FILE__LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | static int selinux_file_fcntl(struct file *file, unsigned int cmd, |
| 3781 | unsigned long arg) |
| 3782 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3783 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | int err = 0; |
| 3785 | |
| 3786 | switch (cmd) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3787 | case F_SETFL: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3788 | if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3789 | err = file_has_perm(cred, file, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3791 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3792 | fallthrough; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3793 | case F_SETOWN: |
| 3794 | case F_SETSIG: |
| 3795 | case F_GETFL: |
| 3796 | case F_GETOWN: |
| 3797 | case F_GETSIG: |
Cyrill Gorcunov | 1d151c3 | 2012-07-30 14:43:00 -0700 | [diff] [blame] | 3798 | case F_GETOWNER_UIDS: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3799 | /* Just check FD__USE permission */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3800 | err = file_has_perm(cred, file, 0); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3801 | break; |
| 3802 | case F_GETLK: |
| 3803 | case F_SETLK: |
| 3804 | case F_SETLKW: |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 3805 | case F_OFD_GETLK: |
| 3806 | case F_OFD_SETLK: |
| 3807 | case F_OFD_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | #if BITS_PER_LONG == 32 |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3809 | case F_GETLK64: |
| 3810 | case F_SETLK64: |
| 3811 | case F_SETLKW64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | #endif |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3813 | err = file_has_perm(cred, file, FILE__LOCK); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3814 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | return err; |
| 3818 | } |
| 3819 | |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 3820 | static void selinux_file_set_fowner(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | struct file_security_struct *fsec; |
| 3823 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3824 | fsec = selinux_file(file); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3825 | fsec->fown_sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | static int selinux_file_send_sigiotask(struct task_struct *tsk, |
| 3829 | struct fown_struct *fown, int signum) |
| 3830 | { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3831 | struct file *file; |
Stephen Smalley | 65c90bc | 2009-05-04 15:43:18 -0400 | [diff] [blame] | 3832 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | struct file_security_struct *fsec; |
| 3835 | |
| 3836 | /* struct fown_struct is never outside the context of a struct file */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3837 | file = container_of(fown, struct file, f_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3839 | fsec = selinux_file(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | |
| 3841 | if (!signum) |
| 3842 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| 3843 | else |
| 3844 | perm = signal_to_av(signum); |
| 3845 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3846 | return avc_has_perm(&selinux_state, |
| 3847 | fsec->fown_sid, sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | SECCLASS_PROCESS, perm, NULL); |
| 3849 | } |
| 3850 | |
| 3851 | static int selinux_file_receive(struct file *file) |
| 3852 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3853 | const struct cred *cred = current_cred(); |
| 3854 | |
| 3855 | return file_has_perm(cred, file, file_to_av(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | } |
| 3857 | |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3858 | static int selinux_file_open(struct file *file) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3859 | { |
| 3860 | struct file_security_struct *fsec; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3861 | struct inode_security_struct *isec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3862 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 3863 | fsec = selinux_file(file); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3864 | isec = inode_security(file_inode(file)); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3865 | /* |
| 3866 | * Save inode label and policy sequence number |
| 3867 | * at open-time so that selinux_file_permission |
| 3868 | * can determine whether revalidation is necessary. |
| 3869 | * Task label is already saved in the file security |
| 3870 | * struct as its SID. |
| 3871 | */ |
| 3872 | fsec->isid = isec->sid; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3873 | fsec->pseqno = avc_policy_seqno(&selinux_state); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3874 | /* |
| 3875 | * Since the inode label or policy seqno may have changed |
| 3876 | * between the selinux_inode_permission check and the saving |
| 3877 | * of state above, recheck that access is still permitted. |
| 3878 | * Otherwise, access might never be revalidated against the |
| 3879 | * new inode label or new policy. |
| 3880 | * This check is not redundant - do not remove. |
| 3881 | */ |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3882 | 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] | 3883 | } |
| 3884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | /* task security operations */ |
| 3886 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 3887 | static int selinux_task_alloc(struct task_struct *task, |
| 3888 | unsigned long clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3890 | u32 sid = current_sid(); |
| 3891 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3892 | return avc_has_perm(&selinux_state, |
| 3893 | sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3896 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3897 | * prepare a new set of credentials for modification |
| 3898 | */ |
| 3899 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3900 | gfp_t gfp) |
| 3901 | { |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 3902 | const struct task_security_struct *old_tsec = selinux_cred(old); |
| 3903 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3904 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 3905 | *tsec = *old_tsec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3906 | return 0; |
| 3907 | } |
| 3908 | |
| 3909 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3910 | * transfer the SELinux data to a blank set of creds |
| 3911 | */ |
| 3912 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3913 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3914 | const struct task_security_struct *old_tsec = selinux_cred(old); |
| 3915 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3916 | |
| 3917 | *tsec = *old_tsec; |
| 3918 | } |
| 3919 | |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 3920 | static void selinux_cred_getsecid(const struct cred *c, u32 *secid) |
| 3921 | { |
| 3922 | *secid = cred_sid(c); |
| 3923 | } |
| 3924 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3925 | /* |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3926 | * set the security data for a kernel service |
| 3927 | * - all the creation contexts are set to unlabelled |
| 3928 | */ |
| 3929 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3930 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3931 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3932 | u32 sid = current_sid(); |
| 3933 | int ret; |
| 3934 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3935 | ret = avc_has_perm(&selinux_state, |
| 3936 | sid, secid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3937 | SECCLASS_KERNEL_SERVICE, |
| 3938 | KERNEL_SERVICE__USE_AS_OVERRIDE, |
| 3939 | NULL); |
| 3940 | if (ret == 0) { |
| 3941 | tsec->sid = secid; |
| 3942 | tsec->create_sid = 0; |
| 3943 | tsec->keycreate_sid = 0; |
| 3944 | tsec->sockcreate_sid = 0; |
| 3945 | } |
| 3946 | return ret; |
| 3947 | } |
| 3948 | |
| 3949 | /* |
| 3950 | * set the file creation context in a security record to the same as the |
| 3951 | * objective context of the specified inode |
| 3952 | */ |
| 3953 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3954 | { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3955 | struct inode_security_struct *isec = inode_security(inode); |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 3956 | struct task_security_struct *tsec = selinux_cred(new); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3957 | u32 sid = current_sid(); |
| 3958 | int ret; |
| 3959 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3960 | ret = avc_has_perm(&selinux_state, |
| 3961 | sid, isec->sid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3962 | SECCLASS_KERNEL_SERVICE, |
| 3963 | KERNEL_SERVICE__CREATE_FILES_AS, |
| 3964 | NULL); |
| 3965 | |
| 3966 | if (ret == 0) |
| 3967 | tsec->create_sid = isec->sid; |
David Howells | ef57471 | 2010-02-26 01:56:16 +0000 | [diff] [blame] | 3968 | return ret; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3969 | } |
| 3970 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3971 | static int selinux_kernel_module_request(char *kmod_name) |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3972 | { |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3973 | struct common_audit_data ad; |
| 3974 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3975 | ad.type = LSM_AUDIT_DATA_KMOD; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3976 | ad.u.kmod_name = kmod_name; |
| 3977 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3978 | return avc_has_perm(&selinux_state, |
| 3979 | current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3980 | SYSTEM__MODULE_REQUEST, &ad); |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3981 | } |
| 3982 | |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3983 | static int selinux_kernel_module_from_file(struct file *file) |
| 3984 | { |
| 3985 | struct common_audit_data ad; |
| 3986 | struct inode_security_struct *isec; |
| 3987 | struct file_security_struct *fsec; |
| 3988 | u32 sid = current_sid(); |
| 3989 | int rc; |
| 3990 | |
| 3991 | /* init_module */ |
| 3992 | if (file == NULL) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3993 | return avc_has_perm(&selinux_state, |
| 3994 | sid, sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3995 | SYSTEM__MODULE_LOAD, NULL); |
| 3996 | |
| 3997 | /* finit_module */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3998 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 3999 | ad.type = LSM_AUDIT_DATA_FILE; |
| 4000 | ad.u.file = file; |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 4001 | |
Casey Schaufler | bb6c6b0 | 2018-09-21 17:22:32 -0700 | [diff] [blame] | 4002 | fsec = selinux_file(file); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 4003 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4004 | rc = avc_has_perm(&selinux_state, |
| 4005 | sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 4006 | if (rc) |
| 4007 | return rc; |
| 4008 | } |
| 4009 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 4010 | isec = inode_security(file_inode(file)); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4011 | return avc_has_perm(&selinux_state, |
| 4012 | sid, isec->sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 4013 | SYSTEM__MODULE_LOAD, &ad); |
| 4014 | } |
| 4015 | |
| 4016 | static int selinux_kernel_read_file(struct file *file, |
Kees Cook | 2039bda | 2020-10-02 10:38:23 -0700 | [diff] [blame] | 4017 | enum kernel_read_file_id id, |
| 4018 | bool contents) |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 4019 | { |
| 4020 | int rc = 0; |
| 4021 | |
| 4022 | switch (id) { |
| 4023 | case READING_MODULE: |
Kees Cook | 2039bda | 2020-10-02 10:38:23 -0700 | [diff] [blame] | 4024 | rc = selinux_kernel_module_from_file(contents ? file : NULL); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 4025 | break; |
| 4026 | default: |
| 4027 | break; |
| 4028 | } |
| 4029 | |
| 4030 | return rc; |
| 4031 | } |
| 4032 | |
Kees Cook | b64fcae | 2020-10-02 10:38:20 -0700 | [diff] [blame] | 4033 | static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents) |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 4034 | { |
| 4035 | int rc = 0; |
| 4036 | |
| 4037 | switch (id) { |
| 4038 | case LOADING_MODULE: |
| 4039 | rc = selinux_kernel_module_from_file(NULL); |
Gustavo A. R. Silva | b2d99bc | 2020-11-20 12:32:26 -0600 | [diff] [blame] | 4040 | break; |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 4041 | default: |
| 4042 | break; |
| 4043 | } |
| 4044 | |
| 4045 | return rc; |
| 4046 | } |
| 4047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) |
| 4049 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4050 | return avc_has_perm(&selinux_state, |
| 4051 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4052 | PROCESS__SETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | static int selinux_task_getpgid(struct task_struct *p) |
| 4056 | { |
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), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4059 | PROCESS__GETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | static int selinux_task_getsid(struct task_struct *p) |
| 4063 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4064 | return avc_has_perm(&selinux_state, |
| 4065 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4066 | PROCESS__GETSESSION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | } |
| 4068 | |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 4069 | static void selinux_task_getsecid(struct task_struct *p, u32 *secid) |
| 4070 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4071 | *secid = task_sid(p); |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 4072 | } |
| 4073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | static int selinux_task_setnice(struct task_struct *p, int nice) |
| 4075 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4076 | return avc_has_perm(&selinux_state, |
| 4077 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4078 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 4081 | static int selinux_task_setioprio(struct task_struct *p, int ioprio) |
| 4082 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4083 | return avc_has_perm(&selinux_state, |
| 4084 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4085 | PROCESS__SETSCHED, NULL); |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 4088 | static int selinux_task_getioprio(struct task_struct *p) |
| 4089 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4090 | return avc_has_perm(&selinux_state, |
| 4091 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4092 | PROCESS__GETSCHED, NULL); |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 4093 | } |
| 4094 | |
Corentin LABBE | 4298555 | 2017-10-04 20:32:18 +0200 | [diff] [blame] | 4095 | static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, |
| 4096 | unsigned int flags) |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 4097 | { |
| 4098 | u32 av = 0; |
| 4099 | |
Stephen Smalley | 84e6885 | 2017-02-28 09:35:08 -0500 | [diff] [blame] | 4100 | if (!flags) |
| 4101 | return 0; |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 4102 | if (flags & LSM_PRLIMIT_WRITE) |
| 4103 | av |= PROCESS__SETRLIMIT; |
| 4104 | if (flags & LSM_PRLIMIT_READ) |
| 4105 | av |= PROCESS__GETRLIMIT; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4106 | return avc_has_perm(&selinux_state, |
| 4107 | cred_sid(cred), cred_sid(tcred), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 4108 | SECCLASS_PROCESS, av, NULL); |
| 4109 | } |
| 4110 | |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 4111 | static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, |
| 4112 | struct rlimit *new_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | { |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 4114 | struct rlimit *old_rlim = p->signal->rlim + resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | |
| 4116 | /* Control the ability to change the hard limit (whether |
| 4117 | lowering or raising it), so that the hard limit can |
| 4118 | later be used as a safe reset point for the soft limit |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 4119 | upon context transitions. See selinux_bprm_committing_creds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | if (old_rlim->rlim_max != new_rlim->rlim_max) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4121 | return avc_has_perm(&selinux_state, |
| 4122 | current_sid(), task_sid(p), |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4123 | SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | |
| 4125 | return 0; |
| 4126 | } |
| 4127 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 4128 | static int selinux_task_setscheduler(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4130 | return avc_has_perm(&selinux_state, |
| 4131 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4132 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | static int selinux_task_getscheduler(struct task_struct *p) |
| 4136 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4137 | return avc_has_perm(&selinux_state, |
| 4138 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4139 | PROCESS__GETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 4142 | static int selinux_task_movememory(struct task_struct *p) |
| 4143 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4144 | return avc_has_perm(&selinux_state, |
| 4145 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4146 | PROCESS__SETSCHED, NULL); |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 4147 | } |
| 4148 | |
Eric W. Biederman | ae7795b | 2018-09-25 11:27:20 +0200 | [diff] [blame] | 4149 | 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] | 4150 | int sig, const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | { |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4152 | u32 secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | if (!sig) |
| 4156 | perm = PROCESS__SIGNULL; /* null signal; existence test */ |
| 4157 | else |
| 4158 | perm = signal_to_av(sig); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4159 | if (!cred) |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4160 | secid = current_sid(); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 4161 | else |
| 4162 | secid = cred_sid(cred); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4163 | return avc_has_perm(&selinux_state, |
| 4164 | secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | static void selinux_task_to_inode(struct task_struct *p, |
| 4168 | struct inode *inode) |
| 4169 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 4170 | struct inode_security_struct *isec = selinux_inode(inode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4171 | u32 sid = task_sid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4173 | spin_lock(&isec->lock); |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 4174 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4175 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4176 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4177 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | } |
| 4179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4181 | static int selinux_parse_skb_ipv4(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4182 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | { |
| 4184 | int offset, ihlen, ret = -EINVAL; |
| 4185 | struct iphdr _iph, *ih; |
| 4186 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4187 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); |
| 4189 | if (ih == NULL) |
| 4190 | goto out; |
| 4191 | |
| 4192 | ihlen = ih->ihl * 4; |
| 4193 | if (ihlen < sizeof(_iph)) |
| 4194 | goto out; |
| 4195 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4196 | ad->u.net->v4info.saddr = ih->saddr; |
| 4197 | ad->u.net->v4info.daddr = ih->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | ret = 0; |
| 4199 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4200 | if (proto) |
| 4201 | *proto = ih->protocol; |
| 4202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | switch (ih->protocol) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4204 | case IPPROTO_TCP: { |
| 4205 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4207 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4208 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | |
| 4210 | offset += ihlen; |
| 4211 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4212 | if (th == NULL) |
| 4213 | break; |
| 4214 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4215 | ad->u.net->sport = th->source; |
| 4216 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4220 | case IPPROTO_UDP: { |
| 4221 | struct udphdr _udph, *uh; |
| 4222 | |
| 4223 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4224 | break; |
| 4225 | |
| 4226 | offset += ihlen; |
| 4227 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4228 | if (uh == NULL) |
| 4229 | break; |
| 4230 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4231 | ad->u.net->sport = uh->source; |
| 4232 | ad->u.net->dport = uh->dest; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4233 | break; |
| 4234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4236 | case IPPROTO_DCCP: { |
| 4237 | struct dccp_hdr _dccph, *dh; |
| 4238 | |
| 4239 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4240 | break; |
| 4241 | |
| 4242 | offset += ihlen; |
| 4243 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4244 | if (dh == NULL) |
| 4245 | break; |
| 4246 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4247 | ad->u.net->sport = dh->dccph_sport; |
| 4248 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4249 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4250 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4251 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4252 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4253 | case IPPROTO_SCTP: { |
| 4254 | struct sctphdr _sctph, *sh; |
| 4255 | |
| 4256 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4257 | break; |
| 4258 | |
| 4259 | offset += ihlen; |
| 4260 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4261 | if (sh == NULL) |
| 4262 | break; |
| 4263 | |
| 4264 | ad->u.net->sport = sh->source; |
| 4265 | ad->u.net->dport = sh->dest; |
| 4266 | break; |
| 4267 | } |
| 4268 | #endif |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4269 | default: |
| 4270 | break; |
| 4271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | out: |
| 4273 | return ret; |
| 4274 | } |
| 4275 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4276 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | |
| 4278 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4279 | static int selinux_parse_skb_ipv6(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4280 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | { |
| 4282 | u8 nexthdr; |
| 4283 | int ret = -EINVAL, offset; |
| 4284 | struct ipv6hdr _ipv6h, *ip6; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4285 | __be16 frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4287 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 4289 | if (ip6 == NULL) |
| 4290 | goto out; |
| 4291 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4292 | ad->u.net->v6info.saddr = ip6->saddr; |
| 4293 | ad->u.net->v6info.daddr = ip6->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | ret = 0; |
| 4295 | |
| 4296 | nexthdr = ip6->nexthdr; |
| 4297 | offset += sizeof(_ipv6h); |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4298 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | if (offset < 0) |
| 4300 | goto out; |
| 4301 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4302 | if (proto) |
| 4303 | *proto = nexthdr; |
| 4304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | switch (nexthdr) { |
| 4306 | case IPPROTO_TCP: { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4307 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | |
| 4309 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4310 | if (th == NULL) |
| 4311 | break; |
| 4312 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4313 | ad->u.net->sport = th->source; |
| 4314 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | break; |
| 4316 | } |
| 4317 | |
| 4318 | case IPPROTO_UDP: { |
| 4319 | struct udphdr _udph, *uh; |
| 4320 | |
| 4321 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4322 | if (uh == NULL) |
| 4323 | break; |
| 4324 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4325 | ad->u.net->sport = uh->source; |
| 4326 | ad->u.net->dport = uh->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | break; |
| 4328 | } |
| 4329 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4330 | case IPPROTO_DCCP: { |
| 4331 | struct dccp_hdr _dccph, *dh; |
| 4332 | |
| 4333 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4334 | if (dh == NULL) |
| 4335 | break; |
| 4336 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4337 | ad->u.net->sport = dh->dccph_sport; |
| 4338 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4339 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4340 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4341 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4342 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4343 | case IPPROTO_SCTP: { |
| 4344 | struct sctphdr _sctph, *sh; |
| 4345 | |
| 4346 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4347 | if (sh == NULL) |
| 4348 | break; |
| 4349 | |
| 4350 | ad->u.net->sport = sh->source; |
| 4351 | ad->u.net->dport = sh->dest; |
| 4352 | break; |
| 4353 | } |
| 4354 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | /* includes fragments */ |
| 4356 | default: |
| 4357 | break; |
| 4358 | } |
| 4359 | out: |
| 4360 | return ret; |
| 4361 | } |
| 4362 | |
| 4363 | #endif /* IPV6 */ |
| 4364 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4365 | 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] | 4366 | char **_addrp, int src, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | { |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4368 | char *addrp; |
| 4369 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4371 | switch (ad->u.net->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | case PF_INET: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4373 | ret = selinux_parse_skb_ipv4(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4374 | if (ret) |
| 4375 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4376 | addrp = (char *)(src ? &ad->u.net->v4info.saddr : |
| 4377 | &ad->u.net->v4info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4378 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4380 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | case PF_INET6: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4382 | ret = selinux_parse_skb_ipv6(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4383 | if (ret) |
| 4384 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4385 | addrp = (char *)(src ? &ad->u.net->v6info.saddr : |
| 4386 | &ad->u.net->v6info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4387 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | #endif /* IPV6 */ |
| 4389 | default: |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4390 | addrp = NULL; |
| 4391 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4394 | parse_error: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4395 | pr_warn( |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4396 | "SELinux: failure in selinux_parse_skb()," |
| 4397 | " unable to parse packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | return ret; |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4399 | |
| 4400 | okay: |
| 4401 | if (_addrp) |
| 4402 | *_addrp = addrp; |
| 4403 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4406 | /** |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4407 | * selinux_skb_peerlbl_sid - Determine the peer label of a packet |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4408 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4409 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4410 | * @sid: the packet's peer label SID |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4411 | * |
| 4412 | * Description: |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4413 | * Check the various different forms of network peer labeling and determine |
| 4414 | * the peer label/SID for the packet; most of the magic actually occurs in |
| 4415 | * the security server function security_net_peersid_cmp(). The function |
| 4416 | * returns zero if the value in @sid is valid (although it may be SECSID_NULL) |
| 4417 | * or -EACCES if @sid is invalid due to inconsistencies with the different |
| 4418 | * peer labels. |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4419 | * |
| 4420 | */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4421 | 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] | 4422 | { |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4423 | int err; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4424 | u32 xfrm_sid; |
| 4425 | u32 nlbl_sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4426 | u32 nlbl_type; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4427 | |
Paul Moore | 817eff7 | 2013-12-10 14:57:54 -0500 | [diff] [blame] | 4428 | err = selinux_xfrm_skb_sid(skb, &xfrm_sid); |
Paul Moore | bed4d7e | 2013-07-23 17:38:40 -0400 | [diff] [blame] | 4429 | if (unlikely(err)) |
| 4430 | return -EACCES; |
| 4431 | err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); |
| 4432 | if (unlikely(err)) |
| 4433 | return -EACCES; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4434 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4435 | err = security_net_peersid_resolve(&selinux_state, nlbl_sid, |
| 4436 | nlbl_type, xfrm_sid, sid); |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4437 | if (unlikely(err)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4438 | pr_warn( |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4439 | "SELinux: failure in selinux_skb_peerlbl_sid()," |
| 4440 | " unable to determine packet's peer label\n"); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4441 | return -EACCES; |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4442 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4443 | |
| 4444 | return 0; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4445 | } |
| 4446 | |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4447 | /** |
| 4448 | * selinux_conn_sid - Determine the child socket label for a connection |
| 4449 | * @sk_sid: the parent socket's SID |
| 4450 | * @skb_sid: the packet's SID |
| 4451 | * @conn_sid: the resulting connection SID |
| 4452 | * |
| 4453 | * If @skb_sid is valid then the user:role:type information from @sk_sid is |
| 4454 | * combined with the MLS information from @skb_sid in order to create |
Randy Dunlap | c76a2f9 | 2020-08-07 09:51:34 -0700 | [diff] [blame] | 4455 | * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4456 | * of @sk_sid. Returns zero on success, negative values on failure. |
| 4457 | * |
| 4458 | */ |
| 4459 | static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) |
| 4460 | { |
| 4461 | int err = 0; |
| 4462 | |
| 4463 | if (skb_sid != SECSID_NULL) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4464 | err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, |
| 4465 | conn_sid); |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4466 | else |
| 4467 | *conn_sid = sk_sid; |
| 4468 | |
| 4469 | return err; |
| 4470 | } |
| 4471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | /* socket security operations */ |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4473 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4474 | static int socket_sockcreate_sid(const struct task_security_struct *tsec, |
| 4475 | u16 secclass, u32 *socksid) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4476 | { |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4477 | if (tsec->sockcreate_sid > SECSID_NULL) { |
| 4478 | *socksid = tsec->sockcreate_sid; |
| 4479 | return 0; |
| 4480 | } |
| 4481 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4482 | return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, |
| 4483 | secclass, NULL, socksid); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4484 | } |
| 4485 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4486 | static int sock_has_perm(struct sock *sk, u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4488 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4489 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4490 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4492 | if (sksec->sid == SECINITSID_KERNEL) |
| 4493 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4495 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4496 | ad.u.net = &net; |
| 4497 | ad.u.net->sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4499 | return avc_has_perm(&selinux_state, |
| 4500 | current_sid(), sksec->sid, sksec->sclass, perms, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4501 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | static int selinux_socket_create(int family, int type, |
| 4505 | int protocol, int kern) |
| 4506 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 4507 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4508 | u32 newsid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4509 | u16 secclass; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4510 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | |
| 4512 | if (kern) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4513 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4515 | secclass = socket_type_to_security_class(family, type, protocol); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4516 | rc = socket_sockcreate_sid(tsec, secclass, &newsid); |
| 4517 | if (rc) |
| 4518 | return rc; |
| 4519 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4520 | return avc_has_perm(&selinux_state, |
| 4521 | tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | } |
| 4523 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4524 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 4525 | int type, int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | { |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 4527 | const struct task_security_struct *tsec = selinux_cred(current_cred()); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4528 | struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4529 | struct sk_security_struct *sksec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4530 | u16 sclass = socket_type_to_security_class(family, type, protocol); |
| 4531 | u32 sid = SECINITSID_KERNEL; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4532 | int err = 0; |
| 4533 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4534 | if (!kern) { |
| 4535 | err = socket_sockcreate_sid(tsec, sclass, &sid); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4536 | if (err) |
| 4537 | return err; |
| 4538 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4539 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4540 | isec->sclass = sclass; |
| 4541 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4542 | isec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4544 | if (sock->sk) { |
| 4545 | sksec = sock->sk->sk_security; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4546 | sksec->sclass = sclass; |
| 4547 | sksec->sid = sid; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4548 | /* Allows detection of the first association on this socket */ |
| 4549 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4550 | sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; |
| 4551 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4552 | err = selinux_netlbl_socket_post_create(sock->sk, family); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4555 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 4558 | static int selinux_socket_socketpair(struct socket *socka, |
| 4559 | struct socket *sockb) |
| 4560 | { |
| 4561 | struct sk_security_struct *sksec_a = socka->sk->sk_security; |
| 4562 | struct sk_security_struct *sksec_b = sockb->sk->sk_security; |
| 4563 | |
| 4564 | sksec_a->peer_sid = sksec_b->sid; |
| 4565 | sksec_b->peer_sid = sksec_a->sid; |
| 4566 | |
| 4567 | return 0; |
| 4568 | } |
| 4569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | /* Range of port numbers used to automatically bind. |
| 4571 | Need to determine whether we should perform a name_bind |
| 4572 | permission check between the socket and the port number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | |
| 4574 | static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4575 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4576 | struct sock *sk = sock->sk; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4577 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | u16 family; |
| 4579 | int err; |
| 4580 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4581 | err = sock_has_perm(sk, SOCKET__BIND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | if (err) |
| 4583 | goto out; |
| 4584 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4585 | /* 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] | 4586 | family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | if (family == PF_INET || family == PF_INET6) { |
| 4588 | char *addrp; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4589 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4590 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | struct sockaddr_in *addr4 = NULL; |
| 4592 | struct sockaddr_in6 *addr6 = NULL; |
Tetsuo Handa | c750e69 | 2019-04-12 19:59:34 +0900 | [diff] [blame] | 4593 | u16 family_sa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | unsigned short snum; |
James Morris | e399f98 | 2008-06-12 01:39:58 +1000 | [diff] [blame] | 4595 | u32 sid, node_perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4597 | /* |
| 4598 | * sctp_bindx(3) calls via selinux_sctp_bind_connect() |
| 4599 | * that validates multiple binding addresses. Because of this |
| 4600 | * need to check address->sa_family as it is possible to have |
| 4601 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4602 | */ |
Tetsuo Handa | c750e69 | 2019-04-12 19:59:34 +0900 | [diff] [blame] | 4603 | if (addrlen < offsetofend(struct sockaddr, sa_family)) |
| 4604 | return -EINVAL; |
| 4605 | family_sa = address->sa_family; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4606 | switch (family_sa) { |
| 4607 | case AF_UNSPEC: |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4608 | case AF_INET: |
| 4609 | if (addrlen < sizeof(struct sockaddr_in)) |
| 4610 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | addr4 = (struct sockaddr_in *)address; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4612 | if (family_sa == AF_UNSPEC) { |
| 4613 | /* see __inet_bind(), we only want to allow |
| 4614 | * AF_UNSPEC if the address is INADDR_ANY |
| 4615 | */ |
| 4616 | if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) |
| 4617 | goto err_af; |
| 4618 | family_sa = AF_INET; |
| 4619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | snum = ntohs(addr4->sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | addrp = (char *)&addr4->sin_addr.s_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4622 | break; |
| 4623 | case AF_INET6: |
| 4624 | if (addrlen < SIN6_LEN_RFC2133) |
| 4625 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | addr6 = (struct sockaddr_in6 *)address; |
| 4627 | snum = ntohs(addr6->sin6_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | addrp = (char *)&addr6->sin6_addr.s6_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4629 | break; |
| 4630 | default: |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4631 | goto err_af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4634 | ad.type = LSM_AUDIT_DATA_NET; |
| 4635 | ad.u.net = &net; |
| 4636 | ad.u.net->sport = htons(snum); |
| 4637 | ad.u.net->family = family_sa; |
| 4638 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4639 | if (snum) { |
| 4640 | int low, high; |
| 4641 | |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 4642 | inet_get_local_port_range(sock_net(sk), &low, &high); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4643 | |
Maciej Żenczykowski | 82f31eb | 2019-11-25 15:37:04 -0800 | [diff] [blame] | 4644 | if (inet_port_requires_bind_service(sock_net(sk), snum) || |
| 4645 | snum < low || snum > high) { |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4646 | err = sel_netport_sid(sk->sk_protocol, |
| 4647 | snum, &sid); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4648 | if (err) |
| 4649 | goto out; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4650 | err = avc_has_perm(&selinux_state, |
| 4651 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4652 | sksec->sclass, |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4653 | SOCKET__NAME_BIND, &ad); |
| 4654 | if (err) |
| 4655 | goto out; |
| 4656 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4658 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4659 | switch (sksec->sclass) { |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4660 | case SECCLASS_TCP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | node_perm = TCP_SOCKET__NODE_BIND; |
| 4662 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4663 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4664 | case SECCLASS_UDP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | node_perm = UDP_SOCKET__NODE_BIND; |
| 4666 | break; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4667 | |
| 4668 | case SECCLASS_DCCP_SOCKET: |
| 4669 | node_perm = DCCP_SOCKET__NODE_BIND; |
| 4670 | break; |
| 4671 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4672 | case SECCLASS_SCTP_SOCKET: |
| 4673 | node_perm = SCTP_SOCKET__NODE_BIND; |
| 4674 | break; |
| 4675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | default: |
| 4677 | node_perm = RAWIP_SOCKET__NODE_BIND; |
| 4678 | break; |
| 4679 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4680 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4681 | err = sel_netnode_sid(addrp, family_sa, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | if (err) |
| 4683 | goto out; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4684 | |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4685 | if (family_sa == AF_INET) |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4686 | ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | else |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4688 | ad.u.net->v6info.saddr = addr6->sin6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4690 | err = avc_has_perm(&selinux_state, |
| 4691 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4692 | sksec->sclass, node_perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | if (err) |
| 4694 | goto out; |
| 4695 | } |
| 4696 | out: |
| 4697 | return err; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4698 | err_af: |
| 4699 | /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ |
| 4700 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4701 | return -EINVAL; |
| 4702 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4705 | /* 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] | 4706 | * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4707 | */ |
| 4708 | static int selinux_socket_connect_helper(struct socket *sock, |
| 4709 | struct sockaddr *address, int addrlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4711 | struct sock *sk = sock->sk; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4712 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | int err; |
| 4714 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4715 | err = sock_has_perm(sk, SOCKET__CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | if (err) |
| 4717 | return err; |
Paolo Abeni | 05174c9 | 2019-05-10 19:12:33 +0200 | [diff] [blame] | 4718 | if (addrlen < offsetofend(struct sockaddr, sa_family)) |
| 4719 | return -EINVAL; |
| 4720 | |
| 4721 | /* connect(AF_UNSPEC) has special handling, as it is a documented |
| 4722 | * way to disconnect the socket |
| 4723 | */ |
| 4724 | if (address->sa_family == AF_UNSPEC) |
| 4725 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | |
| 4727 | /* |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4728 | * If a TCP, DCCP or SCTP socket, check name_connect permission |
| 4729 | * for the port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4731 | if (sksec->sclass == SECCLASS_TCP_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4732 | sksec->sclass == SECCLASS_DCCP_SOCKET || |
| 4733 | sksec->sclass == SECCLASS_SCTP_SOCKET) { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4734 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4735 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | struct sockaddr_in *addr4 = NULL; |
| 4737 | struct sockaddr_in6 *addr6 = NULL; |
| 4738 | unsigned short snum; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4739 | u32 sid, perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4741 | /* sctp_connectx(3) calls via selinux_sctp_bind_connect() |
| 4742 | * that validates multiple connect addresses. Because of this |
| 4743 | * need to check address->sa_family as it is possible to have |
| 4744 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4745 | */ |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4746 | switch (address->sa_family) { |
| 4747 | case AF_INET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | addr4 = (struct sockaddr_in *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4749 | if (addrlen < sizeof(struct sockaddr_in)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | return -EINVAL; |
| 4751 | snum = ntohs(addr4->sin_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4752 | break; |
| 4753 | case AF_INET6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | addr6 = (struct sockaddr_in6 *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4755 | if (addrlen < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | return -EINVAL; |
| 4757 | snum = ntohs(addr6->sin6_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4758 | break; |
| 4759 | default: |
| 4760 | /* Note that SCTP services expect -EINVAL, whereas |
| 4761 | * others expect -EAFNOSUPPORT. |
| 4762 | */ |
| 4763 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4764 | return -EINVAL; |
| 4765 | else |
| 4766 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4769 | err = sel_netport_sid(sk->sk_protocol, snum, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4771 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4773 | switch (sksec->sclass) { |
| 4774 | case SECCLASS_TCP_SOCKET: |
| 4775 | perm = TCP_SOCKET__NAME_CONNECT; |
| 4776 | break; |
| 4777 | case SECCLASS_DCCP_SOCKET: |
| 4778 | perm = DCCP_SOCKET__NAME_CONNECT; |
| 4779 | break; |
| 4780 | case SECCLASS_SCTP_SOCKET: |
| 4781 | perm = SCTP_SOCKET__NAME_CONNECT; |
| 4782 | break; |
| 4783 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4784 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4785 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4786 | ad.u.net = &net; |
| 4787 | ad.u.net->dport = htons(snum); |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4788 | ad.u.net->family = address->sa_family; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4789 | err = avc_has_perm(&selinux_state, |
| 4790 | sksec->sid, sid, sksec->sclass, perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4792 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4795 | return 0; |
| 4796 | } |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4797 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4798 | /* Supports connect(2), see comments in selinux_socket_connect_helper() */ |
| 4799 | static int selinux_socket_connect(struct socket *sock, |
| 4800 | struct sockaddr *address, int addrlen) |
| 4801 | { |
| 4802 | int err; |
| 4803 | struct sock *sk = sock->sk; |
| 4804 | |
| 4805 | err = selinux_socket_connect_helper(sock, address, addrlen); |
| 4806 | if (err) |
| 4807 | return err; |
| 4808 | |
| 4809 | return selinux_netlbl_socket_connect(sk, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | static int selinux_socket_listen(struct socket *sock, int backlog) |
| 4813 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4814 | return sock_has_perm(sock->sk, SOCKET__LISTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | static int selinux_socket_accept(struct socket *sock, struct socket *newsock) |
| 4818 | { |
| 4819 | int err; |
| 4820 | struct inode_security_struct *isec; |
| 4821 | struct inode_security_struct *newisec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4822 | u16 sclass; |
| 4823 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4825 | err = sock_has_perm(sock->sk, SOCKET__ACCEPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | if (err) |
| 4827 | return err; |
| 4828 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4829 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4830 | spin_lock(&isec->lock); |
| 4831 | sclass = isec->sclass; |
| 4832 | sid = isec->sid; |
| 4833 | spin_unlock(&isec->lock); |
| 4834 | |
| 4835 | newisec = inode_security_novalidate(SOCK_INODE(newsock)); |
| 4836 | newisec->sclass = sclass; |
| 4837 | newisec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4838 | newisec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | |
| 4840 | return 0; |
| 4841 | } |
| 4842 | |
| 4843 | static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4844 | int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4846 | return sock_has_perm(sock->sk, SOCKET__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 4850 | int size, int flags) |
| 4851 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4852 | return sock_has_perm(sock->sk, SOCKET__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | static int selinux_socket_getsockname(struct socket *sock) |
| 4856 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4857 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | static int selinux_socket_getpeername(struct socket *sock) |
| 4861 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4862 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | } |
| 4864 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4865 | static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | { |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4867 | int err; |
| 4868 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4869 | err = sock_has_perm(sock->sk, SOCKET__SETOPT); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4870 | if (err) |
| 4871 | return err; |
| 4872 | |
| 4873 | return selinux_netlbl_socket_setsockopt(sock, level, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | static int selinux_socket_getsockopt(struct socket *sock, int level, |
| 4877 | int optname) |
| 4878 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4879 | return sock_has_perm(sock->sk, SOCKET__GETOPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | static int selinux_socket_shutdown(struct socket *sock, int how) |
| 4883 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4884 | return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4887 | static int selinux_socket_unix_stream_connect(struct sock *sock, |
| 4888 | struct sock *other, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | struct sock *newsk) |
| 4890 | { |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4891 | struct sk_security_struct *sksec_sock = sock->sk_security; |
| 4892 | struct sk_security_struct *sksec_other = other->sk_security; |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4893 | struct sk_security_struct *sksec_new = newsk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4894 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4895 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | int err; |
| 4897 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4898 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4899 | ad.u.net = &net; |
| 4900 | ad.u.net->sk = other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4902 | err = avc_has_perm(&selinux_state, |
| 4903 | sksec_sock->sid, sksec_other->sid, |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4904 | sksec_other->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | UNIX_STREAM_SOCKET__CONNECTTO, &ad); |
| 4906 | if (err) |
| 4907 | return err; |
| 4908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | /* server child socket */ |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4910 | sksec_new->peer_sid = sksec_sock->sid; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4911 | err = security_sid_mls_copy(&selinux_state, sksec_other->sid, |
| 4912 | sksec_sock->sid, &sksec_new->sid); |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4913 | if (err) |
| 4914 | return err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4915 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4916 | /* connecting socket */ |
| 4917 | sksec_sock->peer_sid = sksec_new->sid; |
| 4918 | |
| 4919 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | static int selinux_socket_unix_may_send(struct socket *sock, |
| 4923 | struct socket *other) |
| 4924 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4925 | struct sk_security_struct *ssec = sock->sk->sk_security; |
| 4926 | struct sk_security_struct *osec = other->sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4927 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4928 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4930 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4931 | ad.u.net = &net; |
| 4932 | ad.u.net->sk = other->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4934 | return avc_has_perm(&selinux_state, |
| 4935 | ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4936 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | } |
| 4938 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4939 | static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, |
| 4940 | char *addrp, u16 family, u32 peer_sid, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4941 | struct common_audit_data *ad) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4942 | { |
| 4943 | int err; |
| 4944 | u32 if_sid; |
| 4945 | u32 node_sid; |
| 4946 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4947 | err = sel_netif_sid(ns, ifindex, &if_sid); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4948 | if (err) |
| 4949 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4950 | err = avc_has_perm(&selinux_state, |
| 4951 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4952 | SECCLASS_NETIF, NETIF__INGRESS, ad); |
| 4953 | if (err) |
| 4954 | return err; |
| 4955 | |
| 4956 | err = sel_netnode_sid(addrp, family, &node_sid); |
| 4957 | if (err) |
| 4958 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4959 | return avc_has_perm(&selinux_state, |
| 4960 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4961 | SECCLASS_NODE, NODE__RECVFROM, ad); |
| 4962 | } |
| 4963 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4964 | 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] | 4965 | u16 family) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4966 | { |
Paul Moore | 277d342 | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 4967 | int err = 0; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4968 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4969 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4970 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4971 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4972 | char *addrp; |
| 4973 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4974 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4975 | ad.u.net = &net; |
| 4976 | ad.u.net->netif = skb->skb_iif; |
| 4977 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4978 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
| 4979 | if (err) |
| 4980 | return err; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4981 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4982 | if (selinux_secmark_enabled()) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4983 | err = avc_has_perm(&selinux_state, |
| 4984 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4985 | PACKET__RECV, &ad); |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4986 | if (err) |
| 4987 | return err; |
| 4988 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4989 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4990 | err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); |
| 4991 | if (err) |
| 4992 | return err; |
| 4993 | err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4994 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4995 | return err; |
| 4996 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4997 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4998 | static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 4999 | { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5000 | int err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5001 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5002 | u16 family = sk->sk_family; |
| 5003 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5004 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5005 | struct lsm_network_audit net = {0,}; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5006 | char *addrp; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5007 | u8 secmark_active; |
| 5008 | u8 peerlbl_active; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5009 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5010 | if (family != PF_INET && family != PF_INET6) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5011 | return 0; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5012 | |
| 5013 | /* Handle mapped IPv4 packets arriving via IPv6 sockets */ |
Al Viro | 87fcd70 | 2006-12-04 22:00:55 +0000 | [diff] [blame] | 5014 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5015 | family = PF_INET; |
| 5016 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5017 | /* If any sort of compatibility mode is enabled then handoff processing |
| 5018 | * to the selinux_sock_rcv_skb_compat() function to deal with the |
| 5019 | * special handling. We do this in an attempt to keep this function |
| 5020 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5021 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5022 | return selinux_sock_rcv_skb_compat(sk, skb, family); |
| 5023 | |
| 5024 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5025 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5026 | if (!secmark_active && !peerlbl_active) |
| 5027 | return 0; |
| 5028 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5029 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5030 | ad.u.net = &net; |
| 5031 | ad.u.net->netif = skb->skb_iif; |
| 5032 | ad.u.net->family = family; |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 5033 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5034 | if (err) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5035 | return err; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5036 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5037 | if (peerlbl_active) { |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 5038 | u32 peer_sid; |
| 5039 | |
| 5040 | err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); |
| 5041 | if (err) |
| 5042 | return err; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5043 | err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, |
| 5044 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5045 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 5046 | selinux_netlbl_err(skb, family, err, 0); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5047 | return err; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5048 | } |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5049 | err = avc_has_perm(&selinux_state, |
| 5050 | sk_sid, peer_sid, SECCLASS_PEER, |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 5051 | PEER__RECV, &ad); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 5052 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 5053 | selinux_netlbl_err(skb, family, err, 0); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 5054 | return err; |
| 5055 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 5056 | } |
| 5057 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5058 | if (secmark_active) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5059 | err = avc_has_perm(&selinux_state, |
| 5060 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5061 | PACKET__RECV, &ad); |
| 5062 | if (err) |
| 5063 | return err; |
| 5064 | } |
| 5065 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 5066 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5069 | static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, |
| 5070 | int __user *optlen, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | { |
| 5072 | int err = 0; |
| 5073 | char *scontext; |
| 5074 | u32 scontext_len; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5075 | struct sk_security_struct *sksec = sock->sk->sk_security; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 5076 | u32 peer_sid = SECSID_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5078 | if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5079 | sksec->sclass == SECCLASS_TCP_SOCKET || |
| 5080 | sksec->sclass == SECCLASS_SCTP_SOCKET) |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5081 | peer_sid = sksec->peer_sid; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5082 | if (peer_sid == SECSID_NULL) |
| 5083 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5085 | err = security_sid_to_context(&selinux_state, peer_sid, &scontext, |
| 5086 | &scontext_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | if (err) |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5088 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | |
| 5090 | if (scontext_len > len) { |
| 5091 | err = -ERANGE; |
| 5092 | goto out_len; |
| 5093 | } |
| 5094 | |
| 5095 | if (copy_to_user(optval, scontext, scontext_len)) |
| 5096 | err = -EFAULT; |
| 5097 | |
| 5098 | out_len: |
| 5099 | if (put_user(scontext_len, optlen)) |
| 5100 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | kfree(scontext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | return err; |
| 5103 | } |
| 5104 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5105 | 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] | 5106 | { |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5107 | u32 peer_secid = SECSID_NULL; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5108 | u16 family; |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 5109 | struct inode_security_struct *isec; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 5110 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5111 | if (skb && skb->protocol == htons(ETH_P_IP)) |
| 5112 | family = PF_INET; |
| 5113 | else if (skb && skb->protocol == htons(ETH_P_IPV6)) |
| 5114 | family = PF_INET6; |
| 5115 | else if (sock) |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5116 | family = sock->sk->sk_family; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5117 | else |
| 5118 | goto out; |
| 5119 | |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 5120 | if (sock && family == PF_UNIX) { |
| 5121 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
| 5122 | peer_secid = isec->sid; |
| 5123 | } else if (skb) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5124 | selinux_skb_peerlbl_sid(skb, family, &peer_secid); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5125 | |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5126 | out: |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 5127 | *secid = peer_secid; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 5128 | if (peer_secid == SECSID_NULL) |
| 5129 | return -EINVAL; |
| 5130 | return 0; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 5131 | } |
| 5132 | |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 5133 | 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] | 5134 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5135 | struct sk_security_struct *sksec; |
| 5136 | |
| 5137 | sksec = kzalloc(sizeof(*sksec), priority); |
| 5138 | if (!sksec) |
| 5139 | return -ENOMEM; |
| 5140 | |
| 5141 | sksec->peer_sid = SECINITSID_UNLABELED; |
| 5142 | sksec->sid = SECINITSID_UNLABELED; |
Stephen Smalley | 5dee25d | 2015-07-10 17:19:57 -0400 | [diff] [blame] | 5143 | sksec->sclass = SECCLASS_SOCKET; |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5144 | selinux_netlbl_sk_security_reset(sksec); |
| 5145 | sk->sk_security = sksec; |
| 5146 | |
| 5147 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | static void selinux_sk_free_security(struct sock *sk) |
| 5151 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 5152 | struct sk_security_struct *sksec = sk->sk_security; |
| 5153 | |
| 5154 | sk->sk_security = NULL; |
| 5155 | selinux_netlbl_sk_security_free(sksec); |
| 5156 | kfree(sksec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5159 | static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) |
| 5160 | { |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5161 | struct sk_security_struct *sksec = sk->sk_security; |
| 5162 | struct sk_security_struct *newsksec = newsk->sk_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5163 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5164 | newsksec->sid = sksec->sid; |
| 5165 | newsksec->peer_sid = sksec->peer_sid; |
| 5166 | newsksec->sclass = sksec->sclass; |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5167 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 5168 | selinux_netlbl_sk_security_reset(newsksec); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5171 | static void selinux_sk_getsecid(struct sock *sk, u32 *secid) |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5172 | { |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5173 | if (!sk) |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5174 | *secid = SECINITSID_ANY_SOCKET; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5175 | else { |
| 5176 | struct sk_security_struct *sksec = sk->sk_security; |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5177 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 5178 | *secid = sksec->sid; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5179 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5180 | } |
| 5181 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5182 | static void selinux_sock_graft(struct sock *sk, struct socket *parent) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5183 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 5184 | struct inode_security_struct *isec = |
| 5185 | inode_security_novalidate(SOCK_INODE(parent)); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5186 | struct sk_security_struct *sksec = sk->sk_security; |
| 5187 | |
Paul Moore | 2873ead | 2014-07-28 10:42:48 -0400 | [diff] [blame] | 5188 | if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || |
| 5189 | sk->sk_family == PF_UNIX) |
David Woodhouse | 2148ccc | 2006-09-29 15:50:25 -0700 | [diff] [blame] | 5190 | isec->sid = sksec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5191 | sksec->sclass = isec->sclass; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5194 | /* Called whenever SCTP receives an INIT chunk. This happens when an incoming |
| 5195 | * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association |
| 5196 | * already present). |
| 5197 | */ |
| 5198 | static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, |
| 5199 | struct sk_buff *skb) |
| 5200 | { |
| 5201 | struct sk_security_struct *sksec = ep->base.sk->sk_security; |
| 5202 | struct common_audit_data ad; |
| 5203 | struct lsm_network_audit net = {0,}; |
| 5204 | u8 peerlbl_active; |
| 5205 | u32 peer_sid = SECINITSID_UNLABELED; |
| 5206 | u32 conn_sid; |
| 5207 | int err = 0; |
| 5208 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5209 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5210 | return 0; |
| 5211 | |
| 5212 | peerlbl_active = selinux_peerlbl_enabled(); |
| 5213 | |
| 5214 | if (peerlbl_active) { |
| 5215 | /* This will return peer_sid = SECSID_NULL if there are |
| 5216 | * no peer labels, see security_net_peersid_resolve(). |
| 5217 | */ |
| 5218 | err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, |
| 5219 | &peer_sid); |
| 5220 | if (err) |
| 5221 | return err; |
| 5222 | |
| 5223 | if (peer_sid == SECSID_NULL) |
| 5224 | peer_sid = SECINITSID_UNLABELED; |
| 5225 | } |
| 5226 | |
| 5227 | if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { |
| 5228 | sksec->sctp_assoc_state = SCTP_ASSOC_SET; |
| 5229 | |
| 5230 | /* Here as first association on socket. As the peer SID |
| 5231 | * was allowed by peer recv (and the netif/node checks), |
| 5232 | * then it is approved by policy and used as the primary |
| 5233 | * peer SID for getpeercon(3). |
| 5234 | */ |
| 5235 | sksec->peer_sid = peer_sid; |
| 5236 | } else if (sksec->peer_sid != peer_sid) { |
| 5237 | /* Other association peer SIDs are checked to enforce |
| 5238 | * consistency among the peer SIDs. |
| 5239 | */ |
| 5240 | ad.type = LSM_AUDIT_DATA_NET; |
| 5241 | ad.u.net = &net; |
| 5242 | ad.u.net->sk = ep->base.sk; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5243 | err = avc_has_perm(&selinux_state, |
| 5244 | sksec->peer_sid, peer_sid, sksec->sclass, |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5245 | SCTP_SOCKET__ASSOCIATION, &ad); |
| 5246 | if (err) |
| 5247 | return err; |
| 5248 | } |
| 5249 | |
| 5250 | /* Compute the MLS component for the connection and store |
| 5251 | * the information in ep. This will be used by SCTP TCP type |
| 5252 | * sockets and peeled off connections as they cause a new |
| 5253 | * socket to be generated. selinux_sctp_sk_clone() will then |
| 5254 | * plug this into the new socket. |
| 5255 | */ |
| 5256 | err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); |
| 5257 | if (err) |
| 5258 | return err; |
| 5259 | |
| 5260 | ep->secid = conn_sid; |
| 5261 | ep->peer_secid = peer_sid; |
| 5262 | |
| 5263 | /* Set any NetLabel labels including CIPSO/CALIPSO options. */ |
| 5264 | return selinux_netlbl_sctp_assoc_request(ep, skb); |
| 5265 | } |
| 5266 | |
| 5267 | /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting |
| 5268 | * based on their @optname. |
| 5269 | */ |
| 5270 | static int selinux_sctp_bind_connect(struct sock *sk, int optname, |
| 5271 | struct sockaddr *address, |
| 5272 | int addrlen) |
| 5273 | { |
| 5274 | int len, err = 0, walk_size = 0; |
| 5275 | void *addr_buf; |
| 5276 | struct sockaddr *addr; |
| 5277 | struct socket *sock; |
| 5278 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5279 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5280 | return 0; |
| 5281 | |
| 5282 | /* Process one or more addresses that may be IPv4 or IPv6 */ |
| 5283 | sock = sk->sk_socket; |
| 5284 | addr_buf = address; |
| 5285 | |
| 5286 | while (walk_size < addrlen) { |
Ondrej Mosnacek | c138325 | 2018-11-13 16:16:08 +0100 | [diff] [blame] | 5287 | if (walk_size + sizeof(sa_family_t) > addrlen) |
| 5288 | return -EINVAL; |
| 5289 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5290 | addr = addr_buf; |
| 5291 | switch (addr->sa_family) { |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5292 | case AF_UNSPEC: |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5293 | case AF_INET: |
| 5294 | len = sizeof(struct sockaddr_in); |
| 5295 | break; |
| 5296 | case AF_INET6: |
| 5297 | len = sizeof(struct sockaddr_in6); |
| 5298 | break; |
| 5299 | default: |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5300 | return -EINVAL; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
Xin Long | 292c997 | 2019-03-09 00:07:34 +0800 | [diff] [blame] | 5303 | if (walk_size + len > addrlen) |
| 5304 | return -EINVAL; |
| 5305 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5306 | err = -EINVAL; |
| 5307 | switch (optname) { |
| 5308 | /* Bind checks */ |
| 5309 | case SCTP_PRIMARY_ADDR: |
| 5310 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 5311 | case SCTP_SOCKOPT_BINDX_ADD: |
| 5312 | err = selinux_socket_bind(sock, addr, len); |
| 5313 | break; |
| 5314 | /* Connect checks */ |
| 5315 | case SCTP_SOCKOPT_CONNECTX: |
| 5316 | case SCTP_PARAM_SET_PRIMARY: |
| 5317 | case SCTP_PARAM_ADD_IP: |
| 5318 | case SCTP_SENDMSG_CONNECT: |
| 5319 | err = selinux_socket_connect_helper(sock, addr, len); |
| 5320 | if (err) |
| 5321 | return err; |
| 5322 | |
| 5323 | /* As selinux_sctp_bind_connect() is called by the |
| 5324 | * SCTP protocol layer, the socket is already locked, |
Randy Dunlap | c76a2f9 | 2020-08-07 09:51:34 -0700 | [diff] [blame] | 5325 | * therefore selinux_netlbl_socket_connect_locked() |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5326 | * is called here. The situations handled are: |
| 5327 | * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), |
| 5328 | * whenever a new IP address is added or when a new |
| 5329 | * primary address is selected. |
| 5330 | * Note that an SCTP connect(2) call happens before |
| 5331 | * the SCTP protocol layer and is handled via |
| 5332 | * selinux_socket_connect(). |
| 5333 | */ |
| 5334 | err = selinux_netlbl_socket_connect_locked(sk, addr); |
| 5335 | break; |
| 5336 | } |
| 5337 | |
| 5338 | if (err) |
| 5339 | return err; |
| 5340 | |
| 5341 | addr_buf += len; |
| 5342 | walk_size += len; |
| 5343 | } |
| 5344 | |
| 5345 | return 0; |
| 5346 | } |
| 5347 | |
| 5348 | /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ |
| 5349 | static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, |
| 5350 | struct sock *newsk) |
| 5351 | { |
| 5352 | struct sk_security_struct *sksec = sk->sk_security; |
| 5353 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5354 | |
| 5355 | /* If policy does not support SECCLASS_SCTP_SOCKET then call |
| 5356 | * the non-sctp clone version. |
| 5357 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5358 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5359 | return selinux_sk_clone_security(sk, newsk); |
| 5360 | |
| 5361 | newsksec->sid = ep->secid; |
| 5362 | newsksec->peer_sid = ep->peer_secid; |
| 5363 | newsksec->sclass = sksec->sclass; |
| 5364 | selinux_netlbl_sctp_sk_clone(sk, newsk); |
| 5365 | } |
| 5366 | |
Florian Westphal | 41dd959 | 2020-11-30 16:36:29 +0100 | [diff] [blame] | 5367 | static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb, |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5368 | struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5369 | { |
| 5370 | struct sk_security_struct *sksec = sk->sk_security; |
| 5371 | int err; |
Paul Moore | 0b1f24e | 2013-12-03 11:39:13 -0500 | [diff] [blame] | 5372 | u16 family = req->rsk_ops->family; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5373 | u32 connsid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5374 | u32 peersid; |
| 5375 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5376 | err = selinux_skb_peerlbl_sid(skb, family, &peersid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5377 | if (err) |
| 5378 | return err; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5379 | err = selinux_conn_sid(sksec->sid, peersid, &connsid); |
| 5380 | if (err) |
| 5381 | return err; |
| 5382 | req->secid = connsid; |
| 5383 | req->peer_secid = peersid; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 5384 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5385 | return selinux_netlbl_inet_conn_request(req, family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5386 | } |
| 5387 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5388 | static void selinux_inet_csk_clone(struct sock *newsk, |
| 5389 | const struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5390 | { |
| 5391 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5392 | |
| 5393 | newsksec->sid = req->secid; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5394 | newsksec->peer_sid = req->peer_secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5395 | /* NOTE: Ideally, we should also get the isec->sid for the |
| 5396 | new socket in sync, but we don't have the isec available yet. |
| 5397 | So we will wait until sock_graft to do it, by which |
| 5398 | time it will have been created and available. */ |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5399 | |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 5400 | /* We don't need to take any sort of lock here as we are the only |
| 5401 | * thread with access to newsksec */ |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5402 | selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5403 | } |
| 5404 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 5405 | 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] | 5406 | { |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5407 | u16 family = sk->sk_family; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5408 | struct sk_security_struct *sksec = sk->sk_security; |
| 5409 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5410 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 5411 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 5412 | family = PF_INET; |
| 5413 | |
| 5414 | selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5415 | } |
| 5416 | |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5417 | static int selinux_secmark_relabel_packet(u32 sid) |
| 5418 | { |
| 5419 | const struct task_security_struct *__tsec; |
| 5420 | u32 tsid; |
| 5421 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 5422 | __tsec = selinux_cred(current_cred()); |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5423 | tsid = __tsec->sid; |
| 5424 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5425 | return avc_has_perm(&selinux_state, |
| 5426 | tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, |
| 5427 | NULL); |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | static void selinux_secmark_refcount_inc(void) |
| 5431 | { |
| 5432 | atomic_inc(&selinux_secmark_refcount); |
| 5433 | } |
| 5434 | |
| 5435 | static void selinux_secmark_refcount_dec(void) |
| 5436 | { |
| 5437 | atomic_dec(&selinux_secmark_refcount); |
| 5438 | } |
| 5439 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5440 | static void selinux_req_classify_flow(const struct request_sock *req, |
Paul Moore | 3df98d7 | 2020-09-27 22:38:26 -0400 | [diff] [blame] | 5441 | struct flowi_common *flic) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5442 | { |
Paul Moore | 3df98d7 | 2020-09-27 22:38:26 -0400 | [diff] [blame] | 5443 | flic->flowic_secid = req->secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5444 | } |
| 5445 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5446 | static int selinux_tun_dev_alloc_security(void **security) |
| 5447 | { |
| 5448 | struct tun_security_struct *tunsec; |
| 5449 | |
| 5450 | tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); |
| 5451 | if (!tunsec) |
| 5452 | return -ENOMEM; |
| 5453 | tunsec->sid = current_sid(); |
| 5454 | |
| 5455 | *security = tunsec; |
| 5456 | return 0; |
| 5457 | } |
| 5458 | |
| 5459 | static void selinux_tun_dev_free_security(void *security) |
| 5460 | { |
| 5461 | kfree(security); |
| 5462 | } |
| 5463 | |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5464 | static int selinux_tun_dev_create(void) |
| 5465 | { |
| 5466 | u32 sid = current_sid(); |
| 5467 | |
| 5468 | /* we aren't taking into account the "sockcreate" SID since the socket |
| 5469 | * that is being created here is not a socket in the traditional sense, |
| 5470 | * instead it is a private sock, accessible only to the kernel, and |
| 5471 | * representing a wide range of network traffic spanning multiple |
| 5472 | * connections unlike traditional sockets - check the TUN driver to |
| 5473 | * get a better understanding of why this socket is special */ |
| 5474 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5475 | return avc_has_perm(&selinux_state, |
| 5476 | sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5477 | NULL); |
| 5478 | } |
| 5479 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5480 | static int selinux_tun_dev_attach_queue(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5481 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5482 | struct tun_security_struct *tunsec = security; |
| 5483 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5484 | return avc_has_perm(&selinux_state, |
| 5485 | current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5486 | TUN_SOCKET__ATTACH_QUEUE, NULL); |
| 5487 | } |
| 5488 | |
| 5489 | static int selinux_tun_dev_attach(struct sock *sk, void *security) |
| 5490 | { |
| 5491 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5492 | struct sk_security_struct *sksec = sk->sk_security; |
| 5493 | |
| 5494 | /* we don't currently perform any NetLabel based labeling here and it |
| 5495 | * isn't clear that we would want to do so anyway; while we could apply |
| 5496 | * labeling without the support of the TUN user the resulting labeled |
| 5497 | * traffic from the other end of the connection would almost certainly |
| 5498 | * cause confusion to the TUN user that had no idea network labeling |
| 5499 | * protocols were being used */ |
| 5500 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5501 | sksec->sid = tunsec->sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5502 | sksec->sclass = SECCLASS_TUN_SOCKET; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5503 | |
| 5504 | return 0; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5505 | } |
| 5506 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5507 | static int selinux_tun_dev_open(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5508 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5509 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5510 | u32 sid = current_sid(); |
| 5511 | int err; |
| 5512 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5513 | err = avc_has_perm(&selinux_state, |
| 5514 | sid, tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5515 | TUN_SOCKET__RELABELFROM, NULL); |
| 5516 | if (err) |
| 5517 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5518 | err = avc_has_perm(&selinux_state, |
| 5519 | sid, sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5520 | TUN_SOCKET__RELABELTO, NULL); |
| 5521 | if (err) |
| 5522 | return err; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5523 | tunsec->sid = sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5524 | |
| 5525 | return 0; |
| 5526 | } |
| 5527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | #ifdef CONFIG_NETFILTER |
| 5529 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5530 | static unsigned int selinux_ip_forward(struct sk_buff *skb, |
| 5531 | const struct net_device *indev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5532 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5534 | int err; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5535 | char *addrp; |
| 5536 | u32 peer_sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5537 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5538 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5539 | u8 secmark_active; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5540 | u8 netlbl_active; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5541 | u8 peerlbl_active; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5542 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5543 | if (!selinux_policycap_netpeer()) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5544 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5545 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5546 | secmark_active = selinux_secmark_enabled(); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5547 | netlbl_active = netlbl_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5548 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5549 | if (!secmark_active && !peerlbl_active) |
| 5550 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5551 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5552 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) |
| 5553 | return NF_DROP; |
| 5554 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5555 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5556 | ad.u.net = &net; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5557 | ad.u.net->netif = indev->ifindex; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5558 | ad.u.net->family = family; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5559 | if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) |
| 5560 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5562 | if (peerlbl_active) { |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5563 | err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, |
| 5564 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5565 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 5566 | selinux_netlbl_err(skb, family, err, 1); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5567 | return NF_DROP; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5568 | } |
| 5569 | } |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5570 | |
| 5571 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5572 | if (avc_has_perm(&selinux_state, |
| 5573 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5574 | SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) |
| 5575 | return NF_DROP; |
| 5576 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5577 | if (netlbl_active) |
| 5578 | /* we do this in the FORWARD path and not the POST_ROUTING |
| 5579 | * path because we want to make sure we apply the necessary |
| 5580 | * labeling before IPsec is applied so we can leverage AH |
| 5581 | * protection */ |
| 5582 | if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) |
| 5583 | return NF_DROP; |
| 5584 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5585 | return NF_ACCEPT; |
| 5586 | } |
| 5587 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5588 | static unsigned int selinux_ipv4_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5589 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5590 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5591 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5592 | return selinux_ip_forward(skb, state->in, PF_INET); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5593 | } |
| 5594 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5595 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5596 | static unsigned int selinux_ipv6_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5597 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5598 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5599 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5600 | return selinux_ip_forward(skb, state->in, PF_INET6); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5601 | } |
| 5602 | #endif /* IPV6 */ |
| 5603 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5604 | static unsigned int selinux_ip_output(struct sk_buff *skb, |
| 5605 | u16 family) |
| 5606 | { |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5607 | struct sock *sk; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5608 | u32 sid; |
| 5609 | |
| 5610 | if (!netlbl_enabled()) |
| 5611 | return NF_ACCEPT; |
| 5612 | |
| 5613 | /* we do this in the LOCAL_OUT path and not the POST_ROUTING path |
| 5614 | * because we want to make sure we apply the necessary labeling |
| 5615 | * before IPsec is applied so we can leverage AH protection */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5616 | sk = skb->sk; |
| 5617 | if (sk) { |
| 5618 | struct sk_security_struct *sksec; |
| 5619 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5620 | if (sk_listener(sk)) |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5621 | /* if the socket is the listening state then this |
| 5622 | * packet is a SYN-ACK packet which means it needs to |
| 5623 | * be labeled based on the connection/request_sock and |
| 5624 | * not the parent socket. unfortunately, we can't |
| 5625 | * lookup the request_sock yet as it isn't queued on |
| 5626 | * the parent socket until after the SYN-ACK is sent. |
| 5627 | * the "solution" is to simply pass the packet as-is |
| 5628 | * as any IP option based labeling should be copied |
| 5629 | * from the initial connection request (in the IP |
| 5630 | * layer). it is far from ideal, but until we get a |
| 5631 | * security label in the packet itself this is the |
| 5632 | * best we can do. */ |
| 5633 | return NF_ACCEPT; |
| 5634 | |
| 5635 | /* standard practice, label using the parent socket */ |
| 5636 | sksec = sk->sk_security; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5637 | sid = sksec->sid; |
| 5638 | } else |
| 5639 | sid = SECINITSID_KERNEL; |
| 5640 | if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) |
| 5641 | return NF_DROP; |
| 5642 | |
| 5643 | return NF_ACCEPT; |
| 5644 | } |
| 5645 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5646 | static unsigned int selinux_ipv4_output(void *priv, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5647 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5648 | const struct nf_hook_state *state) |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5649 | { |
| 5650 | return selinux_ip_output(skb, PF_INET); |
| 5651 | } |
| 5652 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5653 | #if IS_ENABLED(CONFIG_IPV6) |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 5654 | static unsigned int selinux_ipv6_output(void *priv, |
| 5655 | struct sk_buff *skb, |
| 5656 | const struct nf_hook_state *state) |
| 5657 | { |
| 5658 | return selinux_ip_output(skb, PF_INET6); |
| 5659 | } |
| 5660 | #endif /* IPV6 */ |
| 5661 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5662 | static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, |
| 5663 | int ifindex, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5664 | u16 family) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5665 | { |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5666 | struct sock *sk = skb_to_full_sk(skb); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5667 | struct sk_security_struct *sksec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5668 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5669 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5670 | char *addrp; |
| 5671 | u8 proto; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5672 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5673 | if (sk == NULL) |
| 5674 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5675 | sksec = sk->sk_security; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5676 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5677 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5678 | ad.u.net = &net; |
| 5679 | ad.u.net->netif = ifindex; |
| 5680 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5681 | if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) |
| 5682 | return NF_DROP; |
| 5683 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 5684 | if (selinux_secmark_enabled()) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5685 | if (avc_has_perm(&selinux_state, |
| 5686 | sksec->sid, skb->secmark, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5687 | SECCLASS_PACKET, PACKET__SEND, &ad)) |
Eric Paris | 2fe66ec | 2010-11-23 06:28:08 +0000 | [diff] [blame] | 5688 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5689 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 5690 | if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) |
| 5691 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5692 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5693 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | } |
| 5695 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5696 | static unsigned int selinux_ip_postroute(struct sk_buff *skb, |
| 5697 | const struct net_device *outdev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5698 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5700 | u32 secmark_perm; |
| 5701 | u32 peer_sid; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5702 | int ifindex = outdev->ifindex; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5703 | struct sock *sk; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5704 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5705 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5706 | char *addrp; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5707 | u8 secmark_active; |
| 5708 | u8 peerlbl_active; |
| 5709 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5710 | /* If any sort of compatibility mode is enabled then handoff processing |
| 5711 | * to the selinux_ip_postroute_compat() function to deal with the |
| 5712 | * special handling. We do this in an attempt to keep this function |
| 5713 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5714 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5715 | return selinux_ip_postroute_compat(skb, ifindex, family); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5716 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5717 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5718 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5719 | if (!secmark_active && !peerlbl_active) |
| 5720 | return NF_ACCEPT; |
| 5721 | |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5722 | sk = skb_to_full_sk(skb); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5723 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5724 | #ifdef CONFIG_XFRM |
| 5725 | /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec |
| 5726 | * packet transformation so allow the packet to pass without any checks |
| 5727 | * since we'll have another chance to perform access control checks |
| 5728 | * when the packet is on it's final way out. |
| 5729 | * NOTE: there appear to be some IPv6 multicast cases where skb->dst |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5730 | * is NULL, in this case go ahead and apply access control. |
| 5731 | * NOTE: if this is a local socket (skb->sk != NULL) that is in the |
| 5732 | * TCP listening state we cannot wait until the XFRM processing |
| 5733 | * is done as we will miss out on the SA label if we do; |
| 5734 | * unfortunately, this means more work, but it is only once per |
| 5735 | * connection. */ |
| 5736 | if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5737 | !(sk && sk_listener(sk))) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5738 | return NF_ACCEPT; |
| 5739 | #endif |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5740 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5741 | if (sk == NULL) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5742 | /* Without an associated socket the packet is either coming |
| 5743 | * from the kernel or it is being forwarded; check the packet |
| 5744 | * to determine which and if the packet is being forwarded |
| 5745 | * query the packet directly to determine the security label. */ |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5746 | if (skb->skb_iif) { |
| 5747 | secmark_perm = PACKET__FORWARD_OUT; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5748 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5749 | return NF_DROP; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5750 | } else { |
| 5751 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5752 | peer_sid = SECINITSID_KERNEL; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5753 | } |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5754 | } else if (sk_listener(sk)) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5755 | /* Locally generated packet but the associated socket is in the |
| 5756 | * listening state which means this is a SYN-ACK packet. In |
| 5757 | * this particular case the correct security label is assigned |
| 5758 | * to the connection/request_sock but unfortunately we can't |
| 5759 | * query the request_sock as it isn't queued on the parent |
| 5760 | * socket until after the SYN-ACK packet is sent; the only |
| 5761 | * viable choice is to regenerate the label like we do in |
| 5762 | * selinux_inet_conn_request(). See also selinux_ip_output() |
| 5763 | * for similar problems. */ |
| 5764 | u32 skb_sid; |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5765 | struct sk_security_struct *sksec; |
| 5766 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5767 | sksec = sk->sk_security; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5768 | if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) |
| 5769 | return NF_DROP; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5770 | /* At this point, if the returned skb peerlbl is SECSID_NULL |
| 5771 | * and the packet has been through at least one XFRM |
| 5772 | * transformation then we must be dealing with the "final" |
| 5773 | * form of labeled IPsec packet; since we've already applied |
| 5774 | * all of our access controls on this packet we can safely |
| 5775 | * pass the packet. */ |
| 5776 | if (skb_sid == SECSID_NULL) { |
| 5777 | switch (family) { |
| 5778 | case PF_INET: |
| 5779 | if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) |
| 5780 | return NF_ACCEPT; |
| 5781 | break; |
| 5782 | case PF_INET6: |
| 5783 | if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) |
| 5784 | return NF_ACCEPT; |
Paul Moore | a7a91a1 | 2014-09-03 10:51:59 -0400 | [diff] [blame] | 5785 | break; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5786 | default: |
| 5787 | return NF_DROP_ERR(-ECONNREFUSED); |
| 5788 | } |
| 5789 | } |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5790 | if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) |
| 5791 | return NF_DROP; |
| 5792 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5793 | } else { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5794 | /* Locally generated packet, fetch the security label from the |
| 5795 | * associated socket. */ |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5796 | struct sk_security_struct *sksec = sk->sk_security; |
| 5797 | peer_sid = sksec->sid; |
| 5798 | secmark_perm = PACKET__SEND; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5799 | } |
| 5800 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5801 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5802 | ad.u.net = &net; |
| 5803 | ad.u.net->netif = ifindex; |
| 5804 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5805 | if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5806 | return NF_DROP; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5807 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5808 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5809 | if (avc_has_perm(&selinux_state, |
| 5810 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5811 | SECCLASS_PACKET, secmark_perm, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5812 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5813 | |
| 5814 | if (peerlbl_active) { |
| 5815 | u32 if_sid; |
| 5816 | u32 node_sid; |
| 5817 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5818 | if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5819 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5820 | if (avc_has_perm(&selinux_state, |
| 5821 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5822 | SECCLASS_NETIF, NETIF__EGRESS, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5823 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5824 | |
| 5825 | if (sel_netnode_sid(addrp, family, &node_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5826 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5827 | if (avc_has_perm(&selinux_state, |
| 5828 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5829 | SECCLASS_NODE, NODE__SENDTO, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5830 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | return NF_ACCEPT; |
| 5834 | } |
| 5835 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5836 | static unsigned int selinux_ipv4_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5837 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5838 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5839 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5840 | return selinux_ip_postroute(skb, state->out, PF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | } |
| 5842 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5843 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5844 | static unsigned int selinux_ipv6_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5845 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5846 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5848 | return selinux_ip_postroute(skb, state->out, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | #endif /* IPV6 */ |
| 5851 | |
| 5852 | #endif /* CONFIG_NETFILTER */ |
| 5853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 5855 | { |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5856 | int rc = 0; |
| 5857 | unsigned int msg_len; |
| 5858 | unsigned int data_len = skb->len; |
| 5859 | unsigned char *data = skb->data; |
Huaisheng Ye | df4779b | 2020-01-13 23:03:31 +0800 | [diff] [blame] | 5860 | struct nlmsghdr *nlh; |
| 5861 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5862 | u16 sclass = sksec->sclass; |
| 5863 | u32 perm; |
Huaisheng Ye | df4779b | 2020-01-13 23:03:31 +0800 | [diff] [blame] | 5864 | |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5865 | while (data_len >= nlmsg_total_size(0)) { |
| 5866 | nlh = (struct nlmsghdr *)data; |
Huaisheng Ye | df4779b | 2020-01-13 23:03:31 +0800 | [diff] [blame] | 5867 | |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5868 | /* NOTE: the nlmsg_len field isn't reliably set by some netlink |
| 5869 | * users which means we can't reject skb's with bogus |
| 5870 | * length fields; our solution is to follow what |
| 5871 | * netlink_rcv_skb() does and simply skip processing at |
| 5872 | * messages with length fields that are clearly junk |
| 5873 | */ |
| 5874 | if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len) |
| 5875 | return 0; |
| 5876 | |
| 5877 | rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm); |
| 5878 | if (rc == 0) { |
| 5879 | rc = sock_has_perm(sk, perm); |
| 5880 | if (rc) |
| 5881 | return rc; |
| 5882 | } else if (rc == -EINVAL) { |
| 5883 | /* -EINVAL is a missing msg/perm mapping */ |
Huaisheng Ye | df4779b | 2020-01-13 23:03:31 +0800 | [diff] [blame] | 5884 | pr_warn_ratelimited("SELinux: unrecognized netlink" |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5885 | " message: protocol=%hu nlmsg_type=%hu sclass=%s" |
| 5886 | " pid=%d comm=%s\n", |
| 5887 | sk->sk_protocol, nlh->nlmsg_type, |
| 5888 | secclass_map[sclass - 1].name, |
| 5889 | task_pid_nr(current), current->comm); |
| 5890 | if (enforcing_enabled(&selinux_state) && |
| 5891 | !security_get_allow_unknown(&selinux_state)) |
| 5892 | return rc; |
| 5893 | rc = 0; |
| 5894 | } else if (rc == -ENOENT) { |
| 5895 | /* -ENOENT is a missing socket/class mapping, ignore */ |
| 5896 | rc = 0; |
| 5897 | } else { |
| 5898 | return rc; |
Huaisheng Ye | df4779b | 2020-01-13 23:03:31 +0800 | [diff] [blame] | 5899 | } |
| 5900 | |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5901 | /* move to the next message after applying netlink padding */ |
| 5902 | msg_len = NLMSG_ALIGN(nlh->nlmsg_len); |
| 5903 | if (msg_len >= data_len) |
| 5904 | return 0; |
| 5905 | data_len -= msg_len; |
| 5906 | data += msg_len; |
Huaisheng Ye | df4779b | 2020-01-13 23:03:31 +0800 | [diff] [blame] | 5907 | } |
| 5908 | |
Paul Moore | fb73974 | 2020-04-28 09:59:02 -0400 | [diff] [blame] | 5909 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | } |
| 5911 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5912 | static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | isec->sclass = sclass; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5915 | isec->sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | } |
| 5917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5919 | u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5922 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5923 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 5925 | isec = selinux_ipc(ipc_perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5927 | ad.type = LSM_AUDIT_DATA_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | ad.u.ipc_id = ipc_perms->key; |
| 5929 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5930 | return avc_has_perm(&selinux_state, |
| 5931 | sid, isec->sid, isec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | static int selinux_msg_msg_alloc_security(struct msg_msg *msg) |
| 5935 | { |
Huaisheng Ye | b82f3f6 | 2020-01-10 17:58:56 +0800 | [diff] [blame] | 5936 | struct msg_security_struct *msec; |
| 5937 | |
| 5938 | msec = selinux_msg_msg(msg); |
| 5939 | msec->sid = SECINITSID_UNLABELED; |
| 5940 | |
| 5941 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | } |
| 5943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | /* message queue security operations */ |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5945 | static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5948 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5949 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | int rc; |
| 5951 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5952 | isec = selinux_ipc(msq); |
| 5953 | ipc_init_security(isec, SECCLASS_MSGQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5955 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5956 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5958 | rc = avc_has_perm(&selinux_state, |
| 5959 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | MSGQ__CREATE, &ad); |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 5961 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | } |
| 5963 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5964 | 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] | 5965 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5967 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5968 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 5970 | isec = selinux_ipc(msq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5972 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5973 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5975 | return avc_has_perm(&selinux_state, |
| 5976 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | MSGQ__ASSOCIATE, &ad); |
| 5978 | } |
| 5979 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5980 | 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] | 5981 | { |
| 5982 | int err; |
| 5983 | int perms; |
| 5984 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5985 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | case IPC_INFO: |
| 5987 | case MSG_INFO: |
| 5988 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5989 | return avc_has_perm(&selinux_state, |
| 5990 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5991 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | case IPC_STAT: |
| 5993 | case MSG_STAT: |
Davidlohr Bueso | 23c8cec | 2018-04-10 16:35:30 -0700 | [diff] [blame] | 5994 | case MSG_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; |
| 5996 | break; |
| 5997 | case IPC_SET: |
| 5998 | perms = MSGQ__SETATTR; |
| 5999 | break; |
| 6000 | case IPC_RMID: |
| 6001 | perms = MSGQ__DESTROY; |
| 6002 | break; |
| 6003 | default: |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 6007 | err = ipc_has_perm(msq, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | return err; |
| 6009 | } |
| 6010 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 6011 | 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] | 6012 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | struct ipc_security_struct *isec; |
| 6014 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6015 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6016 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | int rc; |
| 6018 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6019 | isec = selinux_ipc(msq); |
| 6020 | msec = selinux_msg_msg(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | |
| 6022 | /* |
| 6023 | * First time through, need to assign label to the message |
| 6024 | */ |
| 6025 | if (msec->sid == SECINITSID_UNLABELED) { |
| 6026 | /* |
| 6027 | * Compute new sid based on current process and |
| 6028 | * message queue this message will be stored in |
| 6029 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6030 | rc = security_transition_sid(&selinux_state, sid, isec->sid, |
| 6031 | SECCLASS_MSG, NULL, &msec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | if (rc) |
| 6033 | return rc; |
| 6034 | } |
| 6035 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6036 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 6037 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | |
| 6039 | /* Can this process write to the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6040 | rc = avc_has_perm(&selinux_state, |
| 6041 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | MSGQ__WRITE, &ad); |
| 6043 | if (!rc) |
| 6044 | /* Can this process send the message */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6045 | rc = avc_has_perm(&selinux_state, |
| 6046 | sid, msec->sid, SECCLASS_MSG, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6047 | MSG__SEND, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | if (!rc) |
| 6049 | /* Can the message be put in the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6050 | rc = avc_has_perm(&selinux_state, |
| 6051 | msec->sid, isec->sid, SECCLASS_MSGQ, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6052 | MSGQ__ENQUEUE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | |
| 6054 | return rc; |
| 6055 | } |
| 6056 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 6057 | 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] | 6058 | struct task_struct *target, |
| 6059 | long type, int mode) |
| 6060 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | struct ipc_security_struct *isec; |
| 6062 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6063 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6064 | u32 sid = task_sid(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | int rc; |
| 6066 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6067 | isec = selinux_ipc(msq); |
| 6068 | msec = selinux_msg_msg(msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6070 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 6071 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6073 | rc = avc_has_perm(&selinux_state, |
| 6074 | sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | SECCLASS_MSGQ, MSGQ__READ, &ad); |
| 6076 | if (!rc) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6077 | rc = avc_has_perm(&selinux_state, |
| 6078 | sid, msec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | SECCLASS_MSG, MSG__RECEIVE, &ad); |
| 6080 | return rc; |
| 6081 | } |
| 6082 | |
| 6083 | /* Shared Memory security operations */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6084 | static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6087 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6088 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | int rc; |
| 6090 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6091 | isec = selinux_ipc(shp); |
| 6092 | ipc_init_security(isec, SECCLASS_SHM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6094 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6095 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6097 | rc = avc_has_perm(&selinux_state, |
| 6098 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | SHM__CREATE, &ad); |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6100 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | } |
| 6102 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6103 | static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6106 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6107 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6109 | isec = selinux_ipc(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6111 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6112 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6114 | return avc_has_perm(&selinux_state, |
| 6115 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | SHM__ASSOCIATE, &ad); |
| 6117 | } |
| 6118 | |
| 6119 | /* Note, at this point, shp is locked down */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6120 | static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | { |
| 6122 | int perms; |
| 6123 | int err; |
| 6124 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6125 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | case IPC_INFO: |
| 6127 | case SHM_INFO: |
| 6128 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6129 | return avc_has_perm(&selinux_state, |
| 6130 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6131 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | case IPC_STAT: |
| 6133 | case SHM_STAT: |
Davidlohr Bueso | c21a697 | 2018-04-10 16:35:23 -0700 | [diff] [blame] | 6134 | case SHM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | perms = SHM__GETATTR | SHM__ASSOCIATE; |
| 6136 | break; |
| 6137 | case IPC_SET: |
| 6138 | perms = SHM__SETATTR; |
| 6139 | break; |
| 6140 | case SHM_LOCK: |
| 6141 | case SHM_UNLOCK: |
| 6142 | perms = SHM__LOCK; |
| 6143 | break; |
| 6144 | case IPC_RMID: |
| 6145 | perms = SHM__DESTROY; |
| 6146 | break; |
| 6147 | default: |
| 6148 | return 0; |
| 6149 | } |
| 6150 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6151 | err = ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | return err; |
| 6153 | } |
| 6154 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6155 | static int selinux_shm_shmat(struct kern_ipc_perm *shp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | char __user *shmaddr, int shmflg) |
| 6157 | { |
| 6158 | u32 perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | |
| 6160 | if (shmflg & SHM_RDONLY) |
| 6161 | perms = SHM__READ; |
| 6162 | else |
| 6163 | perms = SHM__READ | SHM__WRITE; |
| 6164 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6165 | return ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | /* Semaphore security operations */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6169 | static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6172 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6173 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | int rc; |
| 6175 | |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6176 | isec = selinux_ipc(sma); |
| 6177 | ipc_init_security(isec, SECCLASS_SEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6179 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6180 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6182 | rc = avc_has_perm(&selinux_state, |
| 6183 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | SEM__CREATE, &ad); |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6185 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | } |
| 6187 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6188 | static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6191 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6192 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6194 | isec = selinux_ipc(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6196 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6197 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6199 | return avc_has_perm(&selinux_state, |
| 6200 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6201 | SEM__ASSOCIATE, &ad); |
| 6202 | } |
| 6203 | |
| 6204 | /* Note, at this point, sma is locked down */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6205 | static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6206 | { |
| 6207 | int err; |
| 6208 | u32 perms; |
| 6209 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6210 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | case IPC_INFO: |
| 6212 | case SEM_INFO: |
| 6213 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6214 | return avc_has_perm(&selinux_state, |
| 6215 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6216 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | case GETPID: |
| 6218 | case GETNCNT: |
| 6219 | case GETZCNT: |
| 6220 | perms = SEM__GETATTR; |
| 6221 | break; |
| 6222 | case GETVAL: |
| 6223 | case GETALL: |
| 6224 | perms = SEM__READ; |
| 6225 | break; |
| 6226 | case SETVAL: |
| 6227 | case SETALL: |
| 6228 | perms = SEM__WRITE; |
| 6229 | break; |
| 6230 | case IPC_RMID: |
| 6231 | perms = SEM__DESTROY; |
| 6232 | break; |
| 6233 | case IPC_SET: |
| 6234 | perms = SEM__SETATTR; |
| 6235 | break; |
| 6236 | case IPC_STAT: |
| 6237 | case SEM_STAT: |
Davidlohr Bueso | a280d6d | 2018-04-10 16:35:26 -0700 | [diff] [blame] | 6238 | case SEM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | perms = SEM__GETATTR | SEM__ASSOCIATE; |
| 6240 | break; |
| 6241 | default: |
| 6242 | return 0; |
| 6243 | } |
| 6244 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6245 | err = ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | return err; |
| 6247 | } |
| 6248 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6249 | static int selinux_sem_semop(struct kern_ipc_perm *sma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | struct sembuf *sops, unsigned nsops, int alter) |
| 6251 | { |
| 6252 | u32 perms; |
| 6253 | |
| 6254 | if (alter) |
| 6255 | perms = SEM__READ | SEM__WRITE; |
| 6256 | else |
| 6257 | perms = SEM__READ; |
| 6258 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6259 | return ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | } |
| 6261 | |
| 6262 | static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) |
| 6263 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | u32 av = 0; |
| 6265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | av = 0; |
| 6267 | if (flag & S_IRUGO) |
| 6268 | av |= IPC__UNIX_READ; |
| 6269 | if (flag & S_IWUGO) |
| 6270 | av |= IPC__UNIX_WRITE; |
| 6271 | |
| 6272 | if (av == 0) |
| 6273 | return 0; |
| 6274 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 6275 | return ipc_has_perm(ipcp, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | } |
| 6277 | |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 6278 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 6279 | { |
Casey Schaufler | 7c65382 | 2018-09-21 17:19:45 -0700 | [diff] [blame] | 6280 | struct ipc_security_struct *isec = selinux_ipc(ipcp); |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 6281 | *secid = isec->sid; |
| 6282 | } |
| 6283 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6284 | static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6285 | { |
| 6286 | if (inode) |
| 6287 | inode_doinit_with_dentry(inode, dentry); |
| 6288 | } |
| 6289 | |
| 6290 | static int selinux_getprocattr(struct task_struct *p, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6291 | char *name, char **value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6293 | const struct task_security_struct *__tsec; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 6294 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | int error; |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6296 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6298 | rcu_read_lock(); |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 6299 | __tsec = selinux_cred(__task_cred(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6301 | if (current != p) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6302 | error = avc_has_perm(&selinux_state, |
| 6303 | current_sid(), __tsec->sid, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6304 | SECCLASS_PROCESS, PROCESS__GETATTR, NULL); |
| 6305 | if (error) |
| 6306 | goto bad; |
| 6307 | } |
| 6308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | if (!strcmp(name, "current")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6310 | sid = __tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | else if (!strcmp(name, "prev")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6312 | sid = __tsec->osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | else if (!strcmp(name, "exec")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6314 | sid = __tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6315 | else if (!strcmp(name, "fscreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6316 | sid = __tsec->create_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6317 | else if (!strcmp(name, "keycreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6318 | sid = __tsec->keycreate_sid; |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6319 | else if (!strcmp(name, "sockcreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6320 | sid = __tsec->sockcreate_sid; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6321 | else { |
| 6322 | error = -EINVAL; |
| 6323 | goto bad; |
| 6324 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6325 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | |
| 6327 | if (!sid) |
| 6328 | return 0; |
| 6329 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6330 | error = security_sid_to_context(&selinux_state, sid, value, &len); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6331 | if (error) |
| 6332 | return error; |
| 6333 | return len; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6334 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6335 | bad: |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6336 | rcu_read_unlock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6337 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | } |
| 6339 | |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6340 | static int selinux_setprocattr(const char *name, void *value, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | { |
| 6342 | struct task_security_struct *tsec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6343 | struct cred *new; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6344 | u32 mysid = current_sid(), sid = 0, ptsid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 | int error; |
| 6346 | char *str = value; |
| 6347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | /* |
| 6349 | * Basic control over ability to set these attributes at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | */ |
| 6351 | if (!strcmp(name, "exec")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6352 | error = avc_has_perm(&selinux_state, |
| 6353 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6354 | PROCESS__SETEXEC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6355 | else if (!strcmp(name, "fscreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6356 | error = avc_has_perm(&selinux_state, |
| 6357 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6358 | PROCESS__SETFSCREATE, NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6359 | else if (!strcmp(name, "keycreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6360 | error = avc_has_perm(&selinux_state, |
| 6361 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6362 | PROCESS__SETKEYCREATE, NULL); |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6363 | else if (!strcmp(name, "sockcreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6364 | error = avc_has_perm(&selinux_state, |
| 6365 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6366 | PROCESS__SETSOCKCREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | else if (!strcmp(name, "current")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6368 | error = avc_has_perm(&selinux_state, |
| 6369 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6370 | PROCESS__SETCURRENT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | else |
| 6372 | error = -EINVAL; |
| 6373 | if (error) |
| 6374 | return error; |
| 6375 | |
| 6376 | /* Obtain a SID for the context, if one was specified. */ |
Stephen Smalley | a050a57 | 2017-01-31 11:54:04 -0500 | [diff] [blame] | 6377 | if (size && str[0] && str[0] != '\n') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | if (str[size-1] == '\n') { |
| 6379 | str[size-1] = 0; |
| 6380 | size--; |
| 6381 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6382 | error = security_context_to_sid(&selinux_state, value, size, |
| 6383 | &sid, GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6384 | if (error == -EINVAL && !strcmp(name, "fscreate")) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6385 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6386 | struct audit_buffer *ab; |
| 6387 | size_t audit_size; |
| 6388 | |
| 6389 | /* We strip a nul only if it is at the end, otherwise the |
| 6390 | * context contains a nul and we should audit that */ |
| 6391 | if (str[size - 1] == '\0') |
| 6392 | audit_size = size - 1; |
| 6393 | else |
| 6394 | audit_size = size; |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 6395 | ab = audit_log_start(audit_context(), |
| 6396 | GFP_ATOMIC, |
| 6397 | AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6398 | audit_log_format(ab, "op=fscreate invalid_context="); |
| 6399 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 6400 | audit_log_end(ab); |
| 6401 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6402 | return error; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6403 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6404 | error = security_context_to_sid_force( |
| 6405 | &selinux_state, |
| 6406 | value, size, &sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | if (error) |
| 6409 | return error; |
| 6410 | } |
| 6411 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6412 | new = prepare_creds(); |
| 6413 | if (!new) |
| 6414 | return -ENOMEM; |
| 6415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | /* Permission checking based on the specified context is |
| 6417 | performed during the actual operation (execve, |
| 6418 | open/mkdir/...), when we know the full context of the |
Eric W. Biederman | b8bff59 | 2020-03-22 15:46:24 -0500 | [diff] [blame] | 6419 | operation. See selinux_bprm_creds_for_exec for the execve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | checks and may_create for the file creation checks. The |
| 6421 | operation will then fail if the context is not permitted. */ |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 6422 | tsec = selinux_cred(new); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6423 | if (!strcmp(name, "exec")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | tsec->exec_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6425 | } else if (!strcmp(name, "fscreate")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | tsec->create_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6427 | } else if (!strcmp(name, "keycreate")) { |
Ondrej Mosnacek | 464c258 | 2019-06-12 10:12:26 +0200 | [diff] [blame] | 6428 | if (sid) { |
| 6429 | error = avc_has_perm(&selinux_state, mysid, sid, |
| 6430 | SECCLASS_KEY, KEY__CREATE, NULL); |
| 6431 | if (error) |
| 6432 | goto abort_change; |
| 6433 | } |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6434 | tsec->keycreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6435 | } else if (!strcmp(name, "sockcreate")) { |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6436 | tsec->sockcreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6437 | } else if (!strcmp(name, "current")) { |
| 6438 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | if (sid == 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6440 | goto abort_change; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 6441 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6442 | /* Only allow single threaded processes to change context */ |
| 6443 | error = -EPERM; |
Oleg Nesterov | 5bb459b | 2009-07-10 03:48:23 +0200 | [diff] [blame] | 6444 | if (!current_is_single_threaded()) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6445 | error = security_bounded_transition(&selinux_state, |
| 6446 | tsec->sid, sid); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6447 | if (error) |
| 6448 | goto abort_change; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | |
| 6451 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6452 | error = avc_has_perm(&selinux_state, |
| 6453 | tsec->sid, sid, SECCLASS_PROCESS, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6454 | PROCESS__DYNTRANSITION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6456 | goto abort_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | |
| 6458 | /* Check for ptracing, and update the task SID if ok. |
| 6459 | Otherwise, leave SID unchanged and fail. */ |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6460 | ptsid = ptrace_parent_sid(); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 6461 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6462 | error = avc_has_perm(&selinux_state, |
| 6463 | ptsid, sid, SECCLASS_PROCESS, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6464 | PROCESS__PTRACE, NULL); |
| 6465 | if (error) |
| 6466 | goto abort_change; |
| 6467 | } |
| 6468 | |
| 6469 | tsec->sid = sid; |
| 6470 | } else { |
| 6471 | error = -EINVAL; |
| 6472 | goto abort_change; |
| 6473 | } |
| 6474 | |
| 6475 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | return size; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6477 | |
| 6478 | abort_change: |
| 6479 | abort_creds(new); |
| 6480 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | } |
| 6482 | |
David Quigley | 746df9b | 2013-05-22 12:50:35 -0400 | [diff] [blame] | 6483 | static int selinux_ismaclabel(const char *name) |
| 6484 | { |
| 6485 | return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); |
| 6486 | } |
| 6487 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6488 | static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 6489 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6490 | return security_sid_to_context(&selinux_state, secid, |
| 6491 | secdata, seclen); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6492 | } |
| 6493 | |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 6494 | 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] | 6495 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6496 | return security_context_to_sid(&selinux_state, secdata, seclen, |
| 6497 | secid, GFP_KERNEL); |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6500 | static void selinux_release_secctx(char *secdata, u32 seclen) |
| 6501 | { |
Paul Moore | 088999e | 2007-08-01 11:12:58 -0400 | [diff] [blame] | 6502 | kfree(secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6503 | } |
| 6504 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6505 | static void selinux_inode_invalidate_secctx(struct inode *inode) |
| 6506 | { |
Casey Schaufler | 80788c2 | 2018-09-21 17:19:11 -0700 | [diff] [blame] | 6507 | struct inode_security_struct *isec = selinux_inode(inode); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6508 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6509 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6510 | isec->initialized = LABEL_INVALID; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6511 | spin_unlock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6512 | } |
| 6513 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 6514 | /* |
| 6515 | * called with inode->i_mutex locked |
| 6516 | */ |
| 6517 | static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 6518 | { |
Ondrej Mosnacek | 53e0c2a | 2018-12-21 21:18:53 +0100 | [diff] [blame] | 6519 | int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 6520 | ctx, ctxlen, 0); |
| 6521 | /* Do not return error when suppressing label (SBLABEL_MNT not set). */ |
| 6522 | return rc == -EOPNOTSUPP ? 0 : rc; |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 6523 | } |
| 6524 | |
| 6525 | /* |
| 6526 | * called with inode->i_mutex locked |
| 6527 | */ |
| 6528 | static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 6529 | { |
| 6530 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); |
| 6531 | } |
| 6532 | |
| 6533 | static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 6534 | { |
| 6535 | int len = 0; |
| 6536 | len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 6537 | ctx, true); |
| 6538 | if (len < 0) |
| 6539 | return len; |
| 6540 | *ctxlen = len; |
| 6541 | return 0; |
| 6542 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6543 | #ifdef CONFIG_KEYS |
| 6544 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6545 | static int selinux_key_alloc(struct key *k, const struct cred *cred, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 6546 | unsigned long flags) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6547 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6548 | const struct task_security_struct *tsec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6549 | struct key_security_struct *ksec; |
| 6550 | |
| 6551 | ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); |
| 6552 | if (!ksec) |
| 6553 | return -ENOMEM; |
| 6554 | |
Casey Schaufler | 0c6cfa6 | 2018-09-21 17:17:16 -0700 | [diff] [blame] | 6555 | tsec = selinux_cred(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6556 | if (tsec->keycreate_sid) |
| 6557 | ksec->sid = tsec->keycreate_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6558 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6559 | ksec->sid = tsec->sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6560 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6561 | k->security = ksec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6562 | return 0; |
| 6563 | } |
| 6564 | |
| 6565 | static void selinux_key_free(struct key *k) |
| 6566 | { |
| 6567 | struct key_security_struct *ksec = k->security; |
| 6568 | |
| 6569 | k->security = NULL; |
| 6570 | kfree(ksec); |
| 6571 | } |
| 6572 | |
| 6573 | static int selinux_key_permission(key_ref_t key_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6574 | const struct cred *cred, |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 6575 | enum key_need_perm need_perm) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6576 | { |
| 6577 | struct key *key; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6578 | struct key_security_struct *ksec; |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 6579 | u32 perm, sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6580 | |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 6581 | switch (need_perm) { |
| 6582 | case KEY_NEED_VIEW: |
| 6583 | perm = KEY__VIEW; |
| 6584 | break; |
| 6585 | case KEY_NEED_READ: |
| 6586 | perm = KEY__READ; |
| 6587 | break; |
| 6588 | case KEY_NEED_WRITE: |
| 6589 | perm = KEY__WRITE; |
| 6590 | break; |
| 6591 | case KEY_NEED_SEARCH: |
| 6592 | perm = KEY__SEARCH; |
| 6593 | break; |
| 6594 | case KEY_NEED_LINK: |
| 6595 | perm = KEY__LINK; |
| 6596 | break; |
| 6597 | case KEY_NEED_SETATTR: |
| 6598 | perm = KEY__SETATTR; |
| 6599 | break; |
| 6600 | case KEY_NEED_UNLINK: |
| 6601 | case KEY_SYSADMIN_OVERRIDE: |
| 6602 | case KEY_AUTHTOKEN_OVERRIDE: |
| 6603 | case KEY_DEFER_PERM_CHECK: |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6604 | return 0; |
David Howells | 8c0637e | 2020-05-12 15:16:29 +0100 | [diff] [blame] | 6605 | default: |
| 6606 | WARN_ON(1); |
| 6607 | return -EPERM; |
| 6608 | |
| 6609 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6610 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6611 | sid = cred_sid(cred); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6612 | key = key_ref_to_ptr(key_ref); |
| 6613 | ksec = key->security; |
| 6614 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6615 | return avc_has_perm(&selinux_state, |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 6616 | sid, ksec->sid, SECCLASS_KEY, perm, NULL); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6617 | } |
| 6618 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6619 | static int selinux_key_getsecurity(struct key *key, char **_buffer) |
| 6620 | { |
| 6621 | struct key_security_struct *ksec = key->security; |
| 6622 | char *context = NULL; |
| 6623 | unsigned len; |
| 6624 | int rc; |
| 6625 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6626 | rc = security_sid_to_context(&selinux_state, ksec->sid, |
| 6627 | &context, &len); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6628 | if (!rc) |
| 6629 | rc = len; |
| 6630 | *_buffer = context; |
| 6631 | return rc; |
| 6632 | } |
David Howells | 3e412cc | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 6633 | |
| 6634 | #ifdef CONFIG_KEY_NOTIFICATIONS |
| 6635 | static int selinux_watch_key(struct key *key) |
| 6636 | { |
| 6637 | struct key_security_struct *ksec = key->security; |
| 6638 | u32 sid = current_sid(); |
| 6639 | |
| 6640 | return avc_has_perm(&selinux_state, |
| 6641 | sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL); |
| 6642 | } |
| 6643 | #endif |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6644 | #endif |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6645 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6646 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6647 | static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) |
| 6648 | { |
| 6649 | struct common_audit_data ad; |
| 6650 | int err; |
| 6651 | u32 sid = 0; |
| 6652 | struct ib_security_struct *sec = ib_sec; |
| 6653 | struct lsm_ibpkey_audit ibpkey; |
| 6654 | |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 6655 | err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6656 | if (err) |
| 6657 | return err; |
| 6658 | |
| 6659 | ad.type = LSM_AUDIT_DATA_IBPKEY; |
| 6660 | ibpkey.subnet_prefix = subnet_prefix; |
| 6661 | ibpkey.pkey = pkey_val; |
| 6662 | ad.u.ibpkey = &ibpkey; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6663 | return avc_has_perm(&selinux_state, |
| 6664 | sec->sid, sid, |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6665 | SECCLASS_INFINIBAND_PKEY, |
| 6666 | INFINIBAND_PKEY__ACCESS, &ad); |
| 6667 | } |
| 6668 | |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6669 | static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, |
| 6670 | u8 port_num) |
| 6671 | { |
| 6672 | struct common_audit_data ad; |
| 6673 | int err; |
| 6674 | u32 sid = 0; |
| 6675 | struct ib_security_struct *sec = ib_sec; |
| 6676 | struct lsm_ibendport_audit ibendport; |
| 6677 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6678 | err = security_ib_endport_sid(&selinux_state, dev_name, port_num, |
| 6679 | &sid); |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6680 | |
| 6681 | if (err) |
| 6682 | return err; |
| 6683 | |
| 6684 | ad.type = LSM_AUDIT_DATA_IBENDPORT; |
| 6685 | strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); |
| 6686 | ibendport.port = port_num; |
| 6687 | ad.u.ibendport = &ibendport; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6688 | return avc_has_perm(&selinux_state, |
| 6689 | sec->sid, sid, |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6690 | SECCLASS_INFINIBAND_ENDPORT, |
| 6691 | INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); |
| 6692 | } |
| 6693 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6694 | static int selinux_ib_alloc_security(void **ib_sec) |
| 6695 | { |
| 6696 | struct ib_security_struct *sec; |
| 6697 | |
| 6698 | sec = kzalloc(sizeof(*sec), GFP_KERNEL); |
| 6699 | if (!sec) |
| 6700 | return -ENOMEM; |
| 6701 | sec->sid = current_sid(); |
| 6702 | |
| 6703 | *ib_sec = sec; |
| 6704 | return 0; |
| 6705 | } |
| 6706 | |
| 6707 | static void selinux_ib_free_security(void *ib_sec) |
| 6708 | { |
| 6709 | kfree(ib_sec); |
| 6710 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6711 | #endif |
| 6712 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6713 | #ifdef CONFIG_BPF_SYSCALL |
| 6714 | static int selinux_bpf(int cmd, union bpf_attr *attr, |
| 6715 | unsigned int size) |
| 6716 | { |
| 6717 | u32 sid = current_sid(); |
| 6718 | int ret; |
| 6719 | |
| 6720 | switch (cmd) { |
| 6721 | case BPF_MAP_CREATE: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6722 | ret = avc_has_perm(&selinux_state, |
| 6723 | sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6724 | NULL); |
| 6725 | break; |
| 6726 | case BPF_PROG_LOAD: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6727 | ret = avc_has_perm(&selinux_state, |
| 6728 | sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6729 | NULL); |
| 6730 | break; |
| 6731 | default: |
| 6732 | ret = 0; |
| 6733 | break; |
| 6734 | } |
| 6735 | |
| 6736 | return ret; |
| 6737 | } |
| 6738 | |
| 6739 | static u32 bpf_map_fmode_to_av(fmode_t fmode) |
| 6740 | { |
| 6741 | u32 av = 0; |
| 6742 | |
| 6743 | if (fmode & FMODE_READ) |
| 6744 | av |= BPF__MAP_READ; |
| 6745 | if (fmode & FMODE_WRITE) |
| 6746 | av |= BPF__MAP_WRITE; |
| 6747 | return av; |
| 6748 | } |
| 6749 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6750 | /* This function will check the file pass through unix socket or binder to see |
| 6751 | * if it is a bpf related object. And apply correspinding checks on the bpf |
| 6752 | * object based on the type. The bpf maps and programs, not like other files and |
| 6753 | * socket, are using a shared anonymous inode inside the kernel as their inode. |
| 6754 | * So checking that inode cannot identify if the process have privilege to |
| 6755 | * access the bpf object and that's why we have to add this additional check in |
| 6756 | * selinux_file_receive and selinux_binder_transfer_files. |
| 6757 | */ |
| 6758 | static int bpf_fd_pass(struct file *file, u32 sid) |
| 6759 | { |
| 6760 | struct bpf_security_struct *bpfsec; |
| 6761 | struct bpf_prog *prog; |
| 6762 | struct bpf_map *map; |
| 6763 | int ret; |
| 6764 | |
| 6765 | if (file->f_op == &bpf_map_fops) { |
| 6766 | map = file->private_data; |
| 6767 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6768 | ret = avc_has_perm(&selinux_state, |
| 6769 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6770 | bpf_map_fmode_to_av(file->f_mode), NULL); |
| 6771 | if (ret) |
| 6772 | return ret; |
| 6773 | } else if (file->f_op == &bpf_prog_fops) { |
| 6774 | prog = file->private_data; |
| 6775 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6776 | ret = avc_has_perm(&selinux_state, |
| 6777 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6778 | BPF__PROG_RUN, NULL); |
| 6779 | if (ret) |
| 6780 | return ret; |
| 6781 | } |
| 6782 | return 0; |
| 6783 | } |
| 6784 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6785 | static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) |
| 6786 | { |
| 6787 | u32 sid = current_sid(); |
| 6788 | struct bpf_security_struct *bpfsec; |
| 6789 | |
| 6790 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6791 | return avc_has_perm(&selinux_state, |
| 6792 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6793 | bpf_map_fmode_to_av(fmode), NULL); |
| 6794 | } |
| 6795 | |
| 6796 | static int selinux_bpf_prog(struct bpf_prog *prog) |
| 6797 | { |
| 6798 | u32 sid = current_sid(); |
| 6799 | struct bpf_security_struct *bpfsec; |
| 6800 | |
| 6801 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6802 | return avc_has_perm(&selinux_state, |
| 6803 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6804 | BPF__PROG_RUN, NULL); |
| 6805 | } |
| 6806 | |
| 6807 | static int selinux_bpf_map_alloc(struct bpf_map *map) |
| 6808 | { |
| 6809 | struct bpf_security_struct *bpfsec; |
| 6810 | |
| 6811 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6812 | if (!bpfsec) |
| 6813 | return -ENOMEM; |
| 6814 | |
| 6815 | bpfsec->sid = current_sid(); |
| 6816 | map->security = bpfsec; |
| 6817 | |
| 6818 | return 0; |
| 6819 | } |
| 6820 | |
| 6821 | static void selinux_bpf_map_free(struct bpf_map *map) |
| 6822 | { |
| 6823 | struct bpf_security_struct *bpfsec = map->security; |
| 6824 | |
| 6825 | map->security = NULL; |
| 6826 | kfree(bpfsec); |
| 6827 | } |
| 6828 | |
| 6829 | static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) |
| 6830 | { |
| 6831 | struct bpf_security_struct *bpfsec; |
| 6832 | |
| 6833 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6834 | if (!bpfsec) |
| 6835 | return -ENOMEM; |
| 6836 | |
| 6837 | bpfsec->sid = current_sid(); |
| 6838 | aux->security = bpfsec; |
| 6839 | |
| 6840 | return 0; |
| 6841 | } |
| 6842 | |
| 6843 | static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) |
| 6844 | { |
| 6845 | struct bpf_security_struct *bpfsec = aux->security; |
| 6846 | |
| 6847 | aux->security = NULL; |
| 6848 | kfree(bpfsec); |
| 6849 | } |
| 6850 | #endif |
| 6851 | |
Stephen Smalley | 59438b46 | 2019-11-27 12:04:36 -0500 | [diff] [blame] | 6852 | static int selinux_lockdown(enum lockdown_reason what) |
| 6853 | { |
| 6854 | struct common_audit_data ad; |
| 6855 | u32 sid = current_sid(); |
| 6856 | int invalid_reason = (what <= LOCKDOWN_NONE) || |
| 6857 | (what == LOCKDOWN_INTEGRITY_MAX) || |
| 6858 | (what >= LOCKDOWN_CONFIDENTIALITY_MAX); |
| 6859 | |
| 6860 | if (WARN(invalid_reason, "Invalid lockdown reason")) { |
| 6861 | audit_log(audit_context(), |
| 6862 | GFP_ATOMIC, AUDIT_SELINUX_ERR, |
| 6863 | "lockdown_reason=invalid"); |
| 6864 | return -EINVAL; |
| 6865 | } |
| 6866 | |
| 6867 | ad.type = LSM_AUDIT_DATA_LOCKDOWN; |
| 6868 | ad.u.reason = what; |
| 6869 | |
| 6870 | if (what <= LOCKDOWN_INTEGRITY_MAX) |
| 6871 | return avc_has_perm(&selinux_state, |
| 6872 | sid, sid, SECCLASS_LOCKDOWN, |
| 6873 | LOCKDOWN__INTEGRITY, &ad); |
| 6874 | else |
| 6875 | return avc_has_perm(&selinux_state, |
| 6876 | sid, sid, SECCLASS_LOCKDOWN, |
| 6877 | LOCKDOWN__CONFIDENTIALITY, &ad); |
| 6878 | } |
| 6879 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 6880 | struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { |
| 6881 | .lbs_cred = sizeof(struct task_security_struct), |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame] | 6882 | .lbs_file = sizeof(struct file_security_struct), |
Casey Schaufler | afb1cbe3 | 2018-09-21 17:19:29 -0700 | [diff] [blame] | 6883 | .lbs_inode = sizeof(struct inode_security_struct), |
Casey Schaufler | ecd5f82 | 2018-11-20 11:55:02 -0800 | [diff] [blame] | 6884 | .lbs_ipc = sizeof(struct ipc_security_struct), |
| 6885 | .lbs_msg_msg = sizeof(struct msg_security_struct), |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 6886 | }; |
| 6887 | |
Joel Fernandes (Google) | da97e18 | 2019-10-14 13:03:08 -0400 | [diff] [blame] | 6888 | #ifdef CONFIG_PERF_EVENTS |
| 6889 | static int selinux_perf_event_open(struct perf_event_attr *attr, int type) |
| 6890 | { |
| 6891 | u32 requested, sid = current_sid(); |
| 6892 | |
| 6893 | if (type == PERF_SECURITY_OPEN) |
| 6894 | requested = PERF_EVENT__OPEN; |
| 6895 | else if (type == PERF_SECURITY_CPU) |
| 6896 | requested = PERF_EVENT__CPU; |
| 6897 | else if (type == PERF_SECURITY_KERNEL) |
| 6898 | requested = PERF_EVENT__KERNEL; |
| 6899 | else if (type == PERF_SECURITY_TRACEPOINT) |
| 6900 | requested = PERF_EVENT__TRACEPOINT; |
| 6901 | else |
| 6902 | return -EINVAL; |
| 6903 | |
| 6904 | return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT, |
| 6905 | requested, NULL); |
| 6906 | } |
| 6907 | |
| 6908 | static int selinux_perf_event_alloc(struct perf_event *event) |
| 6909 | { |
| 6910 | struct perf_event_security_struct *perfsec; |
| 6911 | |
| 6912 | perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL); |
| 6913 | if (!perfsec) |
| 6914 | return -ENOMEM; |
| 6915 | |
| 6916 | perfsec->sid = current_sid(); |
| 6917 | event->security = perfsec; |
| 6918 | |
| 6919 | return 0; |
| 6920 | } |
| 6921 | |
| 6922 | static void selinux_perf_event_free(struct perf_event *event) |
| 6923 | { |
| 6924 | struct perf_event_security_struct *perfsec = event->security; |
| 6925 | |
| 6926 | event->security = NULL; |
| 6927 | kfree(perfsec); |
| 6928 | } |
| 6929 | |
| 6930 | static int selinux_perf_event_read(struct perf_event *event) |
| 6931 | { |
| 6932 | struct perf_event_security_struct *perfsec = event->security; |
| 6933 | u32 sid = current_sid(); |
| 6934 | |
| 6935 | return avc_has_perm(&selinux_state, sid, perfsec->sid, |
| 6936 | SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL); |
| 6937 | } |
| 6938 | |
| 6939 | static int selinux_perf_event_write(struct perf_event *event) |
| 6940 | { |
| 6941 | struct perf_event_security_struct *perfsec = event->security; |
| 6942 | u32 sid = current_sid(); |
| 6943 | |
| 6944 | return avc_has_perm(&selinux_state, sid, perfsec->sid, |
| 6945 | SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL); |
| 6946 | } |
| 6947 | #endif |
| 6948 | |
Ondrej Mosnacek | cfff75d | 2020-01-08 15:09:58 +0100 | [diff] [blame] | 6949 | /* |
| 6950 | * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order: |
| 6951 | * 1. any hooks that don't belong to (2.) or (3.) below, |
| 6952 | * 2. hooks that both access structures allocated by other hooks, and allocate |
| 6953 | * structures that can be later accessed by other hooks (mostly "cloning" |
| 6954 | * hooks), |
| 6955 | * 3. hooks that only allocate structures that can be later accessed by other |
| 6956 | * hooks ("allocating" hooks). |
| 6957 | * |
| 6958 | * Please follow block comment delimiters in the list to keep this order. |
| 6959 | * |
| 6960 | * This ordering is needed for SELinux runtime disable to work at least somewhat |
| 6961 | * safely. Breaking the ordering rules above might lead to NULL pointer derefs |
| 6962 | * when disabling SELinux at runtime. |
| 6963 | */ |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 6964 | static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6965 | LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), |
| 6966 | LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), |
| 6967 | LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), |
| 6968 | LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6969 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6970 | LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), |
| 6971 | LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), |
| 6972 | LSM_HOOK_INIT(capget, selinux_capget), |
| 6973 | LSM_HOOK_INIT(capset, selinux_capset), |
| 6974 | LSM_HOOK_INIT(capable, selinux_capable), |
| 6975 | LSM_HOOK_INIT(quotactl, selinux_quotactl), |
| 6976 | LSM_HOOK_INIT(quota_on, selinux_quota_on), |
| 6977 | LSM_HOOK_INIT(syslog, selinux_syslog), |
| 6978 | LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 6979 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6980 | LSM_HOOK_INIT(netlink_send, selinux_netlink_send), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | |
Eric W. Biederman | b8bff59 | 2020-03-22 15:46:24 -0500 | [diff] [blame] | 6982 | LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6983 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), |
| 6984 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6986 | LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 6987 | LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6988 | LSM_HOOK_INIT(sb_remount, selinux_sb_remount), |
| 6989 | LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), |
| 6990 | LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), |
| 6991 | LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), |
| 6992 | LSM_HOOK_INIT(sb_mount, selinux_mount), |
| 6993 | LSM_HOOK_INIT(sb_umount, selinux_umount), |
| 6994 | LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), |
| 6995 | LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | |
Stephen Smalley | 98aa003 | 2020-01-17 15:24:07 -0500 | [diff] [blame] | 6997 | LSM_HOOK_INIT(move_mount, selinux_move_mount), |
| 6998 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6999 | LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 7000 | LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 7001 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7002 | LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), |
| 7003 | LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), |
| 7004 | LSM_HOOK_INIT(inode_create, selinux_inode_create), |
| 7005 | LSM_HOOK_INIT(inode_link, selinux_inode_link), |
| 7006 | LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), |
| 7007 | LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), |
| 7008 | LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), |
| 7009 | LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), |
| 7010 | LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), |
| 7011 | LSM_HOOK_INIT(inode_rename, selinux_inode_rename), |
| 7012 | LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), |
| 7013 | LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), |
| 7014 | LSM_HOOK_INIT(inode_permission, selinux_inode_permission), |
| 7015 | LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), |
| 7016 | LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), |
| 7017 | LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), |
| 7018 | LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), |
| 7019 | LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), |
| 7020 | LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), |
| 7021 | LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), |
| 7022 | LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), |
| 7023 | LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), |
| 7024 | LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), |
| 7025 | LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 7026 | LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 7027 | LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), |
Aaron Goidel | ac5656d | 2019-08-12 11:20:00 -0400 | [diff] [blame] | 7028 | LSM_HOOK_INIT(path_notify, selinux_path_notify), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | |
Ondrej Mosnacek | ec882da | 2019-02-22 15:57:17 +0100 | [diff] [blame] | 7030 | LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security), |
| 7031 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7032 | LSM_HOOK_INIT(file_permission, selinux_file_permission), |
| 7033 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7034 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), |
| 7035 | LSM_HOOK_INIT(mmap_file, selinux_mmap_file), |
| 7036 | LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), |
| 7037 | LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), |
| 7038 | LSM_HOOK_INIT(file_lock, selinux_file_lock), |
| 7039 | LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), |
| 7040 | LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), |
| 7041 | LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), |
| 7042 | LSM_HOOK_INIT(file_receive, selinux_file_receive), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7043 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7044 | LSM_HOOK_INIT(file_open, selinux_file_open), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 7046 | LSM_HOOK_INIT(task_alloc, selinux_task_alloc), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7047 | LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), |
| 7048 | LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 7049 | LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7050 | LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), |
| 7051 | LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), |
| 7052 | LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 7053 | LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 7054 | LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7055 | LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), |
| 7056 | LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), |
| 7057 | LSM_HOOK_INIT(task_getsid, selinux_task_getsid), |
| 7058 | LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), |
| 7059 | LSM_HOOK_INIT(task_setnice, selinux_task_setnice), |
| 7060 | LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), |
| 7061 | LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 7062 | LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7063 | LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), |
| 7064 | LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), |
| 7065 | LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), |
| 7066 | LSM_HOOK_INIT(task_movememory, selinux_task_movememory), |
| 7067 | LSM_HOOK_INIT(task_kill, selinux_task_kill), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7068 | LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 7069 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7070 | LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), |
| 7071 | LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7072 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7073 | LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), |
| 7074 | LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), |
| 7075 | LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), |
| 7076 | LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7078 | LSM_HOOK_INIT(shm_associate, selinux_shm_associate), |
| 7079 | LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), |
| 7080 | LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7082 | LSM_HOOK_INIT(sem_associate, selinux_sem_associate), |
| 7083 | LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), |
| 7084 | LSM_HOOK_INIT(sem_semop, selinux_sem_semop), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7086 | LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7088 | LSM_HOOK_INIT(getprocattr, selinux_getprocattr), |
| 7089 | LSM_HOOK_INIT(setprocattr, selinux_setprocattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7090 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7091 | LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7092 | LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), |
| 7093 | LSM_HOOK_INIT(release_secctx, selinux_release_secctx), |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 7094 | LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7095 | LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), |
| 7096 | LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7098 | LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), |
| 7099 | LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 7100 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7101 | LSM_HOOK_INIT(socket_create, selinux_socket_create), |
| 7102 | LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 7103 | LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7104 | LSM_HOOK_INIT(socket_bind, selinux_socket_bind), |
| 7105 | LSM_HOOK_INIT(socket_connect, selinux_socket_connect), |
| 7106 | LSM_HOOK_INIT(socket_listen, selinux_socket_listen), |
| 7107 | LSM_HOOK_INIT(socket_accept, selinux_socket_accept), |
| 7108 | LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), |
| 7109 | LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), |
| 7110 | LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), |
| 7111 | LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), |
| 7112 | LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), |
| 7113 | LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), |
| 7114 | LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), |
| 7115 | LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), |
| 7116 | LSM_HOOK_INIT(socket_getpeersec_stream, |
| 7117 | selinux_socket_getpeersec_stream), |
| 7118 | LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7119 | LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), |
| 7120 | LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), |
| 7121 | LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), |
| 7122 | LSM_HOOK_INIT(sock_graft, selinux_sock_graft), |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 7123 | LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), |
| 7124 | LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), |
| 7125 | LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7126 | LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), |
| 7127 | LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), |
| 7128 | LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), |
| 7129 | LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), |
| 7130 | LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), |
| 7131 | LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), |
| 7132 | LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7133 | LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), |
| 7134 | LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), |
| 7135 | LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), |
| 7136 | LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), |
| 7137 | LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 7138 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 7139 | LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 7140 | LSM_HOOK_INIT(ib_endport_manage_subnet, |
| 7141 | selinux_ib_endport_manage_subnet), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 7142 | LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), |
| 7143 | #endif |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 7144 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7145 | LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), |
| 7146 | LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7147 | LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), |
| 7148 | LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), |
| 7149 | LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), |
| 7150 | LSM_HOOK_INIT(xfrm_state_pol_flow_match, |
| 7151 | selinux_xfrm_state_pol_flow_match), |
| 7152 | LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 7154 | |
| 7155 | #ifdef CONFIG_KEYS |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7156 | LSM_HOOK_INIT(key_free, selinux_key_free), |
| 7157 | LSM_HOOK_INIT(key_permission, selinux_key_permission), |
| 7158 | LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), |
David Howells | 3e412cc | 2020-01-14 17:07:13 +0000 | [diff] [blame] | 7159 | #ifdef CONFIG_KEY_NOTIFICATIONS |
| 7160 | LSM_HOOK_INIT(watch_key, selinux_watch_key), |
| 7161 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 7162 | #endif |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 7163 | |
| 7164 | #ifdef CONFIG_AUDIT |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 7165 | LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), |
| 7166 | LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), |
| 7167 | LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 7168 | #endif |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 7169 | |
| 7170 | #ifdef CONFIG_BPF_SYSCALL |
| 7171 | LSM_HOOK_INIT(bpf, selinux_bpf), |
| 7172 | LSM_HOOK_INIT(bpf_map, selinux_bpf_map), |
| 7173 | LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 7174 | LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), |
| 7175 | LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), |
| 7176 | #endif |
Joel Fernandes (Google) | da97e18 | 2019-10-14 13:03:08 -0400 | [diff] [blame] | 7177 | |
| 7178 | #ifdef CONFIG_PERF_EVENTS |
| 7179 | LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open), |
Joel Fernandes (Google) | da97e18 | 2019-10-14 13:03:08 -0400 | [diff] [blame] | 7180 | LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free), |
| 7181 | LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read), |
| 7182 | LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write), |
| 7183 | #endif |
Stephen Smalley | 59438b46 | 2019-11-27 12:04:36 -0500 | [diff] [blame] | 7184 | |
| 7185 | LSM_HOOK_INIT(locked_down, selinux_lockdown), |
Ondrej Mosnacek | cfff75d | 2020-01-08 15:09:58 +0100 | [diff] [blame] | 7186 | |
| 7187 | /* |
| 7188 | * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE |
| 7189 | */ |
| 7190 | LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), |
| 7191 | LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), |
| 7192 | LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), |
| 7193 | LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), |
| 7194 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 7195 | LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), |
| 7196 | #endif |
| 7197 | |
| 7198 | /* |
| 7199 | * PUT "ALLOCATING" HOOKS HERE |
| 7200 | */ |
| 7201 | LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), |
| 7202 | LSM_HOOK_INIT(msg_queue_alloc_security, |
| 7203 | selinux_msg_queue_alloc_security), |
| 7204 | LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), |
| 7205 | LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), |
| 7206 | LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), |
| 7207 | LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), |
| 7208 | LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), |
| 7209 | LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), |
| 7210 | LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), |
| 7211 | LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), |
| 7212 | #ifdef CONFIG_SECURITY_INFINIBAND |
| 7213 | LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), |
| 7214 | #endif |
| 7215 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 7216 | LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), |
| 7217 | LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), |
| 7218 | LSM_HOOK_INIT(xfrm_state_alloc_acquire, |
| 7219 | selinux_xfrm_state_alloc_acquire), |
| 7220 | #endif |
| 7221 | #ifdef CONFIG_KEYS |
| 7222 | LSM_HOOK_INIT(key_alloc, selinux_key_alloc), |
| 7223 | #endif |
| 7224 | #ifdef CONFIG_AUDIT |
| 7225 | LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), |
| 7226 | #endif |
| 7227 | #ifdef CONFIG_BPF_SYSCALL |
| 7228 | LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), |
| 7229 | LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), |
| 7230 | #endif |
| 7231 | #ifdef CONFIG_PERF_EVENTS |
| 7232 | LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc), |
| 7233 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | }; |
| 7235 | |
| 7236 | static __init int selinux_init(void) |
| 7237 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7238 | pr_info("SELinux: Initializing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7240 | memset(&selinux_state, 0, sizeof(selinux_state)); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 7241 | enforcing_set(&selinux_state, selinux_enforcing_boot); |
Lakshmi Ramasubramanian | 8861d0a | 2020-09-14 10:31:57 -0700 | [diff] [blame] | 7242 | checkreqprot_set(&selinux_state, selinux_checkreqprot_boot); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 7243 | selinux_avc_init(&selinux_state.avc); |
Ondrej Mosnacek | 4b36cb7 | 2020-01-17 14:15:14 +0100 | [diff] [blame] | 7244 | mutex_init(&selinux_state.status_lock); |
Stephen Smalley | 9ff9abc | 2020-08-26 13:28:53 -0400 | [diff] [blame] | 7245 | mutex_init(&selinux_state.policy_mutex); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7247 | /* Set the security state for the initial task. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 7248 | cred_init_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 7250 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 7251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | avc_init(); |
| 7253 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7254 | avtab_cache_init(); |
| 7255 | |
| 7256 | ebitmap_cache_init(); |
| 7257 | |
| 7258 | hashtab_cache_init(); |
| 7259 | |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 7260 | security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 7262 | if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) |
| 7263 | panic("SELinux: Unable to register AVC netcache callback\n"); |
| 7264 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 7265 | if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) |
| 7266 | panic("SELinux: Unable to register AVC LSM notifier callback\n"); |
| 7267 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7268 | if (selinux_enforcing_boot) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7269 | pr_debug("SELinux: Starting in enforcing mode\n"); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 7270 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7271 | pr_debug("SELinux: Starting in permissive mode\n"); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 7272 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 7273 | fs_validate_description("selinux", selinux_fs_parameters); |
David Howells | 442155c | 2018-11-01 23:07:24 +0000 | [diff] [blame] | 7274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7275 | return 0; |
| 7276 | } |
| 7277 | |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7278 | static void delayed_superblock_init(struct super_block *sb, void *unused) |
| 7279 | { |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 7280 | selinux_set_mnt_opts(sb, NULL, 0, NULL); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7281 | } |
| 7282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | void selinux_complete_init(void) |
| 7284 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7285 | pr_debug("SELinux: Completing initialization.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7286 | |
| 7287 | /* Set up any superblocks initialized prior to the policy load. */ |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7288 | pr_debug("SELinux: Setting up existing superblocks.\n"); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 7289 | iterate_supers(delayed_superblock_init, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | } |
| 7291 | |
| 7292 | /* SELinux requires early initialization in order to label |
| 7293 | all processes and objects when they are created. */ |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7294 | DEFINE_LSM(selinux) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 7295 | .name = "selinux", |
Kees Cook | 14bd99c | 2018-09-19 19:57:06 -0700 | [diff] [blame] | 7296 | .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
Stephen Smalley | 6c5a682 | 2019-12-17 09:15:10 -0500 | [diff] [blame] | 7297 | .enabled = &selinux_enabled_boot, |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 7298 | .blobs = &selinux_blob_sizes, |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7299 | .init = selinux_init, |
| 7300 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7302 | #if defined(CONFIG_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | |
Florian Westphal | 591bb27 | 2017-07-26 11:40:52 +0200 | [diff] [blame] | 7304 | static const struct nf_hook_ops selinux_nf_ops[] = { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7305 | { |
| 7306 | .hook = selinux_ipv4_postroute, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7307 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7308 | .hooknum = NF_INET_POST_ROUTING, |
| 7309 | .priority = NF_IP_PRI_SELINUX_LAST, |
| 7310 | }, |
| 7311 | { |
| 7312 | .hook = selinux_ipv4_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7313 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7314 | .hooknum = NF_INET_FORWARD, |
| 7315 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7316 | }, |
| 7317 | { |
| 7318 | .hook = selinux_ipv4_output, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7319 | .pf = NFPROTO_IPV4, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7320 | .hooknum = NF_INET_LOCAL_OUT, |
| 7321 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7322 | }, |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 7323 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7324 | { |
| 7325 | .hook = selinux_ipv6_postroute, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7326 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7327 | .hooknum = NF_INET_POST_ROUTING, |
| 7328 | .priority = NF_IP6_PRI_SELINUX_LAST, |
| 7329 | }, |
| 7330 | { |
| 7331 | .hook = selinux_ipv6_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7332 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7333 | .hooknum = NF_INET_FORWARD, |
| 7334 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7335 | }, |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 7336 | { |
| 7337 | .hook = selinux_ipv6_output, |
| 7338 | .pf = NFPROTO_IPV6, |
| 7339 | .hooknum = NF_INET_LOCAL_OUT, |
| 7340 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 7341 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7342 | #endif /* IPV6 */ |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7343 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7345 | static int __net_init selinux_nf_register(struct net *net) |
| 7346 | { |
| 7347 | return nf_register_net_hooks(net, selinux_nf_ops, |
| 7348 | ARRAY_SIZE(selinux_nf_ops)); |
| 7349 | } |
| 7350 | |
| 7351 | static void __net_exit selinux_nf_unregister(struct net *net) |
| 7352 | { |
| 7353 | nf_unregister_net_hooks(net, selinux_nf_ops, |
| 7354 | ARRAY_SIZE(selinux_nf_ops)); |
| 7355 | } |
| 7356 | |
| 7357 | static struct pernet_operations selinux_net_ops = { |
| 7358 | .init = selinux_nf_register, |
| 7359 | .exit = selinux_nf_unregister, |
| 7360 | }; |
| 7361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | static int __init selinux_nf_ip_init(void) |
| 7363 | { |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7364 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7365 | |
Stephen Smalley | 6c5a682 | 2019-12-17 09:15:10 -0500 | [diff] [blame] | 7366 | if (!selinux_enabled_boot) |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7367 | return 0; |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7368 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7369 | pr_debug("SELinux: Registering netfilter hooks\n"); |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7370 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7371 | err = register_pernet_subsys(&selinux_net_ops); |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 7372 | if (err) |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7373 | panic("SELinux: register_pernet_subsys: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7375 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7377 | __initcall(selinux_nf_ip_init); |
| 7378 | |
| 7379 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7380 | static void selinux_nf_ip_exit(void) |
| 7381 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7382 | pr_debug("SELinux: Unregistering netfilter hooks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7384 | unregister_pernet_subsys(&selinux_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | } |
| 7386 | #endif |
| 7387 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7388 | #else /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | |
| 7390 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7391 | #define selinux_nf_ip_exit() |
| 7392 | #endif |
| 7393 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7394 | #endif /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7395 | |
| 7396 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7397 | int selinux_disable(struct selinux_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | { |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 7399 | if (selinux_initialized(state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7400 | /* Not permitted after initial policy load. */ |
| 7401 | return -EINVAL; |
| 7402 | } |
| 7403 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 7404 | if (selinux_disabled(state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | /* Only do this once. */ |
| 7406 | return -EINVAL; |
| 7407 | } |
| 7408 | |
Ondrej Mosnacek | 65cddd5 | 2020-01-07 14:31:53 +0100 | [diff] [blame] | 7409 | selinux_mark_disabled(state); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7410 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7411 | pr_info("SELinux: Disabled at runtime.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | |
Ondrej Mosnacek | cfff75d | 2020-01-08 15:09:58 +0100 | [diff] [blame] | 7413 | /* |
| 7414 | * Unregister netfilter hooks. |
| 7415 | * Must be done before security_delete_hooks() to avoid breaking |
| 7416 | * runtime disable. |
| 7417 | */ |
| 7418 | selinux_nf_ip_exit(); |
| 7419 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 7420 | security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | |
Eric Paris | af8ff04 | 2009-09-20 21:23:01 -0400 | [diff] [blame] | 7422 | /* Try to destroy the avc node cache */ |
| 7423 | avc_disable(); |
| 7424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | /* Unregister selinuxfs. */ |
| 7426 | exit_sel_fs(); |
| 7427 | |
| 7428 | return 0; |
| 7429 | } |
| 7430 | #endif |