netlabel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_cache_invalidate - Invalidate the NetLabel cache
  93. *
  94. * Description:
  95. * Invalidate the NetLabel security attribute mapping cache.
  96. *
  97. */
  98. void selinux_netlbl_cache_invalidate(void)
  99. {
  100. netlbl_cache_invalidate();
  101. }
  102. /**
  103. * selinux_netlbl_err - Handle a NetLabel packet error
  104. * @skb: the packet
  105. * @error: the error code
  106. * @gateway: true if host is acting as a gateway, false otherwise
  107. *
  108. * Description:
  109. * When a packet is dropped due to a call to avc_has_perm() pass the error
  110. * code to the NetLabel subsystem so any protocol specific processing can be
  111. * done. This is safe to call even if you are unsure if NetLabel labeling is
  112. * present on the packet, NetLabel is smart enough to only act when it should.
  113. *
  114. */
  115. void selinux_netlbl_err(struct sk_buff *skb, int error, int gateway)
  116. {
  117. netlbl_skbuff_err(skb, error, gateway);
  118. }
  119. /**
  120. * selinux_netlbl_sk_security_free - Free the NetLabel fields
  121. * @sssec: the sk_security_struct
  122. *
  123. * Description:
  124. * Free all of the memory in the NetLabel fields of a sk_security_struct.
  125. *
  126. */
  127. void selinux_netlbl_sk_security_free(struct sk_security_struct *ssec)
  128. {
  129. if (ssec->nlbl_secattr != NULL)
  130. netlbl_secattr_free(ssec->nlbl_secattr);
  131. }
  132. /**
  133. * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
  134. * @ssec: the sk_security_struct
  135. * @family: the socket family
  136. *
  137. * Description:
  138. * Called when the NetLabel state of a sk_security_struct needs to be reset.
  139. * The caller is responsibile for all the NetLabel sk_security_struct locking.
  140. *
  141. */
  142. void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec)
  143. {
  144. ssec->nlbl_state = NLBL_UNSET;
  145. }
  146. /**
  147. * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
  148. * @skb: the packet
  149. * @family: protocol family
  150. * @type: NetLabel labeling protocol type
  151. * @sid: the SID
  152. *
  153. * Description:
  154. * Call the NetLabel mechanism to get the security attributes of the given
  155. * packet and use those attributes to determine the correct context/SID to
  156. * assign to the packet. Returns zero on success, negative values on failure.
  157. *
  158. */
  159. int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
  160. u16 family,
  161. u32 *type,
  162. u32 *sid)
  163. {
  164. int rc;
  165. struct netlbl_lsm_secattr secattr;
  166. if (!netlbl_enabled()) {
  167. *sid = SECSID_NULL;
  168. return 0;
  169. }
  170. netlbl_secattr_init(&secattr);
  171. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  172. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  173. rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid);
  174. else
  175. *sid = SECSID_NULL;
  176. *type = secattr.type;
  177. netlbl_secattr_destroy(&secattr);
  178. return rc;
  179. }
  180. /**
  181. * selinux_netlbl_skbuff_setsid - Set the NetLabel on a packet given a sid
  182. * @skb: the packet
  183. * @family: protocol family
  184. * @sid: the SID
  185. *
  186. * Description
  187. * Call the NetLabel mechanism to set the label of a packet using @sid.
  188. * Returns zero on auccess, negative values on failure.
  189. *
  190. */
  191. int selinux_netlbl_skbuff_setsid(struct sk_buff *skb,
  192. u16 family,
  193. u32 sid)
  194. {
  195. int rc;
  196. struct netlbl_lsm_secattr secattr_storage;
  197. struct netlbl_lsm_secattr *secattr = NULL;
  198. struct sock *sk;
  199. /* if this is a locally generated packet check to see if it is already
  200. * being labeled by it's parent socket, if it is just exit */
  201. sk = skb->sk;
  202. if (sk != NULL) {
  203. struct sk_security_struct *sksec = sk->sk_security;
  204. if (sksec->nlbl_state != NLBL_REQSKB)
  205. return 0;
  206. secattr = sksec->nlbl_secattr;
  207. }
  208. if (secattr == NULL) {
  209. secattr = &secattr_storage;
  210. netlbl_secattr_init(secattr);
  211. rc = security_netlbl_sid_to_secattr(sid, secattr);
  212. if (rc != 0)
  213. goto skbuff_setsid_return;
  214. }
  215. rc = netlbl_skbuff_setattr(skb, family, secattr);
  216. skbuff_setsid_return:
  217. if (secattr == &secattr_storage)
  218. netlbl_secattr_destroy(secattr);
  219. return rc;
  220. }
  221. /**
  222. * selinux_netlbl_inet_conn_request - Label an incoming stream connection
  223. * @req: incoming connection request socket
  224. *
  225. * Description:
  226. * A new incoming connection request is represented by @req, we need to label
  227. * the new request_sock here and the stack will ensure the on-the-wire label
  228. * will get preserved when a full sock is created once the connection handshake
  229. * is complete. Returns zero on success, negative values on failure.
  230. *
  231. */
  232. int selinux_netlbl_inet_conn_request(struct request_sock *req, u16 family)
  233. {
  234. int rc;
  235. struct netlbl_lsm_secattr secattr;
  236. if (family != PF_INET)
  237. return 0;
  238. netlbl_secattr_init(&secattr);
  239. rc = security_netlbl_sid_to_secattr(req->secid, &secattr);
  240. if (rc != 0)
  241. goto inet_conn_request_return;
  242. rc = netlbl_req_setattr(req, &secattr);
  243. inet_conn_request_return:
  244. netlbl_secattr_destroy(&secattr);
  245. return rc;
  246. }
  247. /**
  248. * selinux_netlbl_inet_csk_clone - Initialize the newly created sock
  249. * @sk: the new sock
  250. *
  251. * Description:
  252. * A new connection has been established using @sk, we've already labeled the
  253. * socket via the request_sock struct in selinux_netlbl_inet_conn_request() but
  254. * we need to set the NetLabel state here since we now have a sock structure.
  255. *
  256. */
  257. void selinux_netlbl_inet_csk_clone(struct sock *sk, u16 family)
  258. {
  259. struct sk_security_struct *sksec = sk->sk_security;
  260. if (family == PF_INET)
  261. sksec->nlbl_state = NLBL_LABELED;
  262. else
  263. sksec->nlbl_state = NLBL_UNSET;
  264. }
  265. /**
  266. * selinux_netlbl_socket_post_create - Label a socket using NetLabel
  267. * @sock: the socket to label
  268. * @family: protocol family
  269. *
  270. * Description:
  271. * Attempt to label a socket using the NetLabel mechanism using the given
  272. * SID. Returns zero values on success, negative values on failure.
  273. *
  274. */
  275. int selinux_netlbl_socket_post_create(struct sock *sk, u16 family)
  276. {
  277. int rc;
  278. struct sk_security_struct *sksec = sk->sk_security;
  279. struct netlbl_lsm_secattr *secattr;
  280. if (family != PF_INET)
  281. return 0;
  282. secattr = selinux_netlbl_sock_genattr(sk);
  283. if (secattr == NULL)
  284. return -ENOMEM;
  285. rc = netlbl_sock_setattr(sk, family, secattr);
  286. switch (rc) {
  287. case 0:
  288. sksec->nlbl_state = NLBL_LABELED;
  289. break;
  290. case -EDESTADDRREQ:
  291. sksec->nlbl_state = NLBL_REQSKB;
  292. rc = 0;
  293. break;
  294. }
  295. return rc;
  296. }
  297. /**
  298. * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
  299. * @sksec: the sock's sk_security_struct
  300. * @skb: the packet
  301. * @family: protocol family
  302. * @ad: the audit data
  303. *
  304. * Description:
  305. * Fetch the NetLabel security attributes from @skb and perform an access check
  306. * against the receiving socket. Returns zero on success, negative values on
  307. * error.
  308. *
  309. */
  310. int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
  311. struct sk_buff *skb,
  312. u16 family,
  313. struct avc_audit_data *ad)
  314. {
  315. int rc;
  316. u32 nlbl_sid;
  317. u32 perm;
  318. struct netlbl_lsm_secattr secattr;
  319. if (!netlbl_enabled())
  320. return 0;
  321. netlbl_secattr_init(&secattr);
  322. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  323. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  324. rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid);
  325. else
  326. nlbl_sid = SECINITSID_UNLABELED;
  327. netlbl_secattr_destroy(&secattr);
  328. if (rc != 0)
  329. return rc;
  330. switch (sksec->sclass) {
  331. case SECCLASS_UDP_SOCKET:
  332. perm = UDP_SOCKET__RECVFROM;
  333. break;
  334. case SECCLASS_TCP_SOCKET:
  335. perm = TCP_SOCKET__RECVFROM;
  336. break;
  337. default:
  338. perm = RAWIP_SOCKET__RECVFROM;
  339. }
  340. rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
  341. if (rc == 0)
  342. return 0;
  343. if (nlbl_sid != SECINITSID_UNLABELED)
  344. netlbl_skbuff_err(skb, rc, 0);
  345. return rc;
  346. }
  347. /**
  348. * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
  349. * @sock: the socket
  350. * @level: the socket level or protocol
  351. * @optname: the socket option name
  352. *
  353. * Description:
  354. * Check the setsockopt() call and if the user is trying to replace the IP
  355. * options on a socket and a NetLabel is in place for the socket deny the
  356. * access; otherwise allow the access. Returns zero when the access is
  357. * allowed, -EACCES when denied, and other negative values on error.
  358. *
  359. */
  360. int selinux_netlbl_socket_setsockopt(struct socket *sock,
  361. int level,
  362. int optname)
  363. {
  364. int rc = 0;
  365. struct sock *sk = sock->sk;
  366. struct sk_security_struct *sksec = sk->sk_security;
  367. struct netlbl_lsm_secattr secattr;
  368. if (level == IPPROTO_IP && optname == IP_OPTIONS &&
  369. (sksec->nlbl_state == NLBL_LABELED ||
  370. sksec->nlbl_state == NLBL_CONNLABELED)) {
  371. netlbl_secattr_init(&secattr);
  372. lock_sock(sk);
  373. rc = netlbl_sock_getattr(sk, &secattr);
  374. release_sock(sk);
  375. if (rc == 0)
  376. rc = -EACCES;
  377. else if (rc == -ENOMSG)
  378. rc = 0;
  379. netlbl_secattr_destroy(&secattr);
  380. }
  381. return rc;
  382. }
  383. /**
  384. * selinux_netlbl_socket_connect - Label a client-side socket on connect
  385. * @sk: the socket to label
  386. * @addr: the destination address
  387. *
  388. * Description:
  389. * Attempt to label a connected socket with NetLabel using the given address.
  390. * Returns zero values on success, negative values on failure.
  391. *
  392. */
  393. int selinux_netlbl_socket_connect(struct sock *sk, struct sockaddr *addr)
  394. {
  395. int rc;
  396. struct sk_security_struct *sksec = sk->sk_security;
  397. struct netlbl_lsm_secattr *secattr;
  398. if (sksec->nlbl_state != NLBL_REQSKB &&
  399. sksec->nlbl_state != NLBL_CONNLABELED)
  400. return 0;
  401. local_bh_disable();
  402. bh_lock_sock_nested(sk);
  403. /* connected sockets are allowed to disconnect when the address family
  404. * is set to AF_UNSPEC, if that is what is happening we want to reset
  405. * the socket */
  406. if (addr->sa_family == AF_UNSPEC) {
  407. netlbl_sock_delattr(sk);
  408. sksec->nlbl_state = NLBL_REQSKB;
  409. rc = 0;
  410. goto socket_connect_return;
  411. }
  412. secattr = selinux_netlbl_sock_genattr(sk);
  413. if (secattr == NULL) {
  414. rc = -ENOMEM;
  415. goto socket_connect_return;
  416. }
  417. rc = netlbl_conn_setattr(sk, addr, secattr);
  418. if (rc == 0)
  419. sksec->nlbl_state = NLBL_CONNLABELED;
  420. socket_connect_return:
  421. bh_unlock_sock(sk);
  422. local_bh_enable();
  423. return rc;
  424. }