aboutgitcodebugslistschat
path: root/conf.c
diff options
context:
space:
mode:
Diffstat (limited to 'conf.c')
-rw-r--r--conf.c259
1 files changed, 249 insertions, 10 deletions
diff --git a/conf.c b/conf.c
index 855fe60..d3976d3 100644
--- a/conf.c
+++ b/conf.c
@@ -19,6 +19,9 @@
#include <libgen.h>
#include <string.h>
#include <sched.h>
+#include <sys/inotify.h>
+#include <sys/mount.h>
+#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
@@ -36,6 +39,7 @@
#include <netinet/if_ether.h>
#include "util.h"
+#include "linux_dep.h"
#include "bitmap.h"
#include "ip.h"
#include "passt.h"
@@ -202,6 +206,31 @@ static void add_dns_resolv(struct ctx *c, const char *nameserver,
}
/**
+ * resolv_nameserver() - Get address from a nameserver line of resolv.conf
+ * @line: Line from resolv.conf, terminated, might be modified
+ *
+ * Return: address as string, NULL if @line is not a nameserver line
+ */
+static const char *resolv_nameserver(char *line)
+{
+ const char *p;
+ char *end;
+
+ if (strstr(line, "nameserver ") != line)
+ return NULL;
+
+ p = strrchr(line, ' ');
+ if (!p)
+ return NULL;
+
+ end = strpbrk(line, "%\n");
+ if (end)
+ *end = 0;
+
+ return p + 1;
+}
+
+/**
* get_dns() - Get nameserver addresses from local /etc/resolv.conf
* @c: Execution context
*/
@@ -220,6 +249,14 @@ static void get_dns(struct ctx *c)
dnss_set = !!*s->n || c->no_dns_search;
dns_set = (dns4_set && dns6_set) || c->no_dns;
+ /* Unless given with --dns-host, or unless we don't read nameservers
+ * at all, host nameservers we forward queries to come from resolv.conf
+ */
+ c->ip4.dns_host_resolv = !c->no_dns && !dns4_set &&
+ IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_host);
+ c->ip6.dns_host_resolv = !c->no_dns && !dns6_set &&
+ IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_host);
+
if (dns_set && dnss_set)
return;
@@ -228,16 +265,8 @@ static void get_dns(struct ctx *c)
lineread_init(&resolvconf, fd);
while ((line_len = lineread_get(&resolvconf, &line)) > 0) {
- if (!dns_set && strstr(line, "nameserver ") == line) {
- p = strrchr(line, ' ');
- if (!p)
- continue;
-
- end = strpbrk(line, "%\n");
- if (end)
- *end = 0;
-
- add_dns_resolv(c, p + 1,
+ if (!dns_set && (p = resolv_nameserver(line))) {
+ add_dns_resolv(c, p,
dns4_set ? NULL : &dns4_idx,
dns6_set ? NULL : &dns6_idx);
} else if (!dnss_set && strstr(line, "search ") == line &&
@@ -282,6 +311,216 @@ out:
}
}
+/* Name of host's resolv.conf, or of the target of a symbolic link to it, that
+ * we follow changes to: see conf_resolv_watch()
+ */
+#define RESOLV_NAME "resolv.conf"
+
+/**
+ * conf_resolv_update() - Update host nameservers from host's resolv.conf
+ * @c: Execution context
+ *
+ * Return: 0 on success, negative error code if the file can't be opened
+ *
+ * #syscalls:pasta openat
+ */
+static int conf_resolv_update(struct ctx *c)
+{
+ bool found4 = false, found6 = false;
+ char addr[INET6_ADDRSTRLEN];
+ struct lineread resolvconf;
+ struct in6_addr ns6;
+ struct in_addr ns4;
+ char *line;
+ int fd;
+
+ fd = openat(c->resolv_dir_fd, RESOLV_NAME, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+
+ lineread_init(&resolvconf, fd);
+ while (lineread_get(&resolvconf, &line) > 0) {
+ const char *p = resolv_nameserver(line);
+
+ if (!p)
+ continue;
+
+ if (!found4)
+ found4 = inet_pton(AF_INET, p, &ns4) == 1;
+ if (!found6)
+ found6 = inet_pton(AF_INET6, p, &ns6) == 1;
+ }
+ close(fd);
+
+ if (c->ip4.dns_host_resolv && found4 &&
+ !IN4_ARE_ADDR_EQUAL(&ns4, &c->ip4.dns_host)) {
+ c->ip4.dns_host = ns4;
+ info("Forwarding DNS queries to new host nameserver %s",
+ inet_ntop(AF_INET, &ns4, addr, sizeof(addr)));
+ }
+
+ if (c->ip6.dns_host_resolv && found6 &&
+ !IN6_ARE_ADDR_EQUAL(&ns6, &c->ip6.dns_host)) {
+ c->ip6.dns_host = ns6;
+ info("Forwarding DNS queries to new host nameserver %s",
+ inet_ntop(AF_INET6, &ns6, addr, sizeof(addr)));
+ }
+
+ return 0;
+}
+
+/**
+ * conf_resolv_watch() - Watch host's resolv.conf to follow nameserver changes
+ * @c: Execution context
+ *
+ * The host nameserver we forward DNS queries to can change at any time, for
+ * example as the host joins a different network or connects to a VPN. Watch
+ * the directory containing resolv.conf with inotify, as the file is usually
+ * replaced by renaming a new one over it, not rewritten in place.
+ *
+ * Once sandboxed, we can't open resolv.conf by path, and a descriptor for the
+ * directory itself would let lookups of ".." reach the whole filesystem. Keep
+ * a detached, read-only copy of the directory instead: lookups can't go above
+ * its root. Cloning needs our own mount namespace, before its mounts are made
+ * unbindable: see isolate_prefork().
+ *
+ * To keep what we can open through the copy to a minimum, only follow changes
+ * to a file called resolv.conf, and give up if we can't make the copy
+ * read-only.
+ */
+void conf_resolv_watch(struct ctx *c)
+{
+ struct mount_attr attr = {
+ .attr_set = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID |
+ MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC,
+ .propagation = MS_PRIVATE,
+ };
+ union epoll_ref ref = { .type = EPOLL_TYPE_RESOLV_INOTIFY };
+ char dir[PATH_MAX], *base;
+ int fd = -1, dir_fd, rc;
+
+ if (c->mode != MODE_PASTA)
+ return;
+
+ if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match))
+ c->ip4.dns_host_resolv = false;
+ if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_match))
+ c->ip6.dns_host_resolv = false;
+
+ if (!c->ip4.dns_host_resolv && !c->ip6.dns_host_resolv)
+ return;
+
+ /* With systemd-resolved, resolvconf, or NetworkManager in some
+ * configurations, resolv.conf is a symbolic link: watch its target
+ */
+ if (!realpath("/etc/resolv.conf", dir)) {
+ debug_perror("Not following changes to /etc/resolv.conf");
+ return;
+ }
+
+ /* Absolute path from realpath(): split it into directory and file. The
+ * other usual target of the link, systemd-resolved's stub-resolv.conf,
+ * always points to its local stub resolver: no need to follow that one.
+ * Never copy the root directory, though: that's the whole filesystem.
+ */
+ base = strrchr(dir, '/');
+ if (!base || base == dir || strcmp(base + 1, RESOLV_NAME)) {
+ debug("Not following changes to %s", dir);
+ return;
+ }
+ *base = 0;
+
+ /* AT_RECURSIVE: mounts we inherited in our user namespace are locked,
+ * and the kernel refuses to clone a subtree without its submounts
+ */
+ dir_fd = syscall(SYS_open_tree, AT_FDCWD, dir,
+ OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_RECURSIVE);
+ if (dir_fd < 0) {
+ debug_perror("Can't clone %s, not following changes", dir);
+ return;
+ }
+
+ /* Needs Linux 5.12: don't follow changes through a writable copy */
+ if (syscall(SYS_mount_setattr, dir_fd, "", AT_EMPTY_PATH | AT_RECURSIVE,
+ &attr, sizeof(attr))) {
+ debug_perror("Can't make copy of %s read-only", dir);
+ goto fail;
+ }
+
+ if ((fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) < 0) {
+ debug_perror("Can't follow changes to resolv.conf");
+ goto fail;
+ }
+
+ if (inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO) < 0) {
+ debug_perror("Can't watch %s for changes", dir);
+ goto fail;
+ }
+
+ /* With the watch in place, read the file again: it might have changed
+ * since get_dns() read it. This also checks that we can read it through
+ * the copy, which security policies might not allow.
+ */
+ c->resolv_dir_fd = dir_fd;
+ if ((rc = conf_resolv_update(c))) {
+ debug("Can't read %s/" RESOLV_NAME " through a copy: %s", dir,
+ strerror_(-rc));
+ goto fail;
+ }
+
+ if (fd > FD_REF_MAX) {
+ debug("resolv.conf watch file number %i too big", fd);
+ goto fail;
+ }
+
+ ref.fd = fd;
+ if (epoll_add(c->epollfd, EPOLLIN, ref))
+ goto fail;
+
+ return;
+
+fail:
+ c->resolv_dir_fd = -1;
+ if (fd >= 0)
+ close(fd);
+ close(dir_fd);
+}
+
+/**
+ * conf_resolv_handler() - Handle inotify events for host's resolv.conf
+ * @c: Execution context
+ * @fd: inotify file descriptor watching directory of resolv.conf
+ */
+void conf_resolv_handler(struct ctx *c, int fd)
+{
+ char buf[sizeof(struct inotify_event) + NAME_MAX + 1]
+ __attribute__ ((aligned(__alignof__(struct inotify_event))));
+ const struct inotify_event *ev;
+ bool changed = false;
+ ssize_t n;
+ char *p;
+ int rc;
+
+ if ((n = read(fd, buf, sizeof(buf))) < (ssize_t)sizeof(*ev))
+ return;
+
+ /* cppcheck-suppress [negativeIndex, unmatchedSuppression] */
+ buf[n - 1] = '\0';
+ for (p = buf; p < buf + n; p += sizeof(*ev) + ev->len) {
+ ev = (const struct inotify_event *)p;
+
+ if ((ev->mask & IN_Q_OVERFLOW) ||
+ (ev->len && !strcmp(ev->name, RESOLV_NAME))) {
+ changed = true;
+ break;
+ }
+ }
+
+ if (changed && (rc = conf_resolv_update(c)))
+ warn("Can't read " RESOLV_NAME " to update nameservers: %s",
+ strerror_(-rc));
+}
+
/**
* conf_netns_opt() - Parse --netns option
* @netns: buffer of size PATH_MAX, updated with netns path