The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxcfs/pull/428

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
`dirname()` behaves differently on `glibc` vs POSIX implementations (e.g. `musl`).

Under `glibc`, the use of `dirname()` terminates at `/` as required, but the same code on `musl` ends at, for example, `/lxc.payload.dear-raccoon`, resulting in an infinite loop when trying to read `/proc/meminfo` in a container.

A simple replacement for `dirname()` solves this on non-`glibc` platforms like Alpine. :smile: 
From db36731bf3598d0f6fe5f493b1df59ed6edd16ce Mon Sep 17 00:00:00 2001
From: Jack O'Sullivan <jackos1...@gmail.com>
Date: Thu, 3 Sep 2020 22:39:05 +0100
Subject: [PATCH] Fix `get_min_memlimit()` on non-glibc

Signed-off-by: Jack O'Sullivan <j...@ckos.ie>
---
 src/proc_fuse.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index a99162c..7e957d0 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -213,6 +213,25 @@ static uint64_t get_memlimit(const char *cgroup, bool swap)
        return memlimit;
 }
 
+/*
+ * dirname() behaves differently on musl than glibc, so implement the bits we
+ * need (the path in this case is always absolute and never ends in a `/`)
+ */
+static char *cgroup_parent(char *cgroup)
+{
+       if (strcmp(cgroup, "/") == 0) {
+               return cgroup;
+       }
+
+       char *last_slash = strrchr(cgroup, '/');
+       if (last_slash == cgroup) {
+               last_slash++;
+       }
+       *last_slash = '\0';
+
+       return cgroup;
+}
+
 static uint64_t get_min_memlimit(const char *cgroup, bool swap)
 {
        __do_free char *copy = NULL;
@@ -227,7 +246,7 @@ static uint64_t get_min_memlimit(const char *cgroup, bool 
swap)
        while (strcmp(copy, "/") != 0) {
                char *it = copy;
 
-               it = dirname(it);
+               it = cgroup_parent(it);
                memlimit = get_memlimit(it, swap);
                if (memlimit > 0 && memlimit < retlimit)
                        retlimit = memlimit;
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to