[PATCH v3 1/1] hv_fcopy_uio_daemon: Fix file copy failure between Windows host and Linux guest

yasuenag
2025-06-03T23:43:17Z

Handle file copy request from the host (e.g. Copy-VMFile commandlet)
correctly.
Store file path and name as __u16 arrays in struct hv_start_fcopy.
Convert directly to UTF-8 string without casting to wchar_t* in fcopyd.

Fix string conversion failure caused by wchar_t size difference between
Linux (32bit) and Windows (16bit). Convert each character to char
if the value is less than 0x80 instead of using wcstombs() call.

Add new check to snprintf() call for target path creation to handle
length differences between PATH_MAX (Linux) and W_MAX_PATH (Windows).

Signed-off-by: Yasumasa Suenaga <yasuenag@gmail.com>
---
tools/hv/hv_fcopy_uio_daemon.c | 37 ++++++++++++++--------------------
1 file changed, 15 insertions(+), 22 deletions(-)

diff --git a/tools/hv/hv_fcopy_uio_daemon.c b/tools/hv/hv_fcopy_uio_daemon.c
index 0198321d1..86702f39e 100644
--- a/tools/hv/hv_fcopy_uio_daemon.c
+++ b/tools/hv/hv_fcopy_uio_daemon.c
@@ -62,8 +62,11 @@ static int hv_fcopy_create_file(char *file_name, char *path_name, __u32 flags)

filesize = 0;
p = path_name;
- snprintf(target_fname, sizeof(target_fname), "%s/%s",
- path_name, file_name);
+ if (snprintf(target_fname, sizeof(target_fname), "%s/%s",
+ path_name, file_name) >= sizeof(target_fname)) {
+ /* target file name is too long */
+ goto done;
+ }

/*
* Check to see if the path is already in place; if not,
@@ -273,6 +276,8 @@ static void wcstoutf8(char *dest, const __u16 *src, size_t dest_size)
while (len < dest_size) {
if (src[len] < 0x80)
dest[len++] = (char)(*src++);
+ else if (src[len] == '0')
+ break;
else
dest[len++] = 'X';
}
@@ -282,27 +287,15 @@ static void wcstoutf8(char *dest, const __u16 *src, size_t dest_size)

static int hv_fcopy_start(struct hv_start_fcopy *smsg_in)
{
- setlocale(LC_ALL, "en_US.utf8");
- size_t file_size, path_size;
- char *file_name, *path_name;
- char *in_file_name = (char *)smsg_in->file_name;
- char *in_path_name = (char *)smsg_in->path_name;
-
- file_size = wcstombs(NULL, (const wchar_t *restrict)in_file_name, 0) + 1;
- path_size = wcstombs(NULL, (const wchar_t *restrict)in_path_name, 0) + 1;
-
- file_name = (char *)malloc(file_size * sizeof(char));
- path_name = (char *)malloc(path_size * sizeof(char));
-
- if (!file_name || !path_name) {
- free(file_name);
- free(path_name);
- syslog(LOG_ERR, "Can't allocate memory for file name and/or path name");
- return HV_E_FAIL;
- }
+ /*
+ * file_name and path_name should have same length with appropriate
+ * member of hv_start_fcopy.
+ */
+ char file_name[W_MAX_PATH], path_name[W_MAX_PATH];

- wcstoutf8(file_name, (__u16 *)in_file_name, file_size);
- wcstoutf8(path_name, (__u16 *)in_path_name, path_size);
+ setlocale(LC_ALL, "en_US.utf8");
+ wcstoutf8(file_name, smsg_in->file_name, W_MAX_PATH - 1);
+ wcstoutf8(path_name, smsg_in->path_name, W_MAX_PATH - 1);

return hv_fcopy_create_file(file_name, path_name, smsg_in->copy_flags);
}
--
2.49.0