lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon,  9 Oct 2023 20:27:51 +0200
From: Toke Høiland-Jørgensen <toke@...hat.com>
To: David Ahern <dsahern@...il.com>,
	Stephen Hemminger <stephen@...workplumber.org>
Cc: netdev@...r.kernel.org,
	Toke Høiland-Jørgensen <toke@...hat.com>,
	Nicolas Dichtel <nicolas.dichtel@...nd.com>,
	Christian Brauner <brauner@...nel.org>,
	"Eric W . Biederman" <ebiederm@...ssion.com>,
	David Laight <David.Laight@...LAB.COM>
Subject: [RFC PATCH iproute2-next 3/5] lib/namespace: Factor out code for reuse

Factor out the code that switches namespaces and the code that sets up a new
mount namespace into utility functions that can be reused when we add mount
namespace pinning.

No functional change is intended with this patch.

Signed-off-by: Toke Høiland-Jørgensen <toke@...hat.com>
---
 include/namespace.h |  1 +
 lib/namespace.c     | 73 ++++++++++++++++++++++++++++++++-------------
 2 files changed, 54 insertions(+), 20 deletions(-)

diff --git a/include/namespace.h b/include/namespace.h
index e47f9b5d49d1..b694a12e8397 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -49,6 +49,7 @@ static inline int setns(int fd, int nstype)
 }
 #endif /* HAVE_SETNS */
 
+int prepare_mountns(const char *name, bool do_unshare);
 int netns_switch(char *netns);
 int netns_get_fd(const char *netns);
 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg);
diff --git a/lib/namespace.c b/lib/namespace.c
index 1202fa85f97d..5e310762f34b 100644
--- a/lib/namespace.c
+++ b/lib/namespace.c
@@ -11,6 +11,25 @@
 #include "utils.h"
 #include "namespace.h"
 
+static struct namespace_typename {
+	int		type;		/* CLONE_NEW* */
+	const char	*name;		/* <type> */
+} namespace_names[] = {
+	{ .type = CLONE_NEWNET,   .name = "network"  },
+	{ .type = CLONE_NEWNS,    .name = "mount"  },
+	{ .name = NULL }
+};
+
+static const char *ns_typename(int nstype)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(namespace_names); i++)
+		if (namespace_names[i].type == nstype)
+			return namespace_names[i].name;
+	return NULL;
+}
+
 static void bind_etc(const char *name)
 {
 	char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX];
@@ -42,30 +61,12 @@ static void bind_etc(const char *name)
 	closedir(dir);
 }
 
-int netns_switch(char *name)
+int prepare_mountns(const char *name, bool do_unshare)
 {
-	char net_path[PATH_MAX];
-	int netns;
 	unsigned long mountflags = 0;
 	struct statvfs fsstat;
 
-	snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
-	netns = open(net_path, O_RDONLY | O_CLOEXEC);
-	if (netns < 0) {
-		fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
-			name, strerror(errno));
-		return -1;
-	}
-
-	if (setns(netns, CLONE_NEWNET) < 0) {
-		fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
-			name, strerror(errno));
-		close(netns);
-		return -1;
-	}
-	close(netns);
-
-	if (unshare(CLONE_NEWNS) < 0) {
+	if (do_unshare && unshare(CLONE_NEWNS) < 0) {
 		fprintf(stderr, "unshare failed: %s\n", strerror(errno));
 		return -1;
 	}
@@ -97,6 +98,38 @@ int netns_switch(char *name)
 	return 0;
 }
 
+static int switch_ns(const char *parent_dir, const char *name, int nstype)
+{
+	char ns_path[PATH_MAX];
+	int ns_fd;
+
+	snprintf(ns_path, sizeof(ns_path), "%s/%s", parent_dir, name);
+	ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC);
+	if (ns_fd < 0) {
+		fprintf(stderr, "Cannot open %s namespace \"%s\": %s\n",
+			ns_typename(nstype), name, strerror(errno));
+		return -1;
+	}
+
+	if (setns(ns_fd, nstype) < 0) {
+		fprintf(stderr, "setting the %s namespace \"%s\" failed: %s\n",
+			ns_typename(nstype), name, strerror(errno));
+		close(ns_fd);
+		return -1;
+	}
+	close(ns_fd);
+	return 0;
+}
+
+int netns_switch(char *name)
+{
+
+	if (switch_ns(NETNS_RUN_DIR, name, CLONE_NEWNET))
+		return -1;
+
+	return prepare_mountns(name, true);
+}
+
 int netns_get_fd(const char *name)
 {
 	char pathbuf[PATH_MAX];
-- 
2.42.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