netlabel.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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
  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 <net/sock.h>
  31. #include <net/netlabel.h>
  32. #include "objsec.h"
  33. #include "security.h"
  34. #include "netlabel.h"
  35. /**
  36. * selinux_netlbl_sidlookup_cached - Cache a SID lookup
  37. * @skb: the packet
  38. * @secattr: the NetLabel security attributes
  39. * @sid: the SID
  40. *
  41. * Description:
  42. * Query the SELinux security server to lookup the correct SID for the given
  43. * security attributes. If the query is successful, cache the result to speed
  44. * up future lookups. Returns zero on success, negative values on failure.
  45. *
  46. */
  47. static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb,
  48. struct netlbl_lsm_secattr *secattr,
  49. u32 *sid)
  50. {
  51. int rc;
  52. rc = security_netlbl_secattr_to_sid(secattr, sid);
  53. if (rc == 0 &&
  54. (secattr->flags & NETLBL_SECATTR_CACHEABLE) &&
  55. (secattr->flags & NETLBL_SECATTR_CACHE))
  56. netlbl_cache_add(skb, secattr);
  57. return rc;
  58. }
  59. /**
  60. * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism
  61. * @sk: the socket to label
  62. * @sid: the SID to use
  63. *
  64. * Description:
  65. * Attempt to label a socket using the NetLabel mechanism using the given
  66. * SID. Returns zero values on success, negative values on failure.
  67. *
  68. */
  69. static int selinux_netlbl_sock_setsid(struct sock *sk, u32 sid)
  70. {
  71. int rc;
  72. struct sk_security_struct *sksec = sk->sk_security;
  73. struct netlbl_lsm_secattr secattr;
  74. netlbl_secattr_init(&secattr);
  75. rc = security_netlbl_sid_to_secattr(sid, &secattr);
  76. if (rc != 0)
  77. goto sock_setsid_return;
  78. rc = netlbl_sock_setattr(sk, &secattr);
  79. if (rc == 0)
  80. sksec->nlbl_state = NLBL_LABELED;
  81. sock_setsid_return:
  82. netlbl_secattr_destroy(&secattr);
  83. return rc;
  84. }
  85. /**
  86. * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
  87. *
  88. * Description:
  89. * Invalidate the NetLabel security attribute mapping cache.
  90. *
  91. */
  92. void selinux_netlbl_cache_invalidate(void)
  93. {
  94. netlbl_cache_invalidate();
  95. }
  96. /**
  97. * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
  98. * @ssec: the sk_security_struct
  99. * @family: the socket family
  100. *
  101. * Description:
  102. * Called when the NetLabel state of a sk_security_struct needs to be reset.
  103. * The caller is responsibile for all the NetLabel sk_security_struct locking.
  104. *
  105. */
  106. void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
  107. int family)
  108. {
  109. if (family == PF_INET)
  110. ssec->nlbl_state = NLBL_REQUIRE;
  111. else
  112. ssec->nlbl_state = NLBL_UNSET;
  113. }
  114. /**
  115. * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
  116. * @skb: the packet
  117. * @family: protocol family
  118. * @type: NetLabel labeling protocol type
  119. * @sid: the SID
  120. *
  121. * Description:
  122. * Call the NetLabel mechanism to get the security attributes of the given
  123. * packet and use those attributes to determine the correct context/SID to
  124. * assign to the packet. Returns zero on success, negative values on failure.
  125. *
  126. */
  127. int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
  128. u16 family,
  129. u32 *type,
  130. u32 *sid)
  131. {
  132. int rc;
  133. struct netlbl_lsm_secattr secattr;
  134. if (!netlbl_enabled()) {
  135. *sid = SECSID_NULL;
  136. return 0;
  137. }
  138. netlbl_secattr_init(&secattr);
  139. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  140. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  141. rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid);
  142. else
  143. *sid = SECSID_NULL;
  144. *type = secattr.type;
  145. netlbl_secattr_destroy(&secattr);
  146. return rc;
  147. }
  148. /**
  149. * selinux_netlbl_sock_graft - Netlabel the new socket
  150. * @sk: the new connection
  151. * @sock: the new socket
  152. *
  153. * Description:
  154. * The connection represented by @sk is being grafted onto @sock so set the
  155. * socket's NetLabel to match the SID of @sk.
  156. *
  157. */
  158. void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
  159. {
  160. struct sk_security_struct *sksec = sk->sk_security;
  161. struct netlbl_lsm_secattr secattr;
  162. u32 nlbl_peer_sid;
  163. if (sksec->nlbl_state != NLBL_REQUIRE)
  164. return;
  165. netlbl_secattr_init(&secattr);
  166. if (netlbl_sock_getattr(sk, &secattr) == 0 &&
  167. secattr.flags != NETLBL_SECATTR_NONE &&
  168. security_netlbl_secattr_to_sid(&secattr, &nlbl_peer_sid) == 0)
  169. sksec->peer_sid = nlbl_peer_sid;
  170. netlbl_secattr_destroy(&secattr);
  171. /* Try to set the NetLabel on the socket to save time later, if we fail
  172. * here we will pick up the pieces in later calls to
  173. * selinux_netlbl_inode_permission(). */
  174. selinux_netlbl_sock_setsid(sk, sksec->sid);
  175. }
  176. /**
  177. * selinux_netlbl_socket_post_create - Label a socket using NetLabel
  178. * @sock: the socket to label
  179. *
  180. * Description:
  181. * Attempt to label a socket using the NetLabel mechanism using the given
  182. * SID. Returns zero values on success, negative values on failure.
  183. *
  184. */
  185. int selinux_netlbl_socket_post_create(struct socket *sock)
  186. {
  187. struct sock *sk = sock->sk;
  188. struct sk_security_struct *sksec = sk->sk_security;
  189. if (sksec->nlbl_state != NLBL_REQUIRE)
  190. return 0;
  191. return selinux_netlbl_sock_setsid(sk, sksec->sid);
  192. }
  193. /**
  194. * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
  195. * @inode: the file descriptor's inode
  196. * @mask: the permission mask
  197. *
  198. * Description:
  199. * Looks at a file's inode and if it is marked as a socket protected by
  200. * NetLabel then verify that the socket has been labeled, if not try to label
  201. * the socket now with the inode's SID. Returns zero on success, negative
  202. * values on failure.
  203. *
  204. */
  205. int selinux_netlbl_inode_permission(struct inode *inode, int mask)
  206. {
  207. int rc;
  208. struct sock *sk;
  209. struct socket *sock;
  210. struct sk_security_struct *sksec;
  211. if (!S_ISSOCK(inode->i_mode) ||
  212. ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
  213. return 0;
  214. sock = SOCKET_I(inode);
  215. sk = sock->sk;
  216. sksec = sk->sk_security;
  217. if (sksec->nlbl_state != NLBL_REQUIRE)
  218. return 0;
  219. local_bh_disable();
  220. bh_lock_sock_nested(sk);
  221. if (likely(sksec->nlbl_state == NLBL_REQUIRE))
  222. rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
  223. else
  224. rc = 0;
  225. bh_unlock_sock(sk);
  226. local_bh_enable();
  227. return rc;
  228. }
  229. /**
  230. * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
  231. * @sksec: the sock's sk_security_struct
  232. * @skb: the packet
  233. * @family: protocol family
  234. * @ad: the audit data
  235. *
  236. * Description:
  237. * Fetch the NetLabel security attributes from @skb and perform an access check
  238. * against the receiving socket. Returns zero on success, negative values on
  239. * error.
  240. *
  241. */
  242. int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
  243. struct sk_buff *skb,
  244. u16 family,
  245. struct avc_audit_data *ad)
  246. {
  247. int rc;
  248. u32 nlbl_sid;
  249. u32 perm;
  250. struct netlbl_lsm_secattr secattr;
  251. if (!netlbl_enabled())
  252. return 0;
  253. netlbl_secattr_init(&secattr);
  254. rc = netlbl_skbuff_getattr(skb, family, &secattr);
  255. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  256. rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid);
  257. else
  258. nlbl_sid = SECINITSID_UNLABELED;
  259. netlbl_secattr_destroy(&secattr);
  260. if (rc != 0)
  261. return rc;
  262. switch (sksec->sclass) {
  263. case SECCLASS_UDP_SOCKET:
  264. perm = UDP_SOCKET__RECVFROM;
  265. break;
  266. case SECCLASS_TCP_SOCKET:
  267. perm = TCP_SOCKET__RECVFROM;
  268. break;
  269. default:
  270. perm = RAWIP_SOCKET__RECVFROM;
  271. }
  272. rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
  273. if (rc == 0)
  274. return 0;
  275. if (nlbl_sid != SECINITSID_UNLABELED)
  276. netlbl_skbuff_err(skb, rc);
  277. return rc;
  278. }
  279. /**
  280. * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
  281. * @sock: the socket
  282. * @level: the socket level or protocol
  283. * @optname: the socket option name
  284. *
  285. * Description:
  286. * Check the setsockopt() call and if the user is trying to replace the IP
  287. * options on a socket and a NetLabel is in place for the socket deny the
  288. * access; otherwise allow the access. Returns zero when the access is
  289. * allowed, -EACCES when denied, and other negative values on error.
  290. *
  291. */
  292. int selinux_netlbl_socket_setsockopt(struct socket *sock,
  293. int level,
  294. int optname)
  295. {
  296. int rc = 0;
  297. struct sock *sk = sock->sk;
  298. struct sk_security_struct *sksec = sk->sk_security;
  299. struct netlbl_lsm_secattr secattr;
  300. if (level == IPPROTO_IP && optname == IP_OPTIONS &&
  301. sksec->nlbl_state == NLBL_LABELED) {
  302. netlbl_secattr_init(&secattr);
  303. lock_sock(sk);
  304. rc = netlbl_sock_getattr(sk, &secattr);
  305. release_sock(sk);
  306. if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
  307. rc = -EACCES;
  308. netlbl_secattr_destroy(&secattr);
  309. }
  310. return rc;
  311. }