2
0

netifd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Copyright (C) 2021 Daniel Golle <daniel@makrotopia.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * launch private ubus and netifd instances for containers with managed
  14. * network namespace.
  15. */
  16. #define _GNU_SOURCE /* See feature_test_macros(7) */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <libgen.h>
  22. #include <fcntl.h>
  23. #include <sys/inotify.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <pwd.h>
  27. #include <linux/limits.h>
  28. #include <libubox/uloop.h>
  29. #include <libubox/utils.h>
  30. #include <libubus.h>
  31. #include <libubox/blobmsg.h>
  32. #include <libubox/blobmsg_json.h>
  33. #include <uci.h>
  34. #include "netifd.h"
  35. #include "log.h"
  36. #include "jail.h"
  37. #define INOTIFY_SZ (sizeof(struct inotify_event) + PATH_MAX + 1)
  38. static const char ubusd_path[] = "/sbin/ubusd";
  39. static const char netifd_path[] = "/sbin/netifd";
  40. static const char uci_net[] = "network";
  41. static const char ubus_sock_name[] = "ubus.sock";
  42. static char *jail_name, *ubus_sock_path, *ubus_sock_dir, *uci_config_network = NULL;
  43. static char *inotify_buffer;
  44. static struct uloop_fd fd_inotify_read;
  45. static struct passwd *ubus_pw;
  46. static pid_t ns_pid;
  47. static struct ubus_context *host_ubus_ctx = NULL;
  48. static struct ubus_context *jail_ubus_ctx = NULL;
  49. static struct ubus_subscriber config_watch_subscribe;
  50. /* generate /etc/config/network for jail'ed netifd */
  51. static int gen_jail_uci_network(void)
  52. {
  53. struct uci_context *uci_ctx = uci_alloc_context();
  54. struct uci_package *pkg = NULL;
  55. struct uci_element *e, *t;
  56. bool has_loopback = false;
  57. int ret = 0;
  58. FILE *ucinetf;
  59. /* if no network configuration is active just return */
  60. if (!uci_config_network)
  61. goto uci_out;
  62. /* open output uci network config file */
  63. ucinetf = fopen(uci_config_network, "w");
  64. if (!ucinetf) {
  65. ret = errno;
  66. goto uci_out;
  67. }
  68. /* load network uci package */
  69. if (uci_load(uci_ctx, uci_net, &pkg) != UCI_OK) {
  70. char *err;
  71. uci_get_errorstr(uci_ctx, &err, uci_net);
  72. fprintf(stderr, "unable to load configuration (%s)\n", err);
  73. free(err);
  74. ret = EIO;
  75. goto ucinetf_out;
  76. }
  77. /* remove all sections which don't match jail */
  78. uci_foreach_element_safe(&pkg->sections, t, e) {
  79. struct uci_section *s = uci_to_section(e);
  80. struct uci_option *o = uci_lookup_option(uci_ctx, s, "jail");
  81. struct uci_ptr ptr = { .p = pkg, .s = s };
  82. /* keep match, but remove 'jail' option and rename 'jail_ifname' */
  83. if (o && o->type == UCI_TYPE_STRING && !strcmp(o->v.string, jail_name)) {
  84. ptr.o = o;
  85. struct uci_option *jio = uci_lookup_option(uci_ctx, s, "jail_device");
  86. if (!jio)
  87. jio = uci_lookup_option(uci_ctx, s, "jail_ifname");
  88. if (jio) {
  89. struct uci_ptr ren_ptr = { .p = pkg, .s = s, .o = jio, .value = "device" };
  90. struct uci_option *host_device = uci_lookup_option(uci_ctx, s, "device");
  91. struct uci_option *legacy_ifname = uci_lookup_option(uci_ctx, s, "ifname");
  92. if (host_device && legacy_ifname) {
  93. struct uci_ptr delif_ptr = { .p = pkg, .s = s, .o = legacy_ifname };
  94. uci_delete(uci_ctx, &delif_ptr);
  95. }
  96. struct uci_ptr renif_ptr = { .p = pkg, .s = s, .o = host_device?:legacy_ifname, .value = "host_device" };
  97. uci_rename(uci_ctx, &renif_ptr);
  98. uci_rename(uci_ctx, &ren_ptr);
  99. }
  100. }
  101. uci_delete(uci_ctx, &ptr);
  102. }
  103. /* check if device 'lo' is defined by any remaining interfaces */
  104. uci_foreach_element(&pkg->sections, e) {
  105. struct uci_section *s = uci_to_section(e);
  106. if (strcmp(s->type, "interface"))
  107. continue;
  108. const char *devname = uci_lookup_option_string(uci_ctx, s, "device");
  109. if (devname && !strcmp(devname, "lo")) {
  110. has_loopback = true;
  111. break;
  112. }
  113. }
  114. /* create loopback interface section if not defined */
  115. if (!has_loopback) {
  116. struct uci_ptr ptr = { .p = pkg, .section = "loopback", .value = "interface" };
  117. uci_set(uci_ctx, &ptr);
  118. uci_reorder_section(uci_ctx, ptr.s, 0);
  119. struct uci_ptr ptr1 = { .p = pkg, .s = ptr.s, .option = "device", .value = "lo" };
  120. struct uci_ptr ptr2 = { .p = pkg, .s = ptr.s, .option = "proto", .value = "static" };
  121. struct uci_ptr ptr3 = { .p = pkg, .s = ptr.s, .option = "ipaddr", .value = "127.0.0.1" };
  122. struct uci_ptr ptr4 = { .p = pkg, .s = ptr.s, .option = "netmask", .value = "255.0.0.0" };
  123. uci_set(uci_ctx, &ptr1);
  124. uci_set(uci_ctx, &ptr2);
  125. uci_set(uci_ctx, &ptr3);
  126. uci_set(uci_ctx, &ptr4);
  127. }
  128. ret = uci_export(uci_ctx, ucinetf, pkg, false);
  129. ucinetf_out:
  130. fclose(ucinetf);
  131. uci_out:
  132. uci_free_context(uci_ctx);
  133. return ret;
  134. }
  135. static void run_ubusd(struct uloop_timeout *t)
  136. {
  137. static struct blob_buf req;
  138. void *ins, *in, *cmd;
  139. uint32_t id;
  140. blob_buf_init(&req, 0);
  141. blobmsg_add_string(&req, "name", jail_name);
  142. ins = blobmsg_open_table(&req, "instances");
  143. in = blobmsg_open_table(&req, "ubus");
  144. cmd = blobmsg_open_array(&req, "command");
  145. blobmsg_add_string(&req, "", ubusd_path);
  146. blobmsg_add_string(&req, "", "-s");
  147. blobmsg_add_string(&req, "", ubus_sock_path);
  148. blobmsg_close_array(&req, cmd);
  149. if (ubus_pw) {
  150. blobmsg_add_string(&req, "user", "ubus");
  151. blobmsg_add_string(&req, "group", "ubus");
  152. }
  153. blobmsg_close_table(&req, in);
  154. blobmsg_close_table(&req, ins);
  155. if (!ubus_lookup_id(host_ubus_ctx, "container", &id))
  156. ubus_invoke(host_ubus_ctx, id, "add", req.head, NULL, NULL, 3000);
  157. blob_buf_free(&req);
  158. }
  159. static void run_netifd(struct uloop_timeout *t)
  160. {
  161. static struct blob_buf req;
  162. void *ins, *in, *cmd, *jail, *setns, *setnso, *namespaces, *mount, *pathenv;
  163. char *resolvconf_dir, *resolvconf, *ucimount, *ubusmount;
  164. char uci_dir[] = "/var/containers/ujail-uci-XXXXXX";
  165. uint32_t id;
  166. bool running = false;
  167. uloop_fd_delete(&fd_inotify_read);
  168. close(fd_inotify_read.fd);
  169. jail_ubus_ctx = ubus_connect(ubus_sock_path);
  170. if (!jail_ubus_ctx)
  171. return;
  172. if (asprintf(&resolvconf_dir, "/tmp/resolv.conf-%s.d", jail_name) == -1)
  173. return;
  174. if (asprintf(&resolvconf, "%s/resolv.conf.auto", resolvconf_dir) == -1)
  175. goto netifd_out_resolvconf_dir;
  176. if (!mkdtemp(uci_dir))
  177. goto netifd_out_resolvconf;
  178. if (asprintf(&uci_config_network, "%s/network", uci_dir) == -1)
  179. goto netifd_out_ucidir;
  180. if (asprintf(&ucimount, "%s:/etc/config", uci_dir) == -1)
  181. goto netifd_out_ucinetconf;
  182. if (asprintf(&ubusmount, "%s:/var/run/ubus", ubus_sock_dir) == -1)
  183. goto netifd_out_ucimount;
  184. if (gen_jail_uci_network())
  185. goto netifd_out_ubusmount;
  186. blob_buf_init(&req, 0);
  187. blobmsg_add_string(&req, "name", jail_name);
  188. ins = blobmsg_open_table(&req, "instances");
  189. in = blobmsg_open_table(&req, "netifd");
  190. cmd = blobmsg_open_array(&req, "command");
  191. blobmsg_add_string(&req, "", netifd_path);
  192. blobmsg_add_string(&req, "", "-r");
  193. blobmsg_add_string(&req, "", resolvconf);
  194. blobmsg_close_array(&req, cmd);
  195. pathenv = blobmsg_open_table(&req, "env");
  196. blobmsg_add_string(&req, "PATH", "/usr/sbin:/usr/bin:/sbin:/bin");
  197. blobmsg_close_table(&req, pathenv);
  198. jail = blobmsg_open_table(&req, "jail");
  199. setns = blobmsg_open_array(&req, "setns");
  200. setnso = blobmsg_open_table(&req, "");
  201. blobmsg_add_u32(&req, "pid", ns_pid);
  202. namespaces = blobmsg_open_array(&req, "namespaces");
  203. blobmsg_add_string(&req, "", "net");
  204. blobmsg_add_string(&req, "", "ipc");
  205. blobmsg_add_string(&req, "", "uts");
  206. blobmsg_close_array(&req, namespaces);
  207. blobmsg_close_table(&req, setnso);
  208. blobmsg_close_array(&req, setns);
  209. mount = blobmsg_open_table(&req, "mount");
  210. blobmsg_add_string(&req, ubusmount, "1");
  211. blobmsg_add_string(&req, resolvconf_dir, "1");
  212. blobmsg_add_string(&req, ucimount, "0");
  213. blobmsg_add_string(&req, "/bin/cat", "0");
  214. blobmsg_add_string(&req, "/bin/ipcalc.sh", "0");
  215. blobmsg_add_string(&req, "/bin/kill", "0");
  216. blobmsg_add_string(&req, "/bin/ubus", "0");
  217. blobmsg_add_string(&req, "/etc/hotplug.d", "0");
  218. blobmsg_add_string(&req, "/lib/functions", "0");
  219. blobmsg_add_string(&req, "/lib/functions.sh", "0");
  220. blobmsg_add_string(&req, "/lib/netifd", "0");
  221. blobmsg_add_string(&req, "/lib/network", "0");
  222. blobmsg_add_string(&req, "/usr/bin/awk", "0");
  223. blobmsg_add_string(&req, "/usr/bin/killall", "0");
  224. blobmsg_add_string(&req, "/usr/bin/logger", "0");
  225. blobmsg_add_string(&req, "/usr/bin/jshn", "0");
  226. blobmsg_add_string(&req, "/usr/share/libubox/jshn.sh", "0");
  227. blobmsg_add_string(&req, "/sbin/hotplug-call", "0");
  228. blobmsg_add_string(&req, "/sbin/udhcpc", "0");
  229. blobmsg_close_table(&req, mount);
  230. blobmsg_add_u8(&req, "log", 1);
  231. blobmsg_add_u8(&req, "procfs", 1);
  232. blobmsg_add_u8(&req, "sysfs", 1);
  233. blobmsg_add_u8(&req, "requirejail", 1);
  234. blobmsg_close_table(&req, jail);
  235. blobmsg_add_u8(&req, "stdout", 1);
  236. blobmsg_add_u8(&req, "stderr", 1);
  237. blobmsg_close_table(&req, in);
  238. blobmsg_close_table(&req, ins);
  239. if (!ubus_lookup_id(host_ubus_ctx, "container", &id))
  240. running = !ubus_invoke(host_ubus_ctx, id, "add", req.head, NULL, NULL, 3000);
  241. if (!running)
  242. blob_buf_free(&req);
  243. netifd_out_ubusmount:
  244. free(ubusmount);
  245. netifd_out_ucimount:
  246. free(ucimount);
  247. netifd_out_ucinetconf:
  248. if (!running) {
  249. unlink(uci_config_network);
  250. free(uci_config_network);
  251. }
  252. netifd_out_ucidir:
  253. if (!running)
  254. rmdir(uci_dir);
  255. netifd_out_resolvconf:
  256. free(resolvconf);
  257. netifd_out_resolvconf_dir:
  258. free(resolvconf_dir);
  259. uloop_end();
  260. }
  261. static struct uloop_timeout netifd_start_timeout = { .cb = run_netifd, };
  262. static void inotify_read_handler(struct uloop_fd *u, unsigned int events)
  263. {
  264. int rc;
  265. char *p;
  266. struct inotify_event *in;
  267. /* read inotify events */
  268. while ((rc = read(u->fd, inotify_buffer, INOTIFY_SZ)) == -1 && errno == EINTR);
  269. if (rc <= 0)
  270. return;
  271. /* process events from buffer */
  272. for (p = inotify_buffer;
  273. rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event);
  274. p += sizeof(struct inotify_event) + in->len) {
  275. in = (struct inotify_event*)p;
  276. if (in->len < 4)
  277. continue;
  278. if (!strncmp(ubus_sock_name, in->name, in->len))
  279. uloop_timeout_add(&netifd_start_timeout);
  280. }
  281. }
  282. static void netns_updown(struct ubus_context *ubus, const char *name, bool start, int netns_fd)
  283. {
  284. static struct blob_buf req;
  285. uint32_t id;
  286. if (!ubus)
  287. return;
  288. blob_buf_init(&req, 0);
  289. if (name)
  290. blobmsg_add_string(&req, "jail", name);
  291. blobmsg_add_u8(&req, "start", start);
  292. if (ubus_lookup_id(ubus, "network", &id) ||
  293. ubus_invoke_fd(ubus, id, "netns_updown", req.head, NULL, NULL, 3000, netns_fd)) {
  294. INFO("ubus request failed\n");
  295. }
  296. blob_buf_free(&req);
  297. }
  298. static void jail_network_reload(struct uloop_timeout *t)
  299. {
  300. uint32_t id;
  301. if (!jail_ubus_ctx)
  302. return;
  303. if (gen_jail_uci_network())
  304. return;
  305. if (ubus_lookup_id(jail_ubus_ctx, "network", &id))
  306. return;
  307. ubus_invoke(jail_ubus_ctx, id, "reload", NULL, NULL, NULL, 3000);
  308. }
  309. static const struct blobmsg_policy service_watch_policy = { "config", BLOBMSG_TYPE_STRING };
  310. static struct uloop_timeout jail_network_reload_timeout = { .cb = jail_network_reload, };
  311. static int config_watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
  312. struct ubus_request_data *req, const char *method,
  313. struct blob_attr *msg)
  314. {
  315. struct blob_attr *attr;
  316. const char *config;
  317. if (strcmp(method, "config.change"))
  318. return 0;
  319. blobmsg_parse(&service_watch_policy, 1, &attr, blob_data(msg), blob_len(msg));
  320. if (!attr)
  321. return 1;
  322. config = blobmsg_get_string(attr);
  323. if (strcmp(config, "network"))
  324. return 0;
  325. uloop_timeout_add(&jail_network_reload_timeout);
  326. return 0;
  327. }
  328. static void watch_ubus_service(void)
  329. {
  330. uint32_t id;
  331. config_watch_subscribe.cb = config_watch_notify_cb;
  332. if (ubus_register_subscriber(host_ubus_ctx, &config_watch_subscribe)) {
  333. ERROR("failed to register ubus subscriber\n");
  334. return;
  335. }
  336. if (ubus_lookup_id(host_ubus_ctx, "service", &id))
  337. return;
  338. if (!ubus_subscribe(host_ubus_ctx, &config_watch_subscribe, id))
  339. return;
  340. ERROR("failed to subscribe %d\n", id);
  341. }
  342. static struct uloop_timeout ubus_start_timeout = { .cb = run_ubusd, };
  343. int jail_network_start(struct ubus_context *new_ctx, char *new_jail_name, pid_t new_ns_pid)
  344. {
  345. ubus_pw = getpwnam("ubus");
  346. int ret = 0;
  347. int netns_fd;
  348. host_ubus_ctx = new_ctx;
  349. ns_pid = new_ns_pid;
  350. jail_name = new_jail_name;
  351. if (asprintf(&ubus_sock_dir, "/var/containers/ubus-%s", jail_name) == -1) {
  352. ret = ENOMEM;
  353. goto errout_dir;
  354. }
  355. if (asprintf(&ubus_sock_path, "%s/%s", ubus_sock_dir, ubus_sock_name) == -1) {
  356. ret = ENOMEM;
  357. goto errout_path;
  358. }
  359. mkdir_p(ubus_sock_dir, 0755);
  360. if (ubus_pw) {
  361. ret = chown(ubus_sock_dir, ubus_pw->pw_uid, ubus_pw->pw_gid);
  362. if (ret) {
  363. ret = errno;
  364. goto errout;
  365. }
  366. }
  367. fd_inotify_read.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
  368. fd_inotify_read.cb = inotify_read_handler;
  369. if (fd_inotify_read.fd == -1) {
  370. ERROR("failed to initialize inotify handler\n");
  371. ret = EIO;
  372. goto errout;
  373. }
  374. uloop_fd_add(&fd_inotify_read, ULOOP_READ);
  375. inotify_buffer = calloc(1, INOTIFY_SZ);
  376. if (!inotify_buffer) {
  377. ret = ENOMEM;
  378. goto errout_inotify;
  379. }
  380. if (inotify_add_watch(fd_inotify_read.fd, ubus_sock_dir, IN_CREATE) == -1) {
  381. ERROR("failed to add inotify watch on %s\n", ubus_sock_dir);
  382. free(inotify_buffer);
  383. ret = EIO;
  384. goto errout_inotify;
  385. }
  386. watch_ubus_service();
  387. netns_fd = ns_open_pid("net", ns_pid);
  388. if (netns_fd < 0) {
  389. ret = ESRCH;
  390. goto errout_inotify;
  391. }
  392. netns_updown(host_ubus_ctx, jail_name, true, netns_fd);
  393. close(netns_fd);
  394. uloop_timeout_add(&ubus_start_timeout);
  395. uloop_run();
  396. return 0;
  397. errout_inotify:
  398. close(fd_inotify_read.fd);
  399. errout:
  400. free(ubus_sock_path);
  401. errout_path:
  402. free(ubus_sock_dir);
  403. errout_dir:
  404. return ret;
  405. }
  406. static int jail_delete_instance(const char *instance)
  407. {
  408. static struct blob_buf req;
  409. uint32_t id;
  410. if (ubus_lookup_id(host_ubus_ctx, "container", &id))
  411. return -1;
  412. blob_buf_init(&req, 0);
  413. blobmsg_add_string(&req, "name", jail_name);
  414. blobmsg_add_string(&req, "instance", instance);
  415. return ubus_invoke(host_ubus_ctx, id, "delete", req.head, NULL, NULL, 3000);
  416. }
  417. int jail_network_stop(void)
  418. {
  419. int host_netns = open("/proc/self/ns/net", O_RDONLY);
  420. if (host_netns < 0)
  421. return errno;
  422. netns_updown(jail_ubus_ctx, NULL, false, host_netns);
  423. close(host_netns);
  424. ubus_free(jail_ubus_ctx);
  425. jail_delete_instance("netifd");
  426. jail_delete_instance("ubus");
  427. if (uci_config_network) {
  428. unlink(uci_config_network);
  429. rmdir(dirname(uci_config_network));
  430. free(uci_config_network);
  431. }
  432. free(ubus_sock_path);
  433. rmdir(ubus_sock_dir);
  434. free(ubus_sock_dir);
  435. return 0;
  436. }