netlabel.c 16 KB

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