Skip to contentSkip to conversation

Convert to cap-ng

PATCH In review Dr. David Alan Gilbert (git) 3 participants 7 messages Dec 3, 2019 – Dec 4, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Convert to cap-ng

From: "Dr. David Alan Gilbert" <[email protected]>

Hi,
This changes virtiofsd to use cap-ng rather than cap
(qemu is moving in that direction). It's a little painful because of an interaction with our sandboxing.

I intend to flatten the conversion into Vivek's patch that adds the libcap use.

Dave

Dr. David Alan Gilbert (2):
virtiofsd: cap-ng helpers
virtiofsd: Convert to libcap-ng

 Makefile                           |   2 +-
 contrib/virtiofsd/Makefile.objs    |   2 -
 contrib/virtiofsd/passthrough_ll.c | 162 +++++++++++++++++++----------
 3 files changed, 106 insertions(+), 60 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/2 virtiofsd: cap-ng helpers

From: "Dr. David Alan Gilbert" <[email protected]>

libcap-ng reads /proc during capng_get_caps_process, and virtiofsd's sandboxing doesn't have /proc mounted; thus we have to do the caps read before we sandbox it and save/restore the state.

---
 Makefile                           |  2 +-
 contrib/virtiofsd/passthrough_ll.c | 72 ++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
Makefile +1 -1
index 237cbdfee1..1484c23a9b 100644
@@ -323,7 +323,7 @@ HELPERS-$(call land,$(CONFIG_SOFTMMU),$(CONFIG_LINUX)) = qemu-bridge-helper$(EXE
 
 ifdef CONFIG_LINUX
 ifdef CONFIG_SECCOMP
-ifdef CONFIG_LIBCAP
+ifdef CONFIG_LIBCAP_NG
 HELPERS-y += virtiofsd$(EXESUF)
 vhost-user-json-y += contrib/virtiofsd/50-qemu-virtiofsd.json
 endif
contrib/virtiofsd/passthrough_ll.c +72 -0

108 lines not on this page · raw message

Signature · 1 line
2.23.0
#2 lore raw

Re: [Virtio-fs] [PATCH 1/2] virtiofsd: cap-ng helpers

Quoted 77 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Dr. David Alan Gilbert (git)
Sent: Tuesday, December 3, 2019 10:14 PM
To: [email protected]; [email protected]
Subject: [Virtio-fs] [PATCH 1/2] virtiofsd: cap-ng helpers

From: "Dr. David Alan Gilbert" <[email protected]>

libcap-ng reads /proc during capng_get_caps_process, and virtiofsd's sandboxing doesn't have /proc mounted; thus we have to do the caps read before we sandbox it and save/restore the state.

---
 Makefile                           |  2 +-
 contrib/virtiofsd/passthrough_ll.c | 72 ++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
Makefile +1 -1
index 237cbdfee1..1484c23a9b 100644
@@ -323,7 +323,7 @@ HELPERS-$(call land,$(CONFIG_SOFTMMU),$(CONFIG_LINUX)) = qemu-bridge-helper$(EXE
 
 ifdef CONFIG_LINUX
 ifdef CONFIG_SECCOMP
-ifdef CONFIG_LIBCAP
+ifdef CONFIG_LIBCAP_NG
 HELPERS-y += virtiofsd$(EXESUF)
 vhost-user-json-y += contrib/virtiofsd/50-qemu-virtiofsd.json
 endif
contrib/virtiofsd/passthrough_ll.c +24 -0
index 1688071c10..139261efc1 100644
@@ -47,6 +47,7 @@
 #include <string.h>
 #include <syslog.h>
 #include <limits.h>
+#include <cap-ng.h>
 #include <dirent.h>
 #include <assert.h>
 #include <errno.h>
@@ -216,6 +217,13 @@ static const struct fuse_opt lo_opts[] = {
 };
 static bool use_syslog = false;
 static int current_log_level;
+static struct {
+	pthread_mutex_t mutex;
+	void *saved;
+} cap;
+/* That we loaded cap-ng in the current thread from the saved */
+static __thread bool cap_loaded = 0;
+
 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode, uint64_t n);
 static void put_shared(struct lo_data *lo, struct lo_inode *inode);
 
@@ -242,6 +250,37 @@ static struct lo_data *lo_data(fuse_req_t req)
 	return (struct lo_data *) fuse_req_userdata(req);
 }
 
