Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NSA Security-Enhanced Linux (SELinux) security module |
| 3 | * |
| 4 | * This file contains the SELinux hook function implementations. |
| 5 | * |
Stephen Smalley | 7efbb60 | 2017-08-17 13:32:36 -0400 | [diff] [blame] | 6 | * Authors: Stephen Smalley, <sds@tycho.nsa.gov> |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 7 | * Chris Vance, <cvance@nai.com> |
| 8 | * Wayne Salamon, <wsalamon@nai.com> |
| 9 | * James Morris <jmorris@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2001,2002 Networks Associates Technology, Inc. |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 12 | * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 13 | * Eric Paris <eparis@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 15 | * <dgoeddel@trustedcs.com> |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 16 | * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 17 | * Paul Moore <paul@paul-moore.com> |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 18 | * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 19 | * Yuichi Nakamura <ynakam@hitachisoft.jp> |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 20 | * Copyright (C) 2016 Mellanox Technologies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * |
| 22 | * This program is free software; you can redistribute it and/or modify |
| 23 | * it under the terms of the GNU General Public License version 2, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 24 | * as published by the Free Software Foundation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/init.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 28 | #include <linux/kd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 30 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/errno.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 32 | #include <linux/sched/signal.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 33 | #include <linux/sched/task.h> |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 34 | #include <linux/lsm_hooks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/xattr.h> |
| 36 | #include <linux/capability.h> |
| 37 | #include <linux/unistd.h> |
| 38 | #include <linux/mm.h> |
| 39 | #include <linux/mman.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/pagemap.h> |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 42 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
| 45 | #include <linux/syscalls.h> |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 46 | #include <linux/dcache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 48 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/namei.h> |
| 50 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/netfilter_ipv4.h> |
| 52 | #include <linux/netfilter_ipv6.h> |
| 53 | #include <linux/tty.h> |
| 54 | #include <net/icmp.h> |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 55 | #include <net/ip.h> /* for local_port_range[] */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 57 | #include <net/inet_connection_sock.h> |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 58 | #include <net/net_namespace.h> |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 59 | #include <net/netlabel.h> |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 60 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <asm/ioctls.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 62 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/bitops.h> |
| 64 | #include <linux/interrupt.h> |
| 65 | #include <linux/netdevice.h> /* for network interface checks */ |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 66 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/tcp.h> |
| 68 | #include <linux/udp.h> |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 69 | #include <linux/dccp.h> |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 70 | #include <linux/sctp.h> |
| 71 | #include <net/sctp/structs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/quota.h> |
| 73 | #include <linux/un.h> /* for Unix socket types */ |
| 74 | #include <net/af_unix.h> /* for Unix socket types */ |
| 75 | #include <linux/parser.h> |
| 76 | #include <linux/nfs_mount.h> |
| 77 | #include <net/ipv6.h> |
| 78 | #include <linux/hugetlb.h> |
| 79 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <linux/audit.h> |
Eric Paris | 6931dfc | 2005-06-30 02:58:51 -0700 | [diff] [blame] | 81 | #include <linux/string.h> |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 82 | #include <linux/selinux.h> |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 83 | #include <linux/mutex.h> |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 84 | #include <linux/posix-timers.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 85 | #include <linux/syslog.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 86 | #include <linux/user_namespace.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 87 | #include <linux/export.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 88 | #include <linux/msg.h> |
| 89 | #include <linux/shm.h> |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 90 | #include <linux/bpf.h> |
David Howells | e262e32d | 2018-11-01 23:07:23 +0000 | [diff] [blame] | 91 | #include <uapi/linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | #include "avc.h" |
| 94 | #include "objsec.h" |
| 95 | #include "netif.h" |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 96 | #include "netnode.h" |
Paul Moore | 3e112172 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 97 | #include "netport.h" |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 98 | #include "ibpkey.h" |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 99 | #include "xfrm.h" |
Paul Moore | c60475b | 2007-02-28 15:14:23 -0500 | [diff] [blame] | 100 | #include "netlabel.h" |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 101 | #include "audit.h" |
James Morris | 7b98a58 | 2011-08-30 12:52:32 +1000 | [diff] [blame] | 102 | #include "avc_ss.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 104 | struct selinux_state selinux_state; |
| 105 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 106 | /* SECMARK reference count */ |
James Morris | 56a4ca9 | 2011-08-17 11:08:43 +1000 | [diff] [blame] | 107 | static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #ifdef CONFIG_SECURITY_SELINUX_DEVELOP |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 110 | static int selinux_enforcing_boot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | static int __init enforcing_setup(char *str) |
| 113 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 114 | unsigned long enforcing; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 115 | if (!kstrtoul(str, 0, &enforcing)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 116 | selinux_enforcing_boot = enforcing ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return 1; |
| 118 | } |
| 119 | __setup("enforcing=", enforcing_setup); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 120 | #else |
| 121 | #define selinux_enforcing_boot 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM |
| 125 | int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; |
| 126 | |
| 127 | static int __init selinux_enabled_setup(char *str) |
| 128 | { |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 129 | unsigned long enabled; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 130 | if (!kstrtoul(str, 0, &enabled)) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 131 | selinux_enabled = enabled ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | __setup("selinux=", selinux_enabled_setup); |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 135 | #else |
| 136 | int selinux_enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #endif |
| 138 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 139 | static unsigned int selinux_checkreqprot_boot = |
| 140 | CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; |
| 141 | |
| 142 | static int __init checkreqprot_setup(char *str) |
| 143 | { |
| 144 | unsigned long checkreqprot; |
| 145 | |
| 146 | if (!kstrtoul(str, 0, &checkreqprot)) |
| 147 | selinux_checkreqprot_boot = checkreqprot ? 1 : 0; |
| 148 | return 1; |
| 149 | } |
| 150 | __setup("checkreqprot=", checkreqprot_setup); |
| 151 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 152 | static struct kmem_cache *sel_inode_cache; |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 153 | static struct kmem_cache *file_security_cache; |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 154 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 155 | /** |
| 156 | * selinux_secmark_enabled - Check to see if SECMARK is currently enabled |
| 157 | * |
| 158 | * Description: |
| 159 | * This function checks the SECMARK reference counter to see if any SECMARK |
| 160 | * targets are currently configured, if the reference counter is greater than |
| 161 | * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 162 | * enabled, false (0) if SECMARK is disabled. If the always_check_network |
| 163 | * policy capability is enabled, SECMARK is always considered enabled. |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 164 | * |
| 165 | */ |
| 166 | static int selinux_secmark_enabled(void) |
| 167 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 168 | return (selinux_policycap_alwaysnetwork() || |
| 169 | atomic_read(&selinux_secmark_refcount)); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled |
| 174 | * |
| 175 | * Description: |
| 176 | * This function checks if NetLabel or labeled IPSEC is enabled. Returns true |
| 177 | * (1) if any are enabled or false (0) if neither are enabled. If the |
| 178 | * always_check_network policy capability is enabled, peer labeling |
| 179 | * is always considered enabled. |
| 180 | * |
| 181 | */ |
| 182 | static int selinux_peerlbl_enabled(void) |
| 183 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 184 | return (selinux_policycap_alwaysnetwork() || |
| 185 | netlbl_enabled() || selinux_xfrm_enabled()); |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 188 | static int selinux_netcache_avc_callback(u32 event) |
| 189 | { |
| 190 | if (event == AVC_CALLBACK_RESET) { |
| 191 | sel_netif_flush(); |
| 192 | sel_netnode_flush(); |
| 193 | sel_netport_flush(); |
| 194 | synchronize_net(); |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 199 | static int selinux_lsm_notifier_avc_callback(u32 event) |
| 200 | { |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 201 | if (event == AVC_CALLBACK_RESET) { |
| 202 | sel_ib_pkey_flush(); |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 203 | call_lsm_notifier(LSM_POLICY_CHANGE, NULL); |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 204 | } |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 209 | /* |
| 210 | * initialise the security for the init task |
| 211 | */ |
| 212 | static void cred_init_security(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 214 | struct cred *cred = (struct cred *) current->real_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | struct task_security_struct *tsec; |
| 216 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 217 | tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (!tsec) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 219 | panic("SELinux: Failed to initialize initial task.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 221 | tsec->osid = tsec->sid = SECINITSID_KERNEL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 222 | cred->security = tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 225 | /* |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 226 | * get the security ID of a set of credentials |
| 227 | */ |
| 228 | static inline u32 cred_sid(const struct cred *cred) |
| 229 | { |
| 230 | const struct task_security_struct *tsec; |
| 231 | |
| 232 | tsec = cred->security; |
| 233 | return tsec->sid; |
| 234 | } |
| 235 | |
| 236 | /* |
David Howells | 3b11a1d | 2008-11-14 10:39:26 +1100 | [diff] [blame] | 237 | * get the objective security ID of a task |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 238 | */ |
| 239 | static inline u32 task_sid(const struct task_struct *task) |
| 240 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 241 | u32 sid; |
| 242 | |
| 243 | rcu_read_lock(); |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 244 | sid = cred_sid(__task_cred(task)); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 245 | rcu_read_unlock(); |
| 246 | return sid; |
| 247 | } |
| 248 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 249 | /* Allocate and free functions for each kind of security blob. */ |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static int inode_alloc_security(struct inode *inode) |
| 252 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 254 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Josef Bacik | a02fe13 | 2008-04-04 09:35:05 +1100 | [diff] [blame] | 256 | isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (!isec) |
| 258 | return -ENOMEM; |
| 259 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 260 | spin_lock_init(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | INIT_LIST_HEAD(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | isec->inode = inode; |
| 263 | isec->sid = SECINITSID_UNLABELED; |
| 264 | isec->sclass = SECCLASS_FILE; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 265 | isec->task_sid = sid; |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 266 | isec->initialized = LABEL_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | inode->i_security = isec; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 272 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); |
| 273 | |
| 274 | /* |
| 275 | * Try reloading inode security labels that have been marked as invalid. The |
| 276 | * @may_sleep parameter indicates when sleeping and thus reloading labels is |
Andreas Gruenbacher | 4205911 | 2016-11-10 22:18:27 +0100 | [diff] [blame] | 277 | * allowed; when set to false, returns -ECHILD when the label is |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 278 | * invalid. The @dentry parameter should be set to a dentry of the inode. |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 279 | */ |
| 280 | static int __inode_security_revalidate(struct inode *inode, |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 281 | struct dentry *dentry, |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 282 | bool may_sleep) |
| 283 | { |
| 284 | struct inode_security_struct *isec = inode->i_security; |
| 285 | |
| 286 | might_sleep_if(may_sleep); |
| 287 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 288 | if (selinux_state.initialized && |
| 289 | isec->initialized != LABEL_INITIALIZED) { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 290 | if (!may_sleep) |
| 291 | return -ECHILD; |
| 292 | |
| 293 | /* |
| 294 | * Try reloading the inode security label. This will fail if |
| 295 | * @opt_dentry is NULL and no dentry for this inode can be |
| 296 | * found; in that case, continue using the old label. |
| 297 | */ |
Al Viro | e919328 | 2018-04-24 21:31:02 -0400 | [diff] [blame] | 298 | inode_doinit_with_dentry(inode, dentry); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 303 | static struct inode_security_struct *inode_security_novalidate(struct inode *inode) |
| 304 | { |
| 305 | return inode->i_security; |
| 306 | } |
| 307 | |
| 308 | static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) |
| 309 | { |
| 310 | int error; |
| 311 | |
| 312 | error = __inode_security_revalidate(inode, NULL, !rcu); |
| 313 | if (error) |
| 314 | return ERR_PTR(error); |
| 315 | return inode->i_security; |
| 316 | } |
| 317 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 318 | /* |
| 319 | * Get the security label of an inode. |
| 320 | */ |
| 321 | static struct inode_security_struct *inode_security(struct inode *inode) |
| 322 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 323 | __inode_security_revalidate(inode, NULL, true); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 324 | return inode->i_security; |
| 325 | } |
| 326 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 327 | static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) |
| 328 | { |
| 329 | struct inode *inode = d_backing_inode(dentry); |
| 330 | |
| 331 | return inode->i_security; |
| 332 | } |
| 333 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 334 | /* |
| 335 | * Get the security label of a dentry's backing inode. |
| 336 | */ |
| 337 | static struct inode_security_struct *backing_inode_security(struct dentry *dentry) |
| 338 | { |
| 339 | struct inode *inode = d_backing_inode(dentry); |
| 340 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 341 | __inode_security_revalidate(inode, dentry, true); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 342 | return inode->i_security; |
| 343 | } |
| 344 | |
Steven Rostedt | 3dc91d4 | 2014-01-09 21:46:34 -0500 | [diff] [blame] | 345 | static void inode_free_rcu(struct rcu_head *head) |
| 346 | { |
| 347 | struct inode_security_struct *isec; |
| 348 | |
| 349 | isec = container_of(head, struct inode_security_struct, rcu); |
| 350 | kmem_cache_free(sel_inode_cache, isec); |
| 351 | } |
| 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | static void inode_free_security(struct inode *inode) |
| 354 | { |
| 355 | struct inode_security_struct *isec = inode->i_security; |
| 356 | struct superblock_security_struct *sbsec = inode->i_sb->s_security; |
| 357 | |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 358 | /* |
| 359 | * As not all inode security structures are in a list, we check for |
| 360 | * empty list outside of the lock to make sure that we won't waste |
| 361 | * time taking a lock doing nothing. |
| 362 | * |
| 363 | * The list_del_init() function can be safely called more than once. |
| 364 | * It should not be possible for this function to be called with |
| 365 | * concurrent list_add(), but for better safety against future changes |
| 366 | * in the code, we use list_empty_careful() here. |
| 367 | */ |
| 368 | if (!list_empty_careful(&isec->list)) { |
| 369 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | list_del_init(&isec->list); |
Waiman Long | 9629d04 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 371 | spin_unlock(&sbsec->isec_lock); |
| 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Steven Rostedt | 3dc91d4 | 2014-01-09 21:46:34 -0500 | [diff] [blame] | 374 | /* |
| 375 | * The inode may still be referenced in a path walk and |
| 376 | * a call to selinux_inode_permission() can be made |
| 377 | * after inode_free_security() is called. Ideally, the VFS |
| 378 | * wouldn't do this, but fixing that is a much harder |
| 379 | * job. For now, simply free the i_security via RCU, and |
| 380 | * leave the current inode->i_security pointer intact. |
| 381 | * The inode will be freed after the RCU grace period too. |
| 382 | */ |
| 383 | call_rcu(&isec->rcu, inode_free_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static int file_alloc_security(struct file *file) |
| 387 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | struct file_security_struct *fsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 389 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 391 | fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (!fsec) |
| 393 | return -ENOMEM; |
| 394 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 395 | fsec->sid = sid; |
| 396 | fsec->fown_sid = sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | file->f_security = fsec; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static void file_free_security(struct file *file) |
| 403 | { |
| 404 | struct file_security_struct *fsec = file->f_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | file->f_security = NULL; |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 406 | kmem_cache_free(file_security_cache, fsec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int superblock_alloc_security(struct super_block *sb) |
| 410 | { |
| 411 | struct superblock_security_struct *sbsec; |
| 412 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 413 | sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (!sbsec) |
| 415 | return -ENOMEM; |
| 416 | |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 417 | mutex_init(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | INIT_LIST_HEAD(&sbsec->isec_head); |
| 419 | spin_lock_init(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | sbsec->sb = sb; |
| 421 | sbsec->sid = SECINITSID_UNLABELED; |
| 422 | sbsec->def_sid = SECINITSID_FILE; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 423 | sbsec->mntpoint_sid = SECINITSID_UNLABELED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | sb->s_security = sbsec; |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static void superblock_free_security(struct super_block *sb) |
| 430 | { |
| 431 | struct superblock_security_struct *sbsec = sb->s_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | sb->s_security = NULL; |
| 433 | kfree(sbsec); |
| 434 | } |
| 435 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 436 | struct selinux_mnt_opts { |
| 437 | const char *fscontext, *context, *rootcontext, *defcontext; |
| 438 | }; |
| 439 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 440 | static void selinux_free_mnt_opts(void *mnt_opts) |
| 441 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 442 | struct selinux_mnt_opts *opts = mnt_opts; |
| 443 | kfree(opts->fscontext); |
| 444 | kfree(opts->context); |
| 445 | kfree(opts->rootcontext); |
| 446 | kfree(opts->defcontext); |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 447 | kfree(opts); |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | static inline int inode_doinit(struct inode *inode) |
| 451 | { |
| 452 | return inode_doinit_with_dentry(inode, NULL); |
| 453 | } |
| 454 | |
| 455 | enum { |
Eric Paris | 31e8793 | 2007-09-19 17:19:12 -0400 | [diff] [blame] | 456 | Opt_error = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | Opt_context = 1, |
| 458 | Opt_fscontext = 2, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 459 | Opt_defcontext = 3, |
| 460 | Opt_rootcontext = 4, |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 461 | Opt_seclabel = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 464 | #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] | 465 | static struct { |
| 466 | const char *name; |
| 467 | int len; |
| 468 | int opt; |
| 469 | bool has_arg; |
| 470 | } tokens[] = { |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 471 | A(context, true), |
| 472 | A(fscontext, true), |
| 473 | A(defcontext, true), |
| 474 | A(rootcontext, true), |
| 475 | A(seclabel, false), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | }; |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 477 | #undef A |
| 478 | |
| 479 | static int match_opt_prefix(char *s, int l, char **arg) |
| 480 | { |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 484 | size_t len = tokens[i].len; |
| 485 | if (len > l || memcmp(s, tokens[i].name, len)) |
| 486 | continue; |
| 487 | if (tokens[i].has_arg) { |
| 488 | if (len == l || s[len] != '=') |
| 489 | continue; |
| 490 | *arg = s + len + 1; |
| 491 | } else if (len != l) |
| 492 | continue; |
| 493 | return tokens[i].opt; |
| 494 | } |
| 495 | return Opt_error; |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" |
| 499 | |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 500 | static int may_context_mount_sb_relabel(u32 sid, |
| 501 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 502 | const struct cred *cred) |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 503 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 504 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 505 | int rc; |
| 506 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 507 | rc = avc_has_perm(&selinux_state, |
| 508 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 509 | FILESYSTEM__RELABELFROM, NULL); |
| 510 | if (rc) |
| 511 | return rc; |
| 512 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 513 | rc = avc_has_perm(&selinux_state, |
| 514 | tsec->sid, sid, SECCLASS_FILESYSTEM, |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 515 | FILESYSTEM__RELABELTO, NULL); |
| 516 | return rc; |
| 517 | } |
| 518 | |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 519 | static int may_context_mount_inode_relabel(u32 sid, |
| 520 | struct superblock_security_struct *sbsec, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 521 | const struct cred *cred) |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 522 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 523 | const struct task_security_struct *tsec = cred->security; |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 524 | int rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 525 | rc = avc_has_perm(&selinux_state, |
| 526 | tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 527 | FILESYSTEM__RELABELFROM, NULL); |
| 528 | if (rc) |
| 529 | return rc; |
| 530 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 531 | rc = avc_has_perm(&selinux_state, |
| 532 | sid, sbsec->sid, SECCLASS_FILESYSTEM, |
Eric Paris | 0808925 | 2006-07-10 04:43:55 -0700 | [diff] [blame] | 533 | FILESYSTEM__ASSOCIATE, NULL); |
| 534 | return rc; |
| 535 | } |
| 536 | |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 537 | static int selinux_is_sblabel_mnt(struct super_block *sb) |
| 538 | { |
| 539 | struct superblock_security_struct *sbsec = sb->s_security; |
| 540 | |
Mark Salyzyn | d5f3a5f | 2015-02-04 11:34:30 -0500 | [diff] [blame] | 541 | return sbsec->behavior == SECURITY_FS_USE_XATTR || |
| 542 | sbsec->behavior == SECURITY_FS_USE_TRANS || |
| 543 | sbsec->behavior == SECURITY_FS_USE_TASK || |
J. Bruce Fields | 9fc2b4b | 2015-06-04 15:57:25 -0400 | [diff] [blame] | 544 | sbsec->behavior == SECURITY_FS_USE_NATIVE || |
Mark Salyzyn | d5f3a5f | 2015-02-04 11:34:30 -0500 | [diff] [blame] | 545 | /* Special handling. Genfs but also in-core setxattr handler */ |
| 546 | !strcmp(sb->s_type->name, "sysfs") || |
| 547 | !strcmp(sb->s_type->name, "pstore") || |
| 548 | !strcmp(sb->s_type->name, "debugfs") || |
Yongqin Liu | a2c7c6f | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 549 | !strcmp(sb->s_type->name, "tracefs") || |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 550 | !strcmp(sb->s_type->name, "rootfs") || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 551 | (selinux_policycap_cgroupseclabel() && |
Stephen Smalley | 2651225b | 2017-02-28 10:35:56 -0500 | [diff] [blame] | 552 | (!strcmp(sb->s_type->name, "cgroup") || |
| 553 | !strcmp(sb->s_type->name, "cgroup2"))); |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 554 | } |
| 555 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 556 | static int sb_finish_set_opts(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | struct superblock_security_struct *sbsec = sb->s_security; |
| 559 | struct dentry *root = sb->s_root; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 560 | struct inode *root_inode = d_backing_inode(root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | int rc = 0; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 564 | /* Make sure that the xattr handler exists and that no |
| 565 | error other than -ENODATA is returned by getxattr on |
| 566 | the root directory. -ENODATA is ok, as this may be |
| 567 | the first boot of the SELinux kernel before we have |
| 568 | assigned xattr values to the filesystem. */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 569 | if (!(root_inode->i_opflags & IOP_XATTR)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 570 | pr_warn("SELinux: (dev %s, type %s) has no " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 571 | "xattr support\n", sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | rc = -EOPNOTSUPP; |
| 573 | goto out; |
| 574 | } |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 575 | |
| 576 | rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (rc < 0 && rc != -ENODATA) { |
| 578 | if (rc == -EOPNOTSUPP) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 579 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 580 | "%s) has no security xattr handler\n", |
| 581 | sb->s_id, sb->s_type->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 583 | pr_warn("SELinux: (dev %s, type " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 584 | "%s) getxattr errno %d\n", sb->s_id, |
| 585 | sb->s_type->name, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | goto out; |
| 587 | } |
| 588 | } |
| 589 | |
Eric Paris | eadcabc | 2012-08-24 15:59:14 -0400 | [diff] [blame] | 590 | sbsec->flags |= SE_SBINITIALIZED; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply |
| 594 | * leave the flag untouched because sb_clone_mnt_opts might be handing |
| 595 | * us a superblock that needs the flag to be cleared. |
| 596 | */ |
Eric Paris | b43e725 | 2012-10-10 14:27:35 -0400 | [diff] [blame] | 597 | if (selinux_is_sblabel_mnt(sb)) |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 598 | sbsec->flags |= SBLABEL_MNT; |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 599 | else |
| 600 | sbsec->flags &= ~SBLABEL_MNT; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | /* Initialize the root inode. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 603 | rc = inode_doinit_with_dentry(root_inode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /* Initialize any other inodes associated with the superblock, e.g. |
| 606 | inodes created prior to initial policy load or inodes created |
| 607 | during get_sb by a pseudo filesystem that directly |
| 608 | populates itself. */ |
| 609 | spin_lock(&sbsec->isec_lock); |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 610 | while (!list_empty(&sbsec->isec_head)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | struct inode_security_struct *isec = |
Al Viro | 8d64124 | 2018-12-10 15:34:12 -0500 | [diff] [blame] | 612 | list_first_entry(&sbsec->isec_head, |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 613 | struct inode_security_struct, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | struct inode *inode = isec->inode; |
Stephen Smalley | 923190d | 2014-10-06 16:32:52 -0400 | [diff] [blame] | 615 | list_del_init(&isec->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | spin_unlock(&sbsec->isec_lock); |
| 617 | inode = igrab(inode); |
| 618 | if (inode) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 619 | if (!IS_PRIVATE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | inode_doinit(inode); |
| 621 | iput(inode); |
| 622 | } |
| 623 | spin_lock(&sbsec->isec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | spin_unlock(&sbsec->isec_lock); |
| 626 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 627 | return rc; |
| 628 | } |
| 629 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 630 | static int bad_option(struct superblock_security_struct *sbsec, char flag, |
| 631 | u32 old_sid, u32 new_sid) |
| 632 | { |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 633 | char mnt_flags = sbsec->flags & SE_MNTMASK; |
| 634 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 635 | /* check if the old mount command had the same options */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 636 | if (sbsec->flags & SE_SBINITIALIZED) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 637 | if (!(sbsec->flags & flag) || |
| 638 | (old_sid != new_sid)) |
| 639 | return 1; |
| 640 | |
| 641 | /* check if we were passed the same options twice, |
| 642 | * aka someone passed context=a,context=b |
| 643 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 644 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 645 | if (mnt_flags & flag) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 646 | return 1; |
| 647 | return 0; |
| 648 | } |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 649 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 650 | static int parse_sid(struct super_block *sb, const char *s, u32 *sid) |
| 651 | { |
| 652 | int rc = security_context_str_to_sid(&selinux_state, s, |
| 653 | sid, GFP_KERNEL); |
| 654 | if (rc) |
| 655 | pr_warn("SELinux: security_context_str_to_sid" |
| 656 | "(%s) failed for (dev %s, type %s) errno=%d\n", |
| 657 | s, sb->s_id, sb->s_type->name, rc); |
| 658 | return rc; |
| 659 | } |
| 660 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 661 | /* |
| 662 | * Allow filesystems with binary mount data to explicitly set mount point |
| 663 | * labeling information. |
| 664 | */ |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 665 | static int selinux_set_mnt_opts(struct super_block *sb, |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 666 | void *mnt_opts, |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 667 | unsigned long kern_flags, |
| 668 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 669 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 670 | const struct cred *cred = current_cred(); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 671 | struct superblock_security_struct *sbsec = sb->s_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 672 | struct dentry *root = sbsec->sb->s_root; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 673 | struct selinux_mnt_opts *opts = mnt_opts; |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 674 | struct inode_security_struct *root_isec; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 675 | u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; |
| 676 | u32 defcontext_sid = 0; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 677 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 678 | |
| 679 | mutex_lock(&sbsec->lock); |
| 680 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 681 | if (!selinux_state.initialized) { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 682 | if (!opts) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 683 | /* Defer initialization until selinux_complete_init, |
| 684 | after the initial policy is loaded and the security |
| 685 | server is ready to handle calls. */ |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 686 | goto out; |
| 687 | } |
| 688 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 689 | pr_warn("SELinux: Unable to set superblock options " |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 690 | "before the security server is initialized\n"); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 691 | goto out; |
| 692 | } |
David Quigley | 649f6e7 | 2013-05-22 12:50:36 -0400 | [diff] [blame] | 693 | if (kern_flags && !set_kern_flags) { |
| 694 | /* Specifying internal flags without providing a place to |
| 695 | * place the results is not allowed */ |
| 696 | rc = -EINVAL; |
| 697 | goto out; |
| 698 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 699 | |
| 700 | /* |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 701 | * Binary mount data FS will come through this function twice. Once |
| 702 | * from an explicit call and once from the generic calls from the vfs. |
| 703 | * Since the generic VFS calls will not contain any security mount data |
| 704 | * we need to skip the double mount verification. |
| 705 | * |
| 706 | * This does open a hole in which we will not notice if the first |
| 707 | * mount using this sb set explict options and a second mount using |
| 708 | * this sb does not set any security options. (The first options |
| 709 | * will be used for both mounts) |
| 710 | */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 711 | if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 712 | && !opts) |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 713 | goto out; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 714 | |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 715 | root_isec = backing_inode_security_novalidate(root); |
| 716 | |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 717 | /* |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 718 | * parse the mount options, check if they are valid sids. |
| 719 | * also check if someone is trying to mount the same sb more |
| 720 | * than once with different security options. |
| 721 | */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 722 | if (opts) { |
| 723 | if (opts->fscontext) { |
| 724 | rc = parse_sid(sb, opts->fscontext, &fscontext_sid); |
| 725 | if (rc) |
| 726 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 727 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, |
| 728 | fscontext_sid)) |
| 729 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 730 | sbsec->flags |= FSCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 731 | } |
| 732 | if (opts->context) { |
| 733 | rc = parse_sid(sb, opts->context, &context_sid); |
| 734 | if (rc) |
| 735 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 736 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, |
| 737 | context_sid)) |
| 738 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 739 | sbsec->flags |= CONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 740 | } |
| 741 | if (opts->rootcontext) { |
| 742 | rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); |
| 743 | if (rc) |
| 744 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 745 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, |
| 746 | rootcontext_sid)) |
| 747 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 748 | sbsec->flags |= ROOTCONTEXT_MNT; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 749 | } |
| 750 | if (opts->defcontext) { |
| 751 | rc = parse_sid(sb, opts->defcontext, &defcontext_sid); |
| 752 | if (rc) |
| 753 | goto out; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 754 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, |
| 755 | defcontext_sid)) |
| 756 | goto out_double_mount; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 757 | sbsec->flags |= DEFCONTEXT_MNT; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 761 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 762 | /* previously mounted with options, but not on this attempt? */ |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 763 | if ((sbsec->flags & SE_MNTMASK) && !opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 764 | goto out_double_mount; |
| 765 | rc = 0; |
| 766 | goto out; |
| 767 | } |
| 768 | |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 769 | if (strcmp(sb->s_type->name, "proc") == 0) |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 770 | sbsec->flags |= SE_SBPROC | SE_SBGENFS; |
| 771 | |
Stephen Smalley | 8e01472 | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 772 | if (!strcmp(sb->s_type->name, "debugfs") || |
Jeff Vander Stoep | 6a39118 | 2017-06-20 09:35:33 -0700 | [diff] [blame] | 773 | !strcmp(sb->s_type->name, "tracefs") || |
Stephen Smalley | 8e01472 | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 774 | !strcmp(sb->s_type->name, "sysfs") || |
Antonio Murdaca | 901ef84 | 2017-02-09 17:02:42 +0100 | [diff] [blame] | 775 | !strcmp(sb->s_type->name, "pstore") || |
| 776 | !strcmp(sb->s_type->name, "cgroup") || |
| 777 | !strcmp(sb->s_type->name, "cgroup2")) |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 778 | sbsec->flags |= SE_SBGENFS; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 779 | |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 780 | if (!sbsec->behavior) { |
| 781 | /* |
| 782 | * Determine the labeling behavior to use for this |
| 783 | * filesystem type. |
| 784 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 785 | rc = security_fs_use(&selinux_state, sb); |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 786 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 787 | pr_warn("%s: security_fs_use(%s) returned %d\n", |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 788 | __func__, sb->s_type->name, rc); |
| 789 | goto out; |
| 790 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 791 | } |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 792 | |
| 793 | /* |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 794 | * If this is a user namespace mount and the filesystem type is not |
| 795 | * explicitly whitelisted, then no contexts are allowed on the command |
| 796 | * line and security labels must be ignored. |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 797 | */ |
Stephen Smalley | 01593d3 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 798 | if (sb->s_user_ns != &init_user_ns && |
| 799 | strcmp(sb->s_type->name, "tmpfs") && |
| 800 | strcmp(sb->s_type->name, "ramfs") && |
| 801 | strcmp(sb->s_type->name, "devpts")) { |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 802 | if (context_sid || fscontext_sid || rootcontext_sid || |
| 803 | defcontext_sid) { |
| 804 | rc = -EACCES; |
| 805 | goto out; |
| 806 | } |
| 807 | if (sbsec->behavior == SECURITY_FS_USE_XATTR) { |
| 808 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 809 | rc = security_transition_sid(&selinux_state, |
| 810 | current_sid(), |
| 811 | current_sid(), |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 812 | SECCLASS_FILE, NULL, |
| 813 | &sbsec->mntpoint_sid); |
| 814 | if (rc) |
| 815 | goto out; |
| 816 | } |
| 817 | goto out_set_opts; |
| 818 | } |
| 819 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 820 | /* sets the context of the superblock for the fs being mounted. */ |
| 821 | if (fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 822 | rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 823 | if (rc) |
| 824 | goto out; |
| 825 | |
| 826 | sbsec->sid = fscontext_sid; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Switch to using mount point labeling behavior. |
| 831 | * sets the label used on all file below the mountpoint, and will set |
| 832 | * the superblock context if not already set. |
| 833 | */ |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 834 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { |
| 835 | sbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 836 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 837 | } |
| 838 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 839 | if (context_sid) { |
| 840 | if (!fscontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 841 | rc = may_context_mount_sb_relabel(context_sid, sbsec, |
| 842 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 843 | if (rc) |
| 844 | goto out; |
| 845 | sbsec->sid = context_sid; |
| 846 | } else { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 847 | rc = may_context_mount_inode_relabel(context_sid, sbsec, |
| 848 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 849 | if (rc) |
| 850 | goto out; |
| 851 | } |
| 852 | if (!rootcontext_sid) |
| 853 | rootcontext_sid = context_sid; |
| 854 | |
| 855 | sbsec->mntpoint_sid = context_sid; |
| 856 | sbsec->behavior = SECURITY_FS_USE_MNTPOINT; |
| 857 | } |
| 858 | |
| 859 | if (rootcontext_sid) { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 860 | rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, |
| 861 | cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 862 | if (rc) |
| 863 | goto out; |
| 864 | |
| 865 | root_isec->sid = rootcontext_sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 866 | root_isec->initialized = LABEL_INITIALIZED; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if (defcontext_sid) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 870 | if (sbsec->behavior != SECURITY_FS_USE_XATTR && |
| 871 | sbsec->behavior != SECURITY_FS_USE_NATIVE) { |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 872 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 873 | pr_warn("SELinux: defcontext option is " |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 874 | "invalid for this filesystem type\n"); |
| 875 | goto out; |
| 876 | } |
| 877 | |
| 878 | if (defcontext_sid != sbsec->def_sid) { |
| 879 | rc = may_context_mount_inode_relabel(defcontext_sid, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 880 | sbsec, cred); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 881 | if (rc) |
| 882 | goto out; |
| 883 | } |
| 884 | |
| 885 | sbsec->def_sid = defcontext_sid; |
| 886 | } |
| 887 | |
Seth Forshee | aad8289 | 2016-04-26 14:36:20 -0500 | [diff] [blame] | 888 | out_set_opts: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 889 | rc = sb_finish_set_opts(sb); |
| 890 | out: |
Eric Paris | bc7e982 | 2006-09-25 23:32:02 -0700 | [diff] [blame] | 891 | mutex_unlock(&sbsec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 893 | out_double_mount: |
| 894 | rc = -EINVAL; |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 895 | pr_warn("SELinux: mount invalid. Same superblock, different " |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 896 | "security settings for (dev %s, type %s)\n", sb->s_id, |
| 897 | sb->s_type->name); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 898 | goto out; |
| 899 | } |
| 900 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 901 | static int selinux_cmp_sb_context(const struct super_block *oldsb, |
| 902 | const struct super_block *newsb) |
| 903 | { |
| 904 | struct superblock_security_struct *old = oldsb->s_security; |
| 905 | struct superblock_security_struct *new = newsb->s_security; |
| 906 | char oldflags = old->flags & SE_MNTMASK; |
| 907 | char newflags = new->flags & SE_MNTMASK; |
| 908 | |
| 909 | if (oldflags != newflags) |
| 910 | goto mismatch; |
| 911 | if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) |
| 912 | goto mismatch; |
| 913 | if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) |
| 914 | goto mismatch; |
| 915 | if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) |
| 916 | goto mismatch; |
| 917 | if (oldflags & ROOTCONTEXT_MNT) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 918 | struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); |
| 919 | struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 920 | if (oldroot->sid != newroot->sid) |
| 921 | goto mismatch; |
| 922 | } |
| 923 | return 0; |
| 924 | mismatch: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 925 | pr_warn("SELinux: mount invalid. Same superblock, " |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 926 | "different security settings for (dev %s, " |
| 927 | "type %s)\n", newsb->s_id, newsb->s_type->name); |
| 928 | return -EBUSY; |
| 929 | } |
| 930 | |
| 931 | static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 932 | struct super_block *newsb, |
| 933 | unsigned long kern_flags, |
| 934 | unsigned long *set_kern_flags) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 935 | { |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 936 | int rc = 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 937 | const struct superblock_security_struct *oldsbsec = oldsb->s_security; |
| 938 | struct superblock_security_struct *newsbsec = newsb->s_security; |
| 939 | |
| 940 | int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); |
| 941 | int set_context = (oldsbsec->flags & CONTEXT_MNT); |
| 942 | int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); |
| 943 | |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 944 | /* |
| 945 | * if the parent was able to be mounted it clearly had no special lsm |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 946 | * mount options. thus we can safely deal with this superblock later |
Eric Paris | 0f5e642 | 2008-04-21 16:24:11 -0400 | [diff] [blame] | 947 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 948 | if (!selinux_state.initialized) |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 949 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 950 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 951 | /* |
| 952 | * Specifying internal flags without providing a place to |
| 953 | * place the results is not allowed. |
| 954 | */ |
| 955 | if (kern_flags && !set_kern_flags) |
| 956 | return -EINVAL; |
| 957 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 958 | /* how can we clone if the old one wasn't set up?? */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 959 | BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 960 | |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 961 | /* if fs is reusing a sb, make sure that the contexts match */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 962 | if (newsbsec->flags & SE_SBINITIALIZED) |
Jeff Layton | 094f7b6 | 2013-04-01 08:14:24 -0400 | [diff] [blame] | 963 | return selinux_cmp_sb_context(oldsb, newsb); |
Eric Paris | 5a55261 | 2008-04-09 14:08:35 -0400 | [diff] [blame] | 964 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 965 | mutex_lock(&newsbsec->lock); |
| 966 | |
| 967 | newsbsec->flags = oldsbsec->flags; |
| 968 | |
| 969 | newsbsec->sid = oldsbsec->sid; |
| 970 | newsbsec->def_sid = oldsbsec->def_sid; |
| 971 | newsbsec->behavior = oldsbsec->behavior; |
| 972 | |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 973 | if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && |
| 974 | !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 975 | rc = security_fs_use(&selinux_state, newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 976 | if (rc) |
| 977 | goto out; |
| 978 | } |
| 979 | |
| 980 | if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { |
| 981 | newsbsec->behavior = SECURITY_FS_USE_NATIVE; |
| 982 | *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; |
| 983 | } |
| 984 | |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 985 | if (set_context) { |
| 986 | u32 sid = oldsbsec->mntpoint_sid; |
| 987 | |
| 988 | if (!set_fscontext) |
| 989 | newsbsec->sid = sid; |
| 990 | if (!set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 991 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 992 | newisec->sid = sid; |
| 993 | } |
| 994 | newsbsec->mntpoint_sid = sid; |
| 995 | } |
| 996 | if (set_rootcontext) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 997 | const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); |
| 998 | struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 999 | |
| 1000 | newisec->sid = oldisec->sid; |
| 1001 | } |
| 1002 | |
| 1003 | sb_finish_set_opts(newsb); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 1004 | out: |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1005 | mutex_unlock(&newsbsec->lock); |
Scott Mayhew | 0b4d345 | 2017-06-05 11:45:04 -0400 | [diff] [blame] | 1006 | return rc; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1007 | } |
| 1008 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1009 | static int selinux_add_opt(int token, const char *s, void **mnt_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1010 | { |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1011 | struct selinux_mnt_opts *opts = *mnt_opts; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1012 | |
Al Viro | da3d76a | 2018-12-17 10:14:16 -0500 | [diff] [blame] | 1013 | if (token == Opt_seclabel) /* eaten and completely ignored */ |
Al Viro | 169d68efb | 2018-12-14 22:44:50 -0500 | [diff] [blame] | 1014 | return 0; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1015 | |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1016 | if (!opts) { |
| 1017 | opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); |
| 1018 | if (!opts) |
| 1019 | return -ENOMEM; |
| 1020 | *mnt_opts = opts; |
| 1021 | } |
| 1022 | if (!s) |
| 1023 | return -ENOMEM; |
| 1024 | switch (token) { |
| 1025 | case Opt_context: |
| 1026 | if (opts->context || opts->defcontext) |
| 1027 | goto Einval; |
| 1028 | opts->context = s; |
| 1029 | break; |
| 1030 | case Opt_fscontext: |
| 1031 | if (opts->fscontext) |
| 1032 | goto Einval; |
| 1033 | opts->fscontext = s; |
| 1034 | break; |
| 1035 | case Opt_rootcontext: |
| 1036 | if (opts->rootcontext) |
| 1037 | goto Einval; |
| 1038 | opts->rootcontext = s; |
| 1039 | break; |
| 1040 | case Opt_defcontext: |
| 1041 | if (opts->context || opts->defcontext) |
| 1042 | goto Einval; |
| 1043 | opts->defcontext = s; |
| 1044 | break; |
| 1045 | } |
| 1046 | return 0; |
| 1047 | Einval: |
| 1048 | pr_warn(SEL_MOUNT_FAIL_MSG); |
Al Viro | ba64186 | 2018-12-14 20:28:15 -0500 | [diff] [blame] | 1049 | return -EINVAL; |
| 1050 | } |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1051 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1052 | static int selinux_add_mnt_opt(const char *option, const char *val, int len, |
| 1053 | void **mnt_opts) |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1054 | { |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1055 | int token = Opt_error; |
| 1056 | int rc, i; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1057 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1058 | for (i = 0; i < ARRAY_SIZE(tokens); i++) { |
| 1059 | if (strcmp(option, tokens[i].name) == 0) { |
| 1060 | token = tokens[i].opt; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1061 | break; |
Eric Paris | c9180a5 | 2007-11-30 13:00:35 -0500 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1065 | if (token == Opt_error) |
| 1066 | return -EINVAL; |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 1067 | |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 1068 | if (token != Opt_seclabel) |
| 1069 | val = kmemdup_nul(val, len, GFP_KERNEL); |
| 1070 | rc = selinux_add_opt(token, val, mnt_opts); |
| 1071 | if (unlikely(rc)) { |
| 1072 | kfree(val); |
| 1073 | if (*mnt_opts) { |
| 1074 | selinux_free_mnt_opts(*mnt_opts); |
| 1075 | *mnt_opts = NULL; |
| 1076 | } |
| 1077 | } |
| 1078 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1081 | static int show_sid(struct seq_file *m, u32 sid) |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1082 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1083 | char *context = NULL; |
| 1084 | u32 len; |
| 1085 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1087 | rc = security_sid_to_context(&selinux_state, sid, |
| 1088 | &context, &len); |
| 1089 | if (!rc) { |
| 1090 | bool has_comma = context && strchr(context, ','); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1092 | if (has_comma) |
| 1093 | seq_putc(m, '\"'); |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1094 | seq_escape(m, context, "\"\n\\"); |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1095 | if (has_comma) |
| 1096 | seq_putc(m, '\"'); |
| 1097 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | kfree(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | return rc; |
| 1100 | } |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1101 | |
| 1102 | static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) |
| 1103 | { |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1104 | struct superblock_security_struct *sbsec = sb->s_security; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1105 | int rc; |
| 1106 | |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1107 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 1108 | return 0; |
| 1109 | |
| 1110 | if (!selinux_state.initialized) |
| 1111 | return 0; |
| 1112 | |
| 1113 | if (sbsec->flags & FSCONTEXT_MNT) { |
| 1114 | seq_putc(m, ','); |
| 1115 | seq_puts(m, FSCONTEXT_STR); |
| 1116 | rc = show_sid(m, sbsec->sid); |
| 1117 | if (rc) |
| 1118 | return rc; |
Eric Paris | 383795c | 2008-07-29 17:07:26 -0400 | [diff] [blame] | 1119 | } |
Al Viro | e3489f8 | 2018-12-13 00:24:36 -0500 | [diff] [blame] | 1120 | if (sbsec->flags & CONTEXT_MNT) { |
| 1121 | seq_putc(m, ','); |
| 1122 | seq_puts(m, CONTEXT_STR); |
| 1123 | rc = show_sid(m, sbsec->mntpoint_sid); |
| 1124 | if (rc) |
| 1125 | return rc; |
| 1126 | } |
| 1127 | if (sbsec->flags & DEFCONTEXT_MNT) { |
| 1128 | seq_putc(m, ','); |
| 1129 | seq_puts(m, DEFCONTEXT_STR); |
| 1130 | rc = show_sid(m, sbsec->def_sid); |
| 1131 | if (rc) |
| 1132 | return rc; |
| 1133 | } |
| 1134 | if (sbsec->flags & ROOTCONTEXT_MNT) { |
| 1135 | struct dentry *root = sbsec->sb->s_root; |
| 1136 | struct inode_security_struct *isec = backing_inode_security(root); |
| 1137 | seq_putc(m, ','); |
| 1138 | seq_puts(m, ROOTCONTEXT_STR); |
| 1139 | rc = show_sid(m, isec->sid); |
| 1140 | if (rc) |
| 1141 | return rc; |
| 1142 | } |
| 1143 | if (sbsec->flags & SBLABEL_MNT) { |
| 1144 | seq_putc(m, ','); |
| 1145 | seq_puts(m, LABELSUPP_STR); |
| 1146 | } |
| 1147 | return 0; |
Eric Paris | 2069f45 | 2008-07-04 09:47:13 +1000 | [diff] [blame] | 1148 | } |
| 1149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | static inline u16 inode_mode_to_security_class(umode_t mode) |
| 1151 | { |
| 1152 | switch (mode & S_IFMT) { |
| 1153 | case S_IFSOCK: |
| 1154 | return SECCLASS_SOCK_FILE; |
| 1155 | case S_IFLNK: |
| 1156 | return SECCLASS_LNK_FILE; |
| 1157 | case S_IFREG: |
| 1158 | return SECCLASS_FILE; |
| 1159 | case S_IFBLK: |
| 1160 | return SECCLASS_BLK_FILE; |
| 1161 | case S_IFDIR: |
| 1162 | return SECCLASS_DIR; |
| 1163 | case S_IFCHR: |
| 1164 | return SECCLASS_CHR_FILE; |
| 1165 | case S_IFIFO: |
| 1166 | return SECCLASS_FIFO_FILE; |
| 1167 | |
| 1168 | } |
| 1169 | |
| 1170 | return SECCLASS_FILE; |
| 1171 | } |
| 1172 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1173 | static inline int default_protocol_stream(int protocol) |
| 1174 | { |
| 1175 | return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); |
| 1176 | } |
| 1177 | |
| 1178 | static inline int default_protocol_dgram(int protocol) |
| 1179 | { |
| 1180 | return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); |
| 1181 | } |
| 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | static inline u16 socket_type_to_security_class(int family, int type, int protocol) |
| 1184 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1185 | int extsockclass = selinux_policycap_extsockclass(); |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | switch (family) { |
| 1188 | case PF_UNIX: |
| 1189 | switch (type) { |
| 1190 | case SOCK_STREAM: |
| 1191 | case SOCK_SEQPACKET: |
| 1192 | return SECCLASS_UNIX_STREAM_SOCKET; |
| 1193 | case SOCK_DGRAM: |
Luis Ressel | 2a764b5 | 2017-07-25 15:13:41 -0400 | [diff] [blame] | 1194 | case SOCK_RAW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | return SECCLASS_UNIX_DGRAM_SOCKET; |
| 1196 | } |
| 1197 | break; |
| 1198 | case PF_INET: |
| 1199 | case PF_INET6: |
| 1200 | switch (type) { |
| 1201 | case SOCK_STREAM: |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1202 | case SOCK_SEQPACKET: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1203 | if (default_protocol_stream(protocol)) |
| 1204 | return SECCLASS_TCP_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1205 | else if (extsockclass && protocol == IPPROTO_SCTP) |
| 1206 | return SECCLASS_SCTP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1207 | else |
| 1208 | return SECCLASS_RAWIP_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | case SOCK_DGRAM: |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1210 | if (default_protocol_dgram(protocol)) |
| 1211 | return SECCLASS_UDP_SOCKET; |
Stephen Smalley | ef37979 | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 1212 | else if (extsockclass && (protocol == IPPROTO_ICMP || |
| 1213 | protocol == IPPROTO_ICMPV6)) |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1214 | return SECCLASS_ICMP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1215 | else |
| 1216 | return SECCLASS_RAWIP_SOCKET; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 1217 | case SOCK_DCCP: |
| 1218 | return SECCLASS_DCCP_SOCKET; |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 1219 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | return SECCLASS_RAWIP_SOCKET; |
| 1221 | } |
| 1222 | break; |
| 1223 | case PF_NETLINK: |
| 1224 | switch (protocol) { |
| 1225 | case NETLINK_ROUTE: |
| 1226 | return SECCLASS_NETLINK_ROUTE_SOCKET; |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1227 | case NETLINK_SOCK_DIAG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | return SECCLASS_NETLINK_TCPDIAG_SOCKET; |
| 1229 | case NETLINK_NFLOG: |
| 1230 | return SECCLASS_NETLINK_NFLOG_SOCKET; |
| 1231 | case NETLINK_XFRM: |
| 1232 | return SECCLASS_NETLINK_XFRM_SOCKET; |
| 1233 | case NETLINK_SELINUX: |
| 1234 | return SECCLASS_NETLINK_SELINUX_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1235 | case NETLINK_ISCSI: |
| 1236 | return SECCLASS_NETLINK_ISCSI_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | case NETLINK_AUDIT: |
| 1238 | return SECCLASS_NETLINK_AUDIT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1239 | case NETLINK_FIB_LOOKUP: |
| 1240 | return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; |
| 1241 | case NETLINK_CONNECTOR: |
| 1242 | return SECCLASS_NETLINK_CONNECTOR_SOCKET; |
| 1243 | case NETLINK_NETFILTER: |
| 1244 | return SECCLASS_NETLINK_NETFILTER_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | case NETLINK_DNRTMSG: |
| 1246 | return SECCLASS_NETLINK_DNRT_SOCKET; |
James Morris | 0c9b794 | 2005-04-16 15:24:13 -0700 | [diff] [blame] | 1247 | case NETLINK_KOBJECT_UEVENT: |
| 1248 | return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; |
Stephen Smalley | 6c6d2e9 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 1249 | case NETLINK_GENERIC: |
| 1250 | return SECCLASS_NETLINK_GENERIC_SOCKET; |
| 1251 | case NETLINK_SCSITRANSPORT: |
| 1252 | return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; |
| 1253 | case NETLINK_RDMA: |
| 1254 | return SECCLASS_NETLINK_RDMA_SOCKET; |
| 1255 | case NETLINK_CRYPTO: |
| 1256 | return SECCLASS_NETLINK_CRYPTO_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | default: |
| 1258 | return SECCLASS_NETLINK_SOCKET; |
| 1259 | } |
| 1260 | case PF_PACKET: |
| 1261 | return SECCLASS_PACKET_SOCKET; |
| 1262 | case PF_KEY: |
| 1263 | return SECCLASS_KEY_SOCKET; |
Christopher J. PeBenito | 3e3ff15 | 2006-06-09 00:25:03 -0700 | [diff] [blame] | 1264 | case PF_APPLETALK: |
| 1265 | return SECCLASS_APPLETALK_SOCKET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1268 | if (extsockclass) { |
| 1269 | switch (family) { |
| 1270 | case PF_AX25: |
| 1271 | return SECCLASS_AX25_SOCKET; |
| 1272 | case PF_IPX: |
| 1273 | return SECCLASS_IPX_SOCKET; |
| 1274 | case PF_NETROM: |
| 1275 | return SECCLASS_NETROM_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1276 | case PF_ATMPVC: |
| 1277 | return SECCLASS_ATMPVC_SOCKET; |
| 1278 | case PF_X25: |
| 1279 | return SECCLASS_X25_SOCKET; |
| 1280 | case PF_ROSE: |
| 1281 | return SECCLASS_ROSE_SOCKET; |
| 1282 | case PF_DECnet: |
| 1283 | return SECCLASS_DECNET_SOCKET; |
| 1284 | case PF_ATMSVC: |
| 1285 | return SECCLASS_ATMSVC_SOCKET; |
| 1286 | case PF_RDS: |
| 1287 | return SECCLASS_RDS_SOCKET; |
| 1288 | case PF_IRDA: |
| 1289 | return SECCLASS_IRDA_SOCKET; |
| 1290 | case PF_PPPOX: |
| 1291 | return SECCLASS_PPPOX_SOCKET; |
| 1292 | case PF_LLC: |
| 1293 | return SECCLASS_LLC_SOCKET; |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1294 | case PF_CAN: |
| 1295 | return SECCLASS_CAN_SOCKET; |
| 1296 | case PF_TIPC: |
| 1297 | return SECCLASS_TIPC_SOCKET; |
| 1298 | case PF_BLUETOOTH: |
| 1299 | return SECCLASS_BLUETOOTH_SOCKET; |
| 1300 | case PF_IUCV: |
| 1301 | return SECCLASS_IUCV_SOCKET; |
| 1302 | case PF_RXRPC: |
| 1303 | return SECCLASS_RXRPC_SOCKET; |
| 1304 | case PF_ISDN: |
| 1305 | return SECCLASS_ISDN_SOCKET; |
| 1306 | case PF_PHONET: |
| 1307 | return SECCLASS_PHONET_SOCKET; |
| 1308 | case PF_IEEE802154: |
| 1309 | return SECCLASS_IEEE802154_SOCKET; |
| 1310 | case PF_CAIF: |
| 1311 | return SECCLASS_CAIF_SOCKET; |
| 1312 | case PF_ALG: |
| 1313 | return SECCLASS_ALG_SOCKET; |
| 1314 | case PF_NFC: |
| 1315 | return SECCLASS_NFC_SOCKET; |
| 1316 | case PF_VSOCK: |
| 1317 | return SECCLASS_VSOCK_SOCKET; |
| 1318 | case PF_KCM: |
| 1319 | return SECCLASS_KCM_SOCKET; |
| 1320 | case PF_QIPCRTR: |
| 1321 | return SECCLASS_QIPCRTR_SOCKET; |
Linus Torvalds | 3051bf3 | 2017-02-22 10:15:09 -0800 | [diff] [blame] | 1322 | case PF_SMC: |
| 1323 | return SECCLASS_SMC_SOCKET; |
Björn Töpel | 68e8b84 | 2018-05-02 13:01:22 +0200 | [diff] [blame] | 1324 | case PF_XDP: |
| 1325 | return SECCLASS_XDP_SOCKET; |
| 1326 | #if PF_MAX > 45 |
Stephen Smalley | da69a53 | 2017-01-09 10:07:30 -0500 | [diff] [blame] | 1327 | #error New address family defined, please update this function. |
| 1328 | #endif |
| 1329 | } |
| 1330 | } |
| 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return SECCLASS_SOCKET; |
| 1333 | } |
| 1334 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1335 | static int selinux_genfs_get_sid(struct dentry *dentry, |
| 1336 | u16 tclass, |
| 1337 | u16 flags, |
| 1338 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | { |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1340 | int rc; |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 1341 | struct super_block *sb = dentry->d_sb; |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1342 | char *buffer, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1344 | buffer = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | if (!buffer) |
| 1346 | return -ENOMEM; |
| 1347 | |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1348 | path = dentry_path_raw(dentry, buffer, PAGE_SIZE); |
| 1349 | if (IS_ERR(path)) |
| 1350 | rc = PTR_ERR(path); |
| 1351 | else { |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1352 | if (flags & SE_SBPROC) { |
| 1353 | /* each process gets a /proc/PID/ entry. Strip off the |
| 1354 | * PID part to get a valid selinux labeling. |
| 1355 | * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ |
| 1356 | while (path[1] >= '0' && path[1] <= '9') { |
| 1357 | path[1] = '/'; |
| 1358 | path++; |
| 1359 | } |
Lucian Adrian Grijincu | 8e6c969 | 2011-02-01 18:42:22 +0200 | [diff] [blame] | 1360 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1361 | rc = security_genfs_sid(&selinux_state, sb->s_type->name, |
| 1362 | path, tclass, sid); |
Stephen Smalley | 7bb185e | 2018-09-04 16:51:36 -0400 | [diff] [blame] | 1363 | if (rc == -ENOENT) { |
| 1364 | /* No match in policy, mark as unlabeled. */ |
| 1365 | *sid = SECINITSID_UNLABELED; |
| 1366 | rc = 0; |
| 1367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | free_page((unsigned long)buffer); |
| 1370 | return rc; |
| 1371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | /* The inode's security attributes must be initialized before first use. */ |
| 1374 | static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) |
| 1375 | { |
| 1376 | struct superblock_security_struct *sbsec = NULL; |
| 1377 | struct inode_security_struct *isec = inode->i_security; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1378 | u32 task_sid, sid = 0; |
| 1379 | u16 sclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | struct dentry *dentry; |
| 1381 | #define INITCONTEXTLEN 255 |
| 1382 | char *context = NULL; |
| 1383 | unsigned len = 0; |
| 1384 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1386 | if (isec->initialized == LABEL_INITIALIZED) |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1387 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1389 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1390 | if (isec->initialized == LABEL_INITIALIZED) |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1391 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Andreas Gruenbacher | 13457d0 | 2016-11-10 22:18:29 +0100 | [diff] [blame] | 1393 | if (isec->sclass == SECCLASS_FILE) |
| 1394 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | sbsec = inode->i_sb->s_security; |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 1397 | if (!(sbsec->flags & SE_SBINITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | /* Defer initialization until selinux_complete_init, |
| 1399 | after the initial policy is loaded and the security |
| 1400 | server is ready to handle calls. */ |
| 1401 | spin_lock(&sbsec->isec_lock); |
| 1402 | if (list_empty(&isec->list)) |
| 1403 | list_add(&isec->list, &sbsec->isec_head); |
| 1404 | spin_unlock(&sbsec->isec_lock); |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1405 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1408 | sclass = isec->sclass; |
| 1409 | task_sid = isec->task_sid; |
| 1410 | sid = isec->sid; |
| 1411 | isec->initialized = LABEL_PENDING; |
| 1412 | spin_unlock(&isec->lock); |
| 1413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | switch (sbsec->behavior) { |
David Quigley | eb9ae68 | 2013-05-22 12:50:37 -0400 | [diff] [blame] | 1415 | case SECURITY_FS_USE_NATIVE: |
| 1416 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | case SECURITY_FS_USE_XATTR: |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1418 | if (!(inode->i_opflags & IOP_XATTR)) { |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1419 | sid = sbsec->def_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | break; |
| 1421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | /* Need a dentry, since the xattr API requires one. |
| 1423 | Life would be simpler if we could just pass the inode. */ |
| 1424 | if (opt_dentry) { |
| 1425 | /* Called from d_instantiate or d_splice_alias. */ |
| 1426 | dentry = dget(opt_dentry); |
| 1427 | } else { |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1428 | /* |
| 1429 | * Called from selinux_complete_init, try to find a dentry. |
| 1430 | * Some filesystems really want a connected one, so try |
| 1431 | * that first. We could split SECURITY_FS_USE_XATTR in |
| 1432 | * two, depending upon that... |
| 1433 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1435 | if (!dentry) |
| 1436 | dentry = d_find_any_alias(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
| 1438 | if (!dentry) { |
Eric Paris | df7f54c | 2009-03-09 14:35:58 -0400 | [diff] [blame] | 1439 | /* |
| 1440 | * this is can be hit on boot when a file is accessed |
| 1441 | * before the policy is loaded. When we load policy we |
| 1442 | * may find inodes that have no dentry on the |
| 1443 | * sbsec->isec_head list. No reason to complain as these |
| 1444 | * will get fixed up the next time we go through |
| 1445 | * inode_doinit with a dentry, before these inodes could |
| 1446 | * be used again by userspace. |
| 1447 | */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1448 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | len = INITCONTEXTLEN; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1452 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | if (!context) { |
| 1454 | rc = -ENOMEM; |
| 1455 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1456 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1458 | context[len] = '\0'; |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1459 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | if (rc == -ERANGE) { |
James Morris | 314dabb | 2009-08-10 22:00:13 +1000 | [diff] [blame] | 1461 | kfree(context); |
| 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | /* Need a larger buffer. Query for the right size. */ |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1464 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | if (rc < 0) { |
| 1466 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1467 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | len = rc; |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1470 | context = kmalloc(len+1, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | if (!context) { |
| 1472 | rc = -ENOMEM; |
| 1473 | dput(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1474 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
Eric Paris | 4cb912f | 2009-02-12 14:50:05 -0500 | [diff] [blame] | 1476 | context[len] = '\0'; |
Andreas Gruenbacher | 5d6c319 | 2016-09-29 17:48:42 +0200 | [diff] [blame] | 1477 | rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } |
| 1479 | dput(dentry); |
| 1480 | if (rc < 0) { |
| 1481 | if (rc != -ENODATA) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1482 | pr_warn("SELinux: %s: getxattr returned " |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 1483 | "%d for dev=%s ino=%ld\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | -rc, inode->i_sb->s_id, inode->i_ino); |
| 1485 | kfree(context); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1486 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | /* Map ENODATA to the default file SID */ |
| 1489 | sid = sbsec->def_sid; |
| 1490 | rc = 0; |
| 1491 | } else { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1492 | rc = security_context_to_sid_default(&selinux_state, |
| 1493 | context, rc, &sid, |
Stephen Smalley | 869ab51 | 2008-04-04 08:46:05 -0400 | [diff] [blame] | 1494 | sbsec->def_sid, |
| 1495 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | if (rc) { |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1497 | char *dev = inode->i_sb->s_id; |
| 1498 | unsigned long ino = inode->i_ino; |
| 1499 | |
| 1500 | if (rc == -EINVAL) { |
| 1501 | if (printk_ratelimit()) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1502 | pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid " |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1503 | "context=%s. This indicates you may need to relabel the inode or the " |
| 1504 | "filesystem in question.\n", ino, dev, context); |
| 1505 | } else { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1506 | pr_warn("SELinux: %s: context_to_sid(%s) " |
Eric Paris | 4ba0a8a | 2009-02-12 15:01:10 -0500 | [diff] [blame] | 1507 | "returned %d for dev=%s ino=%ld\n", |
| 1508 | __func__, context, -rc, dev, ino); |
| 1509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | kfree(context); |
| 1511 | /* Leave with the unlabeled SID */ |
| 1512 | rc = 0; |
| 1513 | break; |
| 1514 | } |
| 1515 | } |
| 1516 | kfree(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | break; |
| 1518 | case SECURITY_FS_USE_TASK: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1519 | sid = task_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | break; |
| 1521 | case SECURITY_FS_USE_TRANS: |
| 1522 | /* Default to the fs SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1523 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
| 1525 | /* Try to obtain a transition SID. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1526 | rc = security_transition_sid(&selinux_state, task_sid, sid, |
| 1527 | sclass, NULL, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | if (rc) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1529 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | break; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1531 | case SECURITY_FS_USE_MNTPOINT: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1532 | sid = sbsec->mntpoint_sid; |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1533 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | default: |
Eric Paris | c312feb | 2006-07-10 04:43:53 -0700 | [diff] [blame] | 1535 | /* Default to the fs superblock SID. */ |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1536 | sid = sbsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1538 | if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1539 | /* We must have a dentry to determine the label on |
| 1540 | * procfs inodes */ |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1541 | if (opt_dentry) { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1542 | /* Called from d_instantiate or |
| 1543 | * d_splice_alias. */ |
| 1544 | dentry = dget(opt_dentry); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1545 | } else { |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1546 | /* Called from selinux_complete_init, try to |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1547 | * find a dentry. Some filesystems really want |
| 1548 | * a connected one, so try that first. |
| 1549 | */ |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1550 | dentry = d_find_alias(inode); |
Al Viro | b127125 | 2018-04-25 10:28:38 -0400 | [diff] [blame] | 1551 | if (!dentry) |
| 1552 | dentry = d_find_any_alias(inode); |
| 1553 | } |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1554 | /* |
| 1555 | * This can be hit on boot when a file is accessed |
| 1556 | * before the policy is loaded. When we load policy we |
| 1557 | * may find inodes that have no dentry on the |
| 1558 | * sbsec->isec_head list. No reason to complain as |
| 1559 | * these will get fixed up the next time we go through |
| 1560 | * inode_doinit() with a dentry, before these inodes |
| 1561 | * could be used again by userspace. |
| 1562 | */ |
| 1563 | if (!dentry) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1564 | goto out; |
| 1565 | rc = selinux_genfs_get_sid(dentry, sclass, |
Stephen Smalley | 134509d | 2015-06-04 16:22:17 -0400 | [diff] [blame] | 1566 | sbsec->flags, &sid); |
Paul Moore | f64410e | 2014-03-19 16:46:18 -0400 | [diff] [blame] | 1567 | dput(dentry); |
| 1568 | if (rc) |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1569 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | } |
| 1571 | break; |
| 1572 | } |
| 1573 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1574 | out: |
| 1575 | spin_lock(&isec->lock); |
| 1576 | if (isec->initialized == LABEL_PENDING) { |
| 1577 | if (!sid || rc) { |
| 1578 | isec->initialized = LABEL_INVALID; |
| 1579 | goto out_unlock; |
| 1580 | } |
| 1581 | |
| 1582 | isec->initialized = LABEL_INITIALIZED; |
| 1583 | isec->sid = sid; |
| 1584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
Eric Paris | 2397074 | 2006-09-25 23:32:01 -0700 | [diff] [blame] | 1586 | out_unlock: |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 1587 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | return rc; |
| 1589 | } |
| 1590 | |
| 1591 | /* Convert a Linux signal to an access vector. */ |
| 1592 | static inline u32 signal_to_av(int sig) |
| 1593 | { |
| 1594 | u32 perm = 0; |
| 1595 | |
| 1596 | switch (sig) { |
| 1597 | case SIGCHLD: |
| 1598 | /* Commonly granted from child to parent. */ |
| 1599 | perm = PROCESS__SIGCHLD; |
| 1600 | break; |
| 1601 | case SIGKILL: |
| 1602 | /* Cannot be caught or ignored */ |
| 1603 | perm = PROCESS__SIGKILL; |
| 1604 | break; |
| 1605 | case SIGSTOP: |
| 1606 | /* Cannot be caught or ignored */ |
| 1607 | perm = PROCESS__SIGSTOP; |
| 1608 | break; |
| 1609 | default: |
| 1610 | /* All other signals. */ |
| 1611 | perm = PROCESS__SIGNAL; |
| 1612 | break; |
| 1613 | } |
| 1614 | |
| 1615 | return perm; |
| 1616 | } |
| 1617 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1618 | #if CAP_LAST_CAP > 63 |
| 1619 | #error Fix SELinux to handle capabilities > 63. |
| 1620 | #endif |
| 1621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | /* Check whether a task is allowed to use a capability. */ |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 1623 | static int cred_has_capability(const struct cred *cred, |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1624 | int cap, int audit, bool initns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1626 | struct common_audit_data ad; |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1627 | struct av_decision avd; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1628 | u16 sclass; |
David Howells | 3699c53 | 2009-01-06 22:27:01 +0000 | [diff] [blame] | 1629 | u32 sid = cred_sid(cred); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1630 | u32 av = CAP_TO_MASK(cap); |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1631 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1633 | ad.type = LSM_AUDIT_DATA_CAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | ad.u.cap = cap; |
| 1635 | |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1636 | switch (CAP_TO_INDEX(cap)) { |
| 1637 | case 0: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1638 | sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1639 | break; |
| 1640 | case 1: |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 1641 | sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1642 | break; |
| 1643 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1644 | pr_err("SELinux: out of range capability %d\n", cap); |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1645 | BUG(); |
Eric Paris | a35c6c83 | 2011-04-20 10:21:28 -0400 | [diff] [blame] | 1646 | return -EINVAL; |
Stephen Smalley | b68e418 | 2008-02-07 11:21:04 -0500 | [diff] [blame] | 1647 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1648 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1649 | rc = avc_has_perm_noaudit(&selinux_state, |
| 1650 | sid, sid, sclass, av, 0, &avd); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1651 | if (audit == SECURITY_CAP_AUDIT) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1652 | int rc2 = avc_audit(&selinux_state, |
| 1653 | sid, sid, sclass, av, &avd, rc, &ad, 0); |
Eric Paris | 9ade0cf | 2011-04-25 16:26:29 -0400 | [diff] [blame] | 1654 | if (rc2) |
| 1655 | return rc2; |
| 1656 | } |
Eric Paris | 0611216 | 2008-11-11 22:02:50 +1100 | [diff] [blame] | 1657 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | /* Check whether a task has a particular permission to an inode. |
| 1661 | The 'adp' parameter is optional and allows other audit |
| 1662 | data to be passed (e.g. the dentry). */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1663 | static int inode_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | struct inode *inode, |
| 1665 | u32 perms, |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1666 | struct common_audit_data *adp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | struct inode_security_struct *isec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1669 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 1671 | validate_creds(cred); |
| 1672 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1673 | if (unlikely(IS_PRIVATE(inode))) |
Stephen Smalley | bbaca6c | 2007-02-14 00:34:16 -0800 | [diff] [blame] | 1674 | return 0; |
| 1675 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1676 | sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | isec = inode->i_security; |
| 1678 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1679 | return avc_has_perm(&selinux_state, |
| 1680 | sid, isec->sid, isec->sclass, perms, adp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1684 | the dentry to help the auditing code to more easily generate the |
| 1685 | pathname if needed. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1686 | static inline int dentry_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | struct dentry *dentry, |
| 1688 | u32 av) |
| 1689 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1690 | struct inode *inode = d_backing_inode(dentry); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1691 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1692 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1693 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1694 | ad.u.dentry = dentry; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1695 | __inode_security_revalidate(inode, dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1696 | return inode_has_perm(cred, inode, av, &ad); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /* Same as inode_has_perm, but pass explicit audit data containing |
| 1700 | the path to help the auditing code to more easily generate the |
| 1701 | pathname if needed. */ |
| 1702 | static inline int path_has_perm(const struct cred *cred, |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 1703 | const struct path *path, |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1704 | u32 av) |
| 1705 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 1706 | struct inode *inode = d_backing_inode(path->dentry); |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1707 | struct common_audit_data ad; |
| 1708 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1709 | ad.type = LSM_AUDIT_DATA_PATH; |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 1710 | ad.u.path = *path; |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 1711 | __inode_security_revalidate(inode, path->dentry, true); |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1712 | return inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1715 | /* Same as path_has_perm, but uses the inode from the file struct. */ |
| 1716 | static inline int file_path_has_perm(const struct cred *cred, |
| 1717 | struct file *file, |
| 1718 | u32 av) |
| 1719 | { |
| 1720 | struct common_audit_data ad; |
| 1721 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1722 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1723 | ad.u.file = file; |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1724 | return inode_has_perm(cred, file_inode(file), av, &ad); |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 1725 | } |
| 1726 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1727 | #ifdef CONFIG_BPF_SYSCALL |
| 1728 | static int bpf_fd_pass(struct file *file, u32 sid); |
| 1729 | #endif |
| 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | /* Check whether a task can use an open file descriptor to |
| 1732 | access an inode in a given way. Check access to the |
| 1733 | descriptor itself, and then use dentry_has_perm to |
| 1734 | check a particular permission to the file. |
| 1735 | Access to the descriptor is implicitly granted if it |
| 1736 | has the same SID as the process. If av is zero, then |
| 1737 | access to the file is not checked, e.g. for cases |
| 1738 | where only the descriptor is affected like seek. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1739 | static int file_has_perm(const struct cred *cred, |
| 1740 | struct file *file, |
| 1741 | u32 av) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | struct file_security_struct *fsec = file->f_security; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1744 | struct inode *inode = file_inode(file); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1745 | struct common_audit_data ad; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1746 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | int rc; |
| 1748 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 1749 | ad.type = LSM_AUDIT_DATA_FILE; |
| 1750 | ad.u.file = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1752 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1753 | rc = avc_has_perm(&selinux_state, |
| 1754 | sid, fsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | SECCLASS_FD, |
| 1756 | FD__USE, |
| 1757 | &ad); |
| 1758 | if (rc) |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1759 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1762 | #ifdef CONFIG_BPF_SYSCALL |
| 1763 | rc = bpf_fd_pass(file, cred_sid(cred)); |
| 1764 | if (rc) |
| 1765 | return rc; |
| 1766 | #endif |
| 1767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | /* av is zero if only checking access to the descriptor. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1769 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | if (av) |
Linus Torvalds | 19e4983 | 2013-10-04 12:54:11 -0700 | [diff] [blame] | 1771 | rc = inode_has_perm(cred, inode, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1773 | out: |
| 1774 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1777 | /* |
| 1778 | * Determine the label for an inode that might be unioned. |
| 1779 | */ |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1780 | static int |
| 1781 | selinux_determine_inode_label(const struct task_security_struct *tsec, |
| 1782 | struct inode *dir, |
| 1783 | const struct qstr *name, u16 tclass, |
| 1784 | u32 *_new_isid) |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1785 | { |
| 1786 | const struct superblock_security_struct *sbsec = dir->i_sb->s_security; |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1787 | |
| 1788 | if ((sbsec->flags & SE_SBINITIALIZED) && |
| 1789 | (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { |
| 1790 | *_new_isid = sbsec->mntpoint_sid; |
| 1791 | } else if ((sbsec->flags & SBLABEL_MNT) && |
| 1792 | tsec->create_sid) { |
| 1793 | *_new_isid = tsec->create_sid; |
| 1794 | } else { |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 1795 | const struct inode_security_struct *dsec = inode_security(dir); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 1796 | return security_transition_sid(&selinux_state, tsec->sid, |
| 1797 | dsec->sid, tclass, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1798 | name, _new_isid); |
| 1799 | } |
| 1800 | |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | /* Check whether a task can create a file. */ |
| 1805 | static int may_create(struct inode *dir, |
| 1806 | struct dentry *dentry, |
| 1807 | u16 tclass) |
| 1808 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 1809 | const struct task_security_struct *tsec = current_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | struct inode_security_struct *dsec; |
| 1811 | struct superblock_security_struct *sbsec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1812 | u32 sid, newsid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1813 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | int rc; |
| 1815 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1816 | dsec = inode_security(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | sbsec = dir->i_sb->s_security; |
| 1818 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1819 | sid = tsec->sid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1820 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1821 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1822 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1824 | rc = avc_has_perm(&selinux_state, |
| 1825 | sid, dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | DIR__ADD_NAME | DIR__SEARCH, |
| 1827 | &ad); |
| 1828 | if (rc) |
| 1829 | return rc; |
| 1830 | |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 1831 | rc = selinux_determine_inode_label(current_security(), dir, |
| 1832 | &dentry->d_name, tclass, &newsid); |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 1833 | if (rc) |
| 1834 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1836 | rc = avc_has_perm(&selinux_state, |
| 1837 | sid, newsid, tclass, FILE__CREATE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | if (rc) |
| 1839 | return rc; |
| 1840 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1841 | return avc_has_perm(&selinux_state, |
| 1842 | newsid, sbsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | SECCLASS_FILESYSTEM, |
| 1844 | FILESYSTEM__ASSOCIATE, &ad); |
| 1845 | } |
| 1846 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 1847 | #define MAY_LINK 0 |
| 1848 | #define MAY_UNLINK 1 |
| 1849 | #define MAY_RMDIR 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | |
| 1851 | /* Check whether a task can link, unlink, or rmdir a file/directory. */ |
| 1852 | static int may_link(struct inode *dir, |
| 1853 | struct dentry *dentry, |
| 1854 | int kind) |
| 1855 | |
| 1856 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | struct inode_security_struct *dsec, *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1858 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1859 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | u32 av; |
| 1861 | int rc; |
| 1862 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1863 | dsec = inode_security(dir); |
| 1864 | isec = backing_inode_security(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1866 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1867 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
| 1869 | av = DIR__SEARCH; |
| 1870 | av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1871 | rc = avc_has_perm(&selinux_state, |
| 1872 | sid, dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | if (rc) |
| 1874 | return rc; |
| 1875 | |
| 1876 | switch (kind) { |
| 1877 | case MAY_LINK: |
| 1878 | av = FILE__LINK; |
| 1879 | break; |
| 1880 | case MAY_UNLINK: |
| 1881 | av = FILE__UNLINK; |
| 1882 | break; |
| 1883 | case MAY_RMDIR: |
| 1884 | av = DIR__RMDIR; |
| 1885 | break; |
| 1886 | default: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 1887 | pr_warn("SELinux: %s: unrecognized kind %d\n", |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1888 | __func__, kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | return 0; |
| 1890 | } |
| 1891 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1892 | rc = avc_has_perm(&selinux_state, |
| 1893 | sid, isec->sid, isec->sclass, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | return rc; |
| 1895 | } |
| 1896 | |
| 1897 | static inline int may_rename(struct inode *old_dir, |
| 1898 | struct dentry *old_dentry, |
| 1899 | struct inode *new_dir, |
| 1900 | struct dentry *new_dentry) |
| 1901 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1903 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1904 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | u32 av; |
| 1906 | int old_is_dir, new_is_dir; |
| 1907 | int rc; |
| 1908 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1909 | old_dsec = inode_security(old_dir); |
| 1910 | old_isec = backing_inode_security(old_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1911 | old_is_dir = d_is_dir(old_dentry); |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1912 | new_dsec = inode_security(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 1914 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1916 | ad.u.dentry = old_dentry; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1917 | rc = avc_has_perm(&selinux_state, |
| 1918 | sid, old_dsec->sid, SECCLASS_DIR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | DIR__REMOVE_NAME | DIR__SEARCH, &ad); |
| 1920 | if (rc) |
| 1921 | return rc; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1922 | rc = avc_has_perm(&selinux_state, |
| 1923 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | old_isec->sclass, FILE__RENAME, &ad); |
| 1925 | if (rc) |
| 1926 | return rc; |
| 1927 | if (old_is_dir && new_dir != old_dir) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1928 | rc = avc_has_perm(&selinux_state, |
| 1929 | sid, old_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | old_isec->sclass, DIR__REPARENT, &ad); |
| 1931 | if (rc) |
| 1932 | return rc; |
| 1933 | } |
| 1934 | |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 1935 | ad.u.dentry = new_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | av = DIR__ADD_NAME | DIR__SEARCH; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1937 | if (d_is_positive(new_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | av |= DIR__REMOVE_NAME; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1939 | rc = avc_has_perm(&selinux_state, |
| 1940 | sid, new_dsec->sid, SECCLASS_DIR, av, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | if (rc) |
| 1942 | return rc; |
David Howells | 2c616d4 | 2015-01-29 12:02:33 +0000 | [diff] [blame] | 1943 | if (d_is_positive(new_dentry)) { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 1944 | new_isec = backing_inode_security(new_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1945 | new_is_dir = d_is_dir(new_dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1946 | rc = avc_has_perm(&selinux_state, |
| 1947 | sid, new_isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | new_isec->sclass, |
| 1949 | (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); |
| 1950 | if (rc) |
| 1951 | return rc; |
| 1952 | } |
| 1953 | |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | /* Check whether a task can perform a filesystem operation. */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1958 | static int superblock_has_perm(const struct cred *cred, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | struct super_block *sb, |
| 1960 | u32 perms, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 1961 | struct common_audit_data *ad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | struct superblock_security_struct *sbsec; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 1964 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | sbsec = sb->s_security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 1967 | return avc_has_perm(&selinux_state, |
| 1968 | sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /* Convert a Linux mode and permission mask to an access vector. */ |
| 1972 | static inline u32 file_mask_to_av(int mode, int mask) |
| 1973 | { |
| 1974 | u32 av = 0; |
| 1975 | |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 1976 | if (!S_ISDIR(mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | if (mask & MAY_EXEC) |
| 1978 | av |= FILE__EXECUTE; |
| 1979 | if (mask & MAY_READ) |
| 1980 | av |= FILE__READ; |
| 1981 | |
| 1982 | if (mask & MAY_APPEND) |
| 1983 | av |= FILE__APPEND; |
| 1984 | else if (mask & MAY_WRITE) |
| 1985 | av |= FILE__WRITE; |
| 1986 | |
| 1987 | } else { |
| 1988 | if (mask & MAY_EXEC) |
| 1989 | av |= DIR__SEARCH; |
| 1990 | if (mask & MAY_WRITE) |
| 1991 | av |= DIR__WRITE; |
| 1992 | if (mask & MAY_READ) |
| 1993 | av |= DIR__READ; |
| 1994 | } |
| 1995 | |
| 1996 | return av; |
| 1997 | } |
| 1998 | |
| 1999 | /* Convert a Linux file to an access vector. */ |
| 2000 | static inline u32 file_to_av(struct file *file) |
| 2001 | { |
| 2002 | u32 av = 0; |
| 2003 | |
| 2004 | if (file->f_mode & FMODE_READ) |
| 2005 | av |= FILE__READ; |
| 2006 | if (file->f_mode & FMODE_WRITE) { |
| 2007 | if (file->f_flags & O_APPEND) |
| 2008 | av |= FILE__APPEND; |
| 2009 | else |
| 2010 | av |= FILE__WRITE; |
| 2011 | } |
Stephen Smalley | 0794c66 | 2008-03-17 08:55:18 -0400 | [diff] [blame] | 2012 | if (!av) { |
| 2013 | /* |
| 2014 | * Special file opened with flags 3 for ioctl-only use. |
| 2015 | */ |
| 2016 | av = FILE__IOCTL; |
| 2017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
| 2019 | return av; |
| 2020 | } |
| 2021 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2022 | /* |
| 2023 | * Convert a file to an access vector and include the correct open |
| 2024 | * open permission. |
| 2025 | */ |
| 2026 | static inline u32 open_file_to_av(struct file *file) |
| 2027 | { |
| 2028 | u32 av = file_to_av(file); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 2029 | struct inode *inode = file_inode(file); |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2030 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2031 | if (selinux_policycap_openperm() && |
| 2032 | inode->i_sb->s_magic != SOCKFS_MAGIC) |
Eric Paris | 49b7b8d | 2010-07-23 11:44:09 -0400 | [diff] [blame] | 2033 | av |= FILE__OPEN; |
| 2034 | |
Eric Paris | 8b6a5a3 | 2008-10-29 17:06:46 -0400 | [diff] [blame] | 2035 | return av; |
| 2036 | } |
| 2037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | /* Hook functions begin here. */ |
| 2039 | |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2040 | static int selinux_binder_set_context_mgr(struct task_struct *mgr) |
| 2041 | { |
| 2042 | u32 mysid = current_sid(); |
| 2043 | u32 mgrsid = task_sid(mgr); |
| 2044 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2045 | return avc_has_perm(&selinux_state, |
| 2046 | mysid, mgrsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2047 | BINDER__SET_CONTEXT_MGR, NULL); |
| 2048 | } |
| 2049 | |
| 2050 | static int selinux_binder_transaction(struct task_struct *from, |
| 2051 | struct task_struct *to) |
| 2052 | { |
| 2053 | u32 mysid = current_sid(); |
| 2054 | u32 fromsid = task_sid(from); |
| 2055 | u32 tosid = task_sid(to); |
| 2056 | int rc; |
| 2057 | |
| 2058 | if (mysid != fromsid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2059 | rc = avc_has_perm(&selinux_state, |
| 2060 | mysid, fromsid, SECCLASS_BINDER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2061 | BINDER__IMPERSONATE, NULL); |
| 2062 | if (rc) |
| 2063 | return rc; |
| 2064 | } |
| 2065 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2066 | return avc_has_perm(&selinux_state, |
| 2067 | fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2068 | NULL); |
| 2069 | } |
| 2070 | |
| 2071 | static int selinux_binder_transfer_binder(struct task_struct *from, |
| 2072 | struct task_struct *to) |
| 2073 | { |
| 2074 | u32 fromsid = task_sid(from); |
| 2075 | u32 tosid = task_sid(to); |
| 2076 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2077 | return avc_has_perm(&selinux_state, |
| 2078 | fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2079 | NULL); |
| 2080 | } |
| 2081 | |
| 2082 | static int selinux_binder_transfer_file(struct task_struct *from, |
| 2083 | struct task_struct *to, |
| 2084 | struct file *file) |
| 2085 | { |
| 2086 | u32 sid = task_sid(to); |
| 2087 | struct file_security_struct *fsec = file->f_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2088 | struct dentry *dentry = file->f_path.dentry; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2089 | struct inode_security_struct *isec; |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2090 | struct common_audit_data ad; |
| 2091 | int rc; |
| 2092 | |
| 2093 | ad.type = LSM_AUDIT_DATA_PATH; |
| 2094 | ad.u.path = file->f_path; |
| 2095 | |
| 2096 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2097 | rc = avc_has_perm(&selinux_state, |
| 2098 | sid, fsec->sid, |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2099 | SECCLASS_FD, |
| 2100 | FD__USE, |
| 2101 | &ad); |
| 2102 | if (rc) |
| 2103 | return rc; |
| 2104 | } |
| 2105 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 2106 | #ifdef CONFIG_BPF_SYSCALL |
| 2107 | rc = bpf_fd_pass(file, sid); |
| 2108 | if (rc) |
| 2109 | return rc; |
| 2110 | #endif |
| 2111 | |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2112 | if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2113 | return 0; |
| 2114 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 2115 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2116 | return avc_has_perm(&selinux_state, |
| 2117 | sid, isec->sid, isec->sclass, file_to_av(file), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 2118 | &ad); |
| 2119 | } |
| 2120 | |
Ingo Molnar | 9e48858 | 2009-05-07 19:26:19 +1000 | [diff] [blame] | 2121 | static int selinux_ptrace_access_check(struct task_struct *child, |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2122 | unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2124 | u32 sid = current_sid(); |
| 2125 | u32 csid = task_sid(child); |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 2126 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2127 | if (mode & PTRACE_MODE_READ) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2128 | return avc_has_perm(&selinux_state, |
| 2129 | sid, csid, SECCLASS_FILE, FILE__READ, NULL); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2130 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2131 | return avc_has_perm(&selinux_state, |
| 2132 | sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); |
David Howells | 5cd9c58 | 2008-08-14 11:37:28 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | static int selinux_ptrace_traceme(struct task_struct *parent) |
| 2136 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2137 | return avc_has_perm(&selinux_state, |
| 2138 | task_sid(parent), current_sid(), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2139 | PROCESS__PTRACE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2143 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2145 | return avc_has_perm(&selinux_state, |
| 2146 | current_sid(), task_sid(target), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2147 | PROCESS__GETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2150 | static int selinux_capset(struct cred *new, const struct cred *old, |
| 2151 | const kernel_cap_t *effective, |
| 2152 | const kernel_cap_t *inheritable, |
| 2153 | const kernel_cap_t *permitted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2155 | return avc_has_perm(&selinux_state, |
| 2156 | cred_sid(old), cred_sid(new), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2157 | PROCESS__SETCAP, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
James Morris | 5626d3e | 2009-01-30 10:05:06 +1100 | [diff] [blame] | 2160 | /* |
| 2161 | * (This comment used to live with the selinux_task_setuid hook, |
| 2162 | * which was removed). |
| 2163 | * |
| 2164 | * Since setuid only affects the current process, and since the SELinux |
| 2165 | * controls are not based on the Linux identity attributes, SELinux does not |
| 2166 | * need to control this operation. However, SELinux does control the use of |
| 2167 | * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. |
| 2168 | */ |
| 2169 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 2170 | static int selinux_capable(const struct cred *cred, struct user_namespace *ns, |
| 2171 | int cap, int audit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | { |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 2173 | return cred_has_capability(cred, cap, audit, ns == &init_user_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) |
| 2177 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2178 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | int rc = 0; |
| 2180 | |
| 2181 | if (!sb) |
| 2182 | return 0; |
| 2183 | |
| 2184 | switch (cmds) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2185 | case Q_SYNC: |
| 2186 | case Q_QUOTAON: |
| 2187 | case Q_QUOTAOFF: |
| 2188 | case Q_SETINFO: |
| 2189 | case Q_SETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2190 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2191 | break; |
| 2192 | case Q_GETFMT: |
| 2193 | case Q_GETINFO: |
| 2194 | case Q_GETQUOTA: |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2195 | rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2196 | break; |
| 2197 | default: |
| 2198 | rc = 0; /* let the kernel handle invalid cmds */ |
| 2199 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | } |
| 2201 | return rc; |
| 2202 | } |
| 2203 | |
| 2204 | static int selinux_quota_on(struct dentry *dentry) |
| 2205 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2206 | const struct cred *cred = current_cred(); |
| 2207 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2208 | return dentry_has_perm(cred, dentry, FILE__QUOTAON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 2211 | static int selinux_syslog(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2214 | case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ |
| 2215 | case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2216 | return avc_has_perm(&selinux_state, |
| 2217 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2218 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 2219 | case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ |
| 2220 | case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ |
| 2221 | /* Set level of messages printed to console */ |
| 2222 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2223 | return avc_has_perm(&selinux_state, |
| 2224 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2225 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, |
| 2226 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2228 | /* All other syslog types */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2229 | return avc_has_perm(&selinux_state, |
| 2230 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2231 | SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | * Check that a process has enough memory to allocate a new virtual |
| 2236 | * mapping. 0 means there is enough memory for the allocation to |
| 2237 | * succeed and -ENOMEM implies there is not. |
| 2238 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | * Do not audit the selinux permission check, as this is applied to all |
| 2240 | * processes that allocate mappings. |
| 2241 | */ |
Alan Cox | 34b4e4a | 2007-08-22 14:01:28 -0700 | [diff] [blame] | 2242 | static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | { |
| 2244 | int rc, cap_sys_admin = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2246 | rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 2247 | SECURITY_CAP_NOAUDIT, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | if (rc == 0) |
| 2249 | cap_sys_admin = 1; |
| 2250 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 2251 | return cap_sys_admin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | /* binprm security operations */ |
| 2255 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2256 | static u32 ptrace_parent_sid(void) |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2257 | { |
| 2258 | u32 sid = 0; |
| 2259 | struct task_struct *tracer; |
| 2260 | |
| 2261 | rcu_read_lock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2262 | tracer = ptrace_parent(current); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 2263 | if (tracer) |
| 2264 | sid = task_sid(tracer); |
| 2265 | rcu_read_unlock(); |
| 2266 | |
| 2267 | return sid; |
| 2268 | } |
| 2269 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2270 | static int check_nnp_nosuid(const struct linux_binprm *bprm, |
| 2271 | const struct task_security_struct *old_tsec, |
| 2272 | const struct task_security_struct *new_tsec) |
| 2273 | { |
| 2274 | int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); |
Andy Lutomirski | 380cf5b | 2016-06-23 16:41:05 -0500 | [diff] [blame] | 2275 | int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2276 | int rc; |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2277 | u32 av; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2278 | |
| 2279 | if (!nnp && !nosuid) |
| 2280 | return 0; /* neither NNP nor nosuid */ |
| 2281 | |
| 2282 | if (new_tsec->sid == old_tsec->sid) |
| 2283 | return 0; /* No change in credentials */ |
| 2284 | |
| 2285 | /* |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2286 | * If the policy enables the nnp_nosuid_transition policy capability, |
| 2287 | * then we permit transitions under NNP or nosuid if the |
| 2288 | * policy allows the corresponding permission between |
| 2289 | * the old and new contexts. |
| 2290 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2291 | if (selinux_policycap_nnp_nosuid_transition()) { |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2292 | av = 0; |
| 2293 | if (nnp) |
| 2294 | av |= PROCESS2__NNP_TRANSITION; |
| 2295 | if (nosuid) |
| 2296 | av |= PROCESS2__NOSUID_TRANSITION; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2297 | rc = avc_has_perm(&selinux_state, |
| 2298 | old_tsec->sid, new_tsec->sid, |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2299 | SECCLASS_PROCESS2, av, NULL); |
| 2300 | if (!rc) |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
| 2304 | /* |
| 2305 | * We also permit NNP or nosuid transitions to bounded SIDs, |
| 2306 | * i.e. SIDs that are guaranteed to only be allowed a subset |
| 2307 | * of the permissions of the current SID. |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2308 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2309 | rc = security_bounded_transition(&selinux_state, old_tsec->sid, |
| 2310 | new_tsec->sid); |
Stephen Smalley | af63f41 | 2017-07-31 10:12:46 -0400 | [diff] [blame] | 2311 | if (!rc) |
| 2312 | return 0; |
| 2313 | |
| 2314 | /* |
| 2315 | * On failure, preserve the errno values for NNP vs nosuid. |
| 2316 | * NNP: Operation not permitted for caller. |
| 2317 | * nosuid: Permission denied to file. |
| 2318 | */ |
| 2319 | if (nnp) |
| 2320 | return -EPERM; |
| 2321 | return -EACCES; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2322 | } |
| 2323 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2324 | static int selinux_bprm_set_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2326 | const struct task_security_struct *old_tsec; |
| 2327 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | struct inode_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2329 | struct common_audit_data ad; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2330 | struct inode *inode = file_inode(bprm->file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | int rc; |
| 2332 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2333 | /* SELinux context only depends on initial program or script and not |
| 2334 | * the script interpreter */ |
Kees Cook | ddb4a14 | 2017-07-18 15:25:23 -0700 | [diff] [blame] | 2335 | if (bprm->called_set_creds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | return 0; |
| 2337 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2338 | old_tsec = current_security(); |
| 2339 | new_tsec = bprm->cred->security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 2340 | isec = inode_security(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | |
| 2342 | /* Default to the current task SID. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2343 | new_tsec->sid = old_tsec->sid; |
| 2344 | new_tsec->osid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | |
Michael LeMay | 28eba5b | 2006-06-27 02:53:42 -0700 | [diff] [blame] | 2346 | /* Reset fs, key, and sock SIDs on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2347 | new_tsec->create_sid = 0; |
| 2348 | new_tsec->keycreate_sid = 0; |
| 2349 | new_tsec->sockcreate_sid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2351 | if (old_tsec->exec_sid) { |
| 2352 | new_tsec->sid = old_tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | /* Reset exec SID on execve. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2354 | new_tsec->exec_sid = 0; |
Andy Lutomirski | 259e5e6 | 2012-04-12 16:47:50 -0500 | [diff] [blame] | 2355 | |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2356 | /* Fail on NNP or nosuid if not an allowed transition. */ |
| 2357 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2358 | if (rc) |
| 2359 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | } else { |
| 2361 | /* Check for a default transition on this program. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2362 | rc = security_transition_sid(&selinux_state, old_tsec->sid, |
| 2363 | isec->sid, SECCLASS_PROCESS, NULL, |
Eric Paris | 652bb9b | 2011-02-01 11:05:40 -0500 | [diff] [blame] | 2364 | &new_tsec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | if (rc) |
| 2366 | return rc; |
Stephen Smalley | 7b0d0b4 | 2014-08-04 13:36:49 -0400 | [diff] [blame] | 2367 | |
| 2368 | /* |
| 2369 | * Fallback to old SID on NNP or nosuid if not an allowed |
| 2370 | * transition. |
| 2371 | */ |
| 2372 | rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); |
| 2373 | if (rc) |
| 2374 | new_tsec->sid = old_tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 2377 | ad.type = LSM_AUDIT_DATA_FILE; |
| 2378 | ad.u.file = bprm->file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2380 | if (new_tsec->sid == old_tsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2381 | rc = avc_has_perm(&selinux_state, |
| 2382 | old_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); |
| 2384 | if (rc) |
| 2385 | return rc; |
| 2386 | } else { |
| 2387 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2388 | rc = avc_has_perm(&selinux_state, |
| 2389 | old_tsec->sid, new_tsec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); |
| 2391 | if (rc) |
| 2392 | return rc; |
| 2393 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2394 | rc = avc_has_perm(&selinux_state, |
| 2395 | new_tsec->sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | SECCLASS_FILE, FILE__ENTRYPOINT, &ad); |
| 2397 | if (rc) |
| 2398 | return rc; |
| 2399 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2400 | /* Check for shared state */ |
| 2401 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2402 | rc = avc_has_perm(&selinux_state, |
| 2403 | old_tsec->sid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2404 | SECCLASS_PROCESS, PROCESS__SHARE, |
| 2405 | NULL); |
| 2406 | if (rc) |
| 2407 | return -EPERM; |
| 2408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2410 | /* Make sure that anyone attempting to ptrace over a task that |
| 2411 | * changes its SID has the appropriate permit */ |
Eric W. Biederman | 9227dd2 | 2017-01-23 17:26:31 +1300 | [diff] [blame] | 2412 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2413 | u32 ptsid = ptrace_parent_sid(); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2414 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2415 | rc = avc_has_perm(&selinux_state, |
| 2416 | ptsid, new_tsec->sid, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2417 | SECCLASS_PROCESS, |
| 2418 | PROCESS__PTRACE, NULL); |
| 2419 | if (rc) |
| 2420 | return -EPERM; |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | /* Clear any possibly unsafe personality bits on exec: */ |
| 2425 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | /* Enable secure mode for SIDs transitions unless |
| 2428 | the noatsecure permission is granted between |
| 2429 | the two SIDs, i.e. ahp returns 0. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2430 | rc = avc_has_perm(&selinux_state, |
| 2431 | old_tsec->sid, new_tsec->sid, |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2432 | SECCLASS_PROCESS, PROCESS__NOATSECURE, |
| 2433 | NULL); |
| 2434 | bprm->secureexec |= !!rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Kees Cook | 62874c3 | 2017-07-18 15:25:25 -0700 | [diff] [blame] | 2437 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2440 | static int match_file(const void *p, struct file *file, unsigned fd) |
| 2441 | { |
| 2442 | return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; |
| 2443 | } |
| 2444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | /* Derived from fs/exec.c:flush_old_files. */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 2446 | static inline void flush_unauthorized_files(const struct cred *cred, |
| 2447 | struct files_struct *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | struct file *file, *devnull = NULL; |
Stephen Smalley | b20c812 | 2006-09-25 23:32:03 -0700 | [diff] [blame] | 2450 | struct tty_struct *tty; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2451 | int drop_tty = 0; |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2452 | unsigned n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2454 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | if (tty) { |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2456 | spin_lock(&tty->files_lock); |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2457 | if (!list_empty(&tty->tty_files)) { |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2458 | struct tty_file_private *file_priv; |
Eric Paris | 37dd0bd | 2008-10-31 17:40:00 -0400 | [diff] [blame] | 2459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | /* Revalidate access to controlling tty. |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2461 | Use file_path_has_perm on the tty path directly |
| 2462 | rather than using file_has_perm, as this particular |
| 2463 | open file may belong to another process and we are |
| 2464 | only interested in the inode-based check here. */ |
Nick Piggin | d996b62 | 2010-08-18 04:37:36 +1000 | [diff] [blame] | 2465 | file_priv = list_first_entry(&tty->tty_files, |
| 2466 | struct tty_file_private, list); |
| 2467 | file = file_priv->file; |
David Howells | 13f8e98 | 2013-06-13 23:37:55 +0100 | [diff] [blame] | 2468 | if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2469 | drop_tty = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | } |
Peter Hurley | 4a51096 | 2016-01-09 21:35:23 -0800 | [diff] [blame] | 2471 | spin_unlock(&tty->files_lock); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 2472 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 2474 | /* Reset controlling tty. */ |
| 2475 | if (drop_tty) |
| 2476 | no_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | |
| 2478 | /* Revalidate access to inherited open files. */ |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2479 | n = iterate_fd(files, 0, match_file, cred); |
| 2480 | if (!n) /* none found? */ |
| 2481 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2483 | devnull = dentry_open(&selinux_null, O_RDWR, cred); |
Al Viro | 45525b2 | 2012-10-16 13:30:07 -0400 | [diff] [blame] | 2484 | if (IS_ERR(devnull)) |
| 2485 | devnull = NULL; |
| 2486 | /* replace all the matching ones with this */ |
| 2487 | do { |
| 2488 | replace_fd(n - 1, devnull, 0); |
| 2489 | } while ((n = iterate_fd(files, n, match_file, cred)) != 0); |
| 2490 | if (devnull) |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 2491 | fput(devnull); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | /* |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2495 | * Prepare a process for imminent new credential changes due to exec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2497 | static void selinux_bprm_committing_creds(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | { |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2499 | struct task_security_struct *new_tsec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | struct rlimit *rlim, *initrlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | int rc, i; |
| 2502 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2503 | new_tsec = bprm->cred->security; |
| 2504 | if (new_tsec->sid == new_tsec->osid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | return; |
| 2506 | |
| 2507 | /* Close files for which the new task SID is not authorized. */ |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2508 | flush_unauthorized_files(bprm->cred, current->files); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2510 | /* Always clear parent death signal on SID transitions. */ |
| 2511 | current->pdeath_signal = 0; |
| 2512 | |
| 2513 | /* Check whether the new SID can inherit resource limits from the old |
| 2514 | * SID. If not, reset all soft limits to the lower of the current |
| 2515 | * task's hard limit and the init task's soft limit. |
| 2516 | * |
| 2517 | * Note that the setting of hard limits (even to lower them) can be |
| 2518 | * controlled by the setrlimit check. The inclusion of the init task's |
| 2519 | * soft limit into the computation is to avoid resetting soft limits |
| 2520 | * higher than the default soft limit for cases where the default is |
| 2521 | * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. |
| 2522 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2523 | rc = avc_has_perm(&selinux_state, |
| 2524 | new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2525 | PROCESS__RLIMITINH, NULL); |
| 2526 | if (rc) { |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2527 | /* protect against do_prlimit() */ |
| 2528 | task_lock(current); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2529 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 2530 | rlim = current->signal->rlim + i; |
| 2531 | initrlim = init_task.signal->rlim + i; |
| 2532 | rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); |
| 2533 | } |
Oleg Nesterov | eb2d55a | 2010-06-23 22:43:32 +0200 | [diff] [blame] | 2534 | task_unlock(current); |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2535 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) |
| 2536 | update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | /* |
| 2541 | * Clean up the process immediately after the installation of new credentials |
| 2542 | * due to exec |
| 2543 | */ |
| 2544 | static void selinux_bprm_committed_creds(struct linux_binprm *bprm) |
| 2545 | { |
| 2546 | const struct task_security_struct *tsec = current_security(); |
| 2547 | struct itimerval itimer; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2548 | u32 osid, sid; |
| 2549 | int rc, i; |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2550 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2551 | osid = tsec->osid; |
| 2552 | sid = tsec->sid; |
| 2553 | |
| 2554 | if (sid == osid) |
| 2555 | return; |
| 2556 | |
| 2557 | /* Check whether the new SID can inherit signal state from the old SID. |
| 2558 | * If not, clear itimers to avoid subsequent signal generation and |
| 2559 | * flush and unblock signals. |
| 2560 | * |
| 2561 | * This must occur _after_ the task SID has been updated so that any |
| 2562 | * kill done after the flush will be checked against the new SID. |
| 2563 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2564 | rc = avc_has_perm(&selinux_state, |
| 2565 | osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | if (rc) { |
Nicolas Pitre | baa73d9 | 2016-11-11 00:10:10 -0500 | [diff] [blame] | 2567 | if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { |
| 2568 | memset(&itimer, 0, sizeof itimer); |
| 2569 | for (i = 0; i < 3; i++) |
| 2570 | do_setitimer(i, &itimer, NULL); |
| 2571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2573 | if (!fatal_signal_pending(current)) { |
| 2574 | flush_sigqueue(¤t->pending); |
| 2575 | flush_sigqueue(¤t->signal->shared_pending); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2576 | flush_signal_handlers(current, 1); |
| 2577 | sigemptyset(¤t->blocked); |
Oleg Nesterov | 9e7c8f8 | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 2578 | recalc_sigpending(); |
David Howells | 3bcac02 | 2009-04-29 13:45:05 +0100 | [diff] [blame] | 2579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | spin_unlock_irq(¤t->sighand->siglock); |
| 2581 | } |
| 2582 | |
David Howells | a6f76f2 | 2008-11-14 10:39:24 +1100 | [diff] [blame] | 2583 | /* Wake up the parent if it is waiting so that it can recheck |
| 2584 | * wait permission to the new task SID. */ |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2585 | read_lock(&tasklist_lock); |
Oleg Nesterov | 0b7570e | 2009-09-23 15:56:46 -0700 | [diff] [blame] | 2586 | __wake_up_parent(current, current->real_parent); |
Oleg Nesterov | ecd6de3 | 2009-04-29 16:02:24 +0200 | [diff] [blame] | 2587 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | /* superblock security operations */ |
| 2591 | |
| 2592 | static int selinux_sb_alloc_security(struct super_block *sb) |
| 2593 | { |
| 2594 | return superblock_alloc_security(sb); |
| 2595 | } |
| 2596 | |
| 2597 | static void selinux_sb_free_security(struct super_block *sb) |
| 2598 | { |
| 2599 | superblock_free_security(sb); |
| 2600 | } |
| 2601 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2602 | static inline int opt_len(const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | { |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2604 | bool open_quote = false; |
| 2605 | int len; |
| 2606 | char c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2608 | for (len = 0; (c = s[len]) != '\0'; len++) { |
| 2609 | if (c == '"') |
Cory Olmo | 3528a95 | 2006-09-29 01:58:44 -0700 | [diff] [blame] | 2610 | open_quote = !open_quote; |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2611 | if (c == ',' && !open_quote) |
| 2612 | break; |
| 2613 | } |
| 2614 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2617 | static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2618 | { |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2619 | char *from = options; |
| 2620 | char *to = options; |
| 2621 | bool first = true; |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2622 | |
Al Viro | 99dbbb5 | 2018-12-14 21:56:23 -0500 | [diff] [blame] | 2623 | while (1) { |
| 2624 | int len = opt_len(from); |
| 2625 | int token, rc; |
| 2626 | char *arg = NULL; |
| 2627 | |
| 2628 | token = match_opt_prefix(from, len, &arg); |
| 2629 | |
| 2630 | if (token != Opt_error) { |
| 2631 | char *p, *q; |
| 2632 | |
| 2633 | /* strip quotes */ |
| 2634 | if (arg) { |
| 2635 | for (p = q = arg; p < from + len; p++) { |
| 2636 | char c = *p; |
| 2637 | if (c != '"') |
| 2638 | *q++ = c; |
| 2639 | } |
| 2640 | arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); |
| 2641 | } |
| 2642 | rc = selinux_add_opt(token, arg, mnt_opts); |
| 2643 | if (unlikely(rc)) { |
| 2644 | kfree(arg); |
| 2645 | if (*mnt_opts) { |
| 2646 | selinux_free_mnt_opts(*mnt_opts); |
| 2647 | *mnt_opts = NULL; |
| 2648 | } |
| 2649 | return rc; |
| 2650 | } |
| 2651 | } else { |
| 2652 | if (!first) { // copy with preceding comma |
| 2653 | from--; |
| 2654 | len++; |
| 2655 | } |
| 2656 | if (to != from) |
| 2657 | memmove(to, from, len); |
| 2658 | to += len; |
| 2659 | first = false; |
| 2660 | } |
| 2661 | if (!from[len]) |
| 2662 | break; |
| 2663 | from += len + 1; |
| 2664 | } |
| 2665 | *to = '\0'; |
| 2666 | return 0; |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 2667 | } |
| 2668 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2669 | static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2670 | { |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2671 | struct selinux_mnt_opts *opts = mnt_opts; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2672 | struct superblock_security_struct *sbsec = sb->s_security; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2673 | u32 sid; |
| 2674 | int rc; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2675 | |
| 2676 | if (!(sbsec->flags & SE_SBINITIALIZED)) |
| 2677 | return 0; |
| 2678 | |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 2679 | if (!opts) |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2680 | return 0; |
| 2681 | |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2682 | if (opts->fscontext) { |
| 2683 | rc = parse_sid(sb, opts->fscontext, &sid); |
| 2684 | if (rc) |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2685 | return rc; |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2686 | if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) |
| 2687 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2688 | } |
Al Viro | bd32365 | 2018-12-13 15:04:59 -0500 | [diff] [blame] | 2689 | if (opts->context) { |
| 2690 | rc = parse_sid(sb, opts->context, &sid); |
| 2691 | if (rc) |
| 2692 | return rc; |
| 2693 | if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) |
| 2694 | goto out_bad_option; |
| 2695 | } |
| 2696 | if (opts->rootcontext) { |
| 2697 | struct inode_security_struct *root_isec; |
| 2698 | root_isec = backing_inode_security(sb->s_root); |
| 2699 | rc = parse_sid(sb, opts->rootcontext, &sid); |
| 2700 | if (rc) |
| 2701 | return rc; |
| 2702 | if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) |
| 2703 | goto out_bad_option; |
| 2704 | } |
| 2705 | if (opts->defcontext) { |
| 2706 | rc = parse_sid(sb, opts->defcontext, &sid); |
| 2707 | if (rc) |
| 2708 | return rc; |
| 2709 | if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) |
| 2710 | goto out_bad_option; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2711 | } |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2712 | return 0; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2713 | |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2714 | out_bad_option: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 2715 | pr_warn("SELinux: unable to change security options " |
Linus Torvalds | 29b1deb | 2013-12-15 11:17:45 -0800 | [diff] [blame] | 2716 | "during remount (dev %s, type=%s)\n", sb->s_id, |
| 2717 | sb->s_type->name); |
Al Viro | c039bc3 | 2018-12-01 23:06:57 -0500 | [diff] [blame] | 2718 | return -EINVAL; |
Eric Paris | 026eb16 | 2011-03-03 16:09:14 -0500 | [diff] [blame] | 2719 | } |
| 2720 | |
Al Viro | a10d7c2 | 2018-12-05 11:58:35 -0500 | [diff] [blame] | 2721 | static int selinux_sb_kern_mount(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2723 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2724 | struct common_audit_data ad; |
James Morris | 7419224 | 2008-12-19 11:41:10 +1100 | [diff] [blame] | 2725 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2726 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2727 | ad.u.dentry = sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2728 | return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2731 | static int selinux_sb_statfs(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2733 | const struct cred *cred = current_cred(); |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 2734 | struct common_audit_data ad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2736 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 2737 | ad.u.dentry = dentry->d_sb->s_root; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2738 | return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | } |
| 2740 | |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2741 | static int selinux_mount(const char *dev_name, |
Al Viro | 8a04c43 | 2016-03-25 14:52:53 -0400 | [diff] [blame] | 2742 | const struct path *path, |
Al Viro | 808d4e3 | 2012-10-11 11:42:01 -0400 | [diff] [blame] | 2743 | const char *type, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2744 | unsigned long flags, |
| 2745 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2747 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | |
| 2749 | if (flags & MS_REMOUNT) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 2750 | return superblock_has_perm(cred, path->dentry->d_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2751 | FILESYSTEM__REMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | else |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2753 | return path_has_perm(cred, path, FILE__MOUNTON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | static int selinux_umount(struct vfsmount *mnt, int flags) |
| 2757 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2758 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2760 | return superblock_has_perm(cred, mnt->mnt_sb, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 2761 | FILESYSTEM__UNMOUNT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | /* inode security operations */ |
| 2765 | |
| 2766 | static int selinux_inode_alloc_security(struct inode *inode) |
| 2767 | { |
| 2768 | return inode_alloc_security(inode); |
| 2769 | } |
| 2770 | |
| 2771 | static void selinux_inode_free_security(struct inode *inode) |
| 2772 | { |
| 2773 | inode_free_security(inode); |
| 2774 | } |
| 2775 | |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2776 | static int selinux_dentry_init_security(struct dentry *dentry, int mode, |
Al Viro | 4f3ccd7 | 2016-07-20 16:06:15 -0400 | [diff] [blame] | 2777 | const struct qstr *name, void **ctx, |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2778 | u32 *ctxlen) |
| 2779 | { |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2780 | u32 newsid; |
| 2781 | int rc; |
| 2782 | |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 2783 | rc = selinux_determine_inode_label(current_security(), |
| 2784 | d_inode(dentry->d_parent), name, |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2785 | inode_mode_to_security_class(mode), |
| 2786 | &newsid); |
| 2787 | if (rc) |
| 2788 | return rc; |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2789 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2790 | return security_sid_to_context(&selinux_state, newsid, (char **)ctx, |
| 2791 | ctxlen); |
David Quigley | d47be3d | 2013-05-22 12:50:34 -0400 | [diff] [blame] | 2792 | } |
| 2793 | |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 2794 | static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, |
| 2795 | struct qstr *name, |
| 2796 | const struct cred *old, |
| 2797 | struct cred *new) |
| 2798 | { |
| 2799 | u32 newsid; |
| 2800 | int rc; |
| 2801 | struct task_security_struct *tsec; |
| 2802 | |
| 2803 | rc = selinux_determine_inode_label(old->security, |
| 2804 | d_inode(dentry->d_parent), name, |
| 2805 | inode_mode_to_security_class(mode), |
| 2806 | &newsid); |
| 2807 | if (rc) |
| 2808 | return rc; |
| 2809 | |
| 2810 | tsec = new->security; |
| 2811 | tsec->create_sid = newsid; |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2815 | static int selinux_inode_init_security(struct inode *inode, struct inode *dir, |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2816 | const struct qstr *qstr, |
| 2817 | const char **name, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2818 | void **value, size_t *len) |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2819 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 2820 | const struct task_security_struct *tsec = current_security(); |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2821 | struct superblock_security_struct *sbsec; |
Corentin LABBE | c0d4f46 | 2017-10-04 20:32:17 +0200 | [diff] [blame] | 2822 | u32 newsid, clen; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2823 | int rc; |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2824 | char *context; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2825 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2826 | sbsec = dir->i_sb->s_security; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2827 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2828 | newsid = tsec->create_sid; |
| 2829 | |
Vivek Goyal | c957f6d | 2016-07-13 10:44:51 -0400 | [diff] [blame] | 2830 | rc = selinux_determine_inode_label(current_security(), |
David Howells | c3c188b | 2015-07-10 17:19:58 -0400 | [diff] [blame] | 2831 | dir, qstr, |
| 2832 | inode_mode_to_security_class(inode->i_mode), |
| 2833 | &newsid); |
| 2834 | if (rc) |
| 2835 | return rc; |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2836 | |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2837 | /* Possibly defer initialization to selinux_complete_init. */ |
David P. Quigley | 0d90a7e | 2009-01-16 09:22:02 -0500 | [diff] [blame] | 2838 | if (sbsec->flags & SE_SBINITIALIZED) { |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2839 | struct inode_security_struct *isec = inode->i_security; |
| 2840 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
| 2841 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2842 | isec->initialized = LABEL_INITIALIZED; |
Eric Paris | 296fddf | 2006-09-25 23:32:00 -0700 | [diff] [blame] | 2843 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2844 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2845 | if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) |
Stephen Smalley | 25a74f3 | 2005-11-08 21:34:33 -0800 | [diff] [blame] | 2846 | return -EOPNOTSUPP; |
| 2847 | |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2848 | if (name) |
| 2849 | *name = XATTR_SELINUX_SUFFIX; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2850 | |
| 2851 | if (value && len) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 2852 | rc = security_sid_to_context_force(&selinux_state, newsid, |
| 2853 | &context, &clen); |
Tetsuo Handa | 9548906 | 2013-07-25 05:44:02 +0900 | [diff] [blame] | 2854 | if (rc) |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2855 | return rc; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 2856 | *value = context; |
| 2857 | *len = clen; |
| 2858 | } |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2859 | |
Stephen Smalley | 5e41ff9 | 2005-09-09 13:01:35 -0700 | [diff] [blame] | 2860 | return 0; |
| 2861 | } |
| 2862 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2863 | 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] | 2864 | { |
| 2865 | return may_create(dir, dentry, SECCLASS_FILE); |
| 2866 | } |
| 2867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2869 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | return may_link(dir, old_dentry, MAY_LINK); |
| 2871 | } |
| 2872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 2874 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | return may_link(dir, dentry, MAY_UNLINK); |
| 2876 | } |
| 2877 | |
| 2878 | static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) |
| 2879 | { |
| 2880 | return may_create(dir, dentry, SECCLASS_LNK_FILE); |
| 2881 | } |
| 2882 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2883 | 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] | 2884 | { |
| 2885 | return may_create(dir, dentry, SECCLASS_DIR); |
| 2886 | } |
| 2887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) |
| 2889 | { |
| 2890 | return may_link(dir, dentry, MAY_RMDIR); |
| 2891 | } |
| 2892 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2893 | 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] | 2894 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | return may_create(dir, dentry, inode_mode_to_security_class(mode)); |
| 2896 | } |
| 2897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | 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] | 2899 | struct inode *new_inode, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | { |
| 2901 | return may_rename(old_inode, old_dentry, new_inode, new_dentry); |
| 2902 | } |
| 2903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | static int selinux_inode_readlink(struct dentry *dentry) |
| 2905 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2906 | const struct cred *cred = current_cred(); |
| 2907 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 2908 | return dentry_has_perm(cred, dentry, FILE__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2911 | static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, |
| 2912 | bool rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2914 | const struct cred *cred = current_cred(); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2915 | struct common_audit_data ad; |
| 2916 | struct inode_security_struct *isec; |
| 2917 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2919 | validate_creds(cred); |
| 2920 | |
| 2921 | ad.type = LSM_AUDIT_DATA_DENTRY; |
| 2922 | ad.u.dentry = dentry; |
| 2923 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2924 | isec = inode_security_rcu(inode, rcu); |
| 2925 | if (IS_ERR(isec)) |
| 2926 | return PTR_ERR(isec); |
NeilBrown | bda0be7 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 2927 | |
Stephen Smalley | e46e01e | 2018-12-12 10:10:56 -0500 | [diff] [blame^] | 2928 | return avc_has_perm(&selinux_state, |
| 2929 | sid, isec->sid, isec->sclass, FILE__READ, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2932 | static noinline int audit_inode_permission(struct inode *inode, |
| 2933 | u32 perms, u32 audited, u32 denied, |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 2934 | int result, |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2935 | unsigned flags) |
| 2936 | { |
| 2937 | struct common_audit_data ad; |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2938 | struct inode_security_struct *isec = inode->i_security; |
| 2939 | int rc; |
| 2940 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 2941 | ad.type = LSM_AUDIT_DATA_INODE; |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2942 | ad.u.inode = inode; |
| 2943 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2944 | rc = slow_avc_audit(&selinux_state, |
| 2945 | current_sid(), isec->sid, isec->sclass, perms, |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 2946 | audited, denied, result, &ad, flags); |
Eric Paris | d4cf970d | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2947 | if (rc) |
| 2948 | return rc; |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
Al Viro | e74f71e | 2011-06-20 19:38:15 -0400 | [diff] [blame] | 2952 | static int selinux_inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 2954 | const struct cred *cred = current_cred(); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2955 | u32 perms; |
| 2956 | bool from_access; |
Al Viro | cf1dd1d | 2011-06-20 19:44:08 -0400 | [diff] [blame] | 2957 | unsigned flags = mask & MAY_NOT_BLOCK; |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2958 | struct inode_security_struct *isec; |
| 2959 | u32 sid; |
| 2960 | struct av_decision avd; |
| 2961 | int rc, rc2; |
| 2962 | u32 audited, denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2964 | from_access = mask & MAY_ACCESS; |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 2965 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); |
| 2966 | |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2967 | /* No permission to check. Existence test. */ |
| 2968 | if (!mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2971 | validate_creds(cred); |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2972 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2973 | if (unlikely(IS_PRIVATE(inode))) |
| 2974 | return 0; |
Eric Paris | b782e0a | 2010-07-23 11:44:03 -0400 | [diff] [blame] | 2975 | |
| 2976 | perms = file_mask_to_av(inode->i_mode, mask); |
| 2977 | |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2978 | sid = cred_sid(cred); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 2979 | isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); |
| 2980 | if (IS_ERR(isec)) |
| 2981 | return PTR_ERR(isec); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2982 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 2983 | rc = avc_has_perm_noaudit(&selinux_state, |
Stephen Smalley | 3a28cff | 2018-12-12 10:10:55 -0500 | [diff] [blame] | 2984 | sid, isec->sid, isec->sclass, perms, |
| 2985 | (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0, |
| 2986 | &avd); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2987 | audited = avc_audit_required(perms, &avd, rc, |
| 2988 | from_access ? FILE__AUDIT_ACCESS : 0, |
| 2989 | &denied); |
| 2990 | if (likely(!audited)) |
| 2991 | return rc; |
| 2992 | |
Stephen Smalley | 626b974 | 2014-04-29 11:29:04 -0700 | [diff] [blame] | 2993 | rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); |
Eric Paris | 2e33405 | 2012-04-04 15:01:42 -0400 | [diff] [blame] | 2994 | if (rc2) |
| 2995 | return rc2; |
| 2996 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) |
| 3000 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3001 | const struct cred *cred = current_cred(); |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3002 | struct inode *inode = d_backing_inode(dentry); |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3003 | unsigned int ia_valid = iattr->ia_valid; |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3004 | __u32 av = FILE__WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3006 | /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ |
| 3007 | if (ia_valid & ATTR_FORCE) { |
| 3008 | ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | |
| 3009 | ATTR_FORCE); |
| 3010 | if (!ia_valid) |
| 3011 | return 0; |
| 3012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | |
Amerigo Wang | bc6a600 | 2009-08-20 19:29:02 -0700 | [diff] [blame] | 3014 | if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | |
| 3015 | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3016 | return dentry_has_perm(cred, dentry, FILE__SETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3018 | if (selinux_policycap_openperm() && |
Stephen Smalley | ccb5447 | 2017-05-12 12:41:24 -0400 | [diff] [blame] | 3019 | inode->i_sb->s_magic != SOCKFS_MAGIC && |
| 3020 | (ia_valid & ATTR_SIZE) && |
| 3021 | !(ia_valid & ATTR_FILE)) |
Eric Paris | 95dbf73 | 2012-04-04 13:45:34 -0400 | [diff] [blame] | 3022 | av |= FILE__OPEN; |
| 3023 | |
| 3024 | return dentry_has_perm(cred, dentry, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | } |
| 3026 | |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3027 | static int selinux_inode_getattr(const struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | { |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 3029 | return path_has_perm(current_cred(), path, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3032 | static bool has_cap_mac_admin(bool audit) |
| 3033 | { |
| 3034 | const struct cred *cred = current_cred(); |
| 3035 | int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; |
| 3036 | |
| 3037 | if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) |
| 3038 | return false; |
| 3039 | if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) |
| 3040 | return false; |
| 3041 | return true; |
| 3042 | } |
| 3043 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3044 | static int selinux_inode_setxattr(struct dentry *dentry, const char *name, |
| 3045 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3047 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3048 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | struct superblock_security_struct *sbsec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 3050 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3051 | u32 newsid, sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | int rc = 0; |
| 3053 | |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3054 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3055 | rc = cap_inode_setxattr(dentry, name, value, size, flags); |
| 3056 | if (rc) |
| 3057 | return rc; |
| 3058 | |
| 3059 | /* Not an attribute we recognize, so just check the |
| 3060 | ordinary setattr permission. */ |
| 3061 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
| 3064 | sbsec = inode->i_sb->s_security; |
Eric Paris | 12f348b | 2012-10-09 10:56:25 -0400 | [diff] [blame] | 3065 | if (!(sbsec->flags & SBLABEL_MNT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | return -EOPNOTSUPP; |
| 3067 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 3068 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | return -EPERM; |
| 3070 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3071 | ad.type = LSM_AUDIT_DATA_DENTRY; |
Eric Paris | a269434 | 2011-04-25 13:10:27 -0400 | [diff] [blame] | 3072 | ad.u.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3074 | isec = backing_inode_security(dentry); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3075 | rc = avc_has_perm(&selinux_state, |
| 3076 | sid, isec->sid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | FILE__RELABELFROM, &ad); |
| 3078 | if (rc) |
| 3079 | return rc; |
| 3080 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3081 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3082 | GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3083 | if (rc == -EINVAL) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3084 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3085 | struct audit_buffer *ab; |
| 3086 | size_t audit_size; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3087 | |
| 3088 | /* We strip a nul only if it is at the end, otherwise the |
| 3089 | * context contains a nul and we should audit that */ |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3090 | if (value) { |
Colin Ian King | add2437 | 2017-10-14 13:46:55 +0100 | [diff] [blame] | 3091 | const char *str = value; |
| 3092 | |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3093 | if (str[size - 1] == '\0') |
| 3094 | audit_size = size - 1; |
| 3095 | else |
| 3096 | audit_size = size; |
| 3097 | } else { |
Al Viro | e3fea3f | 2012-06-09 08:15:16 +0100 | [diff] [blame] | 3098 | audit_size = 0; |
| 3099 | } |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 3100 | ab = audit_log_start(audit_context(), |
| 3101 | GFP_ATOMIC, AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3102 | audit_log_format(ab, "op=setxattr invalid_context="); |
| 3103 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 3104 | audit_log_end(ab); |
| 3105 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3106 | return rc; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 3107 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3108 | rc = security_context_to_sid_force(&selinux_state, value, |
| 3109 | size, &newsid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | if (rc) |
| 3112 | return rc; |
| 3113 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3114 | rc = avc_has_perm(&selinux_state, |
| 3115 | sid, newsid, isec->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | FILE__RELABELTO, &ad); |
| 3117 | if (rc) |
| 3118 | return rc; |
| 3119 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3120 | rc = security_validate_transition(&selinux_state, isec->sid, newsid, |
| 3121 | sid, isec->sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | if (rc) |
| 3123 | return rc; |
| 3124 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3125 | return avc_has_perm(&selinux_state, |
| 3126 | newsid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | sbsec->sid, |
| 3128 | SECCLASS_FILESYSTEM, |
| 3129 | FILESYSTEM__ASSOCIATE, |
| 3130 | &ad); |
| 3131 | } |
| 3132 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3133 | static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 3134 | const void *value, size_t size, |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3135 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 3137 | struct inode *inode = d_backing_inode(dentry); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3138 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | u32 newsid; |
| 3140 | int rc; |
| 3141 | |
| 3142 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3143 | /* Not an attribute we recognize, so nothing to do. */ |
| 3144 | return; |
| 3145 | } |
| 3146 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3147 | rc = security_context_to_sid_force(&selinux_state, value, size, |
| 3148 | &newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | if (rc) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 3150 | pr_err("SELinux: unable to map context to SID" |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 3151 | "for (%s, %lu), rc=%d\n", |
| 3152 | inode->i_sb->s_id, inode->i_ino, -rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | return; |
| 3154 | } |
| 3155 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3156 | isec = backing_inode_security(dentry); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3157 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3158 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3160 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3161 | spin_unlock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | return; |
| 3164 | } |
| 3165 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3166 | static int selinux_inode_getxattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3168 | const struct cred *cred = current_cred(); |
| 3169 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3170 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | } |
| 3172 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3173 | static int selinux_inode_listxattr(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3175 | const struct cred *cred = current_cred(); |
| 3176 | |
Eric Paris | 2875fa0 | 2011-04-28 16:04:24 -0400 | [diff] [blame] | 3177 | return dentry_has_perm(cred, dentry, FILE__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 3180 | static int selinux_inode_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | { |
Eric W. Biederman | 6b24030 | 2017-10-02 09:38:20 -0500 | [diff] [blame] | 3182 | if (strcmp(name, XATTR_NAME_SELINUX)) { |
| 3183 | int rc = cap_inode_removexattr(dentry, name); |
| 3184 | if (rc) |
| 3185 | return rc; |
| 3186 | |
| 3187 | /* Not an attribute we recognize, so just check the |
| 3188 | ordinary setattr permission. */ |
| 3189 | return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); |
| 3190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | |
| 3192 | /* No one is allowed to remove a SELinux security label. |
| 3193 | You can change the label, but all data must be labeled. */ |
| 3194 | return -EACCES; |
| 3195 | } |
| 3196 | |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3197 | /* |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3198 | * Copy the inode security context value to the user. |
James Morris | d381d8a | 2005-10-30 14:59:22 -0800 | [diff] [blame] | 3199 | * |
| 3200 | * Permission check is handled by selinux_inode_getxattr hook. |
| 3201 | */ |
Andreas Gruenbacher | ea861df | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3202 | 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] | 3203 | { |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3204 | u32 size; |
| 3205 | int error; |
| 3206 | char *context = NULL; |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3207 | struct inode_security_struct *isec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 3209 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3210 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3212 | /* |
| 3213 | * If the caller has CAP_MAC_ADMIN, then get the raw context |
| 3214 | * value even if it is not defined by current policy; otherwise, |
| 3215 | * use the in-core value under current policy. |
| 3216 | * Use the non-auditing forms of the permission checks since |
| 3217 | * getxattr may be called by unprivileged processes commonly |
| 3218 | * and lack of permission just means that we fall back to the |
| 3219 | * in-core context value, not a denial. |
| 3220 | */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3221 | isec = inode_security(inode); |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 3222 | if (has_cap_mac_admin(false)) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3223 | error = security_sid_to_context_force(&selinux_state, |
| 3224 | isec->sid, &context, |
Stephen Smalley | abc69bb | 2008-05-21 14:16:12 -0400 | [diff] [blame] | 3225 | &size); |
| 3226 | else |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3227 | error = security_sid_to_context(&selinux_state, isec->sid, |
| 3228 | &context, &size); |
David P. Quigley | 4249259 | 2008-02-04 22:29:39 -0800 | [diff] [blame] | 3229 | if (error) |
| 3230 | return error; |
| 3231 | error = size; |
| 3232 | if (alloc) { |
| 3233 | *buffer = context; |
| 3234 | goto out_nofree; |
| 3235 | } |
| 3236 | kfree(context); |
| 3237 | out_nofree: |
| 3238 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | static int selinux_inode_setsecurity(struct inode *inode, const char *name, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3242 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | { |
Paul Moore | 2c97165 | 2016-04-19 16:36:28 -0400 | [diff] [blame] | 3244 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | u32 newsid; |
| 3246 | int rc; |
| 3247 | |
| 3248 | if (strcmp(name, XATTR_SELINUX_SUFFIX)) |
| 3249 | return -EOPNOTSUPP; |
| 3250 | |
| 3251 | if (!value || !size) |
| 3252 | return -EACCES; |
| 3253 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3254 | rc = security_context_to_sid(&selinux_state, value, size, &newsid, |
| 3255 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | if (rc) |
| 3257 | return rc; |
| 3258 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3259 | spin_lock(&isec->lock); |
David Quigley | aa9c266 | 2013-05-22 12:50:44 -0400 | [diff] [blame] | 3260 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | isec->sid = newsid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 3262 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 3263 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | return 0; |
| 3265 | } |
| 3266 | |
| 3267 | static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) |
| 3268 | { |
| 3269 | const int len = sizeof(XATTR_NAME_SELINUX); |
| 3270 | if (buffer && len <= buffer_size) |
| 3271 | memcpy(buffer, XATTR_NAME_SELINUX, len); |
| 3272 | return len; |
| 3273 | } |
| 3274 | |
Andreas Gruenbacher | d6335d7 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3275 | static void selinux_inode_getsecid(struct inode *inode, u32 *secid) |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3276 | { |
Andreas Gruenbacher | e817c2f | 2016-02-18 12:04:08 +0100 | [diff] [blame] | 3277 | struct inode_security_struct *isec = inode_security_novalidate(inode); |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 3278 | *secid = isec->sid; |
| 3279 | } |
| 3280 | |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 3281 | static int selinux_inode_copy_up(struct dentry *src, struct cred **new) |
| 3282 | { |
| 3283 | u32 sid; |
| 3284 | struct task_security_struct *tsec; |
| 3285 | struct cred *new_creds = *new; |
| 3286 | |
| 3287 | if (new_creds == NULL) { |
| 3288 | new_creds = prepare_creds(); |
| 3289 | if (!new_creds) |
| 3290 | return -ENOMEM; |
| 3291 | } |
| 3292 | |
| 3293 | tsec = new_creds->security; |
| 3294 | /* Get label from overlay inode and set it in create_sid */ |
| 3295 | selinux_inode_getsecid(d_inode(src), &sid); |
| 3296 | tsec->create_sid = sid; |
| 3297 | *new = new_creds; |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 3301 | static int selinux_inode_copy_up_xattr(const char *name) |
| 3302 | { |
| 3303 | /* The copy_up hook above sets the initial context on an inode, but we |
| 3304 | * don't then want to overwrite it by blindly copying all the lower |
| 3305 | * xattrs up. Instead, we have to filter out SELinux-related xattrs. |
| 3306 | */ |
| 3307 | if (strcmp(name, XATTR_NAME_SELINUX) == 0) |
| 3308 | return 1; /* Discard */ |
| 3309 | /* |
| 3310 | * Any other attribute apart from SELINUX is not claimed, supported |
| 3311 | * by selinux. |
| 3312 | */ |
| 3313 | return -EOPNOTSUPP; |
| 3314 | } |
| 3315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | /* file security operations */ |
| 3317 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3318 | static int selinux_revalidate_file_permission(struct file *file, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3320 | const struct cred *cred = current_cred(); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3321 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ |
| 3324 | if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) |
| 3325 | mask |= MAY_APPEND; |
| 3326 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3327 | return file_has_perm(cred, file, |
| 3328 | file_mask_to_av(inode->i_mode, mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3331 | static int selinux_file_permission(struct file *file, int mask) |
| 3332 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3333 | struct inode *inode = file_inode(file); |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3334 | struct file_security_struct *fsec = file->f_security; |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3335 | struct inode_security_struct *isec; |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3336 | u32 sid = current_sid(); |
| 3337 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 3338 | if (!mask) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3339 | /* No permission to check. Existence test. */ |
| 3340 | return 0; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3341 | |
Andreas Gruenbacher | b197367 | 2016-01-05 23:12:33 +0100 | [diff] [blame] | 3342 | isec = inode_security(inode); |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3343 | if (sid == fsec->sid && fsec->isid == isec->sid && |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3344 | fsec->pseqno == avc_policy_seqno(&selinux_state)) |
Eric Paris | 83d4985 | 2012-04-04 13:45:40 -0400 | [diff] [blame] | 3345 | /* No change since file_open check. */ |
Stephen Smalley | 20dda18 | 2009-06-22 14:54:53 -0400 | [diff] [blame] | 3346 | return 0; |
| 3347 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3348 | return selinux_revalidate_file_permission(file, mask); |
| 3349 | } |
| 3350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | static int selinux_file_alloc_security(struct file *file) |
| 3352 | { |
| 3353 | return file_alloc_security(file); |
| 3354 | } |
| 3355 | |
| 3356 | static void selinux_file_free_security(struct file *file) |
| 3357 | { |
| 3358 | file_free_security(file); |
| 3359 | } |
| 3360 | |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3361 | /* |
| 3362 | * Check whether a task has the ioctl permission and cmd |
| 3363 | * operation to an inode. |
| 3364 | */ |
Geliang Tang | 1d2a168 | 2015-10-21 17:44:27 -0400 | [diff] [blame] | 3365 | 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] | 3366 | u32 requested, u16 cmd) |
| 3367 | { |
| 3368 | struct common_audit_data ad; |
| 3369 | struct file_security_struct *fsec = file->f_security; |
| 3370 | struct inode *inode = file_inode(file); |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3371 | struct inode_security_struct *isec; |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3372 | struct lsm_ioctlop_audit ioctl; |
| 3373 | u32 ssid = cred_sid(cred); |
| 3374 | int rc; |
| 3375 | u8 driver = cmd >> 8; |
| 3376 | u8 xperm = cmd & 0xff; |
| 3377 | |
| 3378 | ad.type = LSM_AUDIT_DATA_IOCTL_OP; |
| 3379 | ad.u.op = &ioctl; |
| 3380 | ad.u.op->cmd = cmd; |
| 3381 | ad.u.op->path = file->f_path; |
| 3382 | |
| 3383 | if (ssid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3384 | rc = avc_has_perm(&selinux_state, |
| 3385 | ssid, fsec->sid, |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3386 | SECCLASS_FD, |
| 3387 | FD__USE, |
| 3388 | &ad); |
| 3389 | if (rc) |
| 3390 | goto out; |
| 3391 | } |
| 3392 | |
| 3393 | if (unlikely(IS_PRIVATE(inode))) |
| 3394 | return 0; |
| 3395 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3396 | isec = inode_security(inode); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3397 | rc = avc_has_extended_perms(&selinux_state, |
| 3398 | ssid, isec->sid, isec->sclass, |
| 3399 | requested, driver, xperm, &ad); |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3400 | out: |
| 3401 | return rc; |
| 3402 | } |
| 3403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | static int selinux_file_ioctl(struct file *file, unsigned int cmd, |
| 3405 | unsigned long arg) |
| 3406 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3407 | const struct cred *cred = current_cred(); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3408 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3410 | switch (cmd) { |
| 3411 | case FIONREAD: |
| 3412 | /* fall through */ |
| 3413 | case FIBMAP: |
| 3414 | /* fall through */ |
| 3415 | case FIGETBSZ: |
| 3416 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3417 | case FS_IOC_GETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3418 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3419 | case FS_IOC_GETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3420 | error = file_has_perm(cred, file, FILE__GETATTR); |
| 3421 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3423 | case FS_IOC_SETFLAGS: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3424 | /* fall through */ |
Al Viro | 2f99c36 | 2012-03-23 16:04:05 -0400 | [diff] [blame] | 3425 | case FS_IOC_SETVERSION: |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3426 | error = file_has_perm(cred, file, FILE__SETATTR); |
| 3427 | break; |
| 3428 | |
| 3429 | /* sys_ioctl() checks */ |
| 3430 | case FIONBIO: |
| 3431 | /* fall through */ |
| 3432 | case FIOASYNC: |
| 3433 | error = file_has_perm(cred, file, 0); |
| 3434 | break; |
| 3435 | |
| 3436 | case KDSKBENT: |
| 3437 | case KDSKBSENT: |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 3438 | error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, |
Stephen Smalley | 8e4ff6f | 2016-04-08 13:52:00 -0400 | [diff] [blame] | 3439 | SECURITY_CAP_AUDIT, true); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3440 | break; |
| 3441 | |
| 3442 | /* default case assumes that the command will go |
| 3443 | * to the file's ioctl() function. |
| 3444 | */ |
| 3445 | default: |
Jeff Vander Stoep | fa1aa14 | 2015-07-10 17:19:56 -0400 | [diff] [blame] | 3446 | error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); |
Eric Paris | 0b24dcb | 2011-02-25 15:39:20 -0500 | [diff] [blame] | 3447 | } |
| 3448 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | } |
| 3450 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3451 | static int default_noexec; |
| 3452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | static int file_map_prot_check(struct file *file, unsigned long prot, int shared) |
| 3454 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3455 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3456 | u32 sid = cred_sid(cred); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3457 | int rc = 0; |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3458 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3459 | if (default_noexec && |
Stephen Smalley | 892e8ca | 2015-07-10 09:40:59 -0400 | [diff] [blame] | 3460 | (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || |
| 3461 | (!shared && (prot & PROT_WRITE)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | /* |
| 3463 | * We are making executable an anonymous mapping or a |
| 3464 | * private file mapping that will also be writable. |
| 3465 | * This has an additional check. |
| 3466 | */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3467 | rc = avc_has_perm(&selinux_state, |
| 3468 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3469 | PROCESS__EXECMEM, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | if (rc) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3471 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | |
| 3474 | if (file) { |
| 3475 | /* read access is always possible with a mapping */ |
| 3476 | u32 av = FILE__READ; |
| 3477 | |
| 3478 | /* write access only matters if the mapping is shared */ |
| 3479 | if (shared && (prot & PROT_WRITE)) |
| 3480 | av |= FILE__WRITE; |
| 3481 | |
| 3482 | if (prot & PROT_EXEC) |
| 3483 | av |= FILE__EXECUTE; |
| 3484 | |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3485 | return file_has_perm(cred, file, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3487 | |
| 3488 | error: |
| 3489 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3492 | static int selinux_mmap_addr(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 3494 | int rc = 0; |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3495 | |
| 3496 | if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { |
| 3497 | u32 sid = current_sid(); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3498 | rc = avc_has_perm(&selinux_state, |
| 3499 | sid, sid, SECCLASS_MEMPROTECT, |
Paul Moore | 98883bf | 2014-03-19 16:46:11 -0400 | [diff] [blame] | 3500 | MEMPROTECT__MMAP_ZERO, NULL); |
| 3501 | } |
| 3502 | |
| 3503 | return rc; |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 3506 | static int selinux_mmap_file(struct file *file, unsigned long reqprot, |
| 3507 | unsigned long prot, unsigned long flags) |
| 3508 | { |
Stephen Smalley | 3ba4bf5 | 2017-05-05 09:14:48 -0400 | [diff] [blame] | 3509 | struct common_audit_data ad; |
| 3510 | int rc; |
| 3511 | |
| 3512 | if (file) { |
| 3513 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3514 | ad.u.file = file; |
| 3515 | rc = inode_has_perm(current_cred(), file_inode(file), |
| 3516 | FILE__MAP, &ad); |
| 3517 | if (rc) |
| 3518 | return rc; |
| 3519 | } |
| 3520 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3521 | if (selinux_state.checkreqprot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | prot = reqprot; |
| 3523 | |
| 3524 | return file_map_prot_check(file, prot, |
| 3525 | (flags & MAP_TYPE) == MAP_SHARED); |
| 3526 | } |
| 3527 | |
| 3528 | static int selinux_file_mprotect(struct vm_area_struct *vma, |
| 3529 | unsigned long reqprot, |
| 3530 | unsigned long prot) |
| 3531 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3532 | const struct cred *cred = current_cred(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3533 | u32 sid = cred_sid(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3534 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 3535 | if (selinux_state.checkreqprot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | prot = reqprot; |
| 3537 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 3538 | if (default_noexec && |
| 3539 | (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { |
James Morris | d541bbe | 2009-01-29 12:19:51 +1100 | [diff] [blame] | 3540 | int rc = 0; |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3541 | if (vma->vm_start >= vma->vm_mm->start_brk && |
| 3542 | vma->vm_end <= vma->vm_mm->brk) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3543 | rc = avc_has_perm(&selinux_state, |
| 3544 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3545 | PROCESS__EXECHEAP, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3546 | } else if (!vma->vm_file && |
Stephen Smalley | c2316db | 2016-04-08 13:55:03 -0400 | [diff] [blame] | 3547 | ((vma->vm_start <= vma->vm_mm->start_stack && |
| 3548 | vma->vm_end >= vma->vm_mm->start_stack) || |
Andy Lutomirski | d17af50 | 2016-09-30 10:58:58 -0700 | [diff] [blame] | 3549 | vma_is_stack_for_current(vma))) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3550 | rc = avc_has_perm(&selinux_state, |
| 3551 | sid, sid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3552 | PROCESS__EXECSTACK, NULL); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3553 | } else if (vma->vm_file && vma->anon_vma) { |
| 3554 | /* |
| 3555 | * We are making executable a file mapping that has |
| 3556 | * had some COW done. Since pages might have been |
| 3557 | * written, check ability to execute the possibly |
| 3558 | * modified content. This typically should only |
| 3559 | * occur for text relocations. |
| 3560 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3561 | rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); |
Stephen Smalley | db4c964 | 2006-02-01 03:05:54 -0800 | [diff] [blame] | 3562 | } |
Lorenzo Hernandez GarcÃa-Hierro | 6b99219 | 2005-06-25 14:54:34 -0700 | [diff] [blame] | 3563 | if (rc) |
| 3564 | return rc; |
| 3565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | |
| 3567 | return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); |
| 3568 | } |
| 3569 | |
| 3570 | static int selinux_file_lock(struct file *file, unsigned int cmd) |
| 3571 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3572 | const struct cred *cred = current_cred(); |
| 3573 | |
| 3574 | return file_has_perm(cred, file, FILE__LOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | static int selinux_file_fcntl(struct file *file, unsigned int cmd, |
| 3578 | unsigned long arg) |
| 3579 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3580 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | int err = 0; |
| 3582 | |
| 3583 | switch (cmd) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3584 | case F_SETFL: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3585 | if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3586 | err = file_has_perm(cred, file, FILE__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3588 | } |
| 3589 | /* fall through */ |
| 3590 | case F_SETOWN: |
| 3591 | case F_SETSIG: |
| 3592 | case F_GETFL: |
| 3593 | case F_GETOWN: |
| 3594 | case F_GETSIG: |
Cyrill Gorcunov | 1d151c3 | 2012-07-30 14:43:00 -0700 | [diff] [blame] | 3595 | case F_GETOWNER_UIDS: |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3596 | /* Just check FD__USE permission */ |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3597 | err = file_has_perm(cred, file, 0); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3598 | break; |
| 3599 | case F_GETLK: |
| 3600 | case F_SETLK: |
| 3601 | case F_SETLKW: |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 3602 | case F_OFD_GETLK: |
| 3603 | case F_OFD_SETLK: |
| 3604 | case F_OFD_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | #if BITS_PER_LONG == 32 |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3606 | case F_GETLK64: |
| 3607 | case F_SETLK64: |
| 3608 | case F_SETLKW64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | #endif |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3610 | err = file_has_perm(cred, file, FILE__LOCK); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3611 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | return err; |
| 3615 | } |
| 3616 | |
Jeff Layton | e0b93ed | 2014-08-22 11:27:32 -0400 | [diff] [blame] | 3617 | static void selinux_file_set_fowner(struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | struct file_security_struct *fsec; |
| 3620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | fsec = file->f_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3622 | fsec->fown_sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | static int selinux_file_send_sigiotask(struct task_struct *tsk, |
| 3626 | struct fown_struct *fown, int signum) |
| 3627 | { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3628 | struct file *file; |
Stephen Smalley | 65c90bc | 2009-05-04 15:43:18 -0400 | [diff] [blame] | 3629 | u32 sid = task_sid(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | struct file_security_struct *fsec; |
| 3632 | |
| 3633 | /* struct fown_struct is never outside the context of a struct file */ |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 3634 | file = container_of(fown, struct file, f_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | fsec = file->f_security; |
| 3637 | |
| 3638 | if (!signum) |
| 3639 | perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ |
| 3640 | else |
| 3641 | perm = signal_to_av(signum); |
| 3642 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3643 | return avc_has_perm(&selinux_state, |
| 3644 | fsec->fown_sid, sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | SECCLASS_PROCESS, perm, NULL); |
| 3646 | } |
| 3647 | |
| 3648 | static int selinux_file_receive(struct file *file) |
| 3649 | { |
David Howells | 88e67f3 | 2008-11-14 10:39:21 +1100 | [diff] [blame] | 3650 | const struct cred *cred = current_cred(); |
| 3651 | |
| 3652 | return file_has_perm(cred, file, file_to_av(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3655 | static int selinux_file_open(struct file *file) |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3656 | { |
| 3657 | struct file_security_struct *fsec; |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3658 | struct inode_security_struct *isec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3659 | |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3660 | fsec = file->f_security; |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3661 | isec = inode_security(file_inode(file)); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3662 | /* |
| 3663 | * Save inode label and policy sequence number |
| 3664 | * at open-time so that selinux_file_permission |
| 3665 | * can determine whether revalidation is necessary. |
| 3666 | * Task label is already saved in the file security |
| 3667 | * struct as its SID. |
| 3668 | */ |
| 3669 | fsec->isid = isec->sid; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3670 | fsec->pseqno = avc_policy_seqno(&selinux_state); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 3671 | /* |
| 3672 | * Since the inode label or policy seqno may have changed |
| 3673 | * between the selinux_inode_permission check and the saving |
| 3674 | * of state above, recheck that access is still permitted. |
| 3675 | * Otherwise, access might never be revalidated against the |
| 3676 | * new inode label or new policy. |
| 3677 | * This check is not redundant - do not remove. |
| 3678 | */ |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 3679 | 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] | 3680 | } |
| 3681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | /* task security operations */ |
| 3683 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 3684 | static int selinux_task_alloc(struct task_struct *task, |
| 3685 | unsigned long clone_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3687 | u32 sid = current_sid(); |
| 3688 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3689 | return avc_has_perm(&selinux_state, |
| 3690 | sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | } |
| 3692 | |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3693 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3694 | * allocate the SELinux part of blank credentials |
| 3695 | */ |
| 3696 | static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 3697 | { |
| 3698 | struct task_security_struct *tsec; |
| 3699 | |
| 3700 | tsec = kzalloc(sizeof(struct task_security_struct), gfp); |
| 3701 | if (!tsec) |
| 3702 | return -ENOMEM; |
| 3703 | |
| 3704 | cred->security = tsec; |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
| 3708 | /* |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3709 | * detach and free the LSM part of a set of credentials |
| 3710 | */ |
| 3711 | static void selinux_cred_free(struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | { |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3713 | struct task_security_struct *tsec = cred->security; |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3714 | |
Tetsuo Handa | 2edeaa3 | 2011-02-07 13:36:10 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | * cred->security == NULL if security_cred_alloc_blank() or |
| 3717 | * security_prepare_creds() returned an error. |
| 3718 | */ |
| 3719 | BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 3720 | cred->security = (void *) 0x7UL; |
David Howells | f1752ee | 2008-11-14 10:39:17 +1100 | [diff] [blame] | 3721 | kfree(tsec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | } |
| 3723 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3724 | /* |
| 3725 | * prepare a new set of credentials for modification |
| 3726 | */ |
| 3727 | static int selinux_cred_prepare(struct cred *new, const struct cred *old, |
| 3728 | gfp_t gfp) |
| 3729 | { |
| 3730 | const struct task_security_struct *old_tsec; |
| 3731 | struct task_security_struct *tsec; |
| 3732 | |
| 3733 | old_tsec = old->security; |
| 3734 | |
| 3735 | tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); |
| 3736 | if (!tsec) |
| 3737 | return -ENOMEM; |
| 3738 | |
| 3739 | new->security = tsec; |
| 3740 | return 0; |
| 3741 | } |
| 3742 | |
| 3743 | /* |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3744 | * transfer the SELinux data to a blank set of creds |
| 3745 | */ |
| 3746 | static void selinux_cred_transfer(struct cred *new, const struct cred *old) |
| 3747 | { |
| 3748 | const struct task_security_struct *old_tsec = old->security; |
| 3749 | struct task_security_struct *tsec = new->security; |
| 3750 | |
| 3751 | *tsec = *old_tsec; |
| 3752 | } |
| 3753 | |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 3754 | static void selinux_cred_getsecid(const struct cred *c, u32 *secid) |
| 3755 | { |
| 3756 | *secid = cred_sid(c); |
| 3757 | } |
| 3758 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 3759 | /* |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3760 | * set the security data for a kernel service |
| 3761 | * - all the creation contexts are set to unlabelled |
| 3762 | */ |
| 3763 | static int selinux_kernel_act_as(struct cred *new, u32 secid) |
| 3764 | { |
| 3765 | struct task_security_struct *tsec = new->security; |
| 3766 | u32 sid = current_sid(); |
| 3767 | int ret; |
| 3768 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3769 | ret = avc_has_perm(&selinux_state, |
| 3770 | sid, secid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3771 | SECCLASS_KERNEL_SERVICE, |
| 3772 | KERNEL_SERVICE__USE_AS_OVERRIDE, |
| 3773 | NULL); |
| 3774 | if (ret == 0) { |
| 3775 | tsec->sid = secid; |
| 3776 | tsec->create_sid = 0; |
| 3777 | tsec->keycreate_sid = 0; |
| 3778 | tsec->sockcreate_sid = 0; |
| 3779 | } |
| 3780 | return ret; |
| 3781 | } |
| 3782 | |
| 3783 | /* |
| 3784 | * set the file creation context in a security record to the same as the |
| 3785 | * objective context of the specified inode |
| 3786 | */ |
| 3787 | static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) |
| 3788 | { |
Andreas Gruenbacher | 83da53c5 | 2015-12-24 11:09:39 -0500 | [diff] [blame] | 3789 | struct inode_security_struct *isec = inode_security(inode); |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3790 | struct task_security_struct *tsec = new->security; |
| 3791 | u32 sid = current_sid(); |
| 3792 | int ret; |
| 3793 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3794 | ret = avc_has_perm(&selinux_state, |
| 3795 | sid, isec->sid, |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3796 | SECCLASS_KERNEL_SERVICE, |
| 3797 | KERNEL_SERVICE__CREATE_FILES_AS, |
| 3798 | NULL); |
| 3799 | |
| 3800 | if (ret == 0) |
| 3801 | tsec->create_sid = isec->sid; |
David Howells | ef57471 | 2010-02-26 01:56:16 +0000 | [diff] [blame] | 3802 | return ret; |
David Howells | 3a3b7ce | 2008-11-14 10:39:28 +1100 | [diff] [blame] | 3803 | } |
| 3804 | |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3805 | static int selinux_kernel_module_request(char *kmod_name) |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3806 | { |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3807 | struct common_audit_data ad; |
| 3808 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 3809 | ad.type = LSM_AUDIT_DATA_KMOD; |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3810 | ad.u.kmod_name = kmod_name; |
| 3811 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3812 | return avc_has_perm(&selinux_state, |
| 3813 | current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, |
Eric Paris | dd8dbf2 | 2009-11-03 16:35:32 +1100 | [diff] [blame] | 3814 | SYSTEM__MODULE_REQUEST, &ad); |
Eric Paris | 25354c4 | 2009-08-13 09:45:03 -0400 | [diff] [blame] | 3815 | } |
| 3816 | |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3817 | static int selinux_kernel_module_from_file(struct file *file) |
| 3818 | { |
| 3819 | struct common_audit_data ad; |
| 3820 | struct inode_security_struct *isec; |
| 3821 | struct file_security_struct *fsec; |
| 3822 | u32 sid = current_sid(); |
| 3823 | int rc; |
| 3824 | |
| 3825 | /* init_module */ |
| 3826 | if (file == NULL) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3827 | return avc_has_perm(&selinux_state, |
| 3828 | sid, sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3829 | SYSTEM__MODULE_LOAD, NULL); |
| 3830 | |
| 3831 | /* finit_module */ |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3832 | |
Vivek Goyal | 43af5de | 2016-09-09 11:37:49 -0400 | [diff] [blame] | 3833 | ad.type = LSM_AUDIT_DATA_FILE; |
| 3834 | ad.u.file = file; |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3835 | |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3836 | fsec = file->f_security; |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3837 | if (sid != fsec->sid) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3838 | rc = avc_has_perm(&selinux_state, |
| 3839 | sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3840 | if (rc) |
| 3841 | return rc; |
| 3842 | } |
| 3843 | |
Paul Moore | 20cdef8 | 2016-04-04 14:14:42 -0400 | [diff] [blame] | 3844 | isec = inode_security(file_inode(file)); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3845 | return avc_has_perm(&selinux_state, |
| 3846 | sid, isec->sid, SECCLASS_SYSTEM, |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 3847 | SYSTEM__MODULE_LOAD, &ad); |
| 3848 | } |
| 3849 | |
| 3850 | static int selinux_kernel_read_file(struct file *file, |
| 3851 | enum kernel_read_file_id id) |
| 3852 | { |
| 3853 | int rc = 0; |
| 3854 | |
| 3855 | switch (id) { |
| 3856 | case READING_MODULE: |
| 3857 | rc = selinux_kernel_module_from_file(file); |
| 3858 | break; |
| 3859 | default: |
| 3860 | break; |
| 3861 | } |
| 3862 | |
| 3863 | return rc; |
| 3864 | } |
| 3865 | |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 3866 | static int selinux_kernel_load_data(enum kernel_load_data_id id) |
| 3867 | { |
| 3868 | int rc = 0; |
| 3869 | |
| 3870 | switch (id) { |
| 3871 | case LOADING_MODULE: |
| 3872 | rc = selinux_kernel_module_from_file(NULL); |
| 3873 | default: |
| 3874 | break; |
| 3875 | } |
| 3876 | |
| 3877 | return rc; |
| 3878 | } |
| 3879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) |
| 3881 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3882 | return avc_has_perm(&selinux_state, |
| 3883 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3884 | PROCESS__SETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | static int selinux_task_getpgid(struct task_struct *p) |
| 3888 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3889 | return avc_has_perm(&selinux_state, |
| 3890 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3891 | PROCESS__GETPGID, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | static int selinux_task_getsid(struct task_struct *p) |
| 3895 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3896 | return avc_has_perm(&selinux_state, |
| 3897 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3898 | PROCESS__GETSESSION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | } |
| 3900 | |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3901 | static void selinux_task_getsecid(struct task_struct *p, u32 *secid) |
| 3902 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 3903 | *secid = task_sid(p); |
David Quigley | f9008e4c | 2006-06-30 01:55:46 -0700 | [diff] [blame] | 3904 | } |
| 3905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | static int selinux_task_setnice(struct task_struct *p, int nice) |
| 3907 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3908 | return avc_has_perm(&selinux_state, |
| 3909 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3910 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3913 | static int selinux_task_setioprio(struct task_struct *p, int ioprio) |
| 3914 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3915 | return avc_has_perm(&selinux_state, |
| 3916 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3917 | PROCESS__SETSCHED, NULL); |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 3918 | } |
| 3919 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3920 | static int selinux_task_getioprio(struct task_struct *p) |
| 3921 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3922 | return avc_has_perm(&selinux_state, |
| 3923 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3924 | PROCESS__GETSCHED, NULL); |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 3925 | } |
| 3926 | |
Corentin LABBE | 4298555 | 2017-10-04 20:32:18 +0200 | [diff] [blame] | 3927 | static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, |
| 3928 | unsigned int flags) |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 3929 | { |
| 3930 | u32 av = 0; |
| 3931 | |
Stephen Smalley | 84e6885 | 2017-02-28 09:35:08 -0500 | [diff] [blame] | 3932 | if (!flags) |
| 3933 | return 0; |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 3934 | if (flags & LSM_PRLIMIT_WRITE) |
| 3935 | av |= PROCESS__SETRLIMIT; |
| 3936 | if (flags & LSM_PRLIMIT_READ) |
| 3937 | av |= PROCESS__GETRLIMIT; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3938 | return avc_has_perm(&selinux_state, |
| 3939 | cred_sid(cred), cred_sid(tcred), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 3940 | SECCLASS_PROCESS, av, NULL); |
| 3941 | } |
| 3942 | |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3943 | static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, |
| 3944 | struct rlimit *new_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | { |
Jiri Slaby | 8fd00b4 | 2009-08-26 18:41:16 +0200 | [diff] [blame] | 3946 | struct rlimit *old_rlim = p->signal->rlim + resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | |
| 3948 | /* Control the ability to change the hard limit (whether |
| 3949 | lowering or raising it), so that the hard limit can |
| 3950 | later be used as a safe reset point for the soft limit |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 3951 | upon context transitions. See selinux_bprm_committing_creds. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | if (old_rlim->rlim_max != new_rlim->rlim_max) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3953 | return avc_has_perm(&selinux_state, |
| 3954 | current_sid(), task_sid(p), |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3955 | SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | |
| 3957 | return 0; |
| 3958 | } |
| 3959 | |
KOSAKI Motohiro | b0ae198 | 2010-10-15 04:21:18 +0900 | [diff] [blame] | 3960 | static int selinux_task_setscheduler(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3962 | return avc_has_perm(&selinux_state, |
| 3963 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3964 | PROCESS__SETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | static int selinux_task_getscheduler(struct task_struct *p) |
| 3968 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3969 | return avc_has_perm(&selinux_state, |
| 3970 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3971 | PROCESS__GETSCHED, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | } |
| 3973 | |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 3974 | static int selinux_task_movememory(struct task_struct *p) |
| 3975 | { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3976 | return avc_has_perm(&selinux_state, |
| 3977 | current_sid(), task_sid(p), SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3978 | PROCESS__SETSCHED, NULL); |
David Quigley | 3560154 | 2006-06-23 02:04:01 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Eric W. Biederman | ae7795b | 2018-09-25 11:27:20 +0200 | [diff] [blame] | 3981 | 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] | 3982 | int sig, const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | { |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 3984 | u32 secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | u32 perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | if (!sig) |
| 3988 | perm = PROCESS__SIGNULL; /* null signal; existence test */ |
| 3989 | else |
| 3990 | perm = signal_to_av(sig); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 3991 | if (!cred) |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 3992 | secid = current_sid(); |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 3993 | else |
| 3994 | secid = cred_sid(cred); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 3995 | return avc_has_perm(&selinux_state, |
| 3996 | secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | static void selinux_task_to_inode(struct task_struct *p, |
| 4000 | struct inode *inode) |
| 4001 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | struct inode_security_struct *isec = inode->i_security; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4003 | u32 sid = task_sid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4005 | spin_lock(&isec->lock); |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 4006 | isec->sclass = inode_mode_to_security_class(inode->i_mode); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4007 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4008 | isec->initialized = LABEL_INITIALIZED; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4009 | spin_unlock(&isec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4013 | static int selinux_parse_skb_ipv4(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4014 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | { |
| 4016 | int offset, ihlen, ret = -EINVAL; |
| 4017 | struct iphdr _iph, *ih; |
| 4018 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4019 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); |
| 4021 | if (ih == NULL) |
| 4022 | goto out; |
| 4023 | |
| 4024 | ihlen = ih->ihl * 4; |
| 4025 | if (ihlen < sizeof(_iph)) |
| 4026 | goto out; |
| 4027 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4028 | ad->u.net->v4info.saddr = ih->saddr; |
| 4029 | ad->u.net->v4info.daddr = ih->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | ret = 0; |
| 4031 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4032 | if (proto) |
| 4033 | *proto = ih->protocol; |
| 4034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | switch (ih->protocol) { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4036 | case IPPROTO_TCP: { |
| 4037 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4039 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4040 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | |
| 4042 | offset += ihlen; |
| 4043 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4044 | if (th == NULL) |
| 4045 | break; |
| 4046 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4047 | ad->u.net->sport = th->source; |
| 4048 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4052 | case IPPROTO_UDP: { |
| 4053 | struct udphdr _udph, *uh; |
| 4054 | |
| 4055 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4056 | break; |
| 4057 | |
| 4058 | offset += ihlen; |
| 4059 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4060 | if (uh == NULL) |
| 4061 | break; |
| 4062 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4063 | ad->u.net->sport = uh->source; |
| 4064 | ad->u.net->dport = uh->dest; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4065 | break; |
| 4066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4068 | case IPPROTO_DCCP: { |
| 4069 | struct dccp_hdr _dccph, *dh; |
| 4070 | |
| 4071 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4072 | break; |
| 4073 | |
| 4074 | offset += ihlen; |
| 4075 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4076 | if (dh == NULL) |
| 4077 | break; |
| 4078 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4079 | ad->u.net->sport = dh->dccph_sport; |
| 4080 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4081 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4082 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4083 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4084 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4085 | case IPPROTO_SCTP: { |
| 4086 | struct sctphdr _sctph, *sh; |
| 4087 | |
| 4088 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 4089 | break; |
| 4090 | |
| 4091 | offset += ihlen; |
| 4092 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4093 | if (sh == NULL) |
| 4094 | break; |
| 4095 | |
| 4096 | ad->u.net->sport = sh->source; |
| 4097 | ad->u.net->dport = sh->dest; |
| 4098 | break; |
| 4099 | } |
| 4100 | #endif |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4101 | default: |
| 4102 | break; |
| 4103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | out: |
| 4105 | return ret; |
| 4106 | } |
| 4107 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4108 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | |
| 4110 | /* Returns error only if unable to parse addresses */ |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4111 | static int selinux_parse_skb_ipv6(struct sk_buff *skb, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4112 | struct common_audit_data *ad, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | { |
| 4114 | u8 nexthdr; |
| 4115 | int ret = -EINVAL, offset; |
| 4116 | struct ipv6hdr _ipv6h, *ip6; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4117 | __be16 frag_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 4119 | offset = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); |
| 4121 | if (ip6 == NULL) |
| 4122 | goto out; |
| 4123 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4124 | ad->u.net->v6info.saddr = ip6->saddr; |
| 4125 | ad->u.net->v6info.daddr = ip6->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | ret = 0; |
| 4127 | |
| 4128 | nexthdr = ip6->nexthdr; |
| 4129 | offset += sizeof(_ipv6h); |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 4130 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4131 | if (offset < 0) |
| 4132 | goto out; |
| 4133 | |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4134 | if (proto) |
| 4135 | *proto = nexthdr; |
| 4136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | switch (nexthdr) { |
| 4138 | case IPPROTO_TCP: { |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4139 | struct tcphdr _tcph, *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | |
| 4141 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); |
| 4142 | if (th == NULL) |
| 4143 | break; |
| 4144 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4145 | ad->u.net->sport = th->source; |
| 4146 | ad->u.net->dport = th->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | break; |
| 4148 | } |
| 4149 | |
| 4150 | case IPPROTO_UDP: { |
| 4151 | struct udphdr _udph, *uh; |
| 4152 | |
| 4153 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); |
| 4154 | if (uh == NULL) |
| 4155 | break; |
| 4156 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4157 | ad->u.net->sport = uh->source; |
| 4158 | ad->u.net->dport = uh->dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | break; |
| 4160 | } |
| 4161 | |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4162 | case IPPROTO_DCCP: { |
| 4163 | struct dccp_hdr _dccph, *dh; |
| 4164 | |
| 4165 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); |
| 4166 | if (dh == NULL) |
| 4167 | break; |
| 4168 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4169 | ad->u.net->sport = dh->dccph_sport; |
| 4170 | ad->u.net->dport = dh->dccph_dport; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4171 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4172 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4173 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4174 | #if IS_ENABLED(CONFIG_IP_SCTP) |
| 4175 | case IPPROTO_SCTP: { |
| 4176 | struct sctphdr _sctph, *sh; |
| 4177 | |
| 4178 | sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); |
| 4179 | if (sh == NULL) |
| 4180 | break; |
| 4181 | |
| 4182 | ad->u.net->sport = sh->source; |
| 4183 | ad->u.net->dport = sh->dest; |
| 4184 | break; |
| 4185 | } |
| 4186 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | /* includes fragments */ |
| 4188 | default: |
| 4189 | break; |
| 4190 | } |
| 4191 | out: |
| 4192 | return ret; |
| 4193 | } |
| 4194 | |
| 4195 | #endif /* IPV6 */ |
| 4196 | |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4197 | 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] | 4198 | char **_addrp, int src, u8 *proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | { |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4200 | char *addrp; |
| 4201 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4203 | switch (ad->u.net->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | case PF_INET: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4205 | ret = selinux_parse_skb_ipv4(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4206 | if (ret) |
| 4207 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4208 | addrp = (char *)(src ? &ad->u.net->v4info.saddr : |
| 4209 | &ad->u.net->v4info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4210 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 4212 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | case PF_INET6: |
Venkat Yekkirala | 67f83cb | 2006-11-08 17:04:26 -0600 | [diff] [blame] | 4214 | ret = selinux_parse_skb_ipv6(skb, ad, proto); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4215 | if (ret) |
| 4216 | goto parse_error; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4217 | addrp = (char *)(src ? &ad->u.net->v6info.saddr : |
| 4218 | &ad->u.net->v6info.daddr); |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4219 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | #endif /* IPV6 */ |
| 4221 | default: |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4222 | addrp = NULL; |
| 4223 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | } |
| 4225 | |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4226 | parse_error: |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4227 | pr_warn( |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4228 | "SELinux: failure in selinux_parse_skb()," |
| 4229 | " unable to parse packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | return ret; |
David Howells | cf9481e | 2008-07-27 21:31:07 +1000 | [diff] [blame] | 4231 | |
| 4232 | okay: |
| 4233 | if (_addrp) |
| 4234 | *_addrp = addrp; |
| 4235 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4238 | /** |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4239 | * selinux_skb_peerlbl_sid - Determine the peer label of a packet |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4240 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4241 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4242 | * @sid: the packet's peer label SID |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4243 | * |
| 4244 | * Description: |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4245 | * Check the various different forms of network peer labeling and determine |
| 4246 | * the peer label/SID for the packet; most of the magic actually occurs in |
| 4247 | * the security server function security_net_peersid_cmp(). The function |
| 4248 | * returns zero if the value in @sid is valid (although it may be SECSID_NULL) |
| 4249 | * or -EACCES if @sid is invalid due to inconsistencies with the different |
| 4250 | * peer labels. |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4251 | * |
| 4252 | */ |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4253 | 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] | 4254 | { |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4255 | int err; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4256 | u32 xfrm_sid; |
| 4257 | u32 nlbl_sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4258 | u32 nlbl_type; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4259 | |
Paul Moore | 817eff7 | 2013-12-10 14:57:54 -0500 | [diff] [blame] | 4260 | err = selinux_xfrm_skb_sid(skb, &xfrm_sid); |
Paul Moore | bed4d7e | 2013-07-23 17:38:40 -0400 | [diff] [blame] | 4261 | if (unlikely(err)) |
| 4262 | return -EACCES; |
| 4263 | err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); |
| 4264 | if (unlikely(err)) |
| 4265 | return -EACCES; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4266 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4267 | err = security_net_peersid_resolve(&selinux_state, nlbl_sid, |
| 4268 | nlbl_type, xfrm_sid, sid); |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4269 | if (unlikely(err)) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 4270 | pr_warn( |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4271 | "SELinux: failure in selinux_skb_peerlbl_sid()," |
| 4272 | " unable to determine packet's peer label\n"); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4273 | return -EACCES; |
Paul Moore | 71f1cb0 | 2008-01-29 08:51:16 -0500 | [diff] [blame] | 4274 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4275 | |
| 4276 | return 0; |
Paul Moore | 4f6a993 | 2007-03-01 14:35:22 -0500 | [diff] [blame] | 4277 | } |
| 4278 | |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4279 | /** |
| 4280 | * selinux_conn_sid - Determine the child socket label for a connection |
| 4281 | * @sk_sid: the parent socket's SID |
| 4282 | * @skb_sid: the packet's SID |
| 4283 | * @conn_sid: the resulting connection SID |
| 4284 | * |
| 4285 | * If @skb_sid is valid then the user:role:type information from @sk_sid is |
| 4286 | * combined with the MLS information from @skb_sid in order to create |
| 4287 | * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy |
| 4288 | * of @sk_sid. Returns zero on success, negative values on failure. |
| 4289 | * |
| 4290 | */ |
| 4291 | static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) |
| 4292 | { |
| 4293 | int err = 0; |
| 4294 | |
| 4295 | if (skb_sid != SECSID_NULL) |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4296 | err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, |
| 4297 | conn_sid); |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 4298 | else |
| 4299 | *conn_sid = sk_sid; |
| 4300 | |
| 4301 | return err; |
| 4302 | } |
| 4303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | /* socket security operations */ |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4305 | |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4306 | static int socket_sockcreate_sid(const struct task_security_struct *tsec, |
| 4307 | u16 secclass, u32 *socksid) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4308 | { |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4309 | if (tsec->sockcreate_sid > SECSID_NULL) { |
| 4310 | *socksid = tsec->sockcreate_sid; |
| 4311 | return 0; |
| 4312 | } |
| 4313 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4314 | return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, |
| 4315 | secclass, NULL, socksid); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4316 | } |
| 4317 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4318 | static int sock_has_perm(struct sock *sk, u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4320 | struct sk_security_struct *sksec = sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4321 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4322 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4324 | if (sksec->sid == SECINITSID_KERNEL) |
| 4325 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4327 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4328 | ad.u.net = &net; |
| 4329 | ad.u.net->sk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4331 | return avc_has_perm(&selinux_state, |
| 4332 | current_sid(), sksec->sid, sksec->sclass, perms, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4333 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | static int selinux_socket_create(int family, int type, |
| 4337 | int protocol, int kern) |
| 4338 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4339 | const struct task_security_struct *tsec = current_security(); |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4340 | u32 newsid; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4341 | u16 secclass; |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4342 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | |
| 4344 | if (kern) |
Paul Moore | d4f2d97 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4345 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4347 | secclass = socket_type_to_security_class(family, type, protocol); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4348 | rc = socket_sockcreate_sid(tsec, secclass, &newsid); |
| 4349 | if (rc) |
| 4350 | return rc; |
| 4351 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4352 | return avc_has_perm(&selinux_state, |
| 4353 | tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4356 | static int selinux_socket_post_create(struct socket *sock, int family, |
| 4357 | int type, int protocol, int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | { |
Paul Moore | 5fb4987 | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4359 | const struct task_security_struct *tsec = current_security(); |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4360 | struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4361 | struct sk_security_struct *sksec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4362 | u16 sclass = socket_type_to_security_class(family, type, protocol); |
| 4363 | u32 sid = SECINITSID_KERNEL; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4364 | int err = 0; |
| 4365 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4366 | if (!kern) { |
| 4367 | err = socket_sockcreate_sid(tsec, sclass, &sid); |
Harry Ciao | 2ad18bd | 2011-03-02 13:32:34 +0800 | [diff] [blame] | 4368 | if (err) |
| 4369 | return err; |
| 4370 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 4371 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4372 | isec->sclass = sclass; |
| 4373 | isec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4374 | isec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4376 | if (sock->sk) { |
| 4377 | sksec = sock->sk->sk_security; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4378 | sksec->sclass = sclass; |
| 4379 | sksec->sid = sid; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4380 | /* Allows detection of the first association on this socket */ |
| 4381 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4382 | sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; |
| 4383 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 4384 | err = selinux_netlbl_socket_post_create(sock->sk, family); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4385 | } |
| 4386 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 4387 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 4390 | static int selinux_socket_socketpair(struct socket *socka, |
| 4391 | struct socket *sockb) |
| 4392 | { |
| 4393 | struct sk_security_struct *sksec_a = socka->sk->sk_security; |
| 4394 | struct sk_security_struct *sksec_b = sockb->sk->sk_security; |
| 4395 | |
| 4396 | sksec_a->peer_sid = sksec_b->sid; |
| 4397 | sksec_b->peer_sid = sksec_a->sid; |
| 4398 | |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | /* Range of port numbers used to automatically bind. |
| 4403 | Need to determine whether we should perform a name_bind |
| 4404 | permission check between the socket and the port number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | |
| 4406 | static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) |
| 4407 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4408 | struct sock *sk = sock->sk; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4409 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | u16 family; |
| 4411 | int err; |
| 4412 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4413 | err = sock_has_perm(sk, SOCKET__BIND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | if (err) |
| 4415 | goto out; |
| 4416 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4417 | /* 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] | 4418 | family = sk->sk_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | if (family == PF_INET || family == PF_INET6) { |
| 4420 | char *addrp; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4421 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4422 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | struct sockaddr_in *addr4 = NULL; |
| 4424 | struct sockaddr_in6 *addr6 = NULL; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4425 | u16 family_sa = address->sa_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | unsigned short snum; |
James Morris | e399f98 | 2008-06-12 01:39:58 +1000 | [diff] [blame] | 4427 | u32 sid, node_perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4429 | /* |
| 4430 | * sctp_bindx(3) calls via selinux_sctp_bind_connect() |
| 4431 | * that validates multiple binding addresses. Because of this |
| 4432 | * need to check address->sa_family as it is possible to have |
| 4433 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4434 | */ |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4435 | switch (family_sa) { |
| 4436 | case AF_UNSPEC: |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4437 | case AF_INET: |
| 4438 | if (addrlen < sizeof(struct sockaddr_in)) |
| 4439 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | addr4 = (struct sockaddr_in *)address; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4441 | if (family_sa == AF_UNSPEC) { |
| 4442 | /* see __inet_bind(), we only want to allow |
| 4443 | * AF_UNSPEC if the address is INADDR_ANY |
| 4444 | */ |
| 4445 | if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) |
| 4446 | goto err_af; |
| 4447 | family_sa = AF_INET; |
| 4448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | snum = ntohs(addr4->sin_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | addrp = (char *)&addr4->sin_addr.s_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4451 | break; |
| 4452 | case AF_INET6: |
| 4453 | if (addrlen < SIN6_LEN_RFC2133) |
| 4454 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | addr6 = (struct sockaddr_in6 *)address; |
| 4456 | snum = ntohs(addr6->sin6_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | addrp = (char *)&addr6->sin6_addr.s6_addr; |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4458 | break; |
| 4459 | default: |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4460 | goto err_af; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | } |
| 4462 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4463 | ad.type = LSM_AUDIT_DATA_NET; |
| 4464 | ad.u.net = &net; |
| 4465 | ad.u.net->sport = htons(snum); |
| 4466 | ad.u.net->family = family_sa; |
| 4467 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4468 | if (snum) { |
| 4469 | int low, high; |
| 4470 | |
Eric W. Biederman | 0bbf87d | 2013-09-28 14:10:59 -0700 | [diff] [blame] | 4471 | inet_get_local_port_range(sock_net(sk), &low, &high); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4472 | |
Krister Johansen | 4548b68 | 2017-01-20 17:49:11 -0800 | [diff] [blame] | 4473 | if (snum < max(inet_prot_sock(sock_net(sk)), low) || |
| 4474 | snum > high) { |
Paul Moore | 3e112172 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4475 | err = sel_netport_sid(sk->sk_protocol, |
| 4476 | snum, &sid); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4477 | if (err) |
| 4478 | goto out; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4479 | err = avc_has_perm(&selinux_state, |
| 4480 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4481 | sksec->sclass, |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 4482 | SOCKET__NAME_BIND, &ad); |
| 4483 | if (err) |
| 4484 | goto out; |
| 4485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4487 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4488 | switch (sksec->sclass) { |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4489 | case SECCLASS_TCP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | node_perm = TCP_SOCKET__NODE_BIND; |
| 4491 | break; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4492 | |
James Morris | 1340258 | 2005-09-30 14:24:34 -0400 | [diff] [blame] | 4493 | case SECCLASS_UDP_SOCKET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | node_perm = UDP_SOCKET__NODE_BIND; |
| 4495 | break; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4496 | |
| 4497 | case SECCLASS_DCCP_SOCKET: |
| 4498 | node_perm = DCCP_SOCKET__NODE_BIND; |
| 4499 | break; |
| 4500 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4501 | case SECCLASS_SCTP_SOCKET: |
| 4502 | node_perm = SCTP_SOCKET__NODE_BIND; |
| 4503 | break; |
| 4504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | default: |
| 4506 | node_perm = RAWIP_SOCKET__NODE_BIND; |
| 4507 | break; |
| 4508 | } |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4509 | |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4510 | err = sel_netnode_sid(addrp, family_sa, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | if (err) |
| 4512 | goto out; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4513 | |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4514 | if (family_sa == AF_INET) |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4515 | ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | else |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4517 | ad.u.net->v6info.saddr = addr6->sin6_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4519 | err = avc_has_perm(&selinux_state, |
| 4520 | sksec->sid, sid, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4521 | sksec->sclass, node_perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | if (err) |
| 4523 | goto out; |
| 4524 | } |
| 4525 | out: |
| 4526 | return err; |
Alexey Kodanev | 0f8db8c | 2018-05-11 20:15:11 +0300 | [diff] [blame] | 4527 | err_af: |
| 4528 | /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ |
| 4529 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4530 | return -EINVAL; |
| 4531 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | } |
| 4533 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4534 | /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) |
Mauro Carvalho Chehab | 5fb94e9 | 2018-05-08 15:14:57 -0300 | [diff] [blame] | 4535 | * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4536 | */ |
| 4537 | static int selinux_socket_connect_helper(struct socket *sock, |
| 4538 | struct sockaddr *address, int addrlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4540 | struct sock *sk = sock->sk; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4541 | struct sk_security_struct *sksec = sk->sk_security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | int err; |
| 4543 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4544 | err = sock_has_perm(sk, SOCKET__CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | if (err) |
| 4546 | return err; |
| 4547 | |
| 4548 | /* |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4549 | * If a TCP, DCCP or SCTP socket, check name_connect permission |
| 4550 | * for the port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | */ |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4552 | if (sksec->sclass == SECCLASS_TCP_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4553 | sksec->sclass == SECCLASS_DCCP_SOCKET || |
| 4554 | sksec->sclass == SECCLASS_SCTP_SOCKET) { |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4555 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4556 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | struct sockaddr_in *addr4 = NULL; |
| 4558 | struct sockaddr_in6 *addr6 = NULL; |
| 4559 | unsigned short snum; |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4560 | u32 sid, perm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4562 | /* sctp_connectx(3) calls via selinux_sctp_bind_connect() |
| 4563 | * that validates multiple connect addresses. Because of this |
| 4564 | * need to check address->sa_family as it is possible to have |
| 4565 | * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. |
| 4566 | */ |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4567 | switch (address->sa_family) { |
| 4568 | case AF_INET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | addr4 = (struct sockaddr_in *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4570 | if (addrlen < sizeof(struct sockaddr_in)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4571 | return -EINVAL; |
| 4572 | snum = ntohs(addr4->sin_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4573 | break; |
| 4574 | case AF_INET6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | addr6 = (struct sockaddr_in6 *)address; |
Stephen Smalley | 911656f | 2005-07-28 21:16:21 -0700 | [diff] [blame] | 4576 | if (addrlen < SIN6_LEN_RFC2133) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | return -EINVAL; |
| 4578 | snum = ntohs(addr6->sin6_port); |
Richard Haines | 68741a8a | 2018-03-02 19:54:34 +0000 | [diff] [blame] | 4579 | break; |
| 4580 | default: |
| 4581 | /* Note that SCTP services expect -EINVAL, whereas |
| 4582 | * others expect -EAFNOSUPPORT. |
| 4583 | */ |
| 4584 | if (sksec->sclass == SECCLASS_SCTP_SOCKET) |
| 4585 | return -EINVAL; |
| 4586 | else |
| 4587 | return -EAFNOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | } |
| 4589 | |
Paul Moore | 3e112172 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 4590 | err = sel_netport_sid(sk->sk_protocol, snum, &sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4592 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4594 | switch (sksec->sclass) { |
| 4595 | case SECCLASS_TCP_SOCKET: |
| 4596 | perm = TCP_SOCKET__NAME_CONNECT; |
| 4597 | break; |
| 4598 | case SECCLASS_DCCP_SOCKET: |
| 4599 | perm = DCCP_SOCKET__NAME_CONNECT; |
| 4600 | break; |
| 4601 | case SECCLASS_SCTP_SOCKET: |
| 4602 | perm = SCTP_SOCKET__NAME_CONNECT; |
| 4603 | break; |
| 4604 | } |
James Morris | 2ee92d4 | 2006-11-13 16:09:01 -0800 | [diff] [blame] | 4605 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4606 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4607 | ad.u.net = &net; |
| 4608 | ad.u.net->dport = htons(snum); |
Alexey Kodanev | 88b7d37 | 2018-05-11 20:15:12 +0300 | [diff] [blame] | 4609 | ad.u.net->family = address->sa_family; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4610 | err = avc_has_perm(&selinux_state, |
| 4611 | sksec->sid, sid, sksec->sclass, perm, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | if (err) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4613 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4616 | return 0; |
| 4617 | } |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 4618 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4619 | /* Supports connect(2), see comments in selinux_socket_connect_helper() */ |
| 4620 | static int selinux_socket_connect(struct socket *sock, |
| 4621 | struct sockaddr *address, int addrlen) |
| 4622 | { |
| 4623 | int err; |
| 4624 | struct sock *sk = sock->sk; |
| 4625 | |
| 4626 | err = selinux_socket_connect_helper(sock, address, addrlen); |
| 4627 | if (err) |
| 4628 | return err; |
| 4629 | |
| 4630 | return selinux_netlbl_socket_connect(sk, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | static int selinux_socket_listen(struct socket *sock, int backlog) |
| 4634 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4635 | return sock_has_perm(sock->sk, SOCKET__LISTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | static int selinux_socket_accept(struct socket *sock, struct socket *newsock) |
| 4639 | { |
| 4640 | int err; |
| 4641 | struct inode_security_struct *isec; |
| 4642 | struct inode_security_struct *newisec; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4643 | u16 sclass; |
| 4644 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4646 | err = sock_has_perm(sock->sk, SOCKET__ACCEPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | if (err) |
| 4648 | return err; |
| 4649 | |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4650 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 4651 | spin_lock(&isec->lock); |
| 4652 | sclass = isec->sclass; |
| 4653 | sid = isec->sid; |
| 4654 | spin_unlock(&isec->lock); |
| 4655 | |
| 4656 | newisec = inode_security_novalidate(SOCK_INODE(newsock)); |
| 4657 | newisec->sclass = sclass; |
| 4658 | newisec->sid = sid; |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 4659 | newisec->initialized = LABEL_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | |
| 4661 | return 0; |
| 4662 | } |
| 4663 | |
| 4664 | static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4665 | int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4667 | return sock_has_perm(sock->sk, SOCKET__WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 4671 | int size, int flags) |
| 4672 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4673 | return sock_has_perm(sock->sk, SOCKET__READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | } |
| 4675 | |
| 4676 | static int selinux_socket_getsockname(struct socket *sock) |
| 4677 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4678 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | static int selinux_socket_getpeername(struct socket *sock) |
| 4682 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4683 | return sock_has_perm(sock->sk, SOCKET__GETATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 4686 | static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | { |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4688 | int err; |
| 4689 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4690 | err = sock_has_perm(sock->sk, SOCKET__SETOPT); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 4691 | if (err) |
| 4692 | return err; |
| 4693 | |
| 4694 | return selinux_netlbl_socket_setsockopt(sock, level, optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | static int selinux_socket_getsockopt(struct socket *sock, int level, |
| 4698 | int optname) |
| 4699 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4700 | return sock_has_perm(sock->sk, SOCKET__GETOPT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | static int selinux_socket_shutdown(struct socket *sock, int how) |
| 4704 | { |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 4705 | return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | } |
| 4707 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4708 | static int selinux_socket_unix_stream_connect(struct sock *sock, |
| 4709 | struct sock *other, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | struct sock *newsk) |
| 4711 | { |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 4712 | struct sk_security_struct *sksec_sock = sock->sk_security; |
| 4713 | struct sk_security_struct *sksec_other = other->sk_security; |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4714 | struct sk_security_struct *sksec_new = newsk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4715 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4716 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | int err; |
| 4718 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4719 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4720 | ad.u.net = &net; |
| 4721 | ad.u.net->sk = other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4723 | err = avc_has_perm(&selinux_state, |
| 4724 | sksec_sock->sid, sksec_other->sid, |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4725 | sksec_other->sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | UNIX_STREAM_SOCKET__CONNECTTO, &ad); |
| 4727 | if (err) |
| 4728 | return err; |
| 4729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | /* server child socket */ |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4731 | sksec_new->peer_sid = sksec_sock->sid; |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4732 | err = security_sid_mls_copy(&selinux_state, sksec_other->sid, |
| 4733 | sksec_sock->sid, &sksec_new->sid); |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4734 | if (err) |
| 4735 | return err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4736 | |
Paul Moore | 4d1e245 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4737 | /* connecting socket */ |
| 4738 | sksec_sock->peer_sid = sksec_new->sid; |
| 4739 | |
| 4740 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | static int selinux_socket_unix_may_send(struct socket *sock, |
| 4744 | struct socket *other) |
| 4745 | { |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4746 | struct sk_security_struct *ssec = sock->sk->sk_security; |
| 4747 | struct sk_security_struct *osec = other->sk->sk_security; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4748 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4749 | struct lsm_network_audit net = {0,}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4751 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4752 | ad.u.net = &net; |
| 4753 | ad.u.net->sk = other->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4755 | return avc_has_perm(&selinux_state, |
| 4756 | ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4757 | &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | } |
| 4759 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4760 | static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, |
| 4761 | char *addrp, u16 family, u32 peer_sid, |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4762 | struct common_audit_data *ad) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4763 | { |
| 4764 | int err; |
| 4765 | u32 if_sid; |
| 4766 | u32 node_sid; |
| 4767 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4768 | err = sel_netif_sid(ns, ifindex, &if_sid); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4769 | if (err) |
| 4770 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4771 | err = avc_has_perm(&selinux_state, |
| 4772 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4773 | SECCLASS_NETIF, NETIF__INGRESS, ad); |
| 4774 | if (err) |
| 4775 | return err; |
| 4776 | |
| 4777 | err = sel_netnode_sid(addrp, family, &node_sid); |
| 4778 | if (err) |
| 4779 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4780 | return avc_has_perm(&selinux_state, |
| 4781 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4782 | SECCLASS_NODE, NODE__RECVFROM, ad); |
| 4783 | } |
| 4784 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4785 | 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] | 4786 | u16 family) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4787 | { |
Paul Moore | 277d342 | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 4788 | int err = 0; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4789 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4790 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4791 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4792 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4793 | char *addrp; |
| 4794 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4795 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4796 | ad.u.net = &net; |
| 4797 | ad.u.net->netif = skb->skb_iif; |
| 4798 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4799 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
| 4800 | if (err) |
| 4801 | return err; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4802 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4803 | if (selinux_secmark_enabled()) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4804 | err = avc_has_perm(&selinux_state, |
| 4805 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4806 | PACKET__RECV, &ad); |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 4807 | if (err) |
| 4808 | return err; |
| 4809 | } |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4810 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 4811 | err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); |
| 4812 | if (err) |
| 4813 | return err; |
| 4814 | err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4815 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4816 | return err; |
| 4817 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4818 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4819 | static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 4820 | { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4821 | int err; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 4822 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4823 | u16 family = sk->sk_family; |
| 4824 | u32 sk_sid = sksec->sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 4825 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4826 | struct lsm_network_audit net = {0,}; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4827 | char *addrp; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4828 | u8 secmark_active; |
| 4829 | u8 peerlbl_active; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4830 | |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4831 | if (family != PF_INET && family != PF_INET6) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4832 | return 0; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4833 | |
| 4834 | /* Handle mapped IPv4 packets arriving via IPv6 sockets */ |
Al Viro | 87fcd70 | 2006-12-04 22:00:55 +0000 | [diff] [blame] | 4835 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4836 | family = PF_INET; |
| 4837 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4838 | /* If any sort of compatibility mode is enabled then handoff processing |
| 4839 | * to the selinux_sock_rcv_skb_compat() function to deal with the |
| 4840 | * special handling. We do this in an attempt to keep this function |
| 4841 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4842 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4843 | return selinux_sock_rcv_skb_compat(sk, skb, family); |
| 4844 | |
| 4845 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 4846 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4847 | if (!secmark_active && !peerlbl_active) |
| 4848 | return 0; |
| 4849 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 4850 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 4851 | ad.u.net = &net; |
| 4852 | ad.u.net->netif = skb->skb_iif; |
| 4853 | ad.u.net->family = family; |
Paul Moore | 224dfbd | 2008-01-29 08:38:13 -0500 | [diff] [blame] | 4854 | err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4855 | if (err) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4856 | return err; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 4857 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4858 | if (peerlbl_active) { |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4859 | u32 peer_sid; |
| 4860 | |
| 4861 | err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); |
| 4862 | if (err) |
| 4863 | return err; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 4864 | err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, |
| 4865 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4866 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 4867 | selinux_netlbl_err(skb, family, err, 0); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4868 | return err; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 4869 | } |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4870 | err = avc_has_perm(&selinux_state, |
| 4871 | sk_sid, peer_sid, SECCLASS_PEER, |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4872 | PEER__RECV, &ad); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4873 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 4874 | selinux_netlbl_err(skb, family, err, 0); |
Chad Hanson | 46d01d6 | 2013-12-23 17:45:01 -0500 | [diff] [blame] | 4875 | return err; |
| 4876 | } |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4877 | } |
| 4878 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 4879 | if (secmark_active) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 4880 | err = avc_has_perm(&selinux_state, |
| 4881 | sk_sid, skb->secmark, SECCLASS_PACKET, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 4882 | PACKET__RECV, &ad); |
| 4883 | if (err) |
| 4884 | return err; |
| 4885 | } |
| 4886 | |
Paul Moore | d621d35 | 2008-01-29 08:43:36 -0500 | [diff] [blame] | 4887 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | } |
| 4889 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4890 | static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, |
| 4891 | int __user *optlen, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | { |
| 4893 | int err = 0; |
| 4894 | char *scontext; |
| 4895 | u32 scontext_len; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4896 | struct sk_security_struct *sksec = sock->sk->sk_security; |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 4897 | u32 peer_sid = SECSID_NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4899 | if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 4900 | sksec->sclass == SECCLASS_TCP_SOCKET || |
| 4901 | sksec->sclass == SECCLASS_SCTP_SOCKET) |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4902 | peer_sid = sksec->peer_sid; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4903 | if (peer_sid == SECSID_NULL) |
| 4904 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 4906 | err = security_sid_to_context(&selinux_state, peer_sid, &scontext, |
| 4907 | &scontext_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | if (err) |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 4909 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | |
| 4911 | if (scontext_len > len) { |
| 4912 | err = -ERANGE; |
| 4913 | goto out_len; |
| 4914 | } |
| 4915 | |
| 4916 | if (copy_to_user(optval, scontext, scontext_len)) |
| 4917 | err = -EFAULT; |
| 4918 | |
| 4919 | out_len: |
| 4920 | if (put_user(scontext_len, optlen)) |
| 4921 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | kfree(scontext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | return err; |
| 4924 | } |
| 4925 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4926 | 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] | 4927 | { |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4928 | u32 peer_secid = SECSID_NULL; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4929 | u16 family; |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 4930 | struct inode_security_struct *isec; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 4931 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 4932 | if (skb && skb->protocol == htons(ETH_P_IP)) |
| 4933 | family = PF_INET; |
| 4934 | else if (skb && skb->protocol == htons(ETH_P_IPV6)) |
| 4935 | family = PF_INET6; |
| 4936 | else if (sock) |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4937 | family = sock->sk->sk_family; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4938 | else |
| 4939 | goto out; |
| 4940 | |
Paul Moore | 899134f | 2016-03-28 15:19:10 -0400 | [diff] [blame] | 4941 | if (sock && family == PF_UNIX) { |
| 4942 | isec = inode_security_novalidate(SOCK_INODE(sock)); |
| 4943 | peer_secid = isec->sid; |
| 4944 | } else if (skb) |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 4945 | selinux_skb_peerlbl_sid(skb, family, &peer_secid); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4946 | |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4947 | out: |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 4948 | *secid = peer_secid; |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 4949 | if (peer_secid == SECSID_NULL) |
| 4950 | return -EINVAL; |
| 4951 | return 0; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 4952 | } |
| 4953 | |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 4954 | 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] | 4955 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4956 | struct sk_security_struct *sksec; |
| 4957 | |
| 4958 | sksec = kzalloc(sizeof(*sksec), priority); |
| 4959 | if (!sksec) |
| 4960 | return -ENOMEM; |
| 4961 | |
| 4962 | sksec->peer_sid = SECINITSID_UNLABELED; |
| 4963 | sksec->sid = SECINITSID_UNLABELED; |
Stephen Smalley | 5dee25d | 2015-07-10 17:19:57 -0400 | [diff] [blame] | 4964 | sksec->sclass = SECCLASS_SOCKET; |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4965 | selinux_netlbl_sk_security_reset(sksec); |
| 4966 | sk->sk_security = sksec; |
| 4967 | |
| 4968 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | static void selinux_sk_free_security(struct sock *sk) |
| 4972 | { |
Paul Moore | 84914b7 | 2010-04-22 14:46:18 -0400 | [diff] [blame] | 4973 | struct sk_security_struct *sksec = sk->sk_security; |
| 4974 | |
| 4975 | sk->sk_security = NULL; |
| 4976 | selinux_netlbl_sk_security_free(sksec); |
| 4977 | kfree(sksec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | } |
| 4979 | |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4980 | static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) |
| 4981 | { |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4982 | struct sk_security_struct *sksec = sk->sk_security; |
| 4983 | struct sk_security_struct *newsksec = newsk->sk_security; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4984 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4985 | newsksec->sid = sksec->sid; |
| 4986 | newsksec->peer_sid = sksec->peer_sid; |
| 4987 | newsksec->sclass = sksec->sclass; |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 4988 | |
Eric Paris | dd3e783 | 2010-04-07 15:08:46 -0400 | [diff] [blame] | 4989 | selinux_netlbl_sk_security_reset(newsksec); |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4990 | } |
| 4991 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4992 | static void selinux_sk_getsecid(struct sock *sk, u32 *secid) |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4993 | { |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4994 | if (!sk) |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4995 | *secid = SECINITSID_ANY_SOCKET; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 4996 | else { |
| 4997 | struct sk_security_struct *sksec = sk->sk_security; |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 4998 | |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 4999 | *secid = sksec->sid; |
Venkat Yekkirala | 892c141 | 2006-08-04 23:08:56 -0700 | [diff] [blame] | 5000 | } |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 5001 | } |
| 5002 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5003 | static void selinux_sock_graft(struct sock *sk, struct socket *parent) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5004 | { |
Andreas Gruenbacher | 5d226df | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 5005 | struct inode_security_struct *isec = |
| 5006 | inode_security_novalidate(SOCK_INODE(parent)); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5007 | struct sk_security_struct *sksec = sk->sk_security; |
| 5008 | |
Paul Moore | 2873ead | 2014-07-28 10:42:48 -0400 | [diff] [blame] | 5009 | if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || |
| 5010 | sk->sk_family == PF_UNIX) |
David Woodhouse | 2148ccc | 2006-09-29 15:50:25 -0700 | [diff] [blame] | 5011 | isec->sid = sksec->sid; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5012 | sksec->sclass = isec->sclass; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5015 | /* Called whenever SCTP receives an INIT chunk. This happens when an incoming |
| 5016 | * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association |
| 5017 | * already present). |
| 5018 | */ |
| 5019 | static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, |
| 5020 | struct sk_buff *skb) |
| 5021 | { |
| 5022 | struct sk_security_struct *sksec = ep->base.sk->sk_security; |
| 5023 | struct common_audit_data ad; |
| 5024 | struct lsm_network_audit net = {0,}; |
| 5025 | u8 peerlbl_active; |
| 5026 | u32 peer_sid = SECINITSID_UNLABELED; |
| 5027 | u32 conn_sid; |
| 5028 | int err = 0; |
| 5029 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5030 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5031 | return 0; |
| 5032 | |
| 5033 | peerlbl_active = selinux_peerlbl_enabled(); |
| 5034 | |
| 5035 | if (peerlbl_active) { |
| 5036 | /* This will return peer_sid = SECSID_NULL if there are |
| 5037 | * no peer labels, see security_net_peersid_resolve(). |
| 5038 | */ |
| 5039 | err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, |
| 5040 | &peer_sid); |
| 5041 | if (err) |
| 5042 | return err; |
| 5043 | |
| 5044 | if (peer_sid == SECSID_NULL) |
| 5045 | peer_sid = SECINITSID_UNLABELED; |
| 5046 | } |
| 5047 | |
| 5048 | if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { |
| 5049 | sksec->sctp_assoc_state = SCTP_ASSOC_SET; |
| 5050 | |
| 5051 | /* Here as first association on socket. As the peer SID |
| 5052 | * was allowed by peer recv (and the netif/node checks), |
| 5053 | * then it is approved by policy and used as the primary |
| 5054 | * peer SID for getpeercon(3). |
| 5055 | */ |
| 5056 | sksec->peer_sid = peer_sid; |
| 5057 | } else if (sksec->peer_sid != peer_sid) { |
| 5058 | /* Other association peer SIDs are checked to enforce |
| 5059 | * consistency among the peer SIDs. |
| 5060 | */ |
| 5061 | ad.type = LSM_AUDIT_DATA_NET; |
| 5062 | ad.u.net = &net; |
| 5063 | ad.u.net->sk = ep->base.sk; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5064 | err = avc_has_perm(&selinux_state, |
| 5065 | sksec->peer_sid, peer_sid, sksec->sclass, |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5066 | SCTP_SOCKET__ASSOCIATION, &ad); |
| 5067 | if (err) |
| 5068 | return err; |
| 5069 | } |
| 5070 | |
| 5071 | /* Compute the MLS component for the connection and store |
| 5072 | * the information in ep. This will be used by SCTP TCP type |
| 5073 | * sockets and peeled off connections as they cause a new |
| 5074 | * socket to be generated. selinux_sctp_sk_clone() will then |
| 5075 | * plug this into the new socket. |
| 5076 | */ |
| 5077 | err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); |
| 5078 | if (err) |
| 5079 | return err; |
| 5080 | |
| 5081 | ep->secid = conn_sid; |
| 5082 | ep->peer_secid = peer_sid; |
| 5083 | |
| 5084 | /* Set any NetLabel labels including CIPSO/CALIPSO options. */ |
| 5085 | return selinux_netlbl_sctp_assoc_request(ep, skb); |
| 5086 | } |
| 5087 | |
| 5088 | /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting |
| 5089 | * based on their @optname. |
| 5090 | */ |
| 5091 | static int selinux_sctp_bind_connect(struct sock *sk, int optname, |
| 5092 | struct sockaddr *address, |
| 5093 | int addrlen) |
| 5094 | { |
| 5095 | int len, err = 0, walk_size = 0; |
| 5096 | void *addr_buf; |
| 5097 | struct sockaddr *addr; |
| 5098 | struct socket *sock; |
| 5099 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5100 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5101 | return 0; |
| 5102 | |
| 5103 | /* Process one or more addresses that may be IPv4 or IPv6 */ |
| 5104 | sock = sk->sk_socket; |
| 5105 | addr_buf = address; |
| 5106 | |
| 5107 | while (walk_size < addrlen) { |
Ondrej Mosnacek | c138325 | 2018-11-13 16:16:08 +0100 | [diff] [blame] | 5108 | if (walk_size + sizeof(sa_family_t) > addrlen) |
| 5109 | return -EINVAL; |
| 5110 | |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5111 | addr = addr_buf; |
| 5112 | switch (addr->sa_family) { |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5113 | case AF_UNSPEC: |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5114 | case AF_INET: |
| 5115 | len = sizeof(struct sockaddr_in); |
| 5116 | break; |
| 5117 | case AF_INET6: |
| 5118 | len = sizeof(struct sockaddr_in6); |
| 5119 | break; |
| 5120 | default: |
Alexey Kodanev | 4152dc9 | 2018-05-11 20:15:13 +0300 | [diff] [blame] | 5121 | return -EINVAL; |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | err = -EINVAL; |
| 5125 | switch (optname) { |
| 5126 | /* Bind checks */ |
| 5127 | case SCTP_PRIMARY_ADDR: |
| 5128 | case SCTP_SET_PEER_PRIMARY_ADDR: |
| 5129 | case SCTP_SOCKOPT_BINDX_ADD: |
| 5130 | err = selinux_socket_bind(sock, addr, len); |
| 5131 | break; |
| 5132 | /* Connect checks */ |
| 5133 | case SCTP_SOCKOPT_CONNECTX: |
| 5134 | case SCTP_PARAM_SET_PRIMARY: |
| 5135 | case SCTP_PARAM_ADD_IP: |
| 5136 | case SCTP_SENDMSG_CONNECT: |
| 5137 | err = selinux_socket_connect_helper(sock, addr, len); |
| 5138 | if (err) |
| 5139 | return err; |
| 5140 | |
| 5141 | /* As selinux_sctp_bind_connect() is called by the |
| 5142 | * SCTP protocol layer, the socket is already locked, |
| 5143 | * therefore selinux_netlbl_socket_connect_locked() is |
| 5144 | * is called here. The situations handled are: |
| 5145 | * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), |
| 5146 | * whenever a new IP address is added or when a new |
| 5147 | * primary address is selected. |
| 5148 | * Note that an SCTP connect(2) call happens before |
| 5149 | * the SCTP protocol layer and is handled via |
| 5150 | * selinux_socket_connect(). |
| 5151 | */ |
| 5152 | err = selinux_netlbl_socket_connect_locked(sk, addr); |
| 5153 | break; |
| 5154 | } |
| 5155 | |
| 5156 | if (err) |
| 5157 | return err; |
| 5158 | |
| 5159 | addr_buf += len; |
| 5160 | walk_size += len; |
| 5161 | } |
| 5162 | |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
| 5166 | /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ |
| 5167 | static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, |
| 5168 | struct sock *newsk) |
| 5169 | { |
| 5170 | struct sk_security_struct *sksec = sk->sk_security; |
| 5171 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5172 | |
| 5173 | /* If policy does not support SECCLASS_SCTP_SOCKET then call |
| 5174 | * the non-sctp clone version. |
| 5175 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5176 | if (!selinux_policycap_extsockclass()) |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 5177 | return selinux_sk_clone_security(sk, newsk); |
| 5178 | |
| 5179 | newsksec->sid = ep->secid; |
| 5180 | newsksec->peer_sid = ep->peer_secid; |
| 5181 | newsksec->sclass = sksec->sclass; |
| 5182 | selinux_netlbl_sctp_sk_clone(sk, newsk); |
| 5183 | } |
| 5184 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5185 | static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 5186 | struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5187 | { |
| 5188 | struct sk_security_struct *sksec = sk->sk_security; |
| 5189 | int err; |
Paul Moore | 0b1f24e | 2013-12-03 11:39:13 -0500 | [diff] [blame] | 5190 | u16 family = req->rsk_ops->family; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5191 | u32 connsid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5192 | u32 peersid; |
| 5193 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5194 | err = selinux_skb_peerlbl_sid(skb, family, &peersid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 5195 | if (err) |
| 5196 | return err; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5197 | err = selinux_conn_sid(sksec->sid, peersid, &connsid); |
| 5198 | if (err) |
| 5199 | return err; |
| 5200 | req->secid = connsid; |
| 5201 | req->peer_secid = peersid; |
Venkat Yekkirala | a51c64f | 2006-07-27 22:01:34 -0700 | [diff] [blame] | 5202 | |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5203 | return selinux_netlbl_inet_conn_request(req, family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5204 | } |
| 5205 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5206 | static void selinux_inet_csk_clone(struct sock *newsk, |
| 5207 | const struct request_sock *req) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5208 | { |
| 5209 | struct sk_security_struct *newsksec = newsk->sk_security; |
| 5210 | |
| 5211 | newsksec->sid = req->secid; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5212 | newsksec->peer_sid = req->peer_secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5213 | /* NOTE: Ideally, we should also get the isec->sid for the |
| 5214 | new socket in sync, but we don't have the isec available yet. |
| 5215 | So we will wait until sock_graft to do it, by which |
| 5216 | time it will have been created and available. */ |
Paul Moore | 99f59ed | 2006-08-29 17:53:48 -0700 | [diff] [blame] | 5217 | |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 5218 | /* We don't need to take any sort of lock here as we are the only |
| 5219 | * thread with access to newsksec */ |
Paul Moore | 389fb800 | 2009-03-27 17:10:34 -0400 | [diff] [blame] | 5220 | selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5221 | } |
| 5222 | |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 5223 | 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] | 5224 | { |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5225 | u16 family = sk->sk_family; |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5226 | struct sk_security_struct *sksec = sk->sk_security; |
| 5227 | |
Paul Moore | aa86290 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 5228 | /* handle mapped IPv4 packets arriving via IPv6 sockets */ |
| 5229 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) |
| 5230 | family = PF_INET; |
| 5231 | |
| 5232 | selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); |
Venkat Yekkirala | 6b87769 | 2006-11-08 17:04:09 -0600 | [diff] [blame] | 5233 | } |
| 5234 | |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5235 | static int selinux_secmark_relabel_packet(u32 sid) |
| 5236 | { |
| 5237 | const struct task_security_struct *__tsec; |
| 5238 | u32 tsid; |
| 5239 | |
| 5240 | __tsec = current_security(); |
| 5241 | tsid = __tsec->sid; |
| 5242 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5243 | return avc_has_perm(&selinux_state, |
| 5244 | tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, |
| 5245 | NULL); |
Eric Paris | 2606fd1 | 2010-10-13 16:24:41 -0400 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | static void selinux_secmark_refcount_inc(void) |
| 5249 | { |
| 5250 | atomic_inc(&selinux_secmark_refcount); |
| 5251 | } |
| 5252 | |
| 5253 | static void selinux_secmark_refcount_dec(void) |
| 5254 | { |
| 5255 | atomic_dec(&selinux_secmark_refcount); |
| 5256 | } |
| 5257 | |
Adrian Bunk | 9a673e5 | 2006-08-15 00:03:53 -0700 | [diff] [blame] | 5258 | static void selinux_req_classify_flow(const struct request_sock *req, |
| 5259 | struct flowi *fl) |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5260 | { |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 5261 | fl->flowi_secid = req->secid; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5262 | } |
| 5263 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5264 | static int selinux_tun_dev_alloc_security(void **security) |
| 5265 | { |
| 5266 | struct tun_security_struct *tunsec; |
| 5267 | |
| 5268 | tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); |
| 5269 | if (!tunsec) |
| 5270 | return -ENOMEM; |
| 5271 | tunsec->sid = current_sid(); |
| 5272 | |
| 5273 | *security = tunsec; |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
| 5277 | static void selinux_tun_dev_free_security(void *security) |
| 5278 | { |
| 5279 | kfree(security); |
| 5280 | } |
| 5281 | |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5282 | static int selinux_tun_dev_create(void) |
| 5283 | { |
| 5284 | u32 sid = current_sid(); |
| 5285 | |
| 5286 | /* we aren't taking into account the "sockcreate" SID since the socket |
| 5287 | * that is being created here is not a socket in the traditional sense, |
| 5288 | * instead it is a private sock, accessible only to the kernel, and |
| 5289 | * representing a wide range of network traffic spanning multiple |
| 5290 | * connections unlike traditional sockets - check the TUN driver to |
| 5291 | * get a better understanding of why this socket is special */ |
| 5292 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5293 | return avc_has_perm(&selinux_state, |
| 5294 | sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5295 | NULL); |
| 5296 | } |
| 5297 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5298 | static int selinux_tun_dev_attach_queue(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5299 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5300 | struct tun_security_struct *tunsec = security; |
| 5301 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5302 | return avc_has_perm(&selinux_state, |
| 5303 | current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5304 | TUN_SOCKET__ATTACH_QUEUE, NULL); |
| 5305 | } |
| 5306 | |
| 5307 | static int selinux_tun_dev_attach(struct sock *sk, void *security) |
| 5308 | { |
| 5309 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5310 | struct sk_security_struct *sksec = sk->sk_security; |
| 5311 | |
| 5312 | /* we don't currently perform any NetLabel based labeling here and it |
| 5313 | * isn't clear that we would want to do so anyway; while we could apply |
| 5314 | * labeling without the support of the TUN user the resulting labeled |
| 5315 | * traffic from the other end of the connection would almost certainly |
| 5316 | * cause confusion to the TUN user that had no idea network labeling |
| 5317 | * protocols were being used */ |
| 5318 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5319 | sksec->sid = tunsec->sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5320 | sksec->sclass = SECCLASS_TUN_SOCKET; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5321 | |
| 5322 | return 0; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5323 | } |
| 5324 | |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5325 | static int selinux_tun_dev_open(void *security) |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5326 | { |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5327 | struct tun_security_struct *tunsec = security; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5328 | u32 sid = current_sid(); |
| 5329 | int err; |
| 5330 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5331 | err = avc_has_perm(&selinux_state, |
| 5332 | sid, tunsec->sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5333 | TUN_SOCKET__RELABELFROM, NULL); |
| 5334 | if (err) |
| 5335 | return err; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5336 | err = avc_has_perm(&selinux_state, |
| 5337 | sid, sid, SECCLASS_TUN_SOCKET, |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5338 | TUN_SOCKET__RELABELTO, NULL); |
| 5339 | if (err) |
| 5340 | return err; |
Paul Moore | 5dbbaf2 | 2013-01-14 07:12:19 +0000 | [diff] [blame] | 5341 | tunsec->sid = sid; |
Paul Moore | ed6d76e | 2009-08-28 18:12:49 -0400 | [diff] [blame] | 5342 | |
| 5343 | return 0; |
| 5344 | } |
| 5345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) |
| 5347 | { |
| 5348 | int err = 0; |
| 5349 | u32 perm; |
| 5350 | struct nlmsghdr *nlh; |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5351 | struct sk_security_struct *sksec = sk->sk_security; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5352 | |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 5353 | if (skb->len < NLMSG_HDRLEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | err = -EINVAL; |
| 5355 | goto out; |
| 5356 | } |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 5357 | nlh = nlmsg_hdr(skb); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5358 | |
Paul Moore | 253bfae | 2010-04-22 14:46:19 -0400 | [diff] [blame] | 5359 | err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | if (err) { |
| 5361 | if (err == -EINVAL) { |
Vladis Dronov | 7631994 | 2015-12-24 11:09:41 -0500 | [diff] [blame] | 5362 | pr_warn_ratelimited("SELinux: unrecognized netlink" |
| 5363 | " message: protocol=%hu nlmsg_type=%hu sclass=%s" |
| 5364 | " pig=%d comm=%s\n", |
Marek Milkovic | cded3ff | 2015-06-04 16:22:16 -0400 | [diff] [blame] | 5365 | sk->sk_protocol, nlh->nlmsg_type, |
Vladis Dronov | 7631994 | 2015-12-24 11:09:41 -0500 | [diff] [blame] | 5366 | secclass_map[sksec->sclass - 1].name, |
| 5367 | task_pid_nr(current), current->comm); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 5368 | if (!enforcing_enabled(&selinux_state) || |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5369 | security_get_allow_unknown(&selinux_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | err = 0; |
| 5371 | } |
| 5372 | |
| 5373 | /* Ignore */ |
| 5374 | if (err == -ENOENT) |
| 5375 | err = 0; |
| 5376 | goto out; |
| 5377 | } |
| 5378 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5379 | err = sock_has_perm(sk, perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | out: |
| 5381 | return err; |
| 5382 | } |
| 5383 | |
| 5384 | #ifdef CONFIG_NETFILTER |
| 5385 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5386 | static unsigned int selinux_ip_forward(struct sk_buff *skb, |
| 5387 | const struct net_device *indev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5388 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | { |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5390 | int err; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5391 | char *addrp; |
| 5392 | u32 peer_sid; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5393 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5394 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5395 | u8 secmark_active; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5396 | u8 netlbl_active; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5397 | u8 peerlbl_active; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5398 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5399 | if (!selinux_policycap_netpeer()) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5400 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5401 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5402 | secmark_active = selinux_secmark_enabled(); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5403 | netlbl_active = netlbl_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5404 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5405 | if (!secmark_active && !peerlbl_active) |
| 5406 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5407 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5408 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) |
| 5409 | return NF_DROP; |
| 5410 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5411 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5412 | ad.u.net = &net; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5413 | ad.u.net->netif = indev->ifindex; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5414 | ad.u.net->family = family; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5415 | if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) |
| 5416 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5418 | if (peerlbl_active) { |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5419 | err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, |
| 5420 | addrp, family, peer_sid, &ad); |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5421 | if (err) { |
Huw Davies | a04e71f | 2016-06-27 15:06:16 -0400 | [diff] [blame] | 5422 | selinux_netlbl_err(skb, family, err, 1); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5423 | return NF_DROP; |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 5424 | } |
| 5425 | } |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5426 | |
| 5427 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5428 | if (avc_has_perm(&selinux_state, |
| 5429 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5430 | SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) |
| 5431 | return NF_DROP; |
| 5432 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5433 | if (netlbl_active) |
| 5434 | /* we do this in the FORWARD path and not the POST_ROUTING |
| 5435 | * path because we want to make sure we apply the necessary |
| 5436 | * labeling before IPsec is applied so we can leverage AH |
| 5437 | * protection */ |
| 5438 | if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) |
| 5439 | return NF_DROP; |
| 5440 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5441 | return NF_ACCEPT; |
| 5442 | } |
| 5443 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5444 | static unsigned int selinux_ipv4_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5445 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5446 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5447 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5448 | return selinux_ip_forward(skb, state->in, PF_INET); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5449 | } |
| 5450 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5451 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5452 | static unsigned int selinux_ipv6_forward(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5453 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5454 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5455 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5456 | return selinux_ip_forward(skb, state->in, PF_INET6); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5457 | } |
| 5458 | #endif /* IPV6 */ |
| 5459 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5460 | static unsigned int selinux_ip_output(struct sk_buff *skb, |
| 5461 | u16 family) |
| 5462 | { |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5463 | struct sock *sk; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5464 | u32 sid; |
| 5465 | |
| 5466 | if (!netlbl_enabled()) |
| 5467 | return NF_ACCEPT; |
| 5468 | |
| 5469 | /* we do this in the LOCAL_OUT path and not the POST_ROUTING path |
| 5470 | * because we want to make sure we apply the necessary labeling |
| 5471 | * before IPsec is applied so we can leverage AH protection */ |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5472 | sk = skb->sk; |
| 5473 | if (sk) { |
| 5474 | struct sk_security_struct *sksec; |
| 5475 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5476 | if (sk_listener(sk)) |
Paul Moore | 4718006 | 2013-12-04 16:10:45 -0500 | [diff] [blame] | 5477 | /* if the socket is the listening state then this |
| 5478 | * packet is a SYN-ACK packet which means it needs to |
| 5479 | * be labeled based on the connection/request_sock and |
| 5480 | * not the parent socket. unfortunately, we can't |
| 5481 | * lookup the request_sock yet as it isn't queued on |
| 5482 | * the parent socket until after the SYN-ACK is sent. |
| 5483 | * the "solution" is to simply pass the packet as-is |
| 5484 | * as any IP option based labeling should be copied |
| 5485 | * from the initial connection request (in the IP |
| 5486 | * layer). it is far from ideal, but until we get a |
| 5487 | * security label in the packet itself this is the |
| 5488 | * best we can do. */ |
| 5489 | return NF_ACCEPT; |
| 5490 | |
| 5491 | /* standard practice, label using the parent socket */ |
| 5492 | sksec = sk->sk_security; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5493 | sid = sksec->sid; |
| 5494 | } else |
| 5495 | sid = SECINITSID_KERNEL; |
| 5496 | if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) |
| 5497 | return NF_DROP; |
| 5498 | |
| 5499 | return NF_ACCEPT; |
| 5500 | } |
| 5501 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5502 | static unsigned int selinux_ipv4_output(void *priv, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5503 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5504 | const struct nf_hook_state *state) |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 5505 | { |
| 5506 | return selinux_ip_output(skb, PF_INET); |
| 5507 | } |
| 5508 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5509 | #if IS_ENABLED(CONFIG_IPV6) |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 5510 | static unsigned int selinux_ipv6_output(void *priv, |
| 5511 | struct sk_buff *skb, |
| 5512 | const struct nf_hook_state *state) |
| 5513 | { |
| 5514 | return selinux_ip_output(skb, PF_INET6); |
| 5515 | } |
| 5516 | #endif /* IPV6 */ |
| 5517 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5518 | static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, |
| 5519 | int ifindex, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5520 | u16 family) |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5521 | { |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5522 | struct sock *sk = skb_to_full_sk(skb); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5523 | struct sk_security_struct *sksec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5524 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5525 | struct lsm_network_audit net = {0,}; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5526 | char *addrp; |
| 5527 | u8 proto; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5528 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5529 | if (sk == NULL) |
| 5530 | return NF_ACCEPT; |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 5531 | sksec = sk->sk_security; |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5532 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5533 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5534 | ad.u.net = &net; |
| 5535 | ad.u.net->netif = ifindex; |
| 5536 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5537 | if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) |
| 5538 | return NF_DROP; |
| 5539 | |
Paul Moore | 58bfbb5 | 2009-03-27 17:10:41 -0400 | [diff] [blame] | 5540 | if (selinux_secmark_enabled()) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5541 | if (avc_has_perm(&selinux_state, |
| 5542 | sksec->sid, skb->secmark, |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5543 | SECCLASS_PACKET, PACKET__SEND, &ad)) |
Eric Paris | 2fe66ec | 2010-11-23 06:28:08 +0000 | [diff] [blame] | 5544 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5545 | |
Steffen Klassert | b9679a7 | 2011-02-23 12:55:21 +0100 | [diff] [blame] | 5546 | if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) |
| 5547 | return NF_DROP_ERR(-ECONNREFUSED); |
James Morris | 4e5ab4c | 2006-06-09 00:33:33 -0700 | [diff] [blame] | 5548 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5549 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | } |
| 5551 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5552 | static unsigned int selinux_ip_postroute(struct sk_buff *skb, |
| 5553 | const struct net_device *outdev, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5554 | u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5556 | u32 secmark_perm; |
| 5557 | u32 peer_sid; |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5558 | int ifindex = outdev->ifindex; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5559 | struct sock *sk; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5560 | struct common_audit_data ad; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5561 | struct lsm_network_audit net = {0,}; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5562 | char *addrp; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5563 | u8 secmark_active; |
| 5564 | u8 peerlbl_active; |
| 5565 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5566 | /* If any sort of compatibility mode is enabled then handoff processing |
| 5567 | * to the selinux_ip_postroute_compat() function to deal with the |
| 5568 | * special handling. We do this in an attempt to keep this function |
| 5569 | * as fast and as clean as possible. */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5570 | if (!selinux_policycap_netpeer()) |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5571 | return selinux_ip_postroute_compat(skb, ifindex, family); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5572 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5573 | secmark_active = selinux_secmark_enabled(); |
Chris PeBenito | 2be4d74 | 2013-05-03 09:05:39 -0400 | [diff] [blame] | 5574 | peerlbl_active = selinux_peerlbl_enabled(); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5575 | if (!secmark_active && !peerlbl_active) |
| 5576 | return NF_ACCEPT; |
| 5577 | |
Eric Dumazet | 54abc68 | 2015-11-08 10:54:07 -0800 | [diff] [blame] | 5578 | sk = skb_to_full_sk(skb); |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5579 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5580 | #ifdef CONFIG_XFRM |
| 5581 | /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec |
| 5582 | * packet transformation so allow the packet to pass without any checks |
| 5583 | * since we'll have another chance to perform access control checks |
| 5584 | * when the packet is on it's final way out. |
| 5585 | * NOTE: there appear to be some IPv6 multicast cases where skb->dst |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5586 | * is NULL, in this case go ahead and apply access control. |
| 5587 | * NOTE: if this is a local socket (skb->sk != NULL) that is in the |
| 5588 | * TCP listening state we cannot wait until the XFRM processing |
| 5589 | * is done as we will miss out on the SA label if we do; |
| 5590 | * unfortunately, this means more work, but it is only once per |
| 5591 | * connection. */ |
| 5592 | if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5593 | !(sk && sk_listener(sk))) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5594 | return NF_ACCEPT; |
| 5595 | #endif |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5596 | |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5597 | if (sk == NULL) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5598 | /* Without an associated socket the packet is either coming |
| 5599 | * from the kernel or it is being forwarded; check the packet |
| 5600 | * to determine which and if the packet is being forwarded |
| 5601 | * query the packet directly to determine the security label. */ |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5602 | if (skb->skb_iif) { |
| 5603 | secmark_perm = PACKET__FORWARD_OUT; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5604 | if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5605 | return NF_DROP; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5606 | } else { |
| 5607 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5608 | peer_sid = SECINITSID_KERNEL; |
Steffen Klassert | 4a7ab3d | 2011-02-23 12:56:23 +0100 | [diff] [blame] | 5609 | } |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5610 | } else if (sk_listener(sk)) { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5611 | /* Locally generated packet but the associated socket is in the |
| 5612 | * listening state which means this is a SYN-ACK packet. In |
| 5613 | * this particular case the correct security label is assigned |
| 5614 | * to the connection/request_sock but unfortunately we can't |
| 5615 | * query the request_sock as it isn't queued on the parent |
| 5616 | * socket until after the SYN-ACK packet is sent; the only |
| 5617 | * viable choice is to regenerate the label like we do in |
| 5618 | * selinux_inet_conn_request(). See also selinux_ip_output() |
| 5619 | * for similar problems. */ |
| 5620 | u32 skb_sid; |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5621 | struct sk_security_struct *sksec; |
| 5622 | |
Eric Dumazet | e446f9d | 2015-10-08 05:01:55 -0700 | [diff] [blame] | 5623 | sksec = sk->sk_security; |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5624 | if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) |
| 5625 | return NF_DROP; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5626 | /* At this point, if the returned skb peerlbl is SECSID_NULL |
| 5627 | * and the packet has been through at least one XFRM |
| 5628 | * transformation then we must be dealing with the "final" |
| 5629 | * form of labeled IPsec packet; since we've already applied |
| 5630 | * all of our access controls on this packet we can safely |
| 5631 | * pass the packet. */ |
| 5632 | if (skb_sid == SECSID_NULL) { |
| 5633 | switch (family) { |
| 5634 | case PF_INET: |
| 5635 | if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) |
| 5636 | return NF_ACCEPT; |
| 5637 | break; |
| 5638 | case PF_INET6: |
| 5639 | if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) |
| 5640 | return NF_ACCEPT; |
Paul Moore | a7a91a1 | 2014-09-03 10:51:59 -0400 | [diff] [blame] | 5641 | break; |
Paul Moore | c0828e5 | 2013-12-10 14:58:01 -0500 | [diff] [blame] | 5642 | default: |
| 5643 | return NF_DROP_ERR(-ECONNREFUSED); |
| 5644 | } |
| 5645 | } |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5646 | if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) |
| 5647 | return NF_DROP; |
| 5648 | secmark_perm = PACKET__SEND; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5649 | } else { |
Paul Moore | 446b802 | 2013-12-04 16:10:51 -0500 | [diff] [blame] | 5650 | /* Locally generated packet, fetch the security label from the |
| 5651 | * associated socket. */ |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5652 | struct sk_security_struct *sksec = sk->sk_security; |
| 5653 | peer_sid = sksec->sid; |
| 5654 | secmark_perm = PACKET__SEND; |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5655 | } |
| 5656 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5657 | ad.type = LSM_AUDIT_DATA_NET; |
Eric Paris | 48c62af | 2012-04-02 13:15:44 -0400 | [diff] [blame] | 5658 | ad.u.net = &net; |
| 5659 | ad.u.net->netif = ifindex; |
| 5660 | ad.u.net->family = family; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5661 | if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5662 | return NF_DROP; |
Paul Moore | d8395c8 | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 5663 | |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5664 | if (secmark_active) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5665 | if (avc_has_perm(&selinux_state, |
| 5666 | peer_sid, skb->secmark, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5667 | SECCLASS_PACKET, secmark_perm, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5668 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5669 | |
| 5670 | if (peerlbl_active) { |
| 5671 | u32 if_sid; |
| 5672 | u32 node_sid; |
| 5673 | |
Paul Moore | cbe0d6e | 2014-09-10 17:09:57 -0400 | [diff] [blame] | 5674 | if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5675 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5676 | if (avc_has_perm(&selinux_state, |
| 5677 | peer_sid, if_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5678 | SECCLASS_NETIF, NETIF__EGRESS, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5679 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5680 | |
| 5681 | if (sel_netnode_sid(addrp, family, &node_sid)) |
Eric Paris | 04f6d70 | 2010-11-23 06:28:02 +0000 | [diff] [blame] | 5682 | return NF_DROP; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5683 | if (avc_has_perm(&selinux_state, |
| 5684 | peer_sid, node_sid, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5685 | SECCLASS_NODE, NODE__SENDTO, &ad)) |
Eric Paris | 1f1aaf8 | 2010-11-16 11:52:57 +0000 | [diff] [blame] | 5686 | return NF_DROP_ERR(-ECONNREFUSED); |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | return NF_ACCEPT; |
| 5690 | } |
| 5691 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5692 | static unsigned int selinux_ipv4_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5693 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5694 | const struct nf_hook_state *state) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5695 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5696 | return selinux_ip_postroute(skb, state->out, PF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | } |
| 5698 | |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 5699 | #if IS_ENABLED(CONFIG_IPV6) |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 5700 | static unsigned int selinux_ipv6_postroute(void *priv, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 5701 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5702 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | { |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 5704 | return selinux_ip_postroute(skb, state->out, PF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | #endif /* IPV6 */ |
| 5707 | |
| 5708 | #endif /* CONFIG_NETFILTER */ |
| 5709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) |
| 5711 | { |
Stephen Smalley | 941fc5b | 2009-10-01 14:48:23 -0400 | [diff] [blame] | 5712 | return selinux_nlmsg_perm(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5715 | static int ipc_alloc_security(struct kern_ipc_perm *perm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | u16 sclass) |
| 5717 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | struct ipc_security_struct *isec; |
| 5719 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 5720 | isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | if (!isec) |
| 5722 | return -ENOMEM; |
| 5723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | isec->sclass = sclass; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5725 | isec->sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | perm->security = isec; |
| 5727 | |
| 5728 | return 0; |
| 5729 | } |
| 5730 | |
| 5731 | static void ipc_free_security(struct kern_ipc_perm *perm) |
| 5732 | { |
| 5733 | struct ipc_security_struct *isec = perm->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | perm->security = NULL; |
| 5735 | kfree(isec); |
| 5736 | } |
| 5737 | |
| 5738 | static int msg_msg_alloc_security(struct msg_msg *msg) |
| 5739 | { |
| 5740 | struct msg_security_struct *msec; |
| 5741 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 5742 | msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | if (!msec) |
| 5744 | return -ENOMEM; |
| 5745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | msec->sid = SECINITSID_UNLABELED; |
| 5747 | msg->security = msec; |
| 5748 | |
| 5749 | return 0; |
| 5750 | } |
| 5751 | |
| 5752 | static void msg_msg_free_security(struct msg_msg *msg) |
| 5753 | { |
| 5754 | struct msg_security_struct *msec = msg->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
| 5756 | msg->security = NULL; |
| 5757 | kfree(msec); |
| 5758 | } |
| 5759 | |
| 5760 | static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 5761 | u32 perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5764 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5765 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | isec = ipc_perms->security; |
| 5768 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5769 | ad.type = LSM_AUDIT_DATA_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | ad.u.ipc_id = ipc_perms->key; |
| 5771 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5772 | return avc_has_perm(&selinux_state, |
| 5773 | sid, isec->sid, isec->sclass, perms, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | } |
| 5775 | |
| 5776 | static int selinux_msg_msg_alloc_security(struct msg_msg *msg) |
| 5777 | { |
| 5778 | return msg_msg_alloc_security(msg); |
| 5779 | } |
| 5780 | |
| 5781 | static void selinux_msg_msg_free_security(struct msg_msg *msg) |
| 5782 | { |
| 5783 | msg_msg_free_security(msg); |
| 5784 | } |
| 5785 | |
| 5786 | /* message queue security operations */ |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5787 | static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5790 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5791 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | int rc; |
| 5793 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5794 | rc = ipc_alloc_security(msq, SECCLASS_MSGQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | if (rc) |
| 5796 | return rc; |
| 5797 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5798 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5800 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5801 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5802 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5803 | rc = avc_has_perm(&selinux_state, |
| 5804 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | MSGQ__CREATE, &ad); |
| 5806 | if (rc) { |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5807 | ipc_free_security(msq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | return rc; |
| 5809 | } |
| 5810 | return 0; |
| 5811 | } |
| 5812 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5813 | static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | { |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5815 | ipc_free_security(msq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | } |
| 5817 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5818 | 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] | 5819 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5821 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5822 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5824 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5826 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5827 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5829 | return avc_has_perm(&selinux_state, |
| 5830 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | MSGQ__ASSOCIATE, &ad); |
| 5832 | } |
| 5833 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5834 | 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] | 5835 | { |
| 5836 | int err; |
| 5837 | int perms; |
| 5838 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5839 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | case IPC_INFO: |
| 5841 | case MSG_INFO: |
| 5842 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5843 | return avc_has_perm(&selinux_state, |
| 5844 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5845 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | case IPC_STAT: |
| 5847 | case MSG_STAT: |
Davidlohr Bueso | 23c8cec | 2018-04-10 16:35:30 -0700 | [diff] [blame] | 5848 | case MSG_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; |
| 5850 | break; |
| 5851 | case IPC_SET: |
| 5852 | perms = MSGQ__SETATTR; |
| 5853 | break; |
| 5854 | case IPC_RMID: |
| 5855 | perms = MSGQ__DESTROY; |
| 5856 | break; |
| 5857 | default: |
| 5858 | return 0; |
| 5859 | } |
| 5860 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5861 | err = ipc_has_perm(msq, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | return err; |
| 5863 | } |
| 5864 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5865 | 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] | 5866 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | struct ipc_security_struct *isec; |
| 5868 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5869 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5870 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | int rc; |
| 5872 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5873 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | msec = msg->security; |
| 5875 | |
| 5876 | /* |
| 5877 | * First time through, need to assign label to the message |
| 5878 | */ |
| 5879 | if (msec->sid == SECINITSID_UNLABELED) { |
| 5880 | /* |
| 5881 | * Compute new sid based on current process and |
| 5882 | * message queue this message will be stored in |
| 5883 | */ |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 5884 | rc = security_transition_sid(&selinux_state, sid, isec->sid, |
| 5885 | SECCLASS_MSG, NULL, &msec->sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | if (rc) |
| 5887 | return rc; |
| 5888 | } |
| 5889 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5890 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5891 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | |
| 5893 | /* Can this process write to the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5894 | rc = avc_has_perm(&selinux_state, |
| 5895 | sid, isec->sid, SECCLASS_MSGQ, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | MSGQ__WRITE, &ad); |
| 5897 | if (!rc) |
| 5898 | /* Can this process send the message */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5899 | rc = avc_has_perm(&selinux_state, |
| 5900 | sid, msec->sid, SECCLASS_MSG, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5901 | MSG__SEND, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | if (!rc) |
| 5903 | /* Can the message be put in the queue? */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5904 | rc = avc_has_perm(&selinux_state, |
| 5905 | msec->sid, isec->sid, SECCLASS_MSGQ, |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5906 | MSGQ__ENQUEUE, &ad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | |
| 5908 | return rc; |
| 5909 | } |
| 5910 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5911 | 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] | 5912 | struct task_struct *target, |
| 5913 | long type, int mode) |
| 5914 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | struct ipc_security_struct *isec; |
| 5916 | struct msg_security_struct *msec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5917 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5918 | u32 sid = task_sid(target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | int rc; |
| 5920 | |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5921 | isec = msq->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | msec = msg->security; |
| 5923 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5924 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | d8c6e85 | 2018-03-22 21:22:26 -0500 | [diff] [blame] | 5925 | ad.u.ipc_id = msq->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5927 | rc = avc_has_perm(&selinux_state, |
| 5928 | sid, isec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | SECCLASS_MSGQ, MSGQ__READ, &ad); |
| 5930 | if (!rc) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5931 | rc = avc_has_perm(&selinux_state, |
| 5932 | sid, msec->sid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | SECCLASS_MSG, MSG__RECEIVE, &ad); |
| 5934 | return rc; |
| 5935 | } |
| 5936 | |
| 5937 | /* Shared Memory security operations */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5938 | static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5941 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5942 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | int rc; |
| 5944 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5945 | rc = ipc_alloc_security(shp, SECCLASS_SHM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | if (rc) |
| 5947 | return rc; |
| 5948 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5949 | isec = shp->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5951 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5952 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5954 | rc = avc_has_perm(&selinux_state, |
| 5955 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | SHM__CREATE, &ad); |
| 5957 | if (rc) { |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5958 | ipc_free_security(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5959 | return rc; |
| 5960 | } |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5964 | static void selinux_shm_free_security(struct kern_ipc_perm *shp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | { |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5966 | ipc_free_security(shp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | } |
| 5968 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5969 | static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 5972 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 5973 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5975 | isec = shp->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 5977 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5978 | ad.u.ipc_id = shp->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5980 | return avc_has_perm(&selinux_state, |
| 5981 | sid, isec->sid, SECCLASS_SHM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | SHM__ASSOCIATE, &ad); |
| 5983 | } |
| 5984 | |
| 5985 | /* Note, at this point, shp is locked down */ |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 5986 | static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | { |
| 5988 | int perms; |
| 5989 | int err; |
| 5990 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 5991 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | case IPC_INFO: |
| 5993 | case SHM_INFO: |
| 5994 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 5995 | return avc_has_perm(&selinux_state, |
| 5996 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 5997 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | case IPC_STAT: |
| 5999 | case SHM_STAT: |
Davidlohr Bueso | c21a697 | 2018-04-10 16:35:23 -0700 | [diff] [blame] | 6000 | case SHM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | perms = SHM__GETATTR | SHM__ASSOCIATE; |
| 6002 | break; |
| 6003 | case IPC_SET: |
| 6004 | perms = SHM__SETATTR; |
| 6005 | break; |
| 6006 | case SHM_LOCK: |
| 6007 | case SHM_UNLOCK: |
| 6008 | perms = SHM__LOCK; |
| 6009 | break; |
| 6010 | case IPC_RMID: |
| 6011 | perms = SHM__DESTROY; |
| 6012 | break; |
| 6013 | default: |
| 6014 | return 0; |
| 6015 | } |
| 6016 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6017 | err = ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | return err; |
| 6019 | } |
| 6020 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6021 | static int selinux_shm_shmat(struct kern_ipc_perm *shp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | char __user *shmaddr, int shmflg) |
| 6023 | { |
| 6024 | u32 perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | |
| 6026 | if (shmflg & SHM_RDONLY) |
| 6027 | perms = SHM__READ; |
| 6028 | else |
| 6029 | perms = SHM__READ | SHM__WRITE; |
| 6030 | |
Eric W. Biederman | 7191adf | 2018-03-22 21:08:27 -0500 | [diff] [blame] | 6031 | return ipc_has_perm(shp, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | /* Semaphore security operations */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6035 | static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6036 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6038 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6039 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | int rc; |
| 6041 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6042 | rc = ipc_alloc_security(sma, SECCLASS_SEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | if (rc) |
| 6044 | return rc; |
| 6045 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6046 | isec = sma->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6048 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6049 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6051 | rc = avc_has_perm(&selinux_state, |
| 6052 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | SEM__CREATE, &ad); |
| 6054 | if (rc) { |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6055 | ipc_free_security(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | return rc; |
| 6057 | } |
| 6058 | return 0; |
| 6059 | } |
| 6060 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6061 | static void selinux_sem_free_security(struct kern_ipc_perm *sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | { |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6063 | ipc_free_security(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | } |
| 6065 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6066 | static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | struct ipc_security_struct *isec; |
Thomas Liu | 2bf4969 | 2009-07-14 12:14:09 -0400 | [diff] [blame] | 6069 | struct common_audit_data ad; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6070 | u32 sid = current_sid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6072 | isec = sma->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 6074 | ad.type = LSM_AUDIT_DATA_IPC; |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6075 | ad.u.ipc_id = sma->key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6077 | return avc_has_perm(&selinux_state, |
| 6078 | sid, isec->sid, SECCLASS_SEM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | SEM__ASSOCIATE, &ad); |
| 6080 | } |
| 6081 | |
| 6082 | /* Note, at this point, sma is locked down */ |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6083 | static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | { |
| 6085 | int err; |
| 6086 | u32 perms; |
| 6087 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6088 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | case IPC_INFO: |
| 6090 | case SEM_INFO: |
| 6091 | /* No specific object, just general system-wide information. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6092 | return avc_has_perm(&selinux_state, |
| 6093 | current_sid(), SECINITSID_KERNEL, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6094 | SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | case GETPID: |
| 6096 | case GETNCNT: |
| 6097 | case GETZCNT: |
| 6098 | perms = SEM__GETATTR; |
| 6099 | break; |
| 6100 | case GETVAL: |
| 6101 | case GETALL: |
| 6102 | perms = SEM__READ; |
| 6103 | break; |
| 6104 | case SETVAL: |
| 6105 | case SETALL: |
| 6106 | perms = SEM__WRITE; |
| 6107 | break; |
| 6108 | case IPC_RMID: |
| 6109 | perms = SEM__DESTROY; |
| 6110 | break; |
| 6111 | case IPC_SET: |
| 6112 | perms = SEM__SETATTR; |
| 6113 | break; |
| 6114 | case IPC_STAT: |
| 6115 | case SEM_STAT: |
Davidlohr Bueso | a280d6d | 2018-04-10 16:35:26 -0700 | [diff] [blame] | 6116 | case SEM_STAT_ANY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | perms = SEM__GETATTR | SEM__ASSOCIATE; |
| 6118 | break; |
| 6119 | default: |
| 6120 | return 0; |
| 6121 | } |
| 6122 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6123 | err = ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | return err; |
| 6125 | } |
| 6126 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6127 | static int selinux_sem_semop(struct kern_ipc_perm *sma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | struct sembuf *sops, unsigned nsops, int alter) |
| 6129 | { |
| 6130 | u32 perms; |
| 6131 | |
| 6132 | if (alter) |
| 6133 | perms = SEM__READ | SEM__WRITE; |
| 6134 | else |
| 6135 | perms = SEM__READ; |
| 6136 | |
Eric W. Biederman | aefad95 | 2018-03-22 20:52:43 -0500 | [diff] [blame] | 6137 | return ipc_has_perm(sma, perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | } |
| 6139 | |
| 6140 | static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) |
| 6141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | u32 av = 0; |
| 6143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | av = 0; |
| 6145 | if (flag & S_IRUGO) |
| 6146 | av |= IPC__UNIX_READ; |
| 6147 | if (flag & S_IWUGO) |
| 6148 | av |= IPC__UNIX_WRITE; |
| 6149 | |
| 6150 | if (av == 0) |
| 6151 | return 0; |
| 6152 | |
Stephen Smalley | 6af963f | 2005-05-01 08:58:39 -0700 | [diff] [blame] | 6153 | return ipc_has_perm(ipcp, av); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | } |
| 6155 | |
Ahmed S. Darwish | 713a04ae | 2008-03-01 21:52:30 +0200 | [diff] [blame] | 6156 | static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) |
| 6157 | { |
| 6158 | struct ipc_security_struct *isec = ipcp->security; |
| 6159 | *secid = isec->sid; |
| 6160 | } |
| 6161 | |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6162 | static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | { |
| 6164 | if (inode) |
| 6165 | inode_doinit_with_dentry(inode, dentry); |
| 6166 | } |
| 6167 | |
| 6168 | static int selinux_getprocattr(struct task_struct *p, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6169 | char *name, char **value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | { |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6171 | const struct task_security_struct *__tsec; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 6172 | u32 sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | int error; |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6174 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6176 | rcu_read_lock(); |
| 6177 | __tsec = __task_cred(p)->security; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6179 | if (current != p) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6180 | error = avc_has_perm(&selinux_state, |
| 6181 | current_sid(), __tsec->sid, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6182 | SECCLASS_PROCESS, PROCESS__GETATTR, NULL); |
| 6183 | if (error) |
| 6184 | goto bad; |
| 6185 | } |
| 6186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | if (!strcmp(name, "current")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6188 | sid = __tsec->sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | else if (!strcmp(name, "prev")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6190 | sid = __tsec->osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | else if (!strcmp(name, "exec")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6192 | sid = __tsec->exec_sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | else if (!strcmp(name, "fscreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6194 | sid = __tsec->create_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6195 | else if (!strcmp(name, "keycreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6196 | sid = __tsec->keycreate_sid; |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6197 | else if (!strcmp(name, "sockcreate")) |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6198 | sid = __tsec->sockcreate_sid; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6199 | else { |
| 6200 | error = -EINVAL; |
| 6201 | goto bad; |
| 6202 | } |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6203 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | |
| 6205 | if (!sid) |
| 6206 | return 0; |
| 6207 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6208 | error = security_sid_to_context(&selinux_state, sid, value, &len); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 6209 | if (error) |
| 6210 | return error; |
| 6211 | return len; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6212 | |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6213 | bad: |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6214 | rcu_read_unlock(); |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6215 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | } |
| 6217 | |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6218 | static int selinux_setprocattr(const char *name, void *value, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | { |
| 6220 | struct task_security_struct *tsec; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6221 | struct cred *new; |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6222 | u32 mysid = current_sid(), sid = 0, ptsid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | int error; |
| 6224 | char *str = value; |
| 6225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | /* |
| 6227 | * Basic control over ability to set these attributes at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | */ |
| 6229 | if (!strcmp(name, "exec")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6230 | error = avc_has_perm(&selinux_state, |
| 6231 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6232 | PROCESS__SETEXEC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | else if (!strcmp(name, "fscreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6234 | error = avc_has_perm(&selinux_state, |
| 6235 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6236 | PROCESS__SETFSCREATE, NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6237 | else if (!strcmp(name, "keycreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6238 | error = avc_has_perm(&selinux_state, |
| 6239 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6240 | PROCESS__SETKEYCREATE, NULL); |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6241 | else if (!strcmp(name, "sockcreate")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6242 | error = avc_has_perm(&selinux_state, |
| 6243 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6244 | PROCESS__SETSOCKCREATE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | else if (!strcmp(name, "current")) |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6246 | error = avc_has_perm(&selinux_state, |
| 6247 | mysid, mysid, SECCLASS_PROCESS, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6248 | PROCESS__SETCURRENT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | else |
| 6250 | error = -EINVAL; |
| 6251 | if (error) |
| 6252 | return error; |
| 6253 | |
| 6254 | /* Obtain a SID for the context, if one was specified. */ |
Stephen Smalley | a050a57 | 2017-01-31 11:54:04 -0500 | [diff] [blame] | 6255 | if (size && str[0] && str[0] != '\n') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | if (str[size-1] == '\n') { |
| 6257 | str[size-1] = 0; |
| 6258 | size--; |
| 6259 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6260 | error = security_context_to_sid(&selinux_state, value, size, |
| 6261 | &sid, GFP_KERNEL); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6262 | if (error == -EINVAL && !strcmp(name, "fscreate")) { |
Stephen Smalley | db59000 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6263 | if (!has_cap_mac_admin(true)) { |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6264 | struct audit_buffer *ab; |
| 6265 | size_t audit_size; |
| 6266 | |
| 6267 | /* We strip a nul only if it is at the end, otherwise the |
| 6268 | * context contains a nul and we should audit that */ |
| 6269 | if (str[size - 1] == '\0') |
| 6270 | audit_size = size - 1; |
| 6271 | else |
| 6272 | audit_size = size; |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 6273 | ab = audit_log_start(audit_context(), |
| 6274 | GFP_ATOMIC, |
| 6275 | AUDIT_SELINUX_ERR); |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6276 | audit_log_format(ab, "op=fscreate invalid_context="); |
| 6277 | audit_log_n_untrustedstring(ab, value, audit_size); |
| 6278 | audit_log_end(ab); |
| 6279 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6280 | return error; |
Eric Paris | d6ea83e | 2012-04-04 13:45:49 -0400 | [diff] [blame] | 6281 | } |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6282 | error = security_context_to_sid_force( |
| 6283 | &selinux_state, |
| 6284 | value, size, &sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 6285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | if (error) |
| 6287 | return error; |
| 6288 | } |
| 6289 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6290 | new = prepare_creds(); |
| 6291 | if (!new) |
| 6292 | return -ENOMEM; |
| 6293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | /* Permission checking based on the specified context is |
| 6295 | performed during the actual operation (execve, |
| 6296 | open/mkdir/...), when we know the full context of the |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6297 | operation. See selinux_bprm_set_creds for the execve |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6298 | checks and may_create for the file creation checks. The |
| 6299 | operation will then fail if the context is not permitted. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6300 | tsec = new->security; |
| 6301 | if (!strcmp(name, "exec")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | tsec->exec_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6303 | } else if (!strcmp(name, "fscreate")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | tsec->create_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6305 | } else if (!strcmp(name, "keycreate")) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6306 | error = avc_has_perm(&selinux_state, |
| 6307 | mysid, sid, SECCLASS_KEY, KEY__CREATE, |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6308 | NULL); |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6309 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6310 | goto abort_change; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6311 | tsec->keycreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6312 | } else if (!strcmp(name, "sockcreate")) { |
Eric Paris | 42c3e03 | 2006-06-26 00:26:03 -0700 | [diff] [blame] | 6313 | tsec->sockcreate_sid = sid; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6314 | } else if (!strcmp(name, "current")) { |
| 6315 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | if (sid == 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6317 | goto abort_change; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 6318 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6319 | /* Only allow single threaded processes to change context */ |
| 6320 | error = -EPERM; |
Oleg Nesterov | 5bb459b | 2009-07-10 03:48:23 +0200 | [diff] [blame] | 6321 | if (!current_is_single_threaded()) { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6322 | error = security_bounded_transition(&selinux_state, |
| 6323 | tsec->sid, sid); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6324 | if (error) |
| 6325 | goto abort_change; |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | |
| 6328 | /* Check permissions for the transition. */ |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6329 | error = avc_has_perm(&selinux_state, |
| 6330 | tsec->sid, sid, SECCLASS_PROCESS, |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6331 | PROCESS__DYNTRANSITION, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | if (error) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6333 | goto abort_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | |
| 6335 | /* Check for ptracing, and update the task SID if ok. |
| 6336 | Otherwise, leave SID unchanged and fail. */ |
Stephen Smalley | be0554c | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 6337 | ptsid = ptrace_parent_sid(); |
Paul Moore | 0c6181c | 2016-03-30 21:41:21 -0400 | [diff] [blame] | 6338 | if (ptsid != 0) { |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6339 | error = avc_has_perm(&selinux_state, |
| 6340 | ptsid, sid, SECCLASS_PROCESS, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6341 | PROCESS__PTRACE, NULL); |
| 6342 | if (error) |
| 6343 | goto abort_change; |
| 6344 | } |
| 6345 | |
| 6346 | tsec->sid = sid; |
| 6347 | } else { |
| 6348 | error = -EINVAL; |
| 6349 | goto abort_change; |
| 6350 | } |
| 6351 | |
| 6352 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | return size; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6354 | |
| 6355 | abort_change: |
| 6356 | abort_creds(new); |
| 6357 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | } |
| 6359 | |
David Quigley | 746df9b | 2013-05-22 12:50:35 -0400 | [diff] [blame] | 6360 | static int selinux_ismaclabel(const char *name) |
| 6361 | { |
| 6362 | return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); |
| 6363 | } |
| 6364 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6365 | static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) |
| 6366 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6367 | return security_sid_to_context(&selinux_state, secid, |
| 6368 | secdata, seclen); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6369 | } |
| 6370 | |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 6371 | 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] | 6372 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6373 | return security_context_to_sid(&selinux_state, secdata, seclen, |
| 6374 | secid, GFP_KERNEL); |
David Howells | 63cb344 | 2008-01-15 23:47:35 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6377 | static void selinux_release_secctx(char *secdata, u32 seclen) |
| 6378 | { |
Paul Moore | 088999e | 2007-08-01 11:12:58 -0400 | [diff] [blame] | 6379 | kfree(secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6380 | } |
| 6381 | |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6382 | static void selinux_inode_invalidate_secctx(struct inode *inode) |
| 6383 | { |
| 6384 | struct inode_security_struct *isec = inode->i_security; |
| 6385 | |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6386 | spin_lock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6387 | isec->initialized = LABEL_INVALID; |
Andreas Gruenbacher | 9287aed | 2016-11-15 11:06:40 +0100 | [diff] [blame] | 6388 | spin_unlock(&isec->lock); |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6389 | } |
| 6390 | |
David P. Quigley | 1ee65e3 | 2009-09-03 14:25:57 -0400 | [diff] [blame] | 6391 | /* |
| 6392 | * called with inode->i_mutex locked |
| 6393 | */ |
| 6394 | static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) |
| 6395 | { |
| 6396 | return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); |
| 6397 | } |
| 6398 | |
| 6399 | /* |
| 6400 | * called with inode->i_mutex locked |
| 6401 | */ |
| 6402 | static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) |
| 6403 | { |
| 6404 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); |
| 6405 | } |
| 6406 | |
| 6407 | static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) |
| 6408 | { |
| 6409 | int len = 0; |
| 6410 | len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, |
| 6411 | ctx, true); |
| 6412 | if (len < 0) |
| 6413 | return len; |
| 6414 | *ctxlen = len; |
| 6415 | return 0; |
| 6416 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6417 | #ifdef CONFIG_KEYS |
| 6418 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6419 | static int selinux_key_alloc(struct key *k, const struct cred *cred, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 6420 | unsigned long flags) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6421 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6422 | const struct task_security_struct *tsec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6423 | struct key_security_struct *ksec; |
| 6424 | |
| 6425 | ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); |
| 6426 | if (!ksec) |
| 6427 | return -ENOMEM; |
| 6428 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6429 | tsec = cred->security; |
| 6430 | if (tsec->keycreate_sid) |
| 6431 | ksec->sid = tsec->keycreate_sid; |
Michael LeMay | 4eb582c | 2006-06-26 00:24:57 -0700 | [diff] [blame] | 6432 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6433 | ksec->sid = tsec->sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6434 | |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6435 | k->security = ksec; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6436 | return 0; |
| 6437 | } |
| 6438 | |
| 6439 | static void selinux_key_free(struct key *k) |
| 6440 | { |
| 6441 | struct key_security_struct *ksec = k->security; |
| 6442 | |
| 6443 | k->security = NULL; |
| 6444 | kfree(ksec); |
| 6445 | } |
| 6446 | |
| 6447 | static int selinux_key_permission(key_ref_t key_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6448 | const struct cred *cred, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 6449 | unsigned perm) |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6450 | { |
| 6451 | struct key *key; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6452 | struct key_security_struct *ksec; |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6453 | u32 sid; |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6454 | |
| 6455 | /* if no specific permissions are requested, we skip the |
| 6456 | permission check. No serious, additional covert channels |
| 6457 | appear to be created. */ |
| 6458 | if (perm == 0) |
| 6459 | return 0; |
| 6460 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6461 | sid = cred_sid(cred); |
David Howells | 275bb41 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 6462 | |
| 6463 | key = key_ref_to_ptr(key_ref); |
| 6464 | ksec = key->security; |
| 6465 | |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6466 | return avc_has_perm(&selinux_state, |
| 6467 | sid, ksec->sid, SECCLASS_KEY, perm, NULL); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6468 | } |
| 6469 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6470 | static int selinux_key_getsecurity(struct key *key, char **_buffer) |
| 6471 | { |
| 6472 | struct key_security_struct *ksec = key->security; |
| 6473 | char *context = NULL; |
| 6474 | unsigned len; |
| 6475 | int rc; |
| 6476 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6477 | rc = security_sid_to_context(&selinux_state, ksec->sid, |
| 6478 | &context, &len); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6479 | if (!rc) |
| 6480 | rc = len; |
| 6481 | *_buffer = context; |
| 6482 | return rc; |
| 6483 | } |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6484 | #endif |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 6485 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6486 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6487 | static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) |
| 6488 | { |
| 6489 | struct common_audit_data ad; |
| 6490 | int err; |
| 6491 | u32 sid = 0; |
| 6492 | struct ib_security_struct *sec = ib_sec; |
| 6493 | struct lsm_ibpkey_audit ibpkey; |
| 6494 | |
Daniel Jurgens | 409dcf3 | 2017-05-19 15:48:59 +0300 | [diff] [blame] | 6495 | err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6496 | if (err) |
| 6497 | return err; |
| 6498 | |
| 6499 | ad.type = LSM_AUDIT_DATA_IBPKEY; |
| 6500 | ibpkey.subnet_prefix = subnet_prefix; |
| 6501 | ibpkey.pkey = pkey_val; |
| 6502 | ad.u.ibpkey = &ibpkey; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6503 | return avc_has_perm(&selinux_state, |
| 6504 | sec->sid, sid, |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6505 | SECCLASS_INFINIBAND_PKEY, |
| 6506 | INFINIBAND_PKEY__ACCESS, &ad); |
| 6507 | } |
| 6508 | |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6509 | static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, |
| 6510 | u8 port_num) |
| 6511 | { |
| 6512 | struct common_audit_data ad; |
| 6513 | int err; |
| 6514 | u32 sid = 0; |
| 6515 | struct ib_security_struct *sec = ib_sec; |
| 6516 | struct lsm_ibendport_audit ibendport; |
| 6517 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6518 | err = security_ib_endport_sid(&selinux_state, dev_name, port_num, |
| 6519 | &sid); |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6520 | |
| 6521 | if (err) |
| 6522 | return err; |
| 6523 | |
| 6524 | ad.type = LSM_AUDIT_DATA_IBENDPORT; |
| 6525 | strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); |
| 6526 | ibendport.port = port_num; |
| 6527 | ad.u.ibendport = &ibendport; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6528 | return avc_has_perm(&selinux_state, |
| 6529 | sec->sid, sid, |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6530 | SECCLASS_INFINIBAND_ENDPORT, |
| 6531 | INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); |
| 6532 | } |
| 6533 | |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6534 | static int selinux_ib_alloc_security(void **ib_sec) |
| 6535 | { |
| 6536 | struct ib_security_struct *sec; |
| 6537 | |
| 6538 | sec = kzalloc(sizeof(*sec), GFP_KERNEL); |
| 6539 | if (!sec) |
| 6540 | return -ENOMEM; |
| 6541 | sec->sid = current_sid(); |
| 6542 | |
| 6543 | *ib_sec = sec; |
| 6544 | return 0; |
| 6545 | } |
| 6546 | |
| 6547 | static void selinux_ib_free_security(void *ib_sec) |
| 6548 | { |
| 6549 | kfree(ib_sec); |
| 6550 | } |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6551 | #endif |
| 6552 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6553 | #ifdef CONFIG_BPF_SYSCALL |
| 6554 | static int selinux_bpf(int cmd, union bpf_attr *attr, |
| 6555 | unsigned int size) |
| 6556 | { |
| 6557 | u32 sid = current_sid(); |
| 6558 | int ret; |
| 6559 | |
| 6560 | switch (cmd) { |
| 6561 | case BPF_MAP_CREATE: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6562 | ret = avc_has_perm(&selinux_state, |
| 6563 | sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6564 | NULL); |
| 6565 | break; |
| 6566 | case BPF_PROG_LOAD: |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6567 | ret = avc_has_perm(&selinux_state, |
| 6568 | sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6569 | NULL); |
| 6570 | break; |
| 6571 | default: |
| 6572 | ret = 0; |
| 6573 | break; |
| 6574 | } |
| 6575 | |
| 6576 | return ret; |
| 6577 | } |
| 6578 | |
| 6579 | static u32 bpf_map_fmode_to_av(fmode_t fmode) |
| 6580 | { |
| 6581 | u32 av = 0; |
| 6582 | |
| 6583 | if (fmode & FMODE_READ) |
| 6584 | av |= BPF__MAP_READ; |
| 6585 | if (fmode & FMODE_WRITE) |
| 6586 | av |= BPF__MAP_WRITE; |
| 6587 | return av; |
| 6588 | } |
| 6589 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6590 | /* This function will check the file pass through unix socket or binder to see |
| 6591 | * if it is a bpf related object. And apply correspinding checks on the bpf |
| 6592 | * object based on the type. The bpf maps and programs, not like other files and |
| 6593 | * socket, are using a shared anonymous inode inside the kernel as their inode. |
| 6594 | * So checking that inode cannot identify if the process have privilege to |
| 6595 | * access the bpf object and that's why we have to add this additional check in |
| 6596 | * selinux_file_receive and selinux_binder_transfer_files. |
| 6597 | */ |
| 6598 | static int bpf_fd_pass(struct file *file, u32 sid) |
| 6599 | { |
| 6600 | struct bpf_security_struct *bpfsec; |
| 6601 | struct bpf_prog *prog; |
| 6602 | struct bpf_map *map; |
| 6603 | int ret; |
| 6604 | |
| 6605 | if (file->f_op == &bpf_map_fops) { |
| 6606 | map = file->private_data; |
| 6607 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6608 | ret = avc_has_perm(&selinux_state, |
| 6609 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6610 | bpf_map_fmode_to_av(file->f_mode), NULL); |
| 6611 | if (ret) |
| 6612 | return ret; |
| 6613 | } else if (file->f_op == &bpf_prog_fops) { |
| 6614 | prog = file->private_data; |
| 6615 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6616 | ret = avc_has_perm(&selinux_state, |
| 6617 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 6618 | BPF__PROG_RUN, NULL); |
| 6619 | if (ret) |
| 6620 | return ret; |
| 6621 | } |
| 6622 | return 0; |
| 6623 | } |
| 6624 | |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6625 | static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) |
| 6626 | { |
| 6627 | u32 sid = current_sid(); |
| 6628 | struct bpf_security_struct *bpfsec; |
| 6629 | |
| 6630 | bpfsec = map->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6631 | return avc_has_perm(&selinux_state, |
| 6632 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6633 | bpf_map_fmode_to_av(fmode), NULL); |
| 6634 | } |
| 6635 | |
| 6636 | static int selinux_bpf_prog(struct bpf_prog *prog) |
| 6637 | { |
| 6638 | u32 sid = current_sid(); |
| 6639 | struct bpf_security_struct *bpfsec; |
| 6640 | |
| 6641 | bpfsec = prog->aux->security; |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6642 | return avc_has_perm(&selinux_state, |
| 6643 | sid, bpfsec->sid, SECCLASS_BPF, |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6644 | BPF__PROG_RUN, NULL); |
| 6645 | } |
| 6646 | |
| 6647 | static int selinux_bpf_map_alloc(struct bpf_map *map) |
| 6648 | { |
| 6649 | struct bpf_security_struct *bpfsec; |
| 6650 | |
| 6651 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6652 | if (!bpfsec) |
| 6653 | return -ENOMEM; |
| 6654 | |
| 6655 | bpfsec->sid = current_sid(); |
| 6656 | map->security = bpfsec; |
| 6657 | |
| 6658 | return 0; |
| 6659 | } |
| 6660 | |
| 6661 | static void selinux_bpf_map_free(struct bpf_map *map) |
| 6662 | { |
| 6663 | struct bpf_security_struct *bpfsec = map->security; |
| 6664 | |
| 6665 | map->security = NULL; |
| 6666 | kfree(bpfsec); |
| 6667 | } |
| 6668 | |
| 6669 | static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) |
| 6670 | { |
| 6671 | struct bpf_security_struct *bpfsec; |
| 6672 | |
| 6673 | bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); |
| 6674 | if (!bpfsec) |
| 6675 | return -ENOMEM; |
| 6676 | |
| 6677 | bpfsec->sid = current_sid(); |
| 6678 | aux->security = bpfsec; |
| 6679 | |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
| 6683 | static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) |
| 6684 | { |
| 6685 | struct bpf_security_struct *bpfsec = aux->security; |
| 6686 | |
| 6687 | aux->security = NULL; |
| 6688 | kfree(bpfsec); |
| 6689 | } |
| 6690 | #endif |
| 6691 | |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 6692 | static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6693 | LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), |
| 6694 | LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), |
| 6695 | LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), |
| 6696 | LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6697 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6698 | LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), |
| 6699 | LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), |
| 6700 | LSM_HOOK_INIT(capget, selinux_capget), |
| 6701 | LSM_HOOK_INIT(capset, selinux_capset), |
| 6702 | LSM_HOOK_INIT(capable, selinux_capable), |
| 6703 | LSM_HOOK_INIT(quotactl, selinux_quotactl), |
| 6704 | LSM_HOOK_INIT(quota_on, selinux_quota_on), |
| 6705 | LSM_HOOK_INIT(syslog, selinux_syslog), |
| 6706 | LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), |
Stephen Smalley | 79af730 | 2015-01-21 10:54:10 -0500 | [diff] [blame] | 6707 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6708 | LSM_HOOK_INIT(netlink_send, selinux_netlink_send), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6710 | LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), |
| 6711 | LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), |
| 6712 | LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6714 | LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), |
| 6715 | LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), |
Al Viro | 5b40023 | 2018-12-12 20:13:29 -0500 | [diff] [blame] | 6716 | LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 6717 | LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6718 | LSM_HOOK_INIT(sb_remount, selinux_sb_remount), |
| 6719 | LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), |
| 6720 | LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), |
| 6721 | LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), |
| 6722 | LSM_HOOK_INIT(sb_mount, selinux_mount), |
| 6723 | LSM_HOOK_INIT(sb_umount, selinux_umount), |
| 6724 | LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), |
| 6725 | LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), |
Al Viro | 757cbe5 | 2018-12-14 23:42:21 -0500 | [diff] [blame] | 6726 | LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6728 | LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), |
Vivek Goyal | a518b0a | 2016-07-13 10:44:53 -0400 | [diff] [blame] | 6729 | LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), |
Eric Paris | e000752 | 2008-03-05 10:31:54 -0500 | [diff] [blame] | 6730 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6731 | LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), |
| 6732 | LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), |
| 6733 | LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), |
| 6734 | LSM_HOOK_INIT(inode_create, selinux_inode_create), |
| 6735 | LSM_HOOK_INIT(inode_link, selinux_inode_link), |
| 6736 | LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), |
| 6737 | LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), |
| 6738 | LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), |
| 6739 | LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), |
| 6740 | LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), |
| 6741 | LSM_HOOK_INIT(inode_rename, selinux_inode_rename), |
| 6742 | LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), |
| 6743 | LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), |
| 6744 | LSM_HOOK_INIT(inode_permission, selinux_inode_permission), |
| 6745 | LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), |
| 6746 | LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), |
| 6747 | LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), |
| 6748 | LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), |
| 6749 | LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), |
| 6750 | LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), |
| 6751 | LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), |
| 6752 | LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), |
| 6753 | LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), |
| 6754 | LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), |
| 6755 | LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), |
Vivek Goyal | 56909eb | 2016-07-13 10:44:48 -0400 | [diff] [blame] | 6756 | LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), |
Vivek Goyal | 19472b6 | 2016-07-13 10:44:50 -0400 | [diff] [blame] | 6757 | LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6759 | LSM_HOOK_INIT(file_permission, selinux_file_permission), |
| 6760 | LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), |
| 6761 | LSM_HOOK_INIT(file_free_security, selinux_file_free_security), |
| 6762 | LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), |
| 6763 | LSM_HOOK_INIT(mmap_file, selinux_mmap_file), |
| 6764 | LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), |
| 6765 | LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), |
| 6766 | LSM_HOOK_INIT(file_lock, selinux_file_lock), |
| 6767 | LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), |
| 6768 | LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), |
| 6769 | LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), |
| 6770 | LSM_HOOK_INIT(file_receive, selinux_file_receive), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6772 | LSM_HOOK_INIT(file_open, selinux_file_open), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | |
Tetsuo Handa | a79be23 | 2017-03-28 23:08:45 +0900 | [diff] [blame] | 6774 | LSM_HOOK_INIT(task_alloc, selinux_task_alloc), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6775 | LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), |
| 6776 | LSM_HOOK_INIT(cred_free, selinux_cred_free), |
| 6777 | LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), |
| 6778 | LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), |
Matthew Garrett | 3ec3011 | 2018-01-08 13:36:19 -0800 | [diff] [blame] | 6779 | LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6780 | LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), |
| 6781 | LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), |
| 6782 | LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), |
Mimi Zohar | c77b8cd | 2018-07-13 14:06:02 -0400 | [diff] [blame] | 6783 | LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), |
Jeff Vander Stoep | 61d612ea | 2016-04-05 13:06:27 -0700 | [diff] [blame] | 6784 | LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6785 | LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), |
| 6786 | LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), |
| 6787 | LSM_HOOK_INIT(task_getsid, selinux_task_getsid), |
| 6788 | LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), |
| 6789 | LSM_HOOK_INIT(task_setnice, selinux_task_setnice), |
| 6790 | LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), |
| 6791 | LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), |
Stephen Smalley | 791ec49 | 2017-02-17 07:57:00 -0500 | [diff] [blame] | 6792 | LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6793 | LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), |
| 6794 | LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), |
| 6795 | LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), |
| 6796 | LSM_HOOK_INIT(task_movememory, selinux_task_movememory), |
| 6797 | LSM_HOOK_INIT(task_kill, selinux_task_kill), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6798 | LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 6799 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6800 | LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), |
| 6801 | LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6803 | LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), |
| 6804 | LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6806 | LSM_HOOK_INIT(msg_queue_alloc_security, |
| 6807 | selinux_msg_queue_alloc_security), |
| 6808 | LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), |
| 6809 | LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), |
| 6810 | LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), |
| 6811 | LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), |
| 6812 | LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6814 | LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), |
| 6815 | LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), |
| 6816 | LSM_HOOK_INIT(shm_associate, selinux_shm_associate), |
| 6817 | LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), |
| 6818 | LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6820 | LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), |
| 6821 | LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), |
| 6822 | LSM_HOOK_INIT(sem_associate, selinux_sem_associate), |
| 6823 | LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), |
| 6824 | LSM_HOOK_INIT(sem_semop, selinux_sem_semop), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6826 | LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6827 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6828 | LSM_HOOK_INIT(getprocattr, selinux_getprocattr), |
| 6829 | LSM_HOOK_INIT(setprocattr, selinux_setprocattr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6831 | LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), |
| 6832 | LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), |
| 6833 | LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), |
| 6834 | LSM_HOOK_INIT(release_secctx, selinux_release_secctx), |
Andreas Gruenbacher | 6f3be9f | 2015-12-24 11:09:40 -0500 | [diff] [blame] | 6835 | LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6836 | LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), |
| 6837 | LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), |
| 6838 | LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6840 | LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), |
| 6841 | LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 6842 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6843 | LSM_HOOK_INIT(socket_create, selinux_socket_create), |
| 6844 | LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), |
David Herrmann | 0b811db | 2018-05-04 16:28:21 +0200 | [diff] [blame] | 6845 | LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6846 | LSM_HOOK_INIT(socket_bind, selinux_socket_bind), |
| 6847 | LSM_HOOK_INIT(socket_connect, selinux_socket_connect), |
| 6848 | LSM_HOOK_INIT(socket_listen, selinux_socket_listen), |
| 6849 | LSM_HOOK_INIT(socket_accept, selinux_socket_accept), |
| 6850 | LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), |
| 6851 | LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), |
| 6852 | LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), |
| 6853 | LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), |
| 6854 | LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), |
| 6855 | LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), |
| 6856 | LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), |
| 6857 | LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), |
| 6858 | LSM_HOOK_INIT(socket_getpeersec_stream, |
| 6859 | selinux_socket_getpeersec_stream), |
| 6860 | LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), |
| 6861 | LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), |
| 6862 | LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), |
| 6863 | LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), |
| 6864 | LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), |
| 6865 | LSM_HOOK_INIT(sock_graft, selinux_sock_graft), |
Richard Haines | d452930 | 2018-02-13 20:57:18 +0000 | [diff] [blame] | 6866 | LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), |
| 6867 | LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), |
| 6868 | LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6869 | LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), |
| 6870 | LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), |
| 6871 | LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), |
| 6872 | LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), |
| 6873 | LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), |
| 6874 | LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), |
| 6875 | LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), |
| 6876 | LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), |
| 6877 | LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), |
| 6878 | LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), |
| 6879 | LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), |
| 6880 | LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), |
| 6881 | LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6882 | #ifdef CONFIG_SECURITY_INFINIBAND |
Daniel Jurgens | cfc4d88 | 2017-05-19 15:48:57 +0300 | [diff] [blame] | 6883 | LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), |
Daniel Jurgens | ab861df | 2017-05-19 15:48:58 +0300 | [diff] [blame] | 6884 | LSM_HOOK_INIT(ib_endport_manage_subnet, |
| 6885 | selinux_ib_endport_manage_subnet), |
Daniel Jurgens | 3a976fa | 2017-05-19 15:48:56 +0300 | [diff] [blame] | 6886 | LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), |
| 6887 | LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), |
| 6888 | #endif |
Trent Jaeger | d28d1e0 | 2005-12-13 23:12:40 -0800 | [diff] [blame] | 6889 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6890 | LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), |
| 6891 | LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), |
| 6892 | LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), |
| 6893 | LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), |
| 6894 | LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), |
| 6895 | LSM_HOOK_INIT(xfrm_state_alloc_acquire, |
| 6896 | selinux_xfrm_state_alloc_acquire), |
| 6897 | LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), |
| 6898 | LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), |
| 6899 | LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), |
| 6900 | LSM_HOOK_INIT(xfrm_state_pol_flow_match, |
| 6901 | selinux_xfrm_state_pol_flow_match), |
| 6902 | LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | #endif |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6904 | |
| 6905 | #ifdef CONFIG_KEYS |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6906 | LSM_HOOK_INIT(key_alloc, selinux_key_alloc), |
| 6907 | LSM_HOOK_INIT(key_free, selinux_key_free), |
| 6908 | LSM_HOOK_INIT(key_permission, selinux_key_permission), |
| 6909 | LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6910 | #endif |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6911 | |
| 6912 | #ifdef CONFIG_AUDIT |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 6913 | LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), |
| 6914 | LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), |
| 6915 | LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), |
| 6916 | LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 6917 | #endif |
Chenbo Feng | ec27c35 | 2017-10-18 13:00:25 -0700 | [diff] [blame] | 6918 | |
| 6919 | #ifdef CONFIG_BPF_SYSCALL |
| 6920 | LSM_HOOK_INIT(bpf, selinux_bpf), |
| 6921 | LSM_HOOK_INIT(bpf_map, selinux_bpf_map), |
| 6922 | LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), |
| 6923 | LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), |
| 6924 | LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), |
| 6925 | LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), |
| 6926 | LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), |
| 6927 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | }; |
| 6929 | |
| 6930 | static __init int selinux_init(void) |
| 6931 | { |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 6932 | if (!security_module_enable("selinux")) { |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 6933 | selinux_enabled = 0; |
| 6934 | return 0; |
| 6935 | } |
| 6936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | if (!selinux_enabled) { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6938 | pr_info("SELinux: Disabled at boot.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | return 0; |
| 6940 | } |
| 6941 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6942 | pr_info("SELinux: Initializing.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6944 | memset(&selinux_state, 0, sizeof(selinux_state)); |
Paul Moore | e5a5ca9 | 2018-03-01 17:38:30 -0500 | [diff] [blame] | 6945 | enforcing_set(&selinux_state, selinux_enforcing_boot); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6946 | selinux_state.checkreqprot = selinux_checkreqprot_boot; |
| 6947 | selinux_ss_init(&selinux_state.ss); |
Stephen Smalley | 6b6bc62 | 2018-03-05 11:47:56 -0500 | [diff] [blame] | 6948 | selinux_avc_init(&selinux_state.avc); |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | /* Set the security state for the initial task. */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 6951 | cred_init_security(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | |
Stephen Smalley | fcaaade | 2010-04-28 15:57:57 -0400 | [diff] [blame] | 6953 | default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); |
| 6954 | |
James Morris | 7cae7e2 | 2006-03-22 00:09:22 -0800 | [diff] [blame] | 6955 | sel_inode_cache = kmem_cache_create("selinux_inode_security", |
| 6956 | sizeof(struct inode_security_struct), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 6957 | 0, SLAB_PANIC, NULL); |
Sangwoo | 6320565 | 2015-10-21 17:44:30 -0400 | [diff] [blame] | 6958 | file_security_cache = kmem_cache_create("selinux_file_security", |
| 6959 | sizeof(struct file_security_struct), |
| 6960 | 0, SLAB_PANIC, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | avc_init(); |
| 6962 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6963 | avtab_cache_init(); |
| 6964 | |
| 6965 | ebitmap_cache_init(); |
| 6966 | |
| 6967 | hashtab_cache_init(); |
| 6968 | |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 6969 | security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | |
Paul Moore | 615e51f | 2014-06-26 14:33:56 -0400 | [diff] [blame] | 6971 | if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) |
| 6972 | panic("SELinux: Unable to register AVC netcache callback\n"); |
| 6973 | |
Daniel Jurgens | 8f408ab | 2017-05-19 15:48:53 +0300 | [diff] [blame] | 6974 | if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) |
| 6975 | panic("SELinux: Unable to register AVC LSM notifier callback\n"); |
| 6976 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 6977 | if (selinux_enforcing_boot) |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6978 | pr_debug("SELinux: Starting in enforcing mode\n"); |
Eric Paris | 828dfe1 | 2008-04-17 13:17:49 -0400 | [diff] [blame] | 6979 | else |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6980 | pr_debug("SELinux: Starting in permissive mode\n"); |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 6981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | return 0; |
| 6983 | } |
| 6984 | |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 6985 | static void delayed_superblock_init(struct super_block *sb, void *unused) |
| 6986 | { |
Al Viro | 204cc0c | 2018-12-13 13:41:47 -0500 | [diff] [blame] | 6987 | selinux_set_mnt_opts(sb, NULL, 0, NULL); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 6988 | } |
| 6989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | void selinux_complete_init(void) |
| 6991 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6992 | pr_debug("SELinux: Completing initialization.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | |
| 6994 | /* Set up any superblocks initialized prior to the policy load. */ |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 6995 | pr_debug("SELinux: Setting up existing superblocks.\n"); |
Al Viro | e8c2625 | 2010-03-23 06:36:54 -0400 | [diff] [blame] | 6996 | iterate_supers(delayed_superblock_init, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | } |
| 6998 | |
| 6999 | /* SELinux requires early initialization in order to label |
| 7000 | all processes and objects when they are created. */ |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7001 | DEFINE_LSM(selinux) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 7002 | .name = "selinux", |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 7003 | .init = selinux_init, |
| 7004 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7006 | #if defined(CONFIG_NETFILTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | |
Florian Westphal | 591bb27 | 2017-07-26 11:40:52 +0200 | [diff] [blame] | 7008 | static const struct nf_hook_ops selinux_nf_ops[] = { |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7009 | { |
| 7010 | .hook = selinux_ipv4_postroute, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7011 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7012 | .hooknum = NF_INET_POST_ROUTING, |
| 7013 | .priority = NF_IP_PRI_SELINUX_LAST, |
| 7014 | }, |
| 7015 | { |
| 7016 | .hook = selinux_ipv4_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7017 | .pf = NFPROTO_IPV4, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7018 | .hooknum = NF_INET_FORWARD, |
| 7019 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7020 | }, |
| 7021 | { |
| 7022 | .hook = selinux_ipv4_output, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7023 | .pf = NFPROTO_IPV4, |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 7024 | .hooknum = NF_INET_LOCAL_OUT, |
| 7025 | .priority = NF_IP_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7026 | }, |
Javier Martinez Canillas | 1a93a6e | 2016-08-08 13:08:25 -0400 | [diff] [blame] | 7027 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7028 | { |
| 7029 | .hook = selinux_ipv6_postroute, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7030 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7031 | .hooknum = NF_INET_POST_ROUTING, |
| 7032 | .priority = NF_IP6_PRI_SELINUX_LAST, |
| 7033 | }, |
| 7034 | { |
| 7035 | .hook = selinux_ipv6_forward, |
Alban Crequy | 2597a83 | 2012-05-14 03:56:39 +0000 | [diff] [blame] | 7036 | .pf = NFPROTO_IPV6, |
Paul Moore | effad8d | 2008-01-29 08:49:27 -0500 | [diff] [blame] | 7037 | .hooknum = NF_INET_FORWARD, |
| 7038 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7039 | }, |
Huw Davies | 2917f57 | 2016-06-27 15:06:15 -0400 | [diff] [blame] | 7040 | { |
| 7041 | .hook = selinux_ipv6_output, |
| 7042 | .pf = NFPROTO_IPV6, |
| 7043 | .hooknum = NF_INET_LOCAL_OUT, |
| 7044 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 7045 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | #endif /* IPV6 */ |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7047 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7049 | static int __net_init selinux_nf_register(struct net *net) |
| 7050 | { |
| 7051 | return nf_register_net_hooks(net, selinux_nf_ops, |
| 7052 | ARRAY_SIZE(selinux_nf_ops)); |
| 7053 | } |
| 7054 | |
| 7055 | static void __net_exit selinux_nf_unregister(struct net *net) |
| 7056 | { |
| 7057 | nf_unregister_net_hooks(net, selinux_nf_ops, |
| 7058 | ARRAY_SIZE(selinux_nf_ops)); |
| 7059 | } |
| 7060 | |
| 7061 | static struct pernet_operations selinux_net_ops = { |
| 7062 | .init = selinux_nf_register, |
| 7063 | .exit = selinux_nf_unregister, |
| 7064 | }; |
| 7065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | static int __init selinux_nf_ip_init(void) |
| 7067 | { |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7068 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | |
| 7070 | if (!selinux_enabled) |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7071 | return 0; |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7072 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7073 | pr_debug("SELinux: Registering netfilter hooks\n"); |
Eric Paris | fadcdb4 | 2007-02-22 18:11:31 -0500 | [diff] [blame] | 7074 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7075 | err = register_pernet_subsys(&selinux_net_ops); |
Alexey Dobriyan | 6c5a9d2 | 2008-07-26 17:48:15 -0700 | [diff] [blame] | 7076 | if (err) |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7077 | panic("SELinux: register_pernet_subsys: error %d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | |
Jiri Pirko | 25db6be | 2014-09-03 17:42:13 +0200 | [diff] [blame] | 7079 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | __initcall(selinux_nf_ip_init); |
| 7082 | |
| 7083 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7084 | static void selinux_nf_ip_exit(void) |
| 7085 | { |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7086 | pr_debug("SELinux: Unregistering netfilter hooks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | |
Florian Westphal | 8e71bf7 | 2017-04-21 11:49:09 +0200 | [diff] [blame] | 7088 | unregister_pernet_subsys(&selinux_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7089 | } |
| 7090 | #endif |
| 7091 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7092 | #else /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | |
| 7094 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
| 7095 | #define selinux_nf_ip_exit() |
| 7096 | #endif |
| 7097 | |
Stephen Smalley | c2b507f | 2006-02-04 23:27:50 -0800 | [diff] [blame] | 7098 | #endif /* CONFIG_NETFILTER */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7099 | |
| 7100 | #ifdef CONFIG_SECURITY_SELINUX_DISABLE |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7101 | int selinux_disable(struct selinux_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | { |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7103 | if (state->initialized) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | /* Not permitted after initial policy load. */ |
| 7105 | return -EINVAL; |
| 7106 | } |
| 7107 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7108 | if (state->disabled) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7109 | /* Only do this once. */ |
| 7110 | return -EINVAL; |
| 7111 | } |
| 7112 | |
Stephen Smalley | aa8e712 | 2018-03-01 18:48:02 -0500 | [diff] [blame] | 7113 | state->disabled = 1; |
| 7114 | |
peter enderborg | c103a91 | 2018-06-12 10:09:03 +0200 | [diff] [blame] | 7115 | pr_info("SELinux: Disabled at runtime.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | |
Stephen Smalley | 30d5528 | 2006-05-03 10:52:36 -0400 | [diff] [blame] | 7117 | selinux_enabled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 7119 | security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | |
Eric Paris | af8ff04 | 2009-09-20 21:23:01 -0400 | [diff] [blame] | 7121 | /* Try to destroy the avc node cache */ |
| 7122 | avc_disable(); |
| 7123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | /* Unregister netfilter hooks. */ |
| 7125 | selinux_nf_ip_exit(); |
| 7126 | |
| 7127 | /* Unregister selinuxfs. */ |
| 7128 | exit_sel_fs(); |
| 7129 | |
| 7130 | return 0; |
| 7131 | } |
| 7132 | #endif |