summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Down <chris@chrisdown.name>2020-04-14 18:15:04 +0100
committerChris Down <chris@chrisdown.name>2020-04-14 18:37:41 +0100
commite5d9d06353a683f88c750d60ec85af09fb7c2118 (patch)
treeccf3aa01edc07ffa89dbd6fb39fb3bd01ded0d99
parent3cfb7cc50771ca6ee579217c1194534313f03d8d (diff)
downloadsystemd-proot.tar.gz
virt: Detect proot virtualisation by ptrace metadataproot
proot provides userspace-powered emulation of chroot and mount --bind, lending it to be used on environments without unprivileged user namespaces, or in otherwise restricted environments like Android. In order to achieve this, proot makes use of the kernel's ptrace() facility, which we can use in order to detect its presence. Since it doesn't use any kind of namespacing, including PID namespacing, we don't need to do any tricks when trying to get the tracer's metadata. For our purposes, proot is listed as a "container", since we mostly use this also as the bucket for non-container-but-container-like technologies like WSL. As such, it seems like a good fit for this section as well.
-rw-r--r--src/basic/virt.c19
-rw-r--r--src/basic/virt.h1
2 files changed, 20 insertions, 0 deletions
diff --git a/src/basic/virt.c b/src/basic/virt.c
index f567696265..4099c2577e 100644
--- a/src/basic/virt.c
+++ b/src/basic/virt.c
@@ -441,6 +441,7 @@ static const char *const container_table[_VIRTUALIZATION_MAX] = {
[VIRTUALIZATION_PODMAN] = "podman",
[VIRTUALIZATION_RKT] = "rkt",
[VIRTUALIZATION_WSL] = "wsl",
+ [VIRTUALIZATION_PROOT] = "proot",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(container, int);
@@ -449,6 +450,7 @@ int detect_container(void) {
static thread_local int cached_found = _VIRTUALIZATION_INVALID;
_cleanup_free_ char *m = NULL;
_cleanup_free_ char *o = NULL;
+ _cleanup_free_ char *p = NULL;
const char *e = NULL;
int r;
@@ -472,6 +474,22 @@ int detect_container(void) {
goto finish;
}
+ /* proot doesn't use PID namespacing, so we can just check if we have a matching tracer for this
+ * invocation without worrying about it being elsewhere.
+ */
+ r = get_proc_field("/proc/self/status", "TracerPid", WHITESPACE, &p);
+ if (r == 0 && !streq(p, "0")) {
+ pid_t ptrace_pid;
+ r = parse_pid(p, &ptrace_pid);
+ if (r == 0) {
+ const char *pf = procfs_file_alloca(ptrace_pid, "comm");
+ char *ptrace_comm;
+ r = read_one_line_file(pf, &ptrace_comm);
+ if (r >= 0 && startswith(ptrace_comm, "proot"))
+ return VIRTUALIZATION_PROOT;
+ }
+ }
+
if (getpid_cached() == 1) {
/* If we are PID 1 we can just check our own environment variable, and that's authoritative.
* We distinguish three cases:
@@ -660,6 +678,7 @@ static const char *const virtualization_table[_VIRTUALIZATION_MAX] = {
[VIRTUALIZATION_PODMAN] = "podman",
[VIRTUALIZATION_RKT] = "rkt",
[VIRTUALIZATION_WSL] = "wsl",
+ [VIRTUALIZATION_PROOT] = "proot",
[VIRTUALIZATION_CONTAINER_OTHER] = "container-other",
};
diff --git a/src/basic/virt.h b/src/basic/virt.h
index 26f409afd0..d58c582c91 100644
--- a/src/basic/virt.h
+++ b/src/basic/virt.h
@@ -34,6 +34,7 @@ enum {
VIRTUALIZATION_PODMAN,
VIRTUALIZATION_RKT,
VIRTUALIZATION_WSL,
+ VIRTUALIZATION_PROOT,
VIRTUALIZATION_CONTAINER_OTHER,
VIRTUALIZATION_CONTAINER_LAST = VIRTUALIZATION_CONTAINER_OTHER,