+/*
+ * Load capng's state from our saved state if the current thread
+ * hadn't previously been loaded.
+ * returns 0 on success
+ */
+static int load_capng(void)
+{
+	if (!cap_loaded) {
+		pthread_mutex_lock(&cap.mutex);
+		capng_restore_state(&cap.saved);
+		/*
+		 * restore_state free's the saved copy
+		 * so make another.
+		 */
+		cap.saved = capng_save_state();
+		if (!cap.saved) {

contrib/virtiofsd/passthrough_ll.c static struct lo_data *lo_data(fuse_req_t req) · line 278

Quoted 10 lines
contrib/virtiofsd/passthrough_ll.c +10 -0
+			fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
+			return -EINVAL;
+		}
+		pthread_mutex_unlock(&cap.mutex);
+
+		/*
+		 * We want to use the loaded state for our pid,
+		 * not the original
+		 */
+		capng_setpid(gettid());

Hi,

I got build error on Fedora 30 with glibc 2.29 since gettid() wrapper is introduced in glibc 2.30. How about using syscall(__NR_gettid) directly? Or is 2.30 required?

Thanks,
Misono

Quoted 69 lines
contrib/virtiofsd/passthrough_ll.c +38 -1
+		cap_loaded = true;
+	}
+	return 0;
+}
+
 /* Helpers for dropping and regaining effective capabilities. Returns 0
  * on success, error otherwise  */
 static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
@@ -2754,6 +2793,35 @@ static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
 	}
 }
 
+/*
+ * Capture the capability state, we'll need to restore this for individual
+ * threads later; see load_capng.
+ */
+static void setup_capng(void)
+{
+	/* Note this accesses /proc so has to happen before the sandbox */
+	if (capng_get_caps_process()) {
+		fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
+		exit(1);
+	}
+	pthread_mutex_init(&cap.mutex, NULL);
+	pthread_mutex_lock(&cap.mutex);
+	cap.saved = capng_save_state();
+	if (!cap.saved) {
+		fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
+		exit(1);
+	}
+	pthread_mutex_unlock(&cap.mutex);
+}
+
+static void cleanup_capng(void)
+{
+	free(cap.saved);
+	cap.saved = NULL;
+	pthread_mutex_destroy(&cap.mutex);
+}
+
+
 /*
  * Make the source directory our root so symlinks cannot escape and no other
  * files are accessible.  Assumes unshare(CLONE_NEWNS) was already called.
@@ -3173,6 +3241,9 @@ int main(int argc, char *argv[])
 
 	setup_nofile_rlimit();
 
+	/* Must be before sandbox since it wants /proc */
+	setup_capng();
+
 	setup_sandbox(&lo, se, opts.syslog);
 
 	setup_root(&lo, &lo.root);
@@ -3195,6 +3266,7 @@ int main(int argc, char *argv[])
 
 err_out4:
 	fuse_session_unmount(se);
+	cleanup_capng();
 err_out3:
 	fuse_remove_signal_handlers(se);
 err_out2:
--

2.23.0

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

#3 lore raw

Re: [Virtio-fs] [PATCH 1/2] virtiofsd: cap-ng helpers

Quoted 90 lines · * [email protected] ([email protected]) wrote:
Quoted 77 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Dr. David Alan Gilbert (git)
Sent: Tuesday, December 3, 2019 10:14 PM
To: [email protected]; [email protected]
Subject: [Virtio-fs] [PATCH 1/2] virtiofsd: cap-ng helpers

From: "Dr. David Alan Gilbert" <[email protected]>

libcap-ng reads /proc during capng_get_caps_process, and virtiofsd's sandboxing doesn't have /proc mounted; thus we have to do the caps read before we sandbox it and save/restore the state.

---
 Makefile                           |  2 +-
 contrib/virtiofsd/passthrough_ll.c | 72 ++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
Makefile +1 -1
index 237cbdfee1..1484c23a9b 100644
@@ -323,7 +323,7 @@ HELPERS-$(call land,$(CONFIG_SOFTMMU),$(CONFIG_LINUX)) = qemu-bridge-helper$(EXE
 
 ifdef CONFIG_LINUX
 ifdef CONFIG_SECCOMP
-ifdef CONFIG_LIBCAP
+ifdef CONFIG_LIBCAP_NG
 HELPERS-y += virtiofsd$(EXESUF)
 vhost-user-json-y += contrib/virtiofsd/50-qemu-virtiofsd.json
 endif
contrib/virtiofsd/passthrough_ll.c +24 -0
index 1688071c10..139261efc1 100644
@@ -47,6 +47,7 @@
 #include <string.h>
 #include <syslog.h>
 #include <limits.h>
+#include <cap-ng.h>
 #include <dirent.h>
 #include <assert.h>
 #include <errno.h>
@@ -216,6 +217,13 @@ static const struct fuse_opt lo_opts[] = {
 };
 static bool use_syslog = false;
 static int current_log_level;
+static struct {
+	pthread_mutex_t mutex;
+	void *saved;
+} cap;
+/* That we loaded cap-ng in the current thread from the saved */
+static __thread bool cap_loaded = 0;
+
 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode, uint64_t n);
 static void put_shared(struct lo_data *lo, struct lo_inode *inode);
 
@@ -242,6 +250,37 @@ static struct lo_data *lo_data(fuse_req_t req)
 	return (struct lo_data *) fuse_req_userdata(req);
 }
 
+/*
+ * Load capng's state from our saved state if the current thread
+ * hadn't previously been loaded.
+ * returns 0 on success
+ */
+static int load_capng(void)
+{
+	if (!cap_loaded) {
+		pthread_mutex_lock(&cap.mutex);
+		capng_restore_state(&cap.saved);
+		/*
+		 * restore_state free's the saved copy
+		 * so make another.
+		 */
+		cap.saved = capng_save_state();
+		if (!cap.saved) {

contrib/virtiofsd/passthrough_ll.c static struct lo_data *lo_data(fuse_req_t req) · line 278

Quoted 10 lines
contrib/virtiofsd/passthrough_ll.c +10 -0
+			fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
+			return -EINVAL;
+		}
+		pthread_mutex_unlock(&cap.mutex);
+
+		/*
+		 * We want to use the loaded state for our pid,
+		 * not the original
+		 */
+		capng_setpid(gettid());

Hi,

Quoted 2 lines

I got build error on Fedora 30 with glibc 2.29 since gettid() wrapper is introduced in glibc 2.30. How about using syscall(__NR_gettid) directly? Or is 2.30 required?

Thanks for reporting, I'm on Fedora 31 so didn't notice. I'll change it to syscall(__NR_gettid).

Dave

Quoted 68 lines

Thanks,
Misono

Quoted 65 lines
contrib/virtiofsd/passthrough_ll.c +38 -1
+		cap_loaded = true;
+	}
+	return 0;
+}
+
 /* Helpers for dropping and regaining effective capabilities. Returns 0
  * on success, error otherwise  */
 static int drop_effective_cap(const char *cap_name, bool *cap_dropped)
@@ -2754,6 +2793,35 @@ static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
 	}
 }
 
+/*
+ * Capture the capability state, we'll need to restore this for individual
+ * threads later; see load_capng.
+ */
+static void setup_capng(void)
+{
+	/* Note this accesses /proc so has to happen before the sandbox */
+	if (capng_get_caps_process()) {
+		fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
+		exit(1);
+	}
+	pthread_mutex_init(&cap.mutex, NULL);
+	pthread_mutex_lock(&cap.mutex);
+	cap.saved = capng_save_state();
+	if (!cap.saved) {
+		fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
+		exit(1);
+	}
+	pthread_mutex_unlock(&cap.mutex);
+}
+
+static void cleanup_capng(void)
+{
+	free(cap.saved);
+	cap.saved = NULL;
+	pthread_mutex_destroy(&cap.mutex);
+}
+
+
 /*
  * Make the source directory our root so symlinks cannot escape and no other
  * files are accessible.  Assumes unshare(CLONE_NEWNS) was already called.
@@ -3173,6 +3241,9 @@ int main(int argc, char *argv[])
 
 	setup_nofile_rlimit();
 
+	/* Must be before sandbox since it wants /proc */
+	setup_capng();
+
 	setup_sandbox(&lo, se, opts.syslog);
 
 	setup_root(&lo, &lo.root);
@@ -3195,6 +3266,7 @@ int main(int argc, char *argv[])
 
 err_out4:
 	fuse_session_unmount(se);
+	cleanup_capng();
 err_out3:
 	fuse_remove_signal_handlers(se);
 err_out2:
--

2.23.0

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Virtio-fs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virtio-fs

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw

PATCH 2/2 virtiofsd: Convert to libcap-ng

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

QEMU is moving to libcap-ng, so lets move as well.

---
 contrib/virtiofsd/Makefile.objs    |  2 -
 contrib/virtiofsd/passthrough_ll.c | 90 +++++++++++-------------------
 2 files changed, 33 insertions(+), 59 deletions(-)
contrib/virtiofsd/Makefile.objs +0 -2
index 25f1e8dd73..941b19f18e 100644
@@ -11,5 +11,3 @@ virtiofsd-obj-y = buffer.o \
 
 seccomp.o-cflags := $(SECCOMP_CFLAGS)
 seccomp.o-libs := $(SECCOMP_LIBS)
-
-passthrough_ll.o-libs += -lcap
contrib/virtiofsd/passthrough_ll.c +33 -57

163 lines not on this page · raw message

Signature · 1 line
2.23.0
#5 lore raw
In reply to Dr. David Alan Gilbert (git)
Quoted 9 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Hi,
This changes virtiofsd to use cap-ng rather than cap
(qemu is moving in that direction). It's a little painful because of an interaction with our sandboxing.

I intend to flatten the conversion into Vivek's patch that adds the libcap use.

It feels ugly that we store the parent process's state in a global variable and all the threads load their state from there. At the same time I don't have any good idea how to solve this problem given libcap-ng relies on /proc being there. So I am fine with the patches.

Thanks
Vivek

Quoted 15 lines

Dave

Dr. David Alan Gilbert (2):
virtiofsd: cap-ng helpers
virtiofsd: Convert to libcap-ng

 Makefile                           |   2 +-
 contrib/virtiofsd/Makefile.objs    |   2 -
 contrib/virtiofsd/passthrough_ll.c | 162 +++++++++++++++++++----------
 3 files changed, 106 insertions(+), 60 deletions(-)
Signature · 1 line
2.23.0
#6 lore raw
Quoted 8 lines · * Vivek Goyal ([email protected]) wrote:
Quoted 7 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Hi,
This changes virtiofsd to use cap-ng rather than cap
(qemu is moving in that direction). It's a little painful because of an interaction with our sandboxing.

Quoted 7 lines
Quoted 2 lines

I intend to flatten the conversion into Vivek's patch that adds the libcap use.

It feels ugly that we store the parent process's state in a global variable and all the threads load their state from there. At the same time I don't have any good idea how to solve this problem given libcap-ng relies on /proc being there. So I am fine with the patches.

Yep; I raised a cap-ng bug to show the issue, and Steve agrees it's a pain; so at least it's recorded.

Dave

Quoted 18 lines

Thanks
Vivek

Quoted 15 lines

Dave

Dr. David Alan Gilbert (2):
virtiofsd: cap-ng helpers
virtiofsd: Convert to libcap-ng

 Makefile                           |   2 +-
 contrib/virtiofsd/Makefile.objs    |   2 -
 contrib/virtiofsd/passthrough_ll.c | 162 +++++++++++++++++++----------
 3 files changed, 106 insertions(+), 60 deletions(-)
Signature · 1 line
2.23.0

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK