Skip to contentSkip to conversation

virtiofsd capability changes and addition

PATCH Superseded Dr. David Alan Gilbert (git) 2 participants 8 messages Jun 25, 2020 – Jun 26, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover virtiofsd capability changes and addition

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

Hi,
This is a set of changes relating to the capability restirctions
introduced in virtiofsd back in a59feb483b8.

The first one is a potentially important fix; the missing terminator could mean extra capabilities are added based on junk on the stack; although that's not been seen in practice.

Dr. David Alan Gilbert (3):
virtiofsd: Terminate capability list
virtiofsd: Check capability calls
virtiofsd: Allow addition or removal of capabilities

 docs/tools/virtiofsd.rst         |  5 +++
 tools/virtiofsd/helper.c         |  2 +
 tools/virtiofsd/passthrough_ll.c | 68 +++++++++++++++++++++++++++++---
 3 files changed, 70 insertions(+), 5 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/3 virtiofsd: Terminate capability list

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

capng_updatev is a varargs function that needs a -1 to terminate it, but it was missing.

In practice what seems to have been happening is that it's added the capabilities we asked for, then runs into junk on the stack, so if we're unlucky it might be adding some more, but in reality it's failing - but after adding the capabilities we asked for.

Fixes a59feb483b8 ("virtiofsd: only retain file system capabilities")
---
 tools/virtiofsd/passthrough_ll.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/virtiofsd/passthrough_ll.c +3 -1
index 2ce7c96085..e373e3b36e 100644
@@ -2598,7 +2598,9 @@ static void setup_capabilities(void)
             CAP_SETGID,
             CAP_SETUID,
             CAP_MKNOD,
-            CAP_SETFCAP);
+            CAP_SETFCAP,
+            -1);
+
     capng_apply(CAPNG_SELECT_BOTH);
 
     cap.saved = capng_save_state();
Signature · 1 line
2.26.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] virtiofsd: Terminate capability list

Quoted 10 lines · Dr. David Alan Gilbert (git)

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

capng_updatev is a varargs function that needs a -1 to terminate it, but it was missing.

In practice what seems to have been happening is that it's added the capabilities we asked for, then runs into junk on the stack, so if we're unlucky it might be adding some more, but in reality it's failing - but after adding the capabilities we asked for.

Quoted 5 lines
Fixes a59feb483b8 ("virtiofsd: only retain file system capabilities")
---
 tools/virtiofsd/passthrough_ll.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
#3 lore raw

PATCH 2/3 virtiofsd: Check capability calls

In reply to Dr. David Alan Gilbert (git)

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

Check the capability calls worked.

---
 tools/virtiofsd/passthrough_ll.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
tools/virtiofsd/passthrough_ll.c +13 -3
index e373e3b36e..99d562046a 100644
@@ -2589,7 +2589,7 @@ static void setup_capabilities(void)
      */
     capng_setpid(syscall(SYS_gettid));
     capng_clear(CAPNG_SELECT_BOTH);
-    capng_updatev(CAPNG_ADD, CAPNG_PERMITTED | CAPNG_EFFECTIVE,
+    if (capng_updatev(CAPNG_ADD, CAPNG_PERMITTED | CAPNG_EFFECTIVE,
             CAP_CHOWN,
             CAP_DAC_OVERRIDE,
             CAP_DAC_READ_SEARCH,
@@ -2599,11 +2599,21 @@ static void setup_capabilities(void)
             CAP_SETUID,
             CAP_MKNOD,
             CAP_SETFCAP,
-            -1);
+            -1)) {
+        fuse_log(FUSE_LOG_ERR, "%s: capng_updatev failed\n", __func__);
+        exit(1);
+    }
 
-    capng_apply(CAPNG_SELECT_BOTH);
+    if (capng_apply(CAPNG_SELECT_BOTH)) {
+        fuse_log(FUSE_LOG_ERR, "%s: capng_apply failed\n", __func__);
+        exit(1);
+    }
 
     cap.saved = capng_save_state();
+    if (!cap.saved) {
+        fuse_log(FUSE_LOG_ERR, "%s: capng_save_state failed\n", __func__);
+        exit(1);
+    }
     pthread_mutex_unlock(&cap.mutex);
 }
 
Signature · 1 line
2.26.2
#5 lore raw

PATCH 3/3 virtiofsd: Allow addition or removal of capabilities

In reply to Dr. David Alan Gilbert (git)

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

Allow capabilities to be added or removed from the allowed set for the daemon; e.g.

default:
CapPrm: 00000000880000df
CapEff: 00000000880000df

-o modcaps=+sys_admin

CapPrm: 00000000882000df
CapEff: 00000000882000df

-o modcaps=+sys_admin:-chown

CapPrm: 00000000882000de
CapEff: 00000000882000de

---
 docs/tools/virtiofsd.rst         |  5 ++++
 tools/virtiofsd/helper.c         |  2 ++
 tools/virtiofsd/passthrough_ll.c | 50 ++++++++++++++++++++++++++++++--
 3 files changed, 55 insertions(+), 2 deletions(-)
docs/tools/virtiofsd.rst +5 -0
index 378594c422..824e713491 100644
@@ -54,6 +54,11 @@ Options
   * flock|no_flock -
     Enable/disable flock.  The default is ``no_flock``.
 
+  * modcaps=CAPLIST
+    Modify the list of capabilities allowed; CAPLIST is a colon separated
+    list of capabilities, each preceded by either + or -, e.g.
+    ''+sys_admin:-chown''.
+
   * log_level=LEVEL -
     Print only log messages matching LEVEL or more severe.  LEVEL is one of
     ``err``, ``warn``, ``info``, or ``debug``.  The default is ``info``.
tools/virtiofsd/helper.c +2 -0
index 00a1ef666a..3105b6c23a 100644
@@ -174,6 +174,8 @@ void fuse_cmdline_help(void)
            "                               default: no_writeback\n"
            "    -o xattr|no_xattr          enable/disable xattr\n"
            "                               default: no_xattr\n"
+           "    -o modcaps=CAPLIST         Modify the list of capabilities\n"
+           "                               e.g. -o modcaps=+sys_admin:-chown\n"
            "    --rlimit-nofile=<num>      set maximum number of file descriptors\n"
            "                               (0 leaves rlimit unchanged)\n"
            "                               default: min(1000000, fs.file-max - 16384)\n"
tools/virtiofsd/passthrough_ll.c +48 -2

80 lines not on this page · raw message

Signature · 1 line
2.26.2
#6 lore raw

Re: [PATCH 3/3] virtiofsd: Allow addition or removal of capabilities

Quoted 32 lines · Dr. David Alan Gilbert (git)
tools/virtiofsd/passthrough_ll.c +32 -0
+    /*
+     * The modcaps option is a colon separated list of caps,
+     * each preceded by either + or -.
+     */
+    while (lo->modcaps) {
+        capng_act_t action;
+        int cap;
+
+        char *next = strchr(lo->modcaps, ':');
+        if (next) {
+            *next = '\0';
+            next++;
+        }
+
+        switch (lo->modcaps[0]) {
+        case '+':
+            action = CAPNG_ADD;
+            break;
+
+        case '-':
+            action = CAPNG_DROP;
+            break;
+
+        default:
+            fuse_log(FUSE_LOG_ERR,
+                     "%s: Expecting '+'/'-' in modcaps but found '%c'\n",
+                     __func__, lo->modcaps[0]);
+            exit(1);
+        }
+        cap = capng_name_to_capability(lo->modcaps + 1);
+        if (cap < 0) {
+            fuse_log(FUSE_LOG_ERR, "%s: Unknown capability '%s'\n", __func__,

tools/virtiofsd/passthrough_ll.c static void setup_capabilities(void) · line 2650

Quoted 10 lines
tools/virtiofsd/passthrough_ll.c +10 -0
+                     lo->modcaps);
+            exit(1);
+        }
+        if (capng_update(action, CAPNG_PERMITTED | CAPNG_EFFECTIVE, cap)) {
+            fuse_log(FUSE_LOG_ERR, "%s: capng_update failed for '%s'\n",
+                     __func__, lo->modcaps);
+            exit(1);
+        }
+
+        lo->modcaps = next;

How about passing char *modcaps into this function so that lo->modcaps isn't modified by the parsing loop? That seems a bit cleaner and if we ever decide to free lo->modcaps it will work as expected.

Stefan

#7 lore raw

Re: [PATCH 3/3] virtiofsd: Allow addition or removal of capabilities

Quoted 39 lines · * Stefan Hajnoczi ([email protected]) wrote:
Quoted 38 lines · Dr. David Alan Gilbert (git)
tools/virtiofsd/passthrough_ll.c +38 -0
+    /*
+     * The modcaps option is a colon separated list of caps,
+     * each preceded by either + or -.
+     */
+    while (lo->modcaps) {
+        capng_act_t action;
+        int cap;
+
+        char *next = strchr(lo->modcaps, ':');
+        if (next) {
+            *next = '\0';
+            next++;
+        }
+
+        switch (lo->modcaps[0]) {
+        case '+':
+            action = CAPNG_ADD;
+            break;
+
+        case '-':
+            action = CAPNG_DROP;
+            break;
+
+        default:
+            fuse_log(FUSE_LOG_ERR,
+                     "%s: Expecting '+'/'-' in modcaps but found '%c'\n",
+                     __func__, lo->modcaps[0]);
+            exit(1);
+        }
+        cap = capng_name_to_capability(lo->modcaps + 1);
+        if (cap < 0) {
+            fuse_log(FUSE_LOG_ERR, "%s: Unknown capability '%s'\n", __func__,
+                     lo->modcaps);
+            exit(1);
+        }
+        if (capng_update(action, CAPNG_PERMITTED | CAPNG_EFFECTIVE, cap)) {
+            fuse_log(FUSE_LOG_ERR, "%s: capng_update failed for '%s'\n",
+                     __func__, lo->modcaps);
Quoted 8 lines

tools/virtiofsd/passthrough_ll.c static void setup_capabilities(void) · line 2650

Quoted 4 lines
tools/virtiofsd/passthrough_ll.c +4 -0
+            exit(1);
+        }
+
+        lo->modcaps = next;

How about passing char *modcaps into this function so that lo->modcaps isn't modified by the parsing loop? That seems a bit cleaner and if we ever decide to free lo->modcaps it will work as expected.

Yep, can do.

Dave

Quoted 1 line

Stefan

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