in6_jail.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*-
  2. * Copyright (c) 1999 Poul-Henning Kamp.
  3. * Copyright (c) 2008 Bjoern A. Zeeb.
  4. * Copyright (c) 2009 James Gritton.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include "opt_ddb.h"
  31. #include "opt_inet.h"
  32. #include "opt_inet6.h"
  33. #include <sys/param.h>
  34. #include <sys/types.h>
  35. #include <sys/kernel.h>
  36. #include <sys/systm.h>
  37. #include <sys/errno.h>
  38. #include <sys/sysproto.h>
  39. #include <sys/malloc.h>
  40. #include <sys/osd.h>
  41. #include <sys/priv.h>
  42. #include <sys/proc.h>
  43. #include <sys/taskqueue.h>
  44. #include <sys/fcntl.h>
  45. #include <sys/jail.h>
  46. #include <sys/lock.h>
  47. #include <sys/mutex.h>
  48. #include <sys/racct.h>
  49. #include <sys/refcount.h>
  50. #include <sys/sx.h>
  51. #include <sys/sysent.h>
  52. #include <sys/namei.h>
  53. #include <sys/mount.h>
  54. #include <sys/queue.h>
  55. #include <sys/socket.h>
  56. #include <sys/syscallsubr.h>
  57. #include <sys/sysctl.h>
  58. #include <sys/vnode.h>
  59. #include <net/if.h>
  60. #include <net/vnet.h>
  61. #include <netinet/in.h>
  62. int
  63. prison_qcmp_v6(const void *ip1, const void *ip2)
  64. {
  65. const struct in6_addr *ia6a, *ia6b;
  66. int i, rc;
  67. ia6a = (const struct in6_addr *)ip1;
  68. ia6b = (const struct in6_addr *)ip2;
  69. rc = 0;
  70. for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
  71. if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
  72. rc = 1;
  73. else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
  74. rc = -1;
  75. }
  76. return (rc);
  77. }
  78. int
  79. prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
  80. {
  81. int ii, ij, used;
  82. struct prison *ppr;
  83. ppr = pr->pr_parent;
  84. if (!(pr->pr_flags & PR_IP6_USER)) {
  85. /* This has no user settings, so just copy the parent's list. */
  86. if (pr->pr_ip6s < ppr->pr_ip6s) {
  87. /*
  88. * There's no room for the parent's list. Use the
  89. * new list buffer, which is assumed to be big enough
  90. * (if it was passed). If there's no buffer, try to
  91. * allocate one.
  92. */
  93. used = 1;
  94. if (newip6 == NULL) {
  95. newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
  96. M_PRISON, M_NOWAIT);
  97. if (newip6 != NULL)
  98. used = 0;
  99. }
  100. if (newip6 != NULL) {
  101. bcopy(ppr->pr_ip6, newip6,
  102. ppr->pr_ip6s * sizeof(*newip6));
  103. free(pr->pr_ip6, M_PRISON);
  104. pr->pr_ip6 = newip6;
  105. pr->pr_ip6s = ppr->pr_ip6s;
  106. }
  107. return (used);
  108. }
  109. pr->pr_ip6s = ppr->pr_ip6s;
  110. if (pr->pr_ip6s > 0)
  111. bcopy(ppr->pr_ip6, pr->pr_ip6,
  112. pr->pr_ip6s * sizeof(*newip6));
  113. else if (pr->pr_ip6 != NULL) {
  114. free(pr->pr_ip6, M_PRISON);
  115. pr->pr_ip6 = NULL;
  116. }
  117. } else if (pr->pr_ip6s > 0) {
  118. /* Remove addresses that aren't in the parent. */
  119. for (ij = 0; ij < ppr->pr_ip6s; ij++)
  120. if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
  121. &ppr->pr_ip6[ij]))
  122. break;
  123. if (ij < ppr->pr_ip6s)
  124. ii = 1;
  125. else {
  126. bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
  127. --pr->pr_ip6s * sizeof(*pr->pr_ip6));
  128. ii = 0;
  129. }
  130. for (ij = 1; ii < pr->pr_ip6s; ) {
  131. if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
  132. &ppr->pr_ip6[0])) {
  133. ii++;
  134. continue;
  135. }
  136. switch (ij >= ppr->pr_ip6s ? -1 :
  137. prison_qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
  138. case -1:
  139. bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
  140. (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
  141. break;
  142. case 0:
  143. ii++;
  144. ij++;
  145. break;
  146. case 1:
  147. ij++;
  148. break;
  149. }
  150. }
  151. if (pr->pr_ip6s == 0) {
  152. free(pr->pr_ip6, M_PRISON);
  153. pr->pr_ip6 = NULL;
  154. }
  155. }
  156. return 0;
  157. }
  158. /*
  159. * Pass back primary IPv6 address for this jail.
  160. *
  161. * If not restricted return success but do not alter the address. Caller has
  162. * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
  163. *
  164. * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
  165. */
  166. int
  167. prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
  168. {
  169. struct prison *pr;
  170. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  171. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  172. pr = cred->cr_prison;
  173. if (!(pr->pr_flags & PR_IP6))
  174. return (0);
  175. mtx_lock(&pr->pr_mtx);
  176. if (!(pr->pr_flags & PR_IP6)) {
  177. mtx_unlock(&pr->pr_mtx);
  178. return (0);
  179. }
  180. if (pr->pr_ip6 == NULL) {
  181. mtx_unlock(&pr->pr_mtx);
  182. return (EAFNOSUPPORT);
  183. }
  184. bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
  185. mtx_unlock(&pr->pr_mtx);
  186. return (0);
  187. }
  188. /*
  189. * Return 1 if we should do proper source address selection or are not jailed.
  190. * We will return 0 if we should bypass source address selection in favour
  191. * of the primary jail IPv6 address. Only in this case *ia will be updated and
  192. * returned in NBO.
  193. * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
  194. */
  195. int
  196. prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
  197. {
  198. struct prison *pr;
  199. struct in6_addr lia6;
  200. int error;
  201. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  202. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  203. if (!jailed(cred))
  204. return (1);
  205. pr = cred->cr_prison;
  206. if (pr->pr_flags & PR_IP6_SADDRSEL)
  207. return (1);
  208. lia6 = in6addr_any;
  209. error = prison_get_ip6(cred, &lia6);
  210. if (error)
  211. return (error);
  212. if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
  213. return (1);
  214. bcopy(&lia6, ia6, sizeof(struct in6_addr));
  215. return (0);
  216. }
  217. /*
  218. * Return true if pr1 and pr2 have the same IPv6 address restrictions.
  219. */
  220. int
  221. prison_equal_ip6(struct prison *pr1, struct prison *pr2)
  222. {
  223. if (pr1 == pr2)
  224. return (1);
  225. while (pr1 != &prison0 &&
  226. #ifdef VIMAGE
  227. !(pr1->pr_flags & PR_VNET) &&
  228. #endif
  229. !(pr1->pr_flags & PR_IP6_USER))
  230. pr1 = pr1->pr_parent;
  231. while (pr2 != &prison0 &&
  232. #ifdef VIMAGE
  233. !(pr2->pr_flags & PR_VNET) &&
  234. #endif
  235. !(pr2->pr_flags & PR_IP6_USER))
  236. pr2 = pr2->pr_parent;
  237. return (pr1 == pr2);
  238. }
  239. /*
  240. * Make sure our (source) address is set to something meaningful to this jail.
  241. *
  242. * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
  243. * when needed while binding.
  244. *
  245. * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
  246. * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
  247. * doesn't allow IPv6.
  248. */
  249. int
  250. prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
  251. {
  252. struct prison *pr;
  253. int error;
  254. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  255. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  256. pr = cred->cr_prison;
  257. if (!(pr->pr_flags & PR_IP6))
  258. return (0);
  259. mtx_lock(&pr->pr_mtx);
  260. if (!(pr->pr_flags & PR_IP6)) {
  261. mtx_unlock(&pr->pr_mtx);
  262. return (0);
  263. }
  264. if (pr->pr_ip6 == NULL) {
  265. mtx_unlock(&pr->pr_mtx);
  266. return (EAFNOSUPPORT);
  267. }
  268. if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
  269. /*
  270. * In case there is only 1 IPv6 address, and v6only is true,
  271. * then bind directly.
  272. */
  273. if (v6only != 0 && pr->pr_ip6s == 1)
  274. bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
  275. mtx_unlock(&pr->pr_mtx);
  276. return (0);
  277. }
  278. error = prison_check_ip6_locked(pr, ia6);
  279. if (error == EADDRNOTAVAIL && IN6_IS_ADDR_LOOPBACK(ia6)) {
  280. bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
  281. error = 0;
  282. }
  283. mtx_unlock(&pr->pr_mtx);
  284. return (error);
  285. }
  286. /*
  287. * Rewrite destination address in case we will connect to loopback address.
  288. *
  289. * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
  290. */
  291. int
  292. prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
  293. {
  294. struct prison *pr;
  295. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  296. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  297. pr = cred->cr_prison;
  298. if (!(pr->pr_flags & PR_IP6))
  299. return (0);
  300. mtx_lock(&pr->pr_mtx);
  301. if (!(pr->pr_flags & PR_IP6)) {
  302. mtx_unlock(&pr->pr_mtx);
  303. return (0);
  304. }
  305. if (pr->pr_ip6 == NULL) {
  306. mtx_unlock(&pr->pr_mtx);
  307. return (EAFNOSUPPORT);
  308. }
  309. if (IN6_IS_ADDR_LOOPBACK(ia6) &&
  310. prison_check_ip6_locked(pr, ia6) == EADDRNOTAVAIL) {
  311. bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
  312. mtx_unlock(&pr->pr_mtx);
  313. return (0);
  314. }
  315. /*
  316. * Return success because nothing had to be changed.
  317. */
  318. mtx_unlock(&pr->pr_mtx);
  319. return (0);
  320. }
  321. /*
  322. * Check if given address belongs to the jail referenced by cred/prison.
  323. *
  324. * Returns 0 if address belongs to jail,
  325. * EADDRNOTAVAIL if the address doesn't belong to the jail.
  326. */
  327. int
  328. prison_check_ip6_locked(const struct prison *pr, const struct in6_addr *ia6)
  329. {
  330. int i, a, z, d;
  331. /*
  332. * Check the primary IP.
  333. */
  334. if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
  335. return (0);
  336. /*
  337. * All the other IPs are sorted so we can do a binary search.
  338. */
  339. a = 0;
  340. z = pr->pr_ip6s - 2;
  341. while (a <= z) {
  342. i = (a + z) / 2;
  343. d = prison_qcmp_v6(&pr->pr_ip6[i+1], ia6);
  344. if (d > 0)
  345. z = i - 1;
  346. else if (d < 0)
  347. a = i + 1;
  348. else
  349. return (0);
  350. }
  351. return (EADDRNOTAVAIL);
  352. }
  353. int
  354. prison_check_ip6(const struct ucred *cred, const struct in6_addr *ia6)
  355. {
  356. struct prison *pr;
  357. int error;
  358. KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
  359. KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
  360. pr = cred->cr_prison;
  361. if (!(pr->pr_flags & PR_IP6))
  362. return (0);
  363. mtx_lock(&pr->pr_mtx);
  364. if (!(pr->pr_flags & PR_IP6)) {
  365. mtx_unlock(&pr->pr_mtx);
  366. return (0);
  367. }
  368. if (pr->pr_ip6 == NULL) {
  369. mtx_unlock(&pr->pr_mtx);
  370. return (EAFNOSUPPORT);
  371. }
  372. error = prison_check_ip6_locked(pr, ia6);
  373. mtx_unlock(&pr->pr_mtx);
  374. return (error);
  375. }