diff options
Diffstat (limited to 'security/selinux/netlabel.c')
| -rw-r--r-- | security/selinux/netlabel.c | 280 |
1 files changed, 239 insertions, 41 deletions
diff --git a/security/selinux/netlabel.c b/security/selinux/netlabel.c index 89b418392f11..f58701a7b728 100644 --- a/security/selinux/netlabel.c +++ b/security/selinux/netlabel.c | |||
| @@ -9,7 +9,7 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /* | 11 | /* |
| 12 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2007 | 12 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2007, 2008 |
| 13 | * | 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify | 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 | 15 | * it under the terms of the GNU General Public License as published by |
| @@ -29,8 +29,12 @@ | |||
| 29 | 29 | ||
| 30 | #include <linux/spinlock.h> | 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/rcupdate.h> | 31 | #include <linux/rcupdate.h> |
| 32 | #include <linux/ip.h> | ||
| 33 | #include <linux/ipv6.h> | ||
| 32 | #include <net/sock.h> | 34 | #include <net/sock.h> |
| 33 | #include <net/netlabel.h> | 35 | #include <net/netlabel.h> |
| 36 | #include <net/ip.h> | ||
| 37 | #include <net/ipv6.h> | ||
| 34 | 38 | ||
| 35 | #include "objsec.h" | 39 | #include "objsec.h" |
| 36 | #include "security.h" | 40 | #include "security.h" |
| @@ -64,32 +68,69 @@ static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb, | |||
| 64 | } | 68 | } |
| 65 | 69 | ||
| 66 | /** | 70 | /** |
| 71 | * selinux_netlbl_sock_genattr - Generate the NetLabel socket secattr | ||
| 72 | * @sk: the socket | ||
| 73 | * | ||
| 74 | * Description: | ||
| 75 | * Generate the NetLabel security attributes for a socket, making full use of | ||
| 76 | * the socket's attribute cache. Returns a pointer to the security attributes | ||
| 77 | * on success, NULL on failure. | ||
| 78 | * | ||
| 79 | */ | ||
| 80 | static struct netlbl_lsm_secattr *selinux_netlbl_sock_genattr(struct sock *sk) | ||
| 81 | { | ||
| 82 | int rc; | ||
| 83 | struct sk_security_struct *sksec = sk->sk_security; | ||
| 84 | struct netlbl_lsm_secattr *secattr; | ||
| 85 | |||
| 86 | if (sksec->nlbl_secattr != NULL) | ||
| 87 | return sksec->nlbl_secattr; | ||
| 88 | |||
| 89 | secattr = netlbl_secattr_alloc(GFP_ATOMIC); | ||
| 90 | if (secattr == NULL) | ||
| 91 | return NULL; | ||
| 92 | rc = security_netlbl_sid_to_secattr(sksec->sid, secattr); | ||
| 93 | if (rc != 0) { | ||
| 94 | netlbl_secattr_free(secattr); | ||
| 95 | return NULL; | ||
| 96 | } | ||
| 97 | sksec->nlbl_secattr = secattr; | ||
| 98 | |||
| 99 | return secattr; | ||
| 100 | } | ||
| 101 | |||
| 102 | /** | ||
| 67 | * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism | 103 | * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism |
| 68 | * @sk: the socket to label | 104 | * @sk: the socket to label |
| 69 | * @sid: the SID to use | ||
| 70 | * | 105 | * |
| 71 | * Description: | 106 | * Description: |
| 72 | * Attempt to label a socket using the NetLabel mechanism using the given | 107 | * Attempt to label a socket using the NetLabel mechanism. Returns zero values |
| 73 | * SID. Returns zero values on success, negative values on failure. | 108 | * on success, negative values on failure. |
| 74 | * | 109 | * |
| 75 | */ | 110 | */ |
| 76 | static int selinux_netlbl_sock_setsid(struct sock *sk, u32 sid) | 111 | static int selinux_netlbl_sock_setsid(struct sock *sk) |
| 77 | { | 112 | { |
| 78 | int rc; | 113 | int rc; |
| 79 | struct sk_security_struct *sksec = sk->sk_security; | 114 | struct sk_security_struct *sksec = sk->sk_security; |
| 80 | struct netlbl_lsm_secattr secattr; | 115 | struct netlbl_lsm_secattr *secattr; |
| 81 | 116 | ||
| 82 | netlbl_secattr_init(&secattr); | 117 | if (sksec->nlbl_state != NLBL_REQUIRE) |
| 118 | return 0; | ||
| 83 | 119 | ||
| 84 | rc = security_netlbl_sid_to_secattr(sid, &secattr); | 120 | secattr = selinux_netlbl_sock_genattr(sk); |
| 85 | if (rc != 0) | 121 | if (secattr == NULL) |
| 86 | goto sock_setsid_return; | 122 | return -ENOMEM; |
| 87 | rc = netlbl_sock_setattr(sk, &secattr); | 123 | rc = netlbl_sock_setattr(sk, secattr); |
| 88 | if (rc == 0) | 124 | switch (rc) { |
| 125 | case 0: | ||
| 89 | sksec->nlbl_state = NLBL_LABELED; | 126 | sksec->nlbl_state = NLBL_LABELED; |
| 127 | break; | ||
| 128 | case -EDESTADDRREQ: | ||
| 129 | sksec->nlbl_state = NLBL_REQSKB; | ||
| 130 | rc = 0; | ||
| 131 | break; | ||
| 132 | } | ||
| 90 | 133 | ||
| 91 | sock_setsid_return: | ||
| 92 | netlbl_secattr_destroy(&secattr); | ||
| 93 | return rc; | 134 | return rc; |
| 94 | } | 135 | } |
| 95 | 136 | ||
| @@ -106,6 +147,38 @@ void selinux_netlbl_cache_invalidate(void) | |||
| 106 | } | 147 | } |
| 107 | 148 | ||
| 108 | /** | 149 | /** |
| 150 | * selinux_netlbl_err - Handle a NetLabel packet error | ||
| 151 | * @skb: the packet | ||
| 152 | * @error: the error code | ||
| 153 | * @gateway: true if host is acting as a gateway, false otherwise | ||
| 154 | * | ||
| 155 | * Description: | ||
| 156 | * When a packet is dropped due to a call to avc_has_perm() pass the error | ||
| 157 | * code to the NetLabel subsystem so any protocol specific processing can be | ||
| 158 | * done. This is safe to call even if you are unsure if NetLabel labeling is | ||
| 159 | * present on the packet, NetLabel is smart enough to only act when it should. | ||
| 160 | * | ||
| 161 | */ | ||
| 162 | void selinux_netlbl_err(struct sk_buff *skb, int error, int gateway) | ||
| 163 | { | ||
| 164 | netlbl_skbuff_err(skb, error, gateway); | ||
| 165 | } | ||
| 166 | |||
| 167 | /** | ||
| 168 | * selinux_netlbl_sk_security_free - Free the NetLabel fields | ||
| 169 | * @sssec: the sk_security_struct | ||
| 170 | * | ||
| 171 | * Description: | ||
| 172 | * Free all of the memory in the NetLabel fields of a sk_security_struct. | ||
| 173 | * | ||
| 174 | */ | ||
| 175 | void selinux_netlbl_sk_security_free(struct sk_security_struct *ssec) | ||
| 176 | { | ||
| 177 | if (ssec->nlbl_secattr != NULL) | ||
| 178 | netlbl_secattr_free(ssec->nlbl_secattr); | ||
| 179 | } | ||
| 180 | |||
| 181 | /** | ||
| 109 | * selinux_netlbl_sk_security_reset - Reset the NetLabel fields | 182 | * selinux_netlbl_sk_security_reset - Reset the NetLabel fields |
| 110 | * @ssec: the sk_security_struct | 183 | * @ssec: the sk_security_struct |
| 111 | * @family: the socket family | 184 | * @family: the socket family |
| @@ -163,35 +236,118 @@ int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, | |||
| 163 | } | 236 | } |
| 164 | 237 | ||
| 165 | /** | 238 | /** |
| 166 | * selinux_netlbl_sock_graft - Netlabel the new socket | 239 | * selinux_netlbl_skbuff_setsid - Set the NetLabel on a packet given a sid |
| 240 | * @skb: the packet | ||
| 241 | * @family: protocol family | ||
| 242 | * @sid: the SID | ||
| 243 | * | ||
| 244 | * Description | ||
| 245 | * Call the NetLabel mechanism to set the label of a packet using @sid. | ||
| 246 | * Returns zero on auccess, negative values on failure. | ||
| 247 | * | ||
| 248 | */ | ||
| 249 | int selinux_netlbl_skbuff_setsid(struct sk_buff *skb, | ||
| 250 | u16 family, | ||
| 251 | u32 sid) | ||
| 252 | { | ||
| 253 | int rc; | ||
| 254 | struct netlbl_lsm_secattr secattr_storage; | ||
| 255 | struct netlbl_lsm_secattr *secattr = NULL; | ||
| 256 | struct sock *sk; | ||
| 257 | |||
| 258 | /* if this is a locally generated packet check to see if it is already | ||
| 259 | * being labeled by it's parent socket, if it is just exit */ | ||
| 260 | sk = skb->sk; | ||
| 261 | if (sk != NULL) { | ||
| 262 | struct sk_security_struct *sksec = sk->sk_security; | ||
| 263 | if (sksec->nlbl_state != NLBL_REQSKB) | ||
| 264 | return 0; | ||
| 265 | secattr = sksec->nlbl_secattr; | ||
| 266 | } | ||
| 267 | if (secattr == NULL) { | ||
| 268 | secattr = &secattr_storage; | ||
| 269 | netlbl_secattr_init(secattr); | ||
| 270 | rc = security_netlbl_sid_to_secattr(sid, secattr); | ||
| 271 | if (rc != 0) | ||
| 272 | goto skbuff_setsid_return; | ||
| 273 | } | ||
| 274 | |||
| 275 | rc = netlbl_skbuff_setattr(skb, family, secattr); | ||
| 276 | |||
| 277 | skbuff_setsid_return: | ||
| 278 | if (secattr == &secattr_storage) | ||
| 279 | netlbl_secattr_destroy(secattr); | ||
| 280 | return rc; | ||
| 281 | } | ||
| 282 | |||
| 283 | /** | ||
| 284 | * selinux_netlbl_inet_conn_established - Netlabel the newly accepted connection | ||
| 167 | * @sk: the new connection | 285 | * @sk: the new connection |
| 168 | * @sock: the new socket | ||
| 169 | * | 286 | * |
| 170 | * Description: | 287 | * Description: |
| 171 | * The connection represented by @sk is being grafted onto @sock so set the | 288 | * A new connection has been established on @sk so make sure it is labeled |
| 172 | * socket's NetLabel to match the SID of @sk. | 289 | * correctly with the NetLabel susbsystem. |
| 173 | * | 290 | * |
| 174 | */ | 291 | */ |
| 175 | void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock) | 292 | void selinux_netlbl_inet_conn_established(struct sock *sk, u16 family) |
| 176 | { | 293 | { |
| 294 | int rc; | ||
| 177 | struct sk_security_struct *sksec = sk->sk_security; | 295 | struct sk_security_struct *sksec = sk->sk_security; |
| 178 | struct netlbl_lsm_secattr secattr; | 296 | struct netlbl_lsm_secattr *secattr; |
| 179 | u32 nlbl_peer_sid; | 297 | struct inet_sock *sk_inet = inet_sk(sk); |
| 298 | struct sockaddr_in addr; | ||
| 180 | 299 | ||
| 181 | if (sksec->nlbl_state != NLBL_REQUIRE) | 300 | if (sksec->nlbl_state != NLBL_REQUIRE) |
| 182 | return; | 301 | return; |
| 183 | 302 | ||
| 184 | netlbl_secattr_init(&secattr); | 303 | secattr = selinux_netlbl_sock_genattr(sk); |
| 185 | if (netlbl_sock_getattr(sk, &secattr) == 0 && | 304 | if (secattr == NULL) |
| 186 | secattr.flags != NETLBL_SECATTR_NONE && | 305 | return; |
| 187 | security_netlbl_secattr_to_sid(&secattr, &nlbl_peer_sid) == 0) | ||
| 188 | sksec->peer_sid = nlbl_peer_sid; | ||
| 189 | netlbl_secattr_destroy(&secattr); | ||
| 190 | 306 | ||
| 191 | /* Try to set the NetLabel on the socket to save time later, if we fail | 307 | rc = netlbl_sock_setattr(sk, secattr); |
| 192 | * here we will pick up the pieces in later calls to | 308 | switch (rc) { |
| 193 | * selinux_netlbl_inode_permission(). */ | 309 | case 0: |
| 194 | selinux_netlbl_sock_setsid(sk, sksec->sid); | 310 | sksec->nlbl_state = NLBL_LABELED; |
| 311 | break; | ||
| 312 | case -EDESTADDRREQ: | ||
| 313 | /* no PF_INET6 support yet because we don't support any IPv6 | ||
| 314 | * labeling protocols */ | ||
| 315 | if (family != PF_INET) { | ||
| 316 | sksec->nlbl_state = NLBL_UNSET; | ||
| 317 | return; | ||
| 318 | } | ||
| 319 | |||
| 320 | addr.sin_family = family; | ||
| 321 | addr.sin_addr.s_addr = sk_inet->daddr; | ||
| 322 | if (netlbl_conn_setattr(sk, (struct sockaddr *)&addr, | ||
| 323 | secattr) != 0) { | ||
| 324 | /* we failed to label the connected socket (could be | ||
| 325 | * for a variety of reasons, the actual "why" isn't | ||
| 326 | * important here) so we have to go to our backup plan, | ||
| 327 | * labeling the packets individually in the netfilter | ||
| 328 | * local output hook. this is okay but we need to | ||
| 329 | * adjust the MSS of the connection to take into | ||
| 330 | * account any labeling overhead, since we don't know | ||
| 331 | * the exact overhead at this point we'll use the worst | ||
| 332 | * case value which is 40 bytes for IPv4 */ | ||
| 333 | struct inet_connection_sock *sk_conn = inet_csk(sk); | ||
| 334 | sk_conn->icsk_ext_hdr_len += 40 - | ||
| 335 | (sk_inet->opt ? sk_inet->opt->optlen : 0); | ||
| 336 | sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); | ||
| 337 | |||
| 338 | sksec->nlbl_state = NLBL_REQSKB; | ||
| 339 | } else | ||
| 340 | sksec->nlbl_state = NLBL_CONNLABELED; | ||
| 341 | break; | ||
| 342 | default: | ||
| 343 | /* note that we are failing to label the socket which could be | ||
| 344 | * a bad thing since it means traffic could leave the system | ||
| 345 | * without the desired labeling, however, all is not lost as | ||
| 346 | * we have a check in selinux_netlbl_inode_permission() to | ||
| 347 | * pick up the pieces that we might drop here because we can't | ||
| 348 | * return an error code */ | ||
| 349 | break; | ||
| 350 | } | ||
| 195 | } | 351 | } |
| 196 | 352 | ||
| 197 | /** | 353 | /** |
| @@ -205,13 +361,7 @@ void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock) | |||
| 205 | */ | 361 | */ |
| 206 | int selinux_netlbl_socket_post_create(struct socket *sock) | 362 | int selinux_netlbl_socket_post_create(struct socket *sock) |
| 207 | { | 363 | { |
| 208 | struct sock *sk = sock->sk; | 364 | return selinux_netlbl_sock_setsid(sock->sk); |
| 209 | struct sk_security_struct *sksec = sk->sk_security; | ||
| 210 | |||
| 211 | if (sksec->nlbl_state != NLBL_REQUIRE) | ||
| 212 | return 0; | ||
| 213 | |||
| 214 | return selinux_netlbl_sock_setsid(sk, sksec->sid); | ||
| 215 | } | 365 | } |
| 216 | 366 | ||
| 217 | /** | 367 | /** |
| @@ -246,7 +396,7 @@ int selinux_netlbl_inode_permission(struct inode *inode, int mask) | |||
| 246 | local_bh_disable(); | 396 | local_bh_disable(); |
| 247 | bh_lock_sock_nested(sk); | 397 | bh_lock_sock_nested(sk); |
| 248 | if (likely(sksec->nlbl_state == NLBL_REQUIRE)) | 398 | if (likely(sksec->nlbl_state == NLBL_REQUIRE)) |
| 249 | rc = selinux_netlbl_sock_setsid(sk, sksec->sid); | 399 | rc = selinux_netlbl_sock_setsid(sk); |
| 250 | else | 400 | else |
| 251 | rc = 0; | 401 | rc = 0; |
| 252 | bh_unlock_sock(sk); | 402 | bh_unlock_sock(sk); |
| @@ -307,7 +457,7 @@ int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec, | |||
| 307 | return 0; | 457 | return 0; |
| 308 | 458 | ||
| 309 | if (nlbl_sid != SECINITSID_UNLABELED) | 459 | if (nlbl_sid != SECINITSID_UNLABELED) |
| 310 | netlbl_skbuff_err(skb, rc); | 460 | netlbl_skbuff_err(skb, rc, 0); |
| 311 | return rc; | 461 | return rc; |
| 312 | } | 462 | } |
| 313 | 463 | ||
| @@ -334,7 +484,8 @@ int selinux_netlbl_socket_setsockopt(struct socket *sock, | |||
| 334 | struct netlbl_lsm_secattr secattr; | 484 | struct netlbl_lsm_secattr secattr; |
| 335 | 485 | ||
| 336 | if (level == IPPROTO_IP && optname == IP_OPTIONS && | 486 | if (level == IPPROTO_IP && optname == IP_OPTIONS && |
| 337 | sksec->nlbl_state == NLBL_LABELED) { | 487 | (sksec->nlbl_state == NLBL_LABELED || |
| 488 | sksec->nlbl_state == NLBL_CONNLABELED)) { | ||
| 338 | netlbl_secattr_init(&secattr); | 489 | netlbl_secattr_init(&secattr); |
| 339 | lock_sock(sk); | 490 | lock_sock(sk); |
| 340 | rc = netlbl_sock_getattr(sk, &secattr); | 491 | rc = netlbl_sock_getattr(sk, &secattr); |
| @@ -346,3 +497,50 @@ int selinux_netlbl_socket_setsockopt(struct socket *sock, | |||
| 346 | 497 | ||
| 347 | return rc; | 498 | return rc; |
| 348 | } | 499 | } |
| 500 | |||
| 501 | /** | ||
| 502 | * selinux_netlbl_socket_connect - Label a client-side socket on connect | ||
| 503 | * @sk: the socket to label | ||
| 504 | * @addr: the destination address | ||
| 505 | * | ||
| 506 | * Description: | ||
| 507 | * Attempt to label a connected socket with NetLabel using the given address. | ||
| 508 | * Returns zero values on success, negative values on failure. | ||
| 509 | * | ||
| 510 | */ | ||
| 511 | int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr) | ||
| 512 | { | ||
| 513 | int rc; | ||
| 514 | struct sk_security_struct *sksec = sk->sk_security; | ||
| 515 | struct netlbl_lsm_secattr *secattr; | ||
| 516 | |||
| 517 | if (sksec->nlbl_state != NLBL_REQSKB && | ||
| 518 | sksec->nlbl_state != NLBL_CONNLABELED) | ||
| 519 | return 0; | ||
| 520 | |||
| 521 | local_bh_disable(); | ||
| 522 | bh_lock_sock_nested(sk); | ||
| 523 | |||
| 524 | /* connected sockets are allowed to disconnect when the address family | ||
| 525 | * is set to AF_UNSPEC, if that is what is happening we want to reset | ||
| 526 | * the socket */ | ||
| 527 | if (addr->sa_family == AF_UNSPEC) { | ||
| 528 | netlbl_sock_delattr(sk); | ||
| 529 | sksec->nlbl_state = NLBL_REQSKB; | ||
| 530 | rc = 0; | ||
| 531 | goto socket_connect_return; | ||
| 532 | } | ||
| 533 | secattr = selinux_netlbl_sock_genattr(sk); | ||
| 534 | if (secattr == NULL) { | ||
| 535 | rc = -ENOMEM; | ||
| 536 | goto socket_connect_return; | ||
| 537 | } | ||
| 538 | rc = netlbl_conn_setattr(sk, addr, secattr); | ||
| 539 | if (rc == 0) | ||
| 540 | sksec->nlbl_state = NLBL_CONNLABELED; | ||
| 541 | |||
| 542 | socket_connect_return: | ||
| 543 | bh_unlock_sock(sk); | ||
| 544 | local_bh_enable(); | ||
| 545 | return rc; | ||
| 546 | } | ||
