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 | /* |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 12 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2007, 2008 |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 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> |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 32 | #include <linux/ip.h> |
| 33 | #include <linux/ipv6.h> |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 34 | #include <net/sock.h> |
| 35 | #include <net/netlabel.h> |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 36 | #include <net/ip.h> |
| 37 | #include <net/ipv6.h> |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 38 | |
| 39 | #include "objsec.h" |
| 40 | #include "security.h" |
Adrian Bunk | d4ee423 | 2008-02-27 23:20:42 +0200 | [diff] [blame] | 41 | #include "netlabel.h" |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 42 | |
| 43 | /** |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 44 | * selinux_netlbl_sidlookup_cached - Cache a SID lookup |
| 45 | * @skb: the packet |
| 46 | * @secattr: the NetLabel security attributes |
| 47 | * @sid: the SID |
| 48 | * |
| 49 | * Description: |
| 50 | * Query the SELinux security server to lookup the correct SID for the given |
| 51 | * security attributes. If the query is successful, cache the result to speed |
| 52 | * up future lookups. Returns zero on success, negative values on failure. |
| 53 | * |
| 54 | */ |
| 55 | static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb, |
| 56 | struct netlbl_lsm_secattr *secattr, |
| 57 | u32 *sid) |
| 58 | { |
| 59 | int rc; |
| 60 | |
| 61 | rc = security_netlbl_secattr_to_sid(secattr, sid); |
| 62 | if (rc == 0 && |
| 63 | (secattr->flags & NETLBL_SECATTR_CACHEABLE) && |
| 64 | (secattr->flags & NETLBL_SECATTR_CACHE)) |
| 65 | netlbl_cache_add(skb, secattr); |
| 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | |
| 70 | /** |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 71 | * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism |
| 72 | * @sk: the socket to label |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 73 | * |
| 74 | * Description: |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 75 | * Attempt to label a socket using the NetLabel mechanism. Returns zero values |
| 76 | * on success, negative values on failure. |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 77 | * |
| 78 | */ |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 79 | static int selinux_netlbl_sock_setsid(struct sock *sk) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 80 | { |
| 81 | int rc; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 82 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 83 | struct netlbl_lsm_secattr secattr; |
| 84 | |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 85 | if (sksec->nlbl_state != NLBL_REQUIRE) |
| 86 | return 0; |
| 87 | |
Paul Moore | 45c950e | 2008-01-22 09:31:00 +1100 | [diff] [blame] | 88 | netlbl_secattr_init(&secattr); |
| 89 | |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 90 | rc = security_netlbl_sid_to_secattr(sksec->sid, &secattr); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 91 | if (rc != 0) |
Paul Moore | 45c950e | 2008-01-22 09:31:00 +1100 | [diff] [blame] | 92 | goto sock_setsid_return; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 93 | rc = netlbl_sock_setattr(sk, &secattr); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 94 | switch (rc) { |
| 95 | case 0: |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 96 | sksec->nlbl_state = NLBL_LABELED; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 97 | break; |
| 98 | case -EDESTADDRREQ: |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 99 | sksec->nlbl_state = NLBL_REQSKB; |
| 100 | rc = 0; |
| 101 | break; |
| 102 | } |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 103 | |
Paul Moore | 45c950e | 2008-01-22 09:31:00 +1100 | [diff] [blame] | 104 | sock_setsid_return: |
| 105 | netlbl_secattr_destroy(&secattr); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 106 | return rc; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache |
| 111 | * |
| 112 | * Description: |
| 113 | * Invalidate the NetLabel security attribute mapping cache. |
| 114 | * |
| 115 | */ |
| 116 | void selinux_netlbl_cache_invalidate(void) |
| 117 | { |
| 118 | netlbl_cache_invalidate(); |
| 119 | } |
| 120 | |
| 121 | /** |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 122 | * selinux_netlbl_err - Handle a NetLabel packet error |
| 123 | * @skb: the packet |
| 124 | * @error: the error code |
| 125 | * @gateway: true if host is acting as a gateway, false otherwise |
| 126 | * |
| 127 | * Description: |
| 128 | * When a packet is dropped due to a call to avc_has_perm() pass the error |
| 129 | * code to the NetLabel subsystem so any protocol specific processing can be |
| 130 | * done. This is safe to call even if you are unsure if NetLabel labeling is |
| 131 | * present on the packet, NetLabel is smart enough to only act when it should. |
| 132 | * |
| 133 | */ |
| 134 | void selinux_netlbl_err(struct sk_buff *skb, int error, int gateway) |
| 135 | { |
| 136 | netlbl_skbuff_err(skb, error, gateway); |
| 137 | } |
| 138 | |
| 139 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 140 | * selinux_netlbl_sk_security_reset - Reset the NetLabel fields |
| 141 | * @ssec: the sk_security_struct |
| 142 | * @family: the socket family |
| 143 | * |
| 144 | * Description: |
| 145 | * Called when the NetLabel state of a sk_security_struct needs to be reset. |
| 146 | * The caller is responsibile for all the NetLabel sk_security_struct locking. |
| 147 | * |
| 148 | */ |
| 149 | void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec, |
| 150 | int family) |
| 151 | { |
Eric Paris | a6aaafe | 2008-04-18 17:38:23 -0400 | [diff] [blame] | 152 | if (family == PF_INET) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 153 | ssec->nlbl_state = NLBL_REQUIRE; |
| 154 | else |
| 155 | ssec->nlbl_state = NLBL_UNSET; |
| 156 | } |
| 157 | |
| 158 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 159 | * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel |
| 160 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 161 | * @family: protocol family |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 162 | * @type: NetLabel labeling protocol type |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 163 | * @sid: the SID |
| 164 | * |
| 165 | * Description: |
| 166 | * Call the NetLabel mechanism to get the security attributes of the given |
| 167 | * packet and use those attributes to determine the correct context/SID to |
| 168 | * assign to the packet. Returns zero on success, negative values on failure. |
| 169 | * |
| 170 | */ |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 171 | int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, |
| 172 | u16 family, |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 173 | u32 *type, |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 174 | u32 *sid) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 175 | { |
| 176 | int rc; |
| 177 | struct netlbl_lsm_secattr secattr; |
| 178 | |
Paul Moore | 23bcdc1 | 2007-07-18 12:28:45 -0400 | [diff] [blame] | 179 | if (!netlbl_enabled()) { |
| 180 | *sid = SECSID_NULL; |
| 181 | return 0; |
| 182 | } |
| 183 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 184 | netlbl_secattr_init(&secattr); |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 185 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 186 | if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) |
| 187 | rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid); |
| 188 | else |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 189 | *sid = SECSID_NULL; |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 190 | *type = secattr.type; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 191 | netlbl_secattr_destroy(&secattr); |
| 192 | |
| 193 | return rc; |
| 194 | } |
| 195 | |
| 196 | /** |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 197 | * selinux_netlbl_skbuff_setsid - Set the NetLabel on a packet given a sid |
| 198 | * @skb: the packet |
| 199 | * @family: protocol family |
| 200 | * @sid: the SID |
| 201 | * |
| 202 | * Description |
| 203 | * Call the NetLabel mechanism to set the label of a packet using @sid. |
| 204 | * Returns zero on auccess, negative values on failure. |
| 205 | * |
| 206 | */ |
| 207 | int selinux_netlbl_skbuff_setsid(struct sk_buff *skb, |
| 208 | u16 family, |
| 209 | u32 sid) |
| 210 | { |
| 211 | int rc; |
| 212 | struct netlbl_lsm_secattr secattr; |
| 213 | struct sock *sk; |
| 214 | |
| 215 | /* if this is a locally generated packet check to see if it is already |
| 216 | * being labeled by it's parent socket, if it is just exit */ |
| 217 | sk = skb->sk; |
| 218 | if (sk != NULL) { |
| 219 | struct sk_security_struct *sksec = sk->sk_security; |
| 220 | if (sksec->nlbl_state != NLBL_REQSKB) |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | netlbl_secattr_init(&secattr); |
| 225 | rc = security_netlbl_sid_to_secattr(sid, &secattr); |
| 226 | if (rc != 0) |
| 227 | goto skbuff_setsid_return; |
| 228 | rc = netlbl_skbuff_setattr(skb, family, &secattr); |
| 229 | |
| 230 | skbuff_setsid_return: |
| 231 | netlbl_secattr_destroy(&secattr); |
| 232 | return rc; |
| 233 | } |
| 234 | |
| 235 | /** |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 236 | * selinux_netlbl_inet_conn_established - Netlabel the newly accepted connection |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 237 | * @sk: the new connection |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 238 | * |
| 239 | * Description: |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 240 | * A new connection has been established on @sk so make sure it is labeled |
| 241 | * correctly with the NetLabel susbsystem. |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 242 | * |
| 243 | */ |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 244 | void selinux_netlbl_inet_conn_established(struct sock *sk, u16 family) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 245 | { |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 246 | int rc; |
| 247 | struct sk_security_struct *sksec = sk->sk_security; |
| 248 | struct netlbl_lsm_secattr secattr; |
| 249 | struct inet_sock *sk_inet = inet_sk(sk); |
| 250 | struct sockaddr_in addr; |
| 251 | |
| 252 | if (sksec->nlbl_state != NLBL_REQUIRE) |
| 253 | return; |
| 254 | |
| 255 | netlbl_secattr_init(&secattr); |
| 256 | if (security_netlbl_sid_to_secattr(sksec->sid, &secattr) != 0) |
| 257 | goto inet_conn_established_return; |
| 258 | |
| 259 | rc = netlbl_sock_setattr(sk, &secattr); |
| 260 | switch (rc) { |
| 261 | case 0: |
| 262 | sksec->nlbl_state = NLBL_LABELED; |
| 263 | break; |
| 264 | case -EDESTADDRREQ: |
| 265 | /* no PF_INET6 support yet because we don't support any IPv6 |
| 266 | * labeling protocols */ |
| 267 | if (family != PF_INET) { |
| 268 | sksec->nlbl_state = NLBL_UNSET; |
| 269 | goto inet_conn_established_return; |
| 270 | } |
| 271 | |
| 272 | addr.sin_family = family; |
| 273 | addr.sin_addr.s_addr = sk_inet->daddr; |
| 274 | if (netlbl_conn_setattr(sk, (struct sockaddr *)&addr, |
| 275 | &secattr) != 0) { |
| 276 | /* we failed to label the connected socket (could be |
| 277 | * for a variety of reasons, the actual "why" isn't |
| 278 | * important here) so we have to go to our backup plan, |
| 279 | * labeling the packets individually in the netfilter |
| 280 | * local output hook. this is okay but we need to |
| 281 | * adjust the MSS of the connection to take into |
| 282 | * account any labeling overhead, since we don't know |
| 283 | * the exact overhead at this point we'll use the worst |
| 284 | * case value which is 40 bytes for IPv4 */ |
| 285 | struct inet_connection_sock *sk_conn = inet_csk(sk); |
| 286 | sk_conn->icsk_ext_hdr_len += 40 - |
| 287 | (sk_inet->opt ? sk_inet->opt->optlen : 0); |
| 288 | sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); |
| 289 | |
| 290 | sksec->nlbl_state = NLBL_REQSKB; |
| 291 | } else |
| 292 | sksec->nlbl_state = NLBL_CONNLABELED; |
| 293 | break; |
| 294 | default: |
| 295 | /* note that we are failing to label the socket which could be |
| 296 | * a bad thing since it means traffic could leave the system |
| 297 | * without the desired labeling, however, all is not lost as |
| 298 | * we have a check in selinux_netlbl_inode_permission() to |
| 299 | * pick up the pieces that we might drop here because we can't |
| 300 | * return an error code */ |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | inet_conn_established_return: |
| 305 | netlbl_secattr_destroy(&secattr); |
| 306 | return; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * selinux_netlbl_socket_post_create - Label a socket using NetLabel |
| 311 | * @sock: the socket to label |
| 312 | * |
| 313 | * Description: |
| 314 | * Attempt to label a socket using the NetLabel mechanism using the given |
| 315 | * SID. Returns zero values on success, negative values on failure. |
| 316 | * |
| 317 | */ |
| 318 | int selinux_netlbl_socket_post_create(struct socket *sock) |
| 319 | { |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 320 | return selinux_netlbl_sock_setsid(sock->sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled |
| 325 | * @inode: the file descriptor's inode |
| 326 | * @mask: the permission mask |
| 327 | * |
| 328 | * Description: |
| 329 | * Looks at a file's inode and if it is marked as a socket protected by |
| 330 | * NetLabel then verify that the socket has been labeled, if not try to label |
| 331 | * the socket now with the inode's SID. Returns zero on success, negative |
| 332 | * values on failure. |
| 333 | * |
| 334 | */ |
| 335 | int selinux_netlbl_inode_permission(struct inode *inode, int mask) |
| 336 | { |
| 337 | int rc; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 338 | struct sock *sk; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 339 | struct socket *sock; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 340 | struct sk_security_struct *sksec; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 341 | |
| 342 | if (!S_ISSOCK(inode->i_mode) || |
| 343 | ((mask & (MAY_WRITE | MAY_APPEND)) == 0)) |
| 344 | return 0; |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 345 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 346 | sock = SOCKET_I(inode); |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 347 | sk = sock->sk; |
| 348 | sksec = sk->sk_security; |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 349 | if (sksec->nlbl_state != NLBL_REQUIRE) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 350 | return 0; |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 351 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 352 | local_bh_disable(); |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 353 | bh_lock_sock_nested(sk); |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 354 | if (likely(sksec->nlbl_state == NLBL_REQUIRE)) |
Paul Moore | accc609 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 355 | rc = selinux_netlbl_sock_setsid(sk); |
Paul Moore | f74af6e | 2008-02-25 11:40:33 -0500 | [diff] [blame] | 356 | else |
| 357 | rc = 0; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 358 | bh_unlock_sock(sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 359 | local_bh_enable(); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 360 | |
| 361 | return rc; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel |
| 366 | * @sksec: the sock's sk_security_struct |
| 367 | * @skb: the packet |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 368 | * @family: protocol family |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 369 | * @ad: the audit data |
| 370 | * |
| 371 | * Description: |
| 372 | * Fetch the NetLabel security attributes from @skb and perform an access check |
| 373 | * against the receiving socket. Returns zero on success, negative values on |
| 374 | * error. |
| 375 | * |
| 376 | */ |
| 377 | int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec, |
| 378 | struct sk_buff *skb, |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 379 | u16 family, |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 380 | struct avc_audit_data *ad) |
| 381 | { |
| 382 | int rc; |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 383 | u32 nlbl_sid; |
| 384 | u32 perm; |
| 385 | struct netlbl_lsm_secattr secattr; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 386 | |
Paul Moore | 23bcdc1 | 2007-07-18 12:28:45 -0400 | [diff] [blame] | 387 | if (!netlbl_enabled()) |
| 388 | return 0; |
| 389 | |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 390 | netlbl_secattr_init(&secattr); |
Paul Moore | 75e2291 | 2008-01-29 08:38:04 -0500 | [diff] [blame] | 391 | rc = netlbl_skbuff_getattr(skb, family, &secattr); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 392 | if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) |
| 393 | rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid); |
| 394 | else |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 395 | nlbl_sid = SECINITSID_UNLABELED; |
| 396 | netlbl_secattr_destroy(&secattr); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 397 | if (rc != 0) |
| 398 | return rc; |
Linus Torvalds | 8d9107e | 2007-07-13 16:53:18 -0700 | [diff] [blame] | 399 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 400 | switch (sksec->sclass) { |
| 401 | case SECCLASS_UDP_SOCKET: |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 402 | perm = UDP_SOCKET__RECVFROM; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 403 | break; |
| 404 | case SECCLASS_TCP_SOCKET: |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 405 | perm = TCP_SOCKET__RECVFROM; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 406 | break; |
| 407 | default: |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 408 | perm = RAWIP_SOCKET__RECVFROM; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 411 | rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 412 | if (rc == 0) |
| 413 | return 0; |
| 414 | |
Paul Moore | f36158c | 2007-07-18 12:28:46 -0400 | [diff] [blame] | 415 | if (nlbl_sid != SECINITSID_UNLABELED) |
Paul Moore | dfaebe9 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 416 | netlbl_skbuff_err(skb, rc, 0); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 417 | return rc; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel |
| 422 | * @sock: the socket |
| 423 | * @level: the socket level or protocol |
| 424 | * @optname: the socket option name |
| 425 | * |
| 426 | * Description: |
| 427 | * Check the setsockopt() call and if the user is trying to replace the IP |
| 428 | * options on a socket and a NetLabel is in place for the socket deny the |
| 429 | * access; otherwise allow the access. Returns zero when the access is |
| 430 | * allowed, -EACCES when denied, and other negative values on error. |
| 431 | * |
| 432 | */ |
| 433 | int selinux_netlbl_socket_setsockopt(struct socket *sock, |
| 434 | int level, |
| 435 | int optname) |
| 436 | { |
| 437 | int rc = 0; |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 438 | struct sock *sk = sock->sk; |
| 439 | struct sk_security_struct *sksec = sk->sk_security; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 440 | struct netlbl_lsm_secattr secattr; |
| 441 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 442 | if (level == IPPROTO_IP && optname == IP_OPTIONS && |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 443 | (sksec->nlbl_state == NLBL_LABELED || |
| 444 | sksec->nlbl_state == NLBL_CONNLABELED)) { |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 445 | netlbl_secattr_init(&secattr); |
Paul Moore | ba6ff9f | 2007-06-07 18:37:15 -0700 | [diff] [blame] | 446 | lock_sock(sk); |
| 447 | rc = netlbl_sock_getattr(sk, &secattr); |
| 448 | release_sock(sk); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 449 | if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) |
| 450 | rc = -EACCES; |
| 451 | netlbl_secattr_destroy(&secattr); |
| 452 | } |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 453 | |
| 454 | return rc; |
| 455 | } |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame^] | 456 | |
| 457 | /** |
| 458 | * selinux_netlbl_socket_connect - Label a client-side socket on connect |
| 459 | * @sk: the socket to label |
| 460 | * @addr: the destination address |
| 461 | * |
| 462 | * Description: |
| 463 | * Attempt to label a connected socket with NetLabel using the given address. |
| 464 | * Returns zero values on success, negative values on failure. |
| 465 | * |
| 466 | */ |
| 467 | int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr) |
| 468 | { |
| 469 | int rc; |
| 470 | struct sk_security_struct *sksec = sk->sk_security; |
| 471 | struct netlbl_lsm_secattr secattr; |
| 472 | |
| 473 | if (sksec->nlbl_state != NLBL_REQSKB && |
| 474 | sksec->nlbl_state != NLBL_CONNLABELED) |
| 475 | return 0; |
| 476 | |
| 477 | netlbl_secattr_init(&secattr); |
| 478 | local_bh_disable(); |
| 479 | bh_lock_sock_nested(sk); |
| 480 | |
| 481 | /* connected sockets are allowed to disconnect when the address family |
| 482 | * is set to AF_UNSPEC, if that is what is happening we want to reset |
| 483 | * the socket */ |
| 484 | if (addr->sa_family == AF_UNSPEC) { |
| 485 | netlbl_sock_delattr(sk); |
| 486 | sksec->nlbl_state = NLBL_REQSKB; |
| 487 | rc = 0; |
| 488 | goto socket_connect_return; |
| 489 | } |
| 490 | rc = security_netlbl_sid_to_secattr(sksec->sid, &secattr); |
| 491 | if (rc != 0) |
| 492 | goto socket_connect_return; |
| 493 | rc = netlbl_conn_setattr(sk, addr, &secattr); |
| 494 | if (rc != 0) |
| 495 | goto socket_connect_return; |
| 496 | sksec->nlbl_state = NLBL_CONNLABELED; |
| 497 | |
| 498 | socket_connect_return: |
| 499 | bh_unlock_sock(sk); |
| 500 | local_bh_enable(); |
| 501 | netlbl_secattr_destroy(&secattr); |
| 502 | return rc; |
| 503 | } |