netlabel.c 13 KB

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