From 50cbbf1a944a138ae621a36bb155c459b7e54d80 Mon Sep 17 00:00:00 2001 From: Dhanraj E Date: Fri, 20 Nov 2026 16:23:12 +0530 Subject: [PATCH] Fixed stack buffer overflow parsing cifs and panfs mount sources LoadMountInfo() hands the first field of a mount(8) line to GetHostAndSource() when any word on the line is "cifs" or "panfs". That helper split the field into host[256] and source[1024] with copy loops that had no bounds and no end-of-string check: the cifs/panfs branch copied until it had seen three slashes, the nfs branch until it found a ':', and the source copy ran to the end of the field, which can be 4095 bytes. A long server name overflowed host, a long share path overflowed source, and a field with fewer than three slashes or without a ':' was read past its terminating NUL. The panfs and cifs flags were also never reset between lines (only nfs was), so after the first cifs or panfs mount every later line whose source is not an absolute path, such as a tmpfs mounted afterwards, was parsed this way too. mount(8) prints sources and mountpoints verbatim, so such a line can come from a FUSE mount, whose fsname and mountpoint are chosen by the user who mounts it. LoadMountInfo() runs in the root agent for any storage promise. Measure the host and source parts first and leave both empty when they do not fit, and reset panfs and cifs for every line like nfs. Changelog: Title Signed-off-by: Dhanraj E --- cf-agent/nfs.c | 70 ++++++++++++++++++++++++------------------- tests/unit/nfs_test.c | 48 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 31 deletions(-) diff --git a/cf-agent/nfs.c b/cf-agent/nfs.c index e983d3af67a..c8664835d89 100644 --- a/cf-agent/nfs.c +++ b/cf-agent/nfs.c @@ -48,7 +48,8 @@ static int FSTAB_EDITS = 0; /* GLOBAL_X */ static Item *FSTABLIST = NULL; /* GLOBAL_X */ -static void GetHostAndSource(const char *buf, char *host, char *source); +static void GetHostAndSource(const char *buf, char *host, size_t host_size, + char *source, size_t source_size); static bool ConflictingOptions(const char *a, const char *b); static bool OptionPresent(const char *opt, const Seq *actual); @@ -136,49 +137,52 @@ static const char *const VMOUNTOPTS[] = [PLATFORM_CONTEXT_ANDROID] = "defaults", /* android */ }; -static void GetHostAndSource(const char *buf, char *host, char *source) +static void GetHostAndSource(const char *buf, char *host, size_t host_size, + char *source, size_t source_size) /* Extracts from buffer host & source of nfs, cifs and panfs network systems */ { - assert(StringStartsWith(buf, "panfs://") - || StringStartsWith(buf, "//") - || strchr(buf, ':') != NULL); - int index = 0; // Index for buf and host - int source_index = 0; - size_t slashes = 0; - - // panfs & cifs + size_t host_len; + size_t source_start; + + // panfs & cifs, e.g. "//server/share" or "panfs://server/volume": the + // host is everything before the third slash if (StringStartsWith(buf, "panfs://") || StringStartsWith(buf, "//")) { - // copy host - while (slashes != 3) + size_t slashes = 0; + for (host_len = 0; buf[host_len] != '\0'; host_len++) { - // TODO: Add example of what this string looks like - if (buf[index] == '/' || buf[index] == '\\') + if ((buf[host_len] == '/' || buf[host_len] == '\\') && + (++slashes == 3)) { - slashes++; + break; } - host[index] = buf[index]; - index++; } - host[--index] = '\0'; + source_start = host_len; } - // nfs + // nfs, e.g. "server:/export" else { - while (buf[index] != ':') - { - host[index] = buf[index]; - index++; - } - host[index++] = '\0'; + host_len = strcspn(buf, ":"); + source_start = (buf[host_len] == ':') ? host_len + 1 : host_len; } - // copy source - while ((buf[index] != '\0') && (buf[index] != ' ')) + const size_t source_len = strcspn(buf + source_start, " "); + + host[0] = '\0'; + source[0] = '\0'; + + if ((host_len >= host_size) || (source_len >= source_size)) { - source[source_index++] = buf[index++]; + Log(LOG_LEVEL_VERBOSE, + "Mount source '%s' is too long, ignoring its host and source", + buf); + return; } - source[source_index] = '\0'; + + memcpy(host, buf, host_len); + host[host_len] = '\0'; + memcpy(source, buf + source_start, source_len); + source[source_len] = '\0'; } /** @@ -415,6 +419,8 @@ bool LoadMountInfo(Seq *list) { buf1[0] = buf2[0] = buf3[0] = '\0'; nfs = false; + panfs = false; + cifs = false; ssize_t res = CfReadLine(&vbuff, &vbuff_size, pp); if (res == -1) @@ -505,7 +511,8 @@ bool LoadMountInfo(Seq *list) { if (cifs || panfs) { - GetHostAndSource(buf1, host, source); + GetHostAndSource(buf1, host, sizeof(host), + source, sizeof(source)); } else { @@ -545,7 +552,8 @@ bool LoadMountInfo(Seq *list) { if (cifs || panfs) { - GetHostAndSource(buf1, host, source); + GetHostAndSource(buf1, host, sizeof(host), + source, sizeof(source)); } else { diff --git a/tests/unit/nfs_test.c b/tests/unit/nfs_test.c index 2e0c403e812..e960eac5ff3 100644 --- a/tests/unit/nfs_test.c +++ b/tests/unit/nfs_test.c @@ -174,6 +174,53 @@ static void test_GetFstabEntryOptions(void) assert_true(GetFstabEntryOptions("/mnt/opts/nosuchmount") == NULL); } +static void test_GetHostAndSource(void) +{ + char host[256], source[1024]; + + GetHostAndSource("//server/share/dir", host, sizeof(host), + source, sizeof(source)); + assert_true(StringEqual(host, "//server")); + assert_true(StringEqual(source, "/share/dir")); + + GetHostAndSource("panfs://server/volume", host, sizeof(host), + source, sizeof(source)); + assert_true(StringEqual(host, "panfs://server")); + assert_true(StringEqual(source, "/volume")); + + GetHostAndSource("server:/export", host, sizeof(host), + source, sizeof(source)); + assert_true(StringEqual(host, "server")); + assert_true(StringEqual(source, "/export")); + + /* The first field of a mount(8) line is whatever the filesystem was + * mounted from (e.g. a FUSE fsname), so it may lack the separators... */ + GetHostAndSource("//server", host, sizeof(host), source, sizeof(source)); + assert_true(StringEqual(host, "//server")); + assert_true(StringEqual(source, "")); + + GetHostAndSource("tmpfs", host, sizeof(host), source, sizeof(source)); + assert_true(StringEqual(host, "tmpfs")); + assert_true(StringEqual(source, "")); + + /* ...or be longer than host and source. */ + char buf[CF_BUFSIZE]; + + memset(buf, 'h', sizeof(buf)); + memcpy(buf, "//", 2); + memcpy(buf + 2 + sizeof(host), "/share", sizeof("/share")); + GetHostAndSource(buf, host, sizeof(host), source, sizeof(source)); + assert_true(StringEqual(host, "")); + assert_true(StringEqual(source, "")); + + memset(buf, 'd', sizeof(buf)); + memcpy(buf, "//server/share/", 15); + buf[sizeof(buf) - 1] = '\0'; + GetHostAndSource(buf, host, sizeof(host), source, sizeof(source)); + assert_true(StringEqual(host, "")); + assert_true(StringEqual(source, "")); +} + int main() { PRINT_TEST_BANNER(); @@ -183,6 +230,7 @@ int main() unit_test(test_OptionStringExpandDefaults), unit_test(test_MountOptionsFromLine), unit_test(test_GetFstabEntryOptions), + unit_test(test_GetHostAndSource), }; return run_tests(tests);