From ce417b3e1bea6a18889965bafee4094d4dd3e083 Mon Sep 17 00:00:00 2001 From: Christian Soffke Date: Mon, 5 Aug 2024 20:45:40 +0200 Subject: fileop: check dst path length during pre-scan Use the known difference in path length between src and dst in order to detect an insufficient buffer size not just for the source, but also for the destination path during the pre-scan already. Change-Id: I9e4caeb9b9d2cb1e9577f418f2b777ab17718acf --- apps/fileop.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/fileop.c b/apps/fileop.c index 1aaee3bb64..65a3b37e0a 100644 --- a/apps/fileop.c +++ b/apps/fileop.c @@ -51,6 +51,7 @@ struct file_op_params unsigned long long total_size; unsigned long long processed_size; size_t append; /* Append position in 'path' for stack push */ + size_t extra_len; /* Length added by dst path compared to src */ }; static int prompt_name(char* buf, size_t bufsz) @@ -131,6 +132,7 @@ static void init_file_op(struct file_op_params *param, param->toplevel_name = selected_file; } param->is_dir = dir_exists(param->path); + param->extra_len = 0; param->objects = 0; /* how many files and subdirectories*/ param->processed = 0; param->total_size = 0; @@ -209,7 +211,7 @@ static int directory_fileop(struct file_op_params *param, enum file_op_current f break; } - if (param->append >= sizeof (param->path)) { + if (param->append + param->extra_len >= sizeof (param->path)) { rc = FORC_PATH_TOO_LONG; break; /* no space left in buffer */ } @@ -523,6 +525,10 @@ int copy_move_fileobject(const char *src_path, const char *dst_path, unsigned in /* Try renaming first */ rc = move_by_rename(&src, dst.path, &flags); if (rc < FORC_SUCCESS) { + int extra_len = dst.append - src.append; + if (extra_len > 0) + src.extra_len = extra_len; + rc = check_count_fileobjects(&src); if (rc == FORC_SUCCESS) { rc = copy_move_directory(&src, &dst, flags); -- cgit v1.2.3