From 7cdf424c983636f6c7ea0b4b72127e460987bbc3 Mon Sep 17 00:00:00 2001 From: probonopd Date: Mon, 5 Aug 2024 12:55:11 +0200 Subject: [PATCH 1/3] Try to fix #51 --- src/runtime/runtime.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 8c34fa3..4dbb9b1 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -16,7 +16,7 @@ * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * copies of the Software, and to permit persons to whom the Softwaref is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in @@ -865,10 +865,25 @@ bool rm_recursive(const char* const path) { void build_mount_point(char* mount_dir, const char* const argv0, char const* const temp_base, const size_t templen) { const size_t maxnamelen = 6; + // Check for NULL argv0 + if (argv0 == NULL) { + fprintf(stderr, "Error: argv0 is NULL\n"); + return; + } + // need to copy argv0 as it's a const value, basename intends to modify it char* argv0_copy = strdup(argv0); + if (argv0_copy == NULL) { + fprintf(stderr, "Error: strdup failed\n"); + return; + } + char* path_basename = basename(argv0_copy); - free(argv0_copy); + if (path_basename == NULL) { + free(argv0_copy); + fprintf(stderr, "Error: basename returned NULL\n"); + return; + } size_t namelen = strlen(path_basename); // limit length of tempdir name @@ -876,11 +891,23 @@ void build_mount_point(char* mount_dir, const char* const argv0, char const* con namelen = maxnamelen; } + // Ensure mount_dir has enough space + size_t required_length = templen + 8 + namelen + 6 + 1; // +1 for null terminator + if (strlen(temp_base) + required_length > sizeof(mount_dir)) { + free(argv0_copy); + fprintf(stderr, "Error: mount_dir does not have enough space\n"); + return; + } + strcpy(mount_dir, temp_base); strncpy(mount_dir + templen, "/.mount_", 8); strncpy(mount_dir + templen + 8, path_basename, namelen); strncpy(mount_dir + templen + 8 + namelen, "XXXXXX", 6); - mount_dir[templen + 8 + namelen + 6] = 0; // null terminate destination + + // Null terminate the destination + mount_dir[templen + 8 + namelen + 6] = '\0'; // null terminate destination + + free(argv0_copy); } int fusefs_main(int argc, char* argv[], void (* mounted)(void)) { From e6cddd4cdea028eda7b48974f7fda6aedd1ac755 Mon Sep 17 00:00:00 2001 From: probonopd Date: Mon, 5 Aug 2024 13:05:05 +0200 Subject: [PATCH 2/3] Revert recent changes --- src/runtime/runtime.c | 38 ++++---------------------------------- 1 file changed, 4 insertions(+), 34 deletions(-) diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 4dbb9b1..93e5dd8 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -16,7 +16,7 @@ * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Softwaref is + * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in @@ -61,7 +61,6 @@ extern int sqfs_opt_proc(void* data, const char* arg, int key, struct fuse_args* #include #include #include -#include typedef struct { uint32_t lo; @@ -865,25 +864,8 @@ bool rm_recursive(const char* const path) { void build_mount_point(char* mount_dir, const char* const argv0, char const* const temp_base, const size_t templen) { const size_t maxnamelen = 6; - // Check for NULL argv0 - if (argv0 == NULL) { - fprintf(stderr, "Error: argv0 is NULL\n"); - return; - } - - // need to copy argv0 as it's a const value, basename intends to modify it - char* argv0_copy = strdup(argv0); - if (argv0_copy == NULL) { - fprintf(stderr, "Error: strdup failed\n"); - return; - } - - char* path_basename = basename(argv0_copy); - if (path_basename == NULL) { - free(argv0_copy); - fprintf(stderr, "Error: basename returned NULL\n"); - return; - } + char* path_basename; + path_basename = basename(argv0); size_t namelen = strlen(path_basename); // limit length of tempdir name @@ -891,23 +873,11 @@ void build_mount_point(char* mount_dir, const char* const argv0, char const* con namelen = maxnamelen; } - // Ensure mount_dir has enough space - size_t required_length = templen + 8 + namelen + 6 + 1; // +1 for null terminator - if (strlen(temp_base) + required_length > sizeof(mount_dir)) { - free(argv0_copy); - fprintf(stderr, "Error: mount_dir does not have enough space\n"); - return; - } - strcpy(mount_dir, temp_base); strncpy(mount_dir + templen, "/.mount_", 8); strncpy(mount_dir + templen + 8, path_basename, namelen); strncpy(mount_dir + templen + 8 + namelen, "XXXXXX", 6); - - // Null terminate the destination - mount_dir[templen + 8 + namelen + 6] = '\0'; // null terminate destination - - free(argv0_copy); + mount_dir[templen + 8 + namelen + 6] = 0; // null terminate destination } int fusefs_main(int argc, char* argv[], void (* mounted)(void)) { From 39bfcff13727a229cdf0a1e5ec275a52b517c8e6 Mon Sep 17 00:00:00 2001 From: probonopd Date: Mon, 5 Aug 2024 13:12:30 +0200 Subject: [PATCH 3/3] Try to fix build_mount_point --- src/runtime/runtime.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 93e5dd8..8917959 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -61,6 +61,7 @@ extern int sqfs_opt_proc(void* data, const char* arg, int key, struct fuse_args* #include #include #include +#include typedef struct { uint32_t lo; @@ -861,23 +862,26 @@ bool rm_recursive(const char* const path) { return rv == 0; } -void build_mount_point(char* mount_dir, const char* const argv0, char const* const temp_base, const size_t templen) { +void build_mount_point(char* mount_dir, const char* const argv0, const char* const temp_base, const size_t templen) { const size_t maxnamelen = 6; + const size_t prefix_len = 8; // Length of "/.mount_" + const size_t suffix_len = 6; // Length of "XXXXXX" - char* path_basename; - path_basename = basename(argv0); + // Create a modifiable copy of argv0 + char argv0_copy[PATH_MAX]; // Ensure this is large enough for your use case + strncpy(argv0_copy, argv0, sizeof(argv0_copy) - 1); + argv0_copy[sizeof(argv0_copy) - 1] = '\0'; // Ensure null termination + + char* path_basename = basename(argv0_copy); size_t namelen = strlen(path_basename); - // limit length of tempdir name + // Limit length of tempdir name if (namelen > maxnamelen) { namelen = maxnamelen; } - strcpy(mount_dir, temp_base); - strncpy(mount_dir + templen, "/.mount_", 8); - strncpy(mount_dir + templen + 8, path_basename, namelen); - strncpy(mount_dir + templen + 8 + namelen, "XXXXXX", 6); - mount_dir[templen + 8 + namelen + 6] = 0; // null terminate destination + // Ensure mount_dir is large enough before copying + snprintf(mount_dir, templen + prefix_len + namelen + suffix_len + 1, "%s/.mount_%.*sXXXXXX", temp_base, (int)namelen, path_basename); } int fusefs_main(int argc, char* argv[], void (* mounted)(void)) {