From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Gibson To: passt-dev@passt.top Subject: [PATCH 6/8] More deterministic detection of whether argument is a PID, PATH or NAME Date: Fri, 26 Aug 2022 14:58:37 +1000 Message-ID: <20220826045839.1112152-7-david@gibson.dropbear.id.au> In-Reply-To: <20220826045839.1112152-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============6050964646103816331==" --===============6050964646103816331== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable pasta takes as its only non-option argument either a PID to attach to the namespaces of, a PATH to a network namespace or a NAME of a network namespace (relative to /run/netns). Currently to determine which it is we try all 3 in that order, and if anything goes wrong we move onto the next. This has the potential to cause very confusing failure modes. e.g. if the argument is intended to be a network namespace name, but a (non-namespace) file of the same name exists in the current directory. Make behaviour more predictable by choosing how to treat the argument based only on the argument's contents, not anything else on the system: - If it's a decimal integer treat it as a PID - Otherwise, if it has no '/' characters, treat it as a netns name (ip-netns doesn't allow '/' in netns names) - Otherwise, treat it as a netns path If you want to open a persistent netns in the current directory, you can use './netns'. This also allows us to split the parsing of the PID|PATH|NAME option from the actual opening of the namespaces. In turn that allows us to put the opening of existing namespaces next to the opening of new namespaces in pasta_start_ns. That makes the logical flow easier to follow and will enable later cleanups. Caveats: - The separation of functions mean we will always generate the basename and dirname for the netns_quit system, even when using PID namespaces. This is pointless, since the netns_quit system doesn't work for non persistent namespaces, but is harmless. Signed-off-by: David Gibson --- conf.c | 172 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 90 insertions(+), 82 deletions(-) diff --git a/conf.c b/conf.c index 47f3e41..10b7b9f 100644 --- a/conf.c +++ b/conf.c @@ -489,6 +489,51 @@ out: warn("Couldn't get any nameserver address"); } =20 +/** + * conf_ns_opt() - Parse non-option argument to namespace paths + * @userns: buffer of size PATH_MAX, initially contains --userns + * argument (may be empty), updated with userns path + * @netns: buffer of size PATH_MAX, updated with netns path + * @arg: PID, path or name of network namespace + * + * Return: 0 on success, negative error code otherwise + */ +static int conf_ns_opt(char *userns, char *netns, const char *arg) +{ + char *endptr; + long pidval; + int ret; + + pidval =3D strtol(arg, &endptr, 10); + if (!*endptr) { + /* Looks like a pid */ + if (pidval < 0 || pidval > INT_MAX) { + err("Invalid PID %s", arg); + return -EINVAL; + } + + snprintf(netns, PATH_MAX, "/proc/%ld/ns/net", pidval); + if (!*userns) + snprintf(userns, PATH_MAX, "/proc/%ld/ns/user", pidval); + return 0; + } + + if (!strchr(arg, '/')) { + /* looks like a netns name */ + ret =3D snprintf(netns, PATH_MAX, "%s/%s", NETNS_RUN_DIR, arg); + } else { + /* otherwise assume it's a netns path */ + ret =3D snprintf(netns, PATH_MAX, "%s", arg); + } + + if (ret <=3D 0 || ret > PATH_MAX) { + err("Network namespace name/path %s too long"); + return -E2BIG; + } + + return 0; +} + /** * conf_ns_check() - Check if we can enter configured namespaces * @arg: Execution context @@ -508,102 +553,58 @@ static int conf_ns_check(void *arg) } =20 /** - * conf_ns_opt() - Open network, user namespaces descriptors from configurat= ion - * @c: Execution context - * @conf_userns: --userns argument, can be an empty string - * @optarg: PID, path or name of namespace + * conf_ns_open() - Open network, user namespaces descriptors from configura= tion + * @c: Execution context + * @userns: --userns argument, can be an empty string + * @netns: network namespace path * * Return: 0 on success, negative error code otherwise */ -static int conf_ns_opt(struct ctx *c, - const char *conf_userns, const char *optarg) +static int conf_ns_open(struct ctx *c, const char *userns, const char *netns) { - int ufd =3D -1, nfd =3D -1, try, ret, netns_only_reset =3D c->netns_only; - char userns[PATH_MAX] =3D { 0 }, netns[PATH_MAX]; - char *endptr; - long pid_arg; - pid_t pid; + int ufd =3D -1, nfd =3D -1; =20 - if (c->netns_only && *conf_userns) { + if (c->netns_only && *userns) { err("Both --userns and --netns-only given"); return -EINVAL; } =20 - /* It might be a PID, a netns path, or a netns name */ - for (try =3D 0; try < 3; try++) { - if (try =3D=3D 0) { - pid_arg =3D strtol(optarg, &endptr, 10); - if (*endptr || pid_arg < 0 || pid_arg > INT_MAX) - continue; - - pid =3D pid_arg; - - if (!*conf_userns && !c->netns_only) { - ret =3D snprintf(userns, PATH_MAX, - "/proc/%i/ns/user", pid); - if (ret <=3D 0 || ret > (int)sizeof(userns)) - continue; - } - ret =3D snprintf(netns, PATH_MAX, "/proc/%i/ns/net", pid); - if (ret <=3D 0 || ret > (int)sizeof(netns)) - continue; - } else if (try =3D=3D 1) { - if (!*conf_userns) - c->netns_only =3D 1; - - ret =3D snprintf(netns, PATH_MAX, "%s", optarg); - if (ret <=3D 0 || ret > (int)sizeof(userns)) - continue; - } else if (try =3D=3D 2) { - ret =3D snprintf(netns, PATH_MAX, "%s/%s", - NETNS_RUN_DIR, optarg); - if (ret <=3D 0 || ret > (int)sizeof(netns)) - continue; - } - - if (!c->netns_only) { - if (*conf_userns) - ufd =3D open(conf_userns, O_RDONLY | O_CLOEXEC); - else if (*userns) - ufd =3D open(userns, O_RDONLY | O_CLOEXEC); - } - - nfd =3D open(netns, O_RDONLY | O_CLOEXEC); - - if (nfd =3D=3D -1 || (ufd =3D=3D -1 && !c->netns_only)) { - if (nfd >=3D 0) - close(nfd); - - if (ufd >=3D 0) - close(ufd); + nfd =3D open(netns, O_RDONLY | O_CLOEXEC); + if (nfd < 0) { + err("Couldn't open network namespace %s", netns); + return -ENOENT; + } =20 - continue; + if (!c->netns_only && *userns) { + ufd =3D open(userns, O_RDONLY | O_CLOEXEC); + if (ufd < 0) { + close(nfd); + err("Couldn't open user namespace %s", userns); + return -ENOENT; } + } =20 - c->pasta_netns_fd =3D nfd; - c->pasta_userns_fd =3D ufd; - - NS_CALL(conf_ns_check, c); + c->pasta_netns_fd =3D nfd; + c->pasta_userns_fd =3D ufd; + c->netns_only =3D !*userns; =20 - if (c->pasta_netns_fd >=3D 0) { - char buf[PATH_MAX]; + NS_CALL(conf_ns_check, c); =20 - if (try =3D=3D 0 || c->no_netns_quit) - return 0; + if (c->pasta_netns_fd < 0) { + err("Couldn't switch to pasta namespaces"); + return -ENOENT; + } =20 - strncpy(buf, netns, PATH_MAX); - strncpy(c->netns_base, basename(buf), PATH_MAX - 1); - strncpy(buf, netns, PATH_MAX); - strncpy(c->netns_dir, dirname(buf), PATH_MAX - 1); + if (!c->no_netns_quit) { + char buf[PATH_MAX]; =20 - return 0; - } + strncpy(buf, netns, PATH_MAX); + strncpy(c->netns_base, basename(buf), PATH_MAX - 1); + strncpy(buf, netns, PATH_MAX); + strncpy(c->netns_dir, dirname(buf), PATH_MAX - 1); } =20 - c->netns_only =3D netns_only_reset; - - err("Namespace %s not found", optarg); - return -ENOENT; + return 0; } =20 /** @@ -1051,7 +1052,7 @@ void conf(struct ctx *c, int argc, char **argv) { 0 }, }; struct get_bound_ports_ns_arg ns_ports_arg =3D { .c =3D c }; - char userns[PATH_MAX] =3D { 0 }; + char userns[PATH_MAX] =3D { 0 }, netns[PATH_MAX] =3D { 0 }; enum conf_port_type tcp_tap =3D 0, tcp_init =3D 0; enum conf_port_type udp_tap =3D 0, udp_init =3D 0; bool v4_only =3D false, v6_only =3D false; @@ -1446,7 +1447,7 @@ void conf(struct ctx *c, int argc, char **argv) check_root(c); =20 if (c->mode =3D=3D MODE_PASTA && optind + 1 =3D=3D argc) { - ret =3D conf_ns_opt(c, userns, argv[optind]); + ret =3D conf_ns_opt(userns, netns, argv[optind]); if (ret < 0) usage(argv[0]); } else if (c->mode =3D=3D MODE_PASTA && *userns && optind =3D=3D argc) { @@ -1459,8 +1460,15 @@ void conf(struct ctx *c, int argc, char **argv) if (c->pasta_conf_ns) c->no_ra =3D 1; =20 - if (c->mode =3D=3D MODE_PASTA && c->pasta_netns_fd =3D=3D -1) - pasta_start_ns(c); + if (c->mode =3D=3D MODE_PASTA) { + if (*netns) { + ret =3D conf_ns_open(c, userns, netns); + if (ret < 0) + usage(argv[0]); + } else { + pasta_start_ns(c); + } + } =20 if (nl_sock_init(c)) { err("Failed to get netlink socket"); --=20 2.37.2 --===============6050964646103816331==--