netlabel.c 14 KB

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