netlabel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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, 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/gfp.h>
  31. #include <linux/ip.h>
  32. #include <linux/ipv6.h>
  33. #include <net/sock.h>
  34. #include <net/netlabel.h>
  35. #include <net/ip.h>
  36. #include <net/ipv6.h>
  37. #include "objsec.h"
  38. #include "security.h"
  39. #include "netlabel.h"
  40. /**
  41. * selinux_netlbl_sidlookup_cached - Cache a SID lookup
  42. * @skb: the packet
  43. * @secattr: the NetLabel security attributes
  44. * @sid: the SID
  45. *
  46. * Description:
  47. * Query the SELinux security server to lookup the correct SID for the given
  48. * security attributes. If the query is successful, cache the result to speed
  49. * up future lookups. Returns zero on success, negative values on failure.
  50. *
  51. */
  52. static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb,
  53. u16 family,
  54. struct netlbl_lsm_secattr *secattr,
  55. u32 *sid)
  56. {
  57. int rc;
  58. rc = security_netlbl_secattr_to_sid(&selinux_state, secattr, sid);
  59. if (rc == 0 &&
  60. (secattr->flags & NETLBL_SECATTR_CACHEABLE) &&
  61. (secattr->flags & NETLBL_SECATTR_CACHE))
  62. netlbl_cache_add(skb, family, secattr);
  63. return rc;
  64. }
  65. /**
  66. * selinux_netlbl_sock_genattr - Generate the NetLabel socket secattr
  67. * @sk: the socket
  68. *
  69. * Description:
  70. * Generate the NetLabel security attributes for a socket, making full use of
  71. * the socket's attribute cache. Returns a pointer to the security attributes
  72. * on success, NULL on failure.
  73. *
  74. */
  75. static struct netlbl_lsm_secattr *selinux_netlbl_sock_genattr(struct sock *sk)
  76. {
  77. int rc;
  78. struct sk_security_struct *sksec = sk->sk_security;
  79. struct netlbl_lsm_secattr *secattr;
  80. if (sksec->nlbl_secattr != NULL)
  81. return sksec->nlbl_secattr;
  82. secattr = netlbl_secattr_alloc(GFP_ATOMIC);
  83. if (secattr == NULL)
  84. return NULL;
  85. rc = security_netlbl_sid_to_secattr(&selinux_state, sksec->sid,
  86. secattr);
  87. if (rc != 0) {
  88. netlbl_secattr_free(secattr);
  89. return NULL;
  90. }
  91. sksec->nlbl_secattr = secattr;
  92. return secattr;
  93. }
  94. /**
  95. * selinux_netlbl_sock_getattr - Get the cached NetLabel secattr
  96. * @sk: the socket
  97. * @sid: the SID
  98. *
  99. * Query the socket's cached secattr and if the SID matches the cached value
  100. * return the cache, otherwise return NULL.
  101. *
  102. */
  103. static struct netlbl_lsm_secattr *selinux_netlbl_sock_getattr(
  104. const struct sock *sk,
  105. u32 sid)
  106. {
  107. const struct sk_security_struct *sksec = sk->sk_security;
  108. struct netlbl_lsm_secattr *secattr = sksec->nlbl_secattr;
  109. if (secattr == NULL)
  110. return NULL;
  111. if ((secattr->flags & NETLBL_SECATTR_SECID) &&
  112. (secattr->attr.secid == sid))
  113. return secattr;
  114. return NULL;
  115. }
  116. /**
  117. * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
  118. *
  119. * Description:
  120. * Invalidate the NetLabel security attribute mapping cache.
  121. *
  122. */
  123. void selinux_netlbl_cache_invalidate(void)
  124. {
  125. netlbl_cache_invalidate();
  126. }
  127. /**
  128. * selinux_netlbl_err - Handle a NetLabel packet error
  129. * @skb: the packet
  130. * @error: the error code
  131. * @gateway: true if host is acting as a gateway, false otherwise
  132. *
  133. * Description:
  134. * When a packet is dropped due to a call to avc_has_perm() pass the error
  135. * code to the NetLabel subsystem so any protocol specific processing can be
  136. * done. This is safe to call even if you are unsure if NetLabel labeling is
  137. * present on the packet, NetLabel is smart enough to only act when it should.
  138. *
  139. */
  140. void selinux_netlbl_err(struct sk_buff *skb, u16 family, int error, int gateway)
  141. {
  142. netlbl_skbuff_err(skb, family, error, gateway);
  143. }
  144. /**
  145. * selinux_netlbl_sk_security_free - Free the NetLabel fields
  146. * @sksec: the sk_security_struct
  147. *
  148. * Description:
  149. * Free all of the memory in the NetLabel fields of a sk_security_struct.
  150. *
  151. */
  152. void selinux_netlbl_sk_security_free(struct sk_security_struct *sksec)
  153. {
  154. if (sksec->nlbl_secattr != NULL)
  155. netlbl_secattr_free(sksec->nlbl_secattr);
  156. }
  157. /**
  158. * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
  159. * @sksec: the sk_security_struct
  160. * @family: the socket family
  161. *
  162. * Description:
  163. * Called when the NetLabel state of a sk_security_struct needs to be reset.
  164. * The caller is responsible for all the NetLabel sk_security_struct locking.
  165. *
  166. */
  167. void selinux_netlbl_sk_security_reset(struct sk_security_struct *sksec)
  168. {
  169. sksec->nlbl_state = NLBL_UNSET;
  170. }
  171. /**
  172. * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
  173. * @skb: the packet
  174. * @family: protocol family
  175. * @type: NetLabel labeling protocol type
  176. * @sid: the SID
  177. *
  178. * Description:
  179. * Call the NetLabel mechanism to get the security attributes of the given
  180. * packet and use those attributes to determine the correct context/SID to
  181. * assign to the packet. Returns zero on success, negative values on failure.
  182. *
  183. */
  184. int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
  185. u16 family,
  186. u32 *type,
  187. u32 *sid)
  188. {
  189. int rc;
  190. struct netlbl_lsm_secattr secattr;
  191. if (!netlbl_enabled()) {
  192. *sid = SECSID_NULL;
  193. return 0;
  194. }
  195. netlbl_secattr_init(&secattr);
  196. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  197. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  198. rc = selinux_netlbl_sidlookup_cached(skb, family,
  199. &secattr, sid);
  200. else
  201. *sid = SECSID_NULL;
  202. *type = secattr.type;
  203. netlbl_secattr_destroy(&secattr);
  204. return rc;
  205. }
  206. /**
  207. * selinux_netlbl_skbuff_setsid - Set the NetLabel on a packet given a sid
  208. * @skb: the packet
  209. * @family: protocol family
  210. * @sid: the SID
  211. *
  212. * Description
  213. * Call the NetLabel mechanism to set the label of a packet using @sid.
  214. * Returns zero on success, negative values on failure.
  215. *
  216. */
  217. int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,
  218. u16 family,
  219. u32 sid)
  220. {
  221. int rc;
  222. struct netlbl_lsm_secattr secattr_storage;
  223. struct netlbl_lsm_secattr *secattr = NULL;
  224. struct sock *sk;
  225. /* if this is a locally generated packet check to see if it is already
  226. * being labeled by it's parent socket, if it is just exit */
  227. sk = skb_to_full_sk(skb);
  228. if (sk != NULL) {
  229. struct sk_security_struct *sksec = sk->sk_security;
  230. if (sksec->nlbl_state != NLBL_REQSKB)
  231. return 0;
  232. secattr = selinux_netlbl_sock_getattr(sk, sid);
  233. }
  234. if (secattr == NULL) {
  235. secattr = &secattr_storage;
  236. netlbl_secattr_init(secattr);
  237. rc = security_netlbl_sid_to_secattr(&selinux_state, sid,
  238. secattr);
  239. if (rc != 0)
  240. goto skbuff_setsid_return;
  241. }
  242. rc = netlbl_skbuff_setattr(skb, family, secattr);
  243. skbuff_setsid_return:
  244. if (secattr == &secattr_storage)
  245. netlbl_secattr_destroy(secattr);
  246. return rc;
  247. }
  248. /**
  249. * selinux_netlbl_inet_conn_request - Label an incoming stream connection
  250. * @req: incoming connection request socket
  251. *
  252. * Description:
  253. * A new incoming connection request is represented by @req, we need to label
  254. * the new request_sock here and the stack will ensure the on-the-wire label
  255. * will get preserved when a full sock is created once the connection handshake
  256. * is complete. Returns zero on success, negative values on failure.
  257. *
  258. */
  259. int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)
  260. {
  261. int rc;
  262. struct netlbl_lsm_secattr secattr;
  263. if (family != PF_INET && family != PF_INET6)
  264. return 0;
  265. netlbl_secattr_init(&secattr);
  266. rc = security_netlbl_sid_to_secattr(&selinux_state, req->secid,
  267. &secattr);
  268. if (rc != 0)
  269. goto inet_conn_request_return;
  270. rc = netlbl_req_setattr(req, &secattr);
  271. inet_conn_request_return:
  272. netlbl_secattr_destroy(&secattr);
  273. return rc;
  274. }
  275. /**
  276. * selinux_netlbl_inet_csk_clone - Initialize the newly created sock
  277. * @sk: the new sock
  278. *
  279. * Description:
  280. * A new connection has been established using @sk, we've already labeled the
  281. * socket via the request_sock struct in selinux_netlbl_inet_conn_request() but
  282. * we need to set the NetLabel state here since we now have a sock structure.
  283. *
  284. */
  285. void selinux_netlbl_inet_csk_clone(struct sock *sk, u16 family)
  286. {
  287. struct sk_security_struct *sksec = sk->sk_security;
  288. if (family == PF_INET)
  289. sksec->nlbl_state = NLBL_LABELED;
  290. else
  291. sksec->nlbl_state = NLBL_UNSET;
  292. }
  293. /**
  294. * selinux_netlbl_socket_post_create - Label a socket using NetLabel
  295. * @sock: the socket to label
  296. * @family: protocol family
  297. *
  298. * Description:
  299. * Attempt to label a socket using the NetLabel mechanism using the given
  300. * SID. Returns zero values on success, negative values on failure.
  301. *
  302. */
  303. int selinux_netlbl_socket_post_create(struct sock *sk, u16 family)
  304. {
  305. int rc;
  306. struct sk_security_struct *sksec = sk->sk_security;
  307. struct netlbl_lsm_secattr *secattr;
  308. if (family != PF_INET && family != PF_INET6)
  309. return 0;
  310. secattr = selinux_netlbl_sock_genattr(sk);
  311. if (secattr == NULL)
  312. return -ENOMEM;
  313. rc = netlbl_sock_setattr(sk, family, secattr);
  314. switch (rc) {
  315. case 0:
  316. sksec->nlbl_state = NLBL_LABELED;
  317. break;
  318. case -EDESTADDRREQ:
  319. sksec->nlbl_state = NLBL_REQSKB;
  320. rc = 0;
  321. break;
  322. }
  323. return rc;
  324. }
  325. /**
  326. * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
  327. * @sksec: the sock's sk_security_struct
  328. * @skb: the packet
  329. * @family: protocol family
  330. * @ad: the audit data
  331. *
  332. * Description:
  333. * Fetch the NetLabel security attributes from @skb and perform an access check
  334. * against the receiving socket. Returns zero on success, negative values on
  335. * error.
  336. *
  337. */
  338. int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
  339. struct sk_buff *skb,
  340. u16 family,
  341. struct common_audit_data *ad)
  342. {
  343. int rc;
  344. u32 nlbl_sid;
  345. u32 perm;
  346. struct netlbl_lsm_secattr secattr;
  347. if (!netlbl_enabled())
  348. return 0;
  349. netlbl_secattr_init(&secattr);
  350. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  351. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  352. rc = selinux_netlbl_sidlookup_cached(skb, family,
  353. &secattr, &nlbl_sid);
  354. else
  355. nlbl_sid = SECINITSID_UNLABELED;
  356. netlbl_secattr_destroy(&secattr);
  357. if (rc != 0)
  358. return rc;
  359. switch (sksec->sclass) {
  360. case SECCLASS_UDP_SOCKET:
  361. perm = UDP_SOCKET__RECVFROM;
  362. break;
  363. case SECCLASS_TCP_SOCKET:
  364. perm = TCP_SOCKET__RECVFROM;
  365. break;
  366. default:
  367. perm = RAWIP_SOCKET__RECVFROM;
  368. }
  369. rc = avc_has_perm(&selinux_state,
  370. sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
  371. if (rc == 0)
  372. return 0;
  373. if (nlbl_sid != SECINITSID_UNLABELED)
  374. netlbl_skbuff_err(skb, family, rc, 0);
  375. return rc;
  376. }
  377. /**
  378. * selinux_netlbl_option - Is this a NetLabel option
  379. * @level: the socket level or protocol
  380. * @optname: the socket option name
  381. *
  382. * Description:
  383. * Returns true if @level and @optname refer to a NetLabel option.
  384. * Helper for selinux_netlbl_socket_setsockopt().
  385. */
  386. static inline int selinux_netlbl_option(int level, int optname)
  387. {
  388. return (level == IPPROTO_IP && optname == IP_OPTIONS) ||
  389. (level == IPPROTO_IPV6 && optname == IPV6_HOPOPTS);
  390. }
  391. /**
  392. * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
  393. * @sock: the socket
  394. * @level: the socket level or protocol
  395. * @optname: the socket option name
  396. *
  397. * Description:
  398. * Check the setsockopt() call and if the user is trying to replace the IP
  399. * options on a socket and a NetLabel is in place for the socket deny the
  400. * access; otherwise allow the access. Returns zero when the access is
  401. * allowed, -EACCES when denied, and other negative values on error.
  402. *
  403. */
  404. int selinux_netlbl_socket_setsockopt(struct socket *sock,
  405. int level,
  406. int optname)
  407. {
  408. int rc = 0;
  409. struct sock *sk = sock->sk;
  410. struct sk_security_struct *sksec = sk->sk_security;
  411. struct netlbl_lsm_secattr secattr;
  412. if (selinux_netlbl_option(level, optname) &&
  413. (sksec->nlbl_state == NLBL_LABELED ||
  414. sksec->nlbl_state == NLBL_CONNLABELED)) {
  415. netlbl_secattr_init(&secattr);
  416. lock_sock(sk);
  417. /* call the netlabel function directly as we want to see the
  418. * on-the-wire label that is assigned via the socket's options
  419. * and not the cached netlabel/lsm attributes */
  420. rc = netlbl_sock_getattr(sk, &secattr);
  421. release_sock(sk);
  422. if (rc == 0)
  423. rc = -EACCES;
  424. else if (rc == -ENOMSG)
  425. rc = 0;
  426. netlbl_secattr_destroy(&secattr);
  427. }
  428. return rc;
  429. }
  430. /**
  431. * selinux_netlbl_socket_connect - Label a client-side socket on connect
  432. * @sk: the socket to label
  433. * @addr: the destination address
  434. *
  435. * Description:
  436. * Attempt to label a connected socket with NetLabel using the given address.
  437. * Returns zero values on success, negative values on failure.
  438. *
  439. */
  440. int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr)
  441. {
  442. int rc;
  443. struct sk_security_struct *sksec = sk->sk_security;
  444. struct netlbl_lsm_secattr *secattr;
  445. if (sksec->nlbl_state != NLBL_REQSKB &&
  446. sksec->nlbl_state != NLBL_CONNLABELED)
  447. return 0;
  448. lock_sock(sk);
  449. /* connected sockets are allowed to disconnect when the address family
  450. * is set to AF_UNSPEC, if that is what is happening we want to reset
  451. * the socket */
  452. if (addr->sa_family == AF_UNSPEC) {
  453. netlbl_sock_delattr(sk);
  454. sksec->nlbl_state = NLBL_REQSKB;
  455. rc = 0;
  456. goto socket_connect_return;
  457. }
  458. secattr = selinux_netlbl_sock_genattr(sk);
  459. if (secattr == NULL) {
  460. rc = -ENOMEM;
  461. goto socket_connect_return;
  462. }
  463. rc = netlbl_conn_setattr(sk, addr, secattr);
  464. if (rc == 0)
  465. sksec->nlbl_state = NLBL_CONNLABELED;
  466. socket_connect_return:
  467. release_sock(sk);
  468. return rc;
  469. }