Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | * SELinux NetLabel Support |
| 3 | * |
| 4 | * This file provides the necessary glue to tie NetLabel into the SELinux |
| 5 | * subsystem. |
| 6 | * |
| 7 | * Author: Paul Moore <paul.moore@hp.com> |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2007 |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 22 | * the GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/rcupdate.h> |
| 32 | #include <net/sock.h> |
| 33 | #include <net/netlabel.h> |
| 34 | |
| 35 | #include "objsec.h" |
| 36 | #include "security.h" |
Adrian Bunk | d4ee423 | 2008-02-27 23:20:42 +0200 | [diff] [blame] | 37 | #include "netlabel.h" |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 38 | |
| 39 | /** |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 40 | * selinux_netlbl_sidlookup_cached - Cache a SID lookup |
| 41 | * @skb: the packet |
| 42 | * @secattr: the NetLabel security attributes |
| 43 | * @sid: the SID |
| 44 | * |
| 45 | * Description: |
| 46 | * Query the SELinux security server to lookup the correct SID for the given |
| 47 | * security attributes. If the query is successful, cache the result to speed |
| 48 | * up future lookups. Returns zero on success, negative values on failure. |
| 49 | * |
| 50 | */ |
| 51 | static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb, |
| 52 | struct netlbl_lsm_secattr *secattr, |
| 53 | u32 *sid) |
| 54 | { |
| 55 | int rc; |
| 56 | |
| 57 | rc = security_netlbl_secattr_to_sid(secattr, sid); |
| 58 | if (rc == 0 && |
| 59 | (secattr->flags & NETLBL_SECATTR_CACHEABLE) && |
| 60 | (secattr->flags & NETLBL_SECATTR_CACHE)) |
| 61 | netlbl_cache_add(skb, secattr); |
| 62 | |
| 63 | return rc; |
| 64 | } |
| 65 | |
| 66 | /** |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 67 | * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism |
| 68 | * @sk: the socket to label |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 69 | * |
| 70 | * Description: |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 71 | * Attempt to label a socket using the NetLabel mechanism. Returns zero values |
| 72 | * on success, negative values on failure. |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 73 | * |
| 74 | */ |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 75 | static int selinux_netlbl_sock_setsid(struct sock *sk) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 76 | { |
| 77 | int rc; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 78 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 79 | struct netlbl_lsm_secattr secattr; |
| 80 | |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 81 | if (sksec->nlbl_state != NLBL_REQUIRE) |
| 82 | return 0; |
| 83 | |
Paul Moore | 45c950e | 2008-01-22 09:31:00 +1100 | [diff] [blame] | 84 | netlbl_secattr_init(&secattr); |
| 85 | |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 86 | rc = security_netlbl_sid_to_secattr(sksec->sid, &secattr); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 87 | if (rc != 0) |
Paul Moore | 45c950e | 2008-01-22 09:31:00 +1100 | [diff] [blame] | 88 | goto sock_setsid_return; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 89 | rc = netlbl_sock_setattr(sk, &secattr); |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 90 | if (rc == 0) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 91 | sksec->nlbl_state = NLBL_LABELED; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 92 | |
Paul Moore | 45c950e | 2008-01-22 09:31:00 +1100 | [diff] [blame] | 93 | sock_setsid_return: |
| 94 | netlbl_secattr_destroy(&secattr); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 95 | return rc; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache |
| 100 | * |
| 101 | * Description: |
| 102 | * Invalidate the NetLabel security attribute mapping cache. |
| 103 | * |
| 104 | */ |
| 105 | void selinux_netlbl_cache_invalidate(void) |
| 106 | { |
| 107 | netlbl_cache_invalidate(); |
| 108 | } |
| 109 | |
| 110 | /** |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame^] | 111 | * selinux_netlbl_err - Handle a NetLabel packet error |
| 112 | * @skb: the packet |
| 113 | * @error: the error code |
| 114 | * @gateway: true if host is acting as a gateway, false otherwise |
| 115 | * |
| 116 | * Description: |
| 117 | * When a packet is dropped due to a call to avc_has_perm() pass the error |
| 118 | * code to the NetLabel subsystem so any protocol specific processing can be |
| 119 | * done. This is safe to call even if you are unsure if NetLabel labeling is |
| 120 | * present on the packet, NetLabel is smart enough to only act when it should. |
| 121 | * |
| 122 | */ |
| 123 | void selinux_netlbl_err(struct sk_buff *skb, int error, int gateway) |
| 124 | { |
| 125 | netlbl_skbuff_err(skb, error, gateway); |
| 126 | } |
| 127 | |
| 128 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 129 | * selinux_netlbl_sk_security_reset - Reset the NetLabel fields |
| 130 | * @ssec: the sk_security_struct |
| 131 | * @family: the socket family |
| 132 | * |
| 133 | * Description: |
| 134 | * Called when the NetLabel state of a sk_security_struct needs to be reset. |
| 135 | * The caller is responsibile for all the NetLabel sk_security_struct locking. |
| 136 | * |
| 137 | */ |
| 138 | void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec, |
| 139 | int family) |
| 140 | { |
Eric Paris | a6aaafe | 2008-04-18 17:38:23 -0400 | [diff] [blame] | 141 | if (family == PF_INET) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 142 | ssec->nlbl_state = NLBL_REQUIRE; |
| 143 | else |
| 144 | ssec->nlbl_state = NLBL_UNSET; |
| 145 | } |
| 146 | |
| 147 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 148 | * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel |
| 149 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 150 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 151 | * @type: NetLabel labeling protocol type |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 152 | * @sid: the SID |
| 153 | * |
| 154 | * Description: |
| 155 | * Call the NetLabel mechanism to get the security attributes of the given |
| 156 | * packet and use those attributes to determine the correct context/SID to |
| 157 | * assign to the packet. Returns zero on success, negative values on failure. |
| 158 | * |
| 159 | */ |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 160 | int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, |
| 161 | u16 family, |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 162 | u32 *type, |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 163 | u32 *sid) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 164 | { |
| 165 | int rc; |
| 166 | struct netlbl_lsm_secattr secattr; |
| 167 | |
Paul Moore | 23bcdc1 | 2007-07-18 12:28:45 -0400 | [diff] [blame] | 168 | if (!netlbl_enabled()) { |
| 169 | *sid = SECSID_NULL; |
| 170 | return 0; |
| 171 | } |
| 172 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 173 | netlbl_secattr_init(&secattr); |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 174 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 175 | if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) |
| 176 | rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid); |
| 177 | else |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 178 | *sid = SECSID_NULL; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 179 | *type = secattr.type; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 180 | netlbl_secattr_destroy(&secattr); |
| 181 | |
| 182 | return rc; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * selinux_netlbl_sock_graft - Netlabel the new socket |
| 187 | * @sk: the new connection |
| 188 | * @sock: the new socket |
| 189 | * |
| 190 | * Description: |
| 191 | * The connection represented by @sk is being grafted onto @sock so set the |
| 192 | * socket's NetLabel to match the SID of @sk. |
| 193 | * |
| 194 | */ |
| 195 | void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock) |
| 196 | { |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 197 | /* Try to set the NetLabel on the socket to save time later, if we fail |
| 198 | * here we will pick up the pieces in later calls to |
| 199 | * selinux_netlbl_inode_permission(). */ |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 200 | selinux_netlbl_sock_setsid(sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * selinux_netlbl_socket_post_create - Label a socket using NetLabel |
| 205 | * @sock: the socket to label |
| 206 | * |
| 207 | * Description: |
| 208 | * Attempt to label a socket using the NetLabel mechanism using the given |
| 209 | * SID. Returns zero values on success, negative values on failure. |
| 210 | * |
| 211 | */ |
| 212 | int selinux_netlbl_socket_post_create(struct socket *sock) |
| 213 | { |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 214 | return selinux_netlbl_sock_setsid(sock->sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled |
| 219 | * @inode: the file descriptor's inode |
| 220 | * @mask: the permission mask |
| 221 | * |
| 222 | * Description: |
| 223 | * Looks at a file's inode and if it is marked as a socket protected by |
| 224 | * NetLabel then verify that the socket has been labeled, if not try to label |
| 225 | * the socket now with the inode's SID. Returns zero on success, negative |
| 226 | * values on failure. |
| 227 | * |
| 228 | */ |
| 229 | int selinux_netlbl_inode_permission(struct inode *inode, int mask) |
| 230 | { |
| 231 | int rc; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 232 | struct sock *sk; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 233 | struct socket *sock; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 234 | struct sk_security_struct *sksec; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 235 | |
| 236 | if (!S_ISSOCK(inode->i_mode) || |
| 237 | ((mask & (MAY_WRITE | MAY_APPEND)) == 0)) |
| 238 | return 0; |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 239 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 240 | sock = SOCKET_I(inode); |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 241 | sk = sock->sk; |
| 242 | sksec = sk->sk_security; |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 243 | if (sksec->nlbl_state != NLBL_REQUIRE) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 244 | return 0; |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 245 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 246 | local_bh_disable(); |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 247 | bh_lock_sock_nested(sk); |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 248 | if (likely(sksec->nlbl_state == NLBL_REQUIRE)) |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 249 | rc = selinux_netlbl_sock_setsid(sk); |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 250 | else |
| 251 | rc = 0; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 252 | bh_unlock_sock(sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 253 | local_bh_enable(); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 254 | |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel |
| 260 | * @sksec: the sock's sk_security_struct |
| 261 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 262 | * @family: protocol family |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 263 | * @ad: the audit data |
| 264 | * |
| 265 | * Description: |
| 266 | * Fetch the NetLabel security attributes from @skb and perform an access check |
| 267 | * against the receiving socket. Returns zero on success, negative values on |
| 268 | * error. |
| 269 | * |
| 270 | */ |
| 271 | int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec, |
| 272 | struct sk_buff *skb, |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 273 | u16 family, |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 274 | struct avc_audit_data *ad) |
| 275 | { |
| 276 | int rc; |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 277 | u32 nlbl_sid; |
| 278 | u32 perm; |
| 279 | struct netlbl_lsm_secattr secattr; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 280 | |
Paul Moore | 23bcdc1 | 2007-07-18 12:28:45 -0400 | [diff] [blame] | 281 | if (!netlbl_enabled()) |
| 282 | return 0; |
| 283 | |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 284 | netlbl_secattr_init(&secattr); |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 285 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 286 | if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) |
| 287 | rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid); |
| 288 | else |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 289 | nlbl_sid = SECINITSID_UNLABELED; |
| 290 | netlbl_secattr_destroy(&secattr); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 291 | if (rc != 0) |
| 292 | return rc; |
Linus Torvalds | 8d9107e | 2007-07-13 16:53:18 -0700 | [diff] [blame] | 293 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 294 | switch (sksec->sclass) { |
| 295 | case SECCLASS_UDP_SOCKET: |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 296 | perm = UDP_SOCKET__RECVFROM; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 297 | break; |
| 298 | case SECCLASS_TCP_SOCKET: |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 299 | perm = TCP_SOCKET__RECVFROM; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 300 | break; |
| 301 | default: |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 302 | perm = RAWIP_SOCKET__RECVFROM; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 305 | rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 306 | if (rc == 0) |
| 307 | return 0; |
| 308 | |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 309 | if (nlbl_sid != SECINITSID_UNLABELED) |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame^] | 310 | netlbl_skbuff_err(skb, rc, 0); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 311 | return rc; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel |
| 316 | * @sock: the socket |
| 317 | * @level: the socket level or protocol |
| 318 | * @optname: the socket option name |
| 319 | * |
| 320 | * Description: |
| 321 | * Check the setsockopt() call and if the user is trying to replace the IP |
| 322 | * options on a socket and a NetLabel is in place for the socket deny the |
| 323 | * access; otherwise allow the access. Returns zero when the access is |
| 324 | * allowed, -EACCES when denied, and other negative values on error. |
| 325 | * |
| 326 | */ |
| 327 | int selinux_netlbl_socket_setsockopt(struct socket *sock, |
| 328 | int level, |
| 329 | int optname) |
| 330 | { |
| 331 | int rc = 0; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 332 | struct sock *sk = sock->sk; |
| 333 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 334 | struct netlbl_lsm_secattr secattr; |
| 335 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 336 | if (level == IPPROTO_IP && optname == IP_OPTIONS && |
| 337 | sksec->nlbl_state == NLBL_LABELED) { |
| 338 | netlbl_secattr_init(&secattr); |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 339 | lock_sock(sk); |
| 340 | rc = netlbl_sock_getattr(sk, &secattr); |
| 341 | release_sock(sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 342 | if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) |
| 343 | rc = -EACCES; |
| 344 | netlbl_secattr_destroy(&secattr); |
| 345 | } |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 346 | |
| 347 | return rc; |
| 348 | } |