netlabel.c 10 KB

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