netlabel.c 9.0 KB

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