netlabel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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@paul-moore.com>
  8. *
  9. */
  10. /*
  11. * (c) Copyright Hewlett-Packard Development Company, L.P., 2007, 2008
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  21. * the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. #include <linux/spinlock.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/gfp.h>
  30. #include <linux/ip.h>
  31. #include <linux/ipv6.h>
  32. #include <net/sock.h>
  33. #include <net/netlabel.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include "objsec.h"
  37. #include "security.h"
  38. #include "netlabel.h"
  39. /**
  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. u16 family,
  53. struct netlbl_lsm_secattr *secattr,
  54. u32 *sid)
  55. {
  56. int rc;
  57. rc = security_netlbl_secattr_to_sid(&selinux_state, secattr, sid);
  58. if (rc == 0 &&
  59. (secattr->flags & NETLBL_SECATTR_CACHEABLE) &&
  60. (secattr->flags & NETLBL_SECATTR_CACHE))
  61. netlbl_cache_add(skb, family, secattr);
  62. return rc;
  63. }
  64. /**
  65. * selinux_netlbl_sock_genattr - Generate the NetLabel socket secattr
  66. * @sk: the socket
  67. *
  68. * Description:
  69. * Generate the NetLabel security attributes for a socket, making full use of
  70. * the socket's attribute cache. Returns a pointer to the security attributes
  71. * on success, NULL on failure.
  72. *
  73. */
  74. static struct netlbl_lsm_secattr *selinux_netlbl_sock_genattr(struct sock *sk)
  75. {
  76. int rc;
  77. struct sk_security_struct *sksec = sk->sk_security;
  78. struct netlbl_lsm_secattr *secattr;
  79. if (sksec->nlbl_secattr != NULL)
  80. return sksec->nlbl_secattr;
  81. secattr = netlbl_secattr_alloc(GFP_ATOMIC);
  82. if (secattr == NULL)
  83. return NULL;
  84. rc = security_netlbl_sid_to_secattr(&selinux_state, sksec->sid,
  85. secattr);
  86. if (rc != 0) {
  87. netlbl_secattr_free(secattr);
  88. return NULL;
  89. }
  90. sksec->nlbl_secattr = secattr;
  91. return secattr;
  92. }
  93. /**
  94. * selinux_netlbl_sock_getattr - Get the cached NetLabel secattr
  95. * @sk: the socket
  96. * @sid: the SID
  97. *
  98. * Query the socket's cached secattr and if the SID matches the cached value
  99. * return the cache, otherwise return NULL.
  100. *
  101. */
  102. static struct netlbl_lsm_secattr *selinux_netlbl_sock_getattr(
  103. const struct sock *sk,
  104. u32 sid)
  105. {
  106. struct sk_security_struct *sksec = sk->sk_security;
  107. struct netlbl_lsm_secattr *secattr = sksec->nlbl_secattr;
  108. if (secattr == NULL)
  109. return NULL;
  110. if ((secattr->flags & NETLBL_SECATTR_SECID) &&
  111. (secattr->attr.secid == sid))
  112. return secattr;
  113. return NULL;
  114. }
  115. /**
  116. * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
  117. *
  118. * Description:
  119. * Invalidate the NetLabel security attribute mapping cache.
  120. *
  121. */
  122. void selinux_netlbl_cache_invalidate(void)
  123. {
  124. netlbl_cache_invalidate();
  125. }
  126. /**
  127. * selinux_netlbl_err - Handle a NetLabel packet error
  128. * @skb: the packet
  129. * @error: the error code
  130. * @gateway: true if host is acting as a gateway, false otherwise
  131. *
  132. * Description:
  133. * When a packet is dropped due to a call to avc_has_perm() pass the error
  134. * code to the NetLabel subsystem so any protocol specific processing can be
  135. * done. This is safe to call even if you are unsure if NetLabel labeling is
  136. * present on the packet, NetLabel is smart enough to only act when it should.
  137. *
  138. */
  139. void selinux_netlbl_err(struct sk_buff *skb, u16 family, int error, int gateway)
  140. {
  141. netlbl_skbuff_err(skb, family, error, gateway);
  142. }
  143. /**
  144. * selinux_netlbl_sk_security_free - Free the NetLabel fields
  145. * @sksec: the sk_security_struct
  146. *
  147. * Description:
  148. * Free all of the memory in the NetLabel fields of a sk_security_struct.
  149. *
  150. */
  151. void selinux_netlbl_sk_security_free(struct sk_security_struct *sksec)
  152. {
  153. if (sksec->nlbl_secattr != NULL)
  154. netlbl_secattr_free(sksec->nlbl_secattr);
  155. }
  156. /**
  157. * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
  158. * @sksec: the sk_security_struct
  159. * @family: the socket family
  160. *
  161. * Description:
  162. * Called when the NetLabel state of a sk_security_struct needs to be reset.
  163. * The caller is responsible for all the NetLabel sk_security_struct locking.
  164. *
  165. */
  166. void selinux_netlbl_sk_security_reset(struct sk_security_struct *sksec)
  167. {
  168. sksec->nlbl_state = NLBL_UNSET;
  169. }
  170. /**
  171. * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
  172. * @skb: the packet
  173. * @family: protocol family
  174. * @type: NetLabel labeling protocol type
  175. * @sid: the SID
  176. *
  177. * Description:
  178. * Call the NetLabel mechanism to get the security attributes of the given
  179. * packet and use those attributes to determine the correct context/SID to
  180. * assign to the packet. Returns zero on success, negative values on failure.
  181. *
  182. */
  183. int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
  184. u16 family,
  185. u32 *type,
  186. u32 *sid)
  187. {
  188. int rc;
  189. struct netlbl_lsm_secattr secattr;
  190. if (!netlbl_enabled()) {
  191. *sid = SECSID_NULL;
  192. return 0;
  193. }
  194. netlbl_secattr_init(&secattr);
  195. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  196. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  197. rc = selinux_netlbl_sidlookup_cached(skb, family,
  198. &secattr, sid);
  199. else
  200. *sid = SECSID_NULL;
  201. *type = secattr.type;
  202. netlbl_secattr_destroy(&secattr);
  203. return rc;
  204. }
  205. /**
  206. * selinux_netlbl_skbuff_setsid - Set the NetLabel on a packet given a sid
  207. * @skb: the packet
  208. * @family: protocol family
  209. * @sid: the SID
  210. *
  211. * Description
  212. * Call the NetLabel mechanism to set the label of a packet using @sid.
  213. * Returns zero on success, negative values on failure.
  214. *
  215. */
  216. int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,
  217. u16 family,
  218. u32 sid)
  219. {
  220. int rc;
  221. struct netlbl_lsm_secattr secattr_storage;
  222. struct netlbl_lsm_secattr *secattr = NULL;
  223. struct sock *sk;
  224. /* if this is a locally generated packet check to see if it is already
  225. * being labeled by it's parent socket, if it is just exit */
  226. sk = skb_to_full_sk(skb);
  227. if (sk != NULL) {
  228. struct sk_security_struct *sksec = sk->sk_security;
  229. if (sksec->nlbl_state != NLBL_REQSKB)
  230. return 0;
  231. secattr = selinux_netlbl_sock_getattr(sk, sid);
  232. }
  233. if (secattr == NULL) {
  234. secattr = &secattr_storage;
  235. netlbl_secattr_init(secattr);
  236. rc = security_netlbl_sid_to_secattr(&selinux_state, sid,
  237. secattr);
  238. if (rc != 0)
  239. goto skbuff_setsid_return;
  240. }
  241. rc = netlbl_skbuff_setattr(skb, family, secattr);
  242. skbuff_setsid_return:
  243. if (secattr == &secattr_storage)
  244. netlbl_secattr_destroy(secattr);
  245. return rc;
  246. }
  247. /**
  248. * selinux_netlbl_sctp_assoc_request - Label an incoming sctp association.
  249. * @ep: incoming association endpoint.
  250. * @skb: the packet.
  251. *
  252. * Description:
  253. * A new incoming connection is represented by @ep, ......
  254. * Returns zero on success, negative values on failure.
  255. *
  256. */
  257. int selinux_netlbl_sctp_assoc_request(struct sctp_endpoint *ep,
  258. struct sk_buff *skb)
  259. {
  260. int rc;
  261. struct netlbl_lsm_secattr secattr;
  262. struct sk_security_struct *sksec = ep->base.sk->sk_security;
  263. struct sockaddr_in addr4;
  264. struct sockaddr_in6 addr6;
  265. if (ep->base.sk->sk_family != PF_INET &&
  266. ep->base.sk->sk_family != PF_INET6)
  267. return 0;
  268. netlbl_secattr_init(&secattr);
  269. rc = security_netlbl_sid_to_secattr(&selinux_state,
  270. ep->secid, &secattr);
  271. if (rc != 0)
  272. goto assoc_request_return;
  273. /* Move skb hdr address info to a struct sockaddr and then call
  274. * netlbl_conn_setattr().
  275. */
  276. if (ip_hdr(skb)->version == 4) {
  277. addr4.sin_family = AF_INET;
  278. addr4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  279. rc = netlbl_conn_setattr(ep->base.sk, (void *)&addr4, &secattr);
  280. } else if (IS_ENABLED(CONFIG_IPV6) && ip_hdr(skb)->version == 6) {
  281. addr6.sin6_family = AF_INET6;
  282. addr6.sin6_addr = ipv6_hdr(skb)->saddr;
  283. rc = netlbl_conn_setattr(ep->base.sk, (void *)&addr6, &secattr);
  284. } else {
  285. rc = -EAFNOSUPPORT;
  286. }
  287. if (rc == 0)
  288. sksec->nlbl_state = NLBL_LABELED;
  289. assoc_request_return:
  290. netlbl_secattr_destroy(&secattr);
  291. return rc;
  292. }
  293. /**
  294. * selinux_netlbl_inet_conn_request - Label an incoming stream connection
  295. * @req: incoming connection request socket
  296. *
  297. * Description:
  298. * A new incoming connection request is represented by @req, we need to label
  299. * the new request_sock here and the stack will ensure the on-the-wire label
  300. * will get preserved when a full sock is created once the connection handshake
  301. * is complete. Returns zero on success, negative values on failure.
  302. *
  303. */
  304. int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)
  305. {
  306. int rc;
  307. struct netlbl_lsm_secattr secattr;
  308. if (family != PF_INET && family != PF_INET6)
  309. return 0;
  310. netlbl_secattr_init(&secattr);
  311. rc = security_netlbl_sid_to_secattr(&selinux_state, req->secid,
  312. &secattr);
  313. if (rc != 0)
  314. goto inet_conn_request_return;
  315. rc = netlbl_req_setattr(req, &secattr);
  316. inet_conn_request_return:
  317. netlbl_secattr_destroy(&secattr);
  318. return rc;
  319. }
  320. /**
  321. * selinux_netlbl_inet_csk_clone - Initialize the newly created sock
  322. * @sk: the new sock
  323. *
  324. * Description:
  325. * A new connection has been established using @sk, we've already labeled the
  326. * socket via the request_sock struct in selinux_netlbl_inet_conn_request() but
  327. * we need to set the NetLabel state here since we now have a sock structure.
  328. *
  329. */
  330. void selinux_netlbl_inet_csk_clone(struct sock *sk, u16 family)
  331. {
  332. struct sk_security_struct *sksec = sk->sk_security;
  333. if (family == PF_INET)
  334. sksec->nlbl_state = NLBL_LABELED;
  335. else
  336. sksec->nlbl_state = NLBL_UNSET;
  337. }
  338. /**
  339. * selinux_netlbl_sctp_sk_clone - Copy state to the newly created sock
  340. * @sk: current sock
  341. * @newsk: the new sock
  342. *
  343. * Description:
  344. * Called whenever a new socket is created by accept(2) or sctp_peeloff(3).
  345. */
  346. void selinux_netlbl_sctp_sk_clone(struct sock *sk, struct sock *newsk)
  347. {
  348. struct sk_security_struct *sksec = sk->sk_security;
  349. struct sk_security_struct *newsksec = newsk->sk_security;
  350. newsksec->nlbl_state = sksec->nlbl_state;
  351. }
  352. /**
  353. * selinux_netlbl_socket_post_create - Label a socket using NetLabel
  354. * @sock: the socket to label
  355. * @family: protocol family
  356. *
  357. * Description:
  358. * Attempt to label a socket using the NetLabel mechanism using the given
  359. * SID. Returns zero values on success, negative values on failure.
  360. *
  361. */
  362. int selinux_netlbl_socket_post_create(struct sock *sk, u16 family)
  363. {
  364. int rc;
  365. struct sk_security_struct *sksec = sk->sk_security;
  366. struct netlbl_lsm_secattr *secattr;
  367. if (family != PF_INET && family != PF_INET6)
  368. return 0;
  369. secattr = selinux_netlbl_sock_genattr(sk);
  370. if (secattr == NULL)
  371. return -ENOMEM;
  372. rc = netlbl_sock_setattr(sk, family, secattr);
  373. switch (rc) {
  374. case 0:
  375. sksec->nlbl_state = NLBL_LABELED;
  376. break;
  377. case -EDESTADDRREQ:
  378. sksec->nlbl_state = NLBL_REQSKB;
  379. rc = 0;
  380. break;
  381. }
  382. return rc;
  383. }
  384. /**
  385. * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
  386. * @sksec: the sock's sk_security_struct
  387. * @skb: the packet
  388. * @family: protocol family
  389. * @ad: the audit data
  390. *
  391. * Description:
  392. * Fetch the NetLabel security attributes from @skb and perform an access check
  393. * against the receiving socket. Returns zero on success, negative values on
  394. * error.
  395. *
  396. */
  397. int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
  398. struct sk_buff *skb,
  399. u16 family,
  400. struct common_audit_data *ad)
  401. {
  402. int rc;
  403. u32 nlbl_sid;
  404. u32 perm;
  405. struct netlbl_lsm_secattr secattr;
  406. if (!netlbl_enabled())
  407. return 0;
  408. netlbl_secattr_init(&secattr);
  409. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  410. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  411. rc = selinux_netlbl_sidlookup_cached(skb, family,
  412. &secattr, &nlbl_sid);
  413. else
  414. nlbl_sid = SECINITSID_UNLABELED;
  415. netlbl_secattr_destroy(&secattr);
  416. if (rc != 0)
  417. return rc;
  418. switch (sksec->sclass) {
  419. case SECCLASS_UDP_SOCKET:
  420. perm = UDP_SOCKET__RECVFROM;
  421. break;
  422. case SECCLASS_TCP_SOCKET:
  423. perm = TCP_SOCKET__RECVFROM;
  424. break;
  425. default:
  426. perm = RAWIP_SOCKET__RECVFROM;
  427. }
  428. rc = avc_has_perm(&selinux_state,
  429. sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
  430. if (rc == 0)
  431. return 0;
  432. if (nlbl_sid != SECINITSID_UNLABELED)
  433. netlbl_skbuff_err(skb, family, rc, 0);
  434. return rc;
  435. }
  436. /**
  437. * selinux_netlbl_option - Is this a NetLabel option
  438. * @level: the socket level or protocol
  439. * @optname: the socket option name
  440. *
  441. * Description:
  442. * Returns true if @level and @optname refer to a NetLabel option.
  443. * Helper for selinux_netlbl_socket_setsockopt().
  444. */
  445. static inline int selinux_netlbl_option(int level, int optname)
  446. {
  447. return (level == IPPROTO_IP && optname == IP_OPTIONS) ||
  448. (level == IPPROTO_IPV6 && optname == IPV6_HOPOPTS);
  449. }
  450. /**
  451. * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
  452. * @sock: the socket
  453. * @level: the socket level or protocol
  454. * @optname: the socket option name
  455. *
  456. * Description:
  457. * Check the setsockopt() call and if the user is trying to replace the IP
  458. * options on a socket and a NetLabel is in place for the socket deny the
  459. * access; otherwise allow the access. Returns zero when the access is
  460. * allowed, -EACCES when denied, and other negative values on error.
  461. *
  462. */
  463. int selinux_netlbl_socket_setsockopt(struct socket *sock,
  464. int level,
  465. int optname)
  466. {
  467. int rc = 0;
  468. struct sock *sk = sock->sk;
  469. struct sk_security_struct *sksec = sk->sk_security;
  470. struct netlbl_lsm_secattr secattr;
  471. if (selinux_netlbl_option(level, optname) &&
  472. (sksec->nlbl_state == NLBL_LABELED ||
  473. sksec->nlbl_state == NLBL_CONNLABELED)) {
  474. netlbl_secattr_init(&secattr);
  475. lock_sock(sk);
  476. /* call the netlabel function directly as we want to see the
  477. * on-the-wire label that is assigned via the socket's options
  478. * and not the cached netlabel/lsm attributes */
  479. rc = netlbl_sock_getattr(sk, &secattr);
  480. release_sock(sk);
  481. if (rc == 0)
  482. rc = -EACCES;
  483. else if (rc == -ENOMSG)
  484. rc = 0;
  485. netlbl_secattr_destroy(&secattr);
  486. }
  487. return rc;
  488. }
  489. /**
  490. * selinux_netlbl_socket_connect_helper - Help label a client-side socket on
  491. * connect
  492. * @sk: the socket to label
  493. * @addr: the destination address
  494. *
  495. * Description:
  496. * Attempt to label a connected socket with NetLabel using the given address.
  497. * Returns zero values on success, negative values on failure.
  498. *
  499. */
  500. static int selinux_netlbl_socket_connect_helper(struct sock *sk,
  501. struct sockaddr *addr)
  502. {
  503. int rc;
  504. struct sk_security_struct *sksec = sk->sk_security;
  505. struct netlbl_lsm_secattr *secattr;
  506. /* connected sockets are allowed to disconnect when the address family
  507. * is set to AF_UNSPEC, if that is what is happening we want to reset
  508. * the socket */
  509. if (addr->sa_family == AF_UNSPEC) {
  510. netlbl_sock_delattr(sk);
  511. sksec->nlbl_state = NLBL_REQSKB;
  512. rc = 0;
  513. return rc;
  514. }
  515. secattr = selinux_netlbl_sock_genattr(sk);
  516. if (secattr == NULL) {
  517. rc = -ENOMEM;
  518. return rc;
  519. }
  520. rc = netlbl_conn_setattr(sk, addr, secattr);
  521. if (rc == 0)
  522. sksec->nlbl_state = NLBL_CONNLABELED;
  523. return rc;
  524. }
  525. /**
  526. * selinux_netlbl_socket_connect_locked - Label a client-side socket on
  527. * connect
  528. * @sk: the socket to label
  529. * @addr: the destination address
  530. *
  531. * Description:
  532. * Attempt to label a connected socket that already has the socket locked
  533. * with NetLabel using the given address.
  534. * Returns zero values on success, negative values on failure.
  535. *
  536. */
  537. int selinux_netlbl_socket_connect_locked(struct sock *sk,
  538. struct sockaddr *addr)
  539. {
  540. struct sk_security_struct *sksec = sk->sk_security;
  541. if (sksec->nlbl_state != NLBL_REQSKB &&
  542. sksec->nlbl_state != NLBL_CONNLABELED)
  543. return 0;
  544. return selinux_netlbl_socket_connect_helper(sk, addr);
  545. }
  546. /**
  547. * selinux_netlbl_socket_connect - Label a client-side socket on connect
  548. * @sk: the socket to label
  549. * @addr: the destination address
  550. *
  551. * Description:
  552. * Attempt to label a connected socket with NetLabel using the given address.
  553. * Returns zero values on success, negative values on failure.
  554. *
  555. */
  556. int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr)
  557. {
  558. int rc;
  559. lock_sock(sk);
  560. rc = selinux_netlbl_socket_connect_locked(sk, addr);
  561. release_sock(sk);
  562. return rc;
  563. }