The problem: several call sites relied on unchecked string-copy APIs whose safety depended on separate allocation calculations.
- w32dlfcn.h used wcscpy() when appending a DLL name to a dynamically allocated Windows path. The destination was sized beforehand, but the unbounded copy made the safety requirement implicit. - mscl.c used strcpy() and strcat() to build host paths and the WSLENV value. It also used strcpy() to append .d and .tmp suffixes to allocated dependency-file paths. - avio.c used strcpy() to copy a URL filename into an allocation sized from strlen(filename), and to initialize the fixed-size protocol buffer. These operations were currently sized correctly, but the APIs provided no protection if the surrounding calculations changed. The fix: replace wcscpy() with wmemcpy() using the known wide-string length, and replace the strcpy()/strcat() sequences with explicit length calculations and memcpy() calls that preserve the terminating null byte. Use FFmpeg's av_strlcpy() for the fixed-size protocol buffer. Valid path, environment, filename, and protocol behavior remains unchanged, while the destination sizes and copied lengths are now explicit and easier to review. No public API behavior is changed. Signed-off-by:Mohammed Mustafa <[email protected]> --- compat/w32dlfcn.h | 4 ++-- compat/windows/mscl.c | 23 ++++++++++++++--------- libavformat/avio.c | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h index ac20e83a7a..e2c265770b 100644 --- a/compat/w32dlfcn.h +++ b/compat/w32dlfcn.h @@ -89,7 +89,7 @@ static inline HMODULE win32_dlopen(const char *name) if (!new_path) goto exit; path = new_path; - wcscpy(path + pathlen + 1, name_w); + wmemcpy(path + pathlen + 1, name_w, namelen + 1); module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); if (module == NULL) { // Next try System32 directory @@ -111,7 +111,7 @@ static inline HMODULE win32_dlopen(const char *name) goto exit; } path[pathlen] = L'\\'; - wcscpy(path + pathlen + 1, name_w); + wmemcpy(path + pathlen + 1, name_w, namelen + 1); module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); } exit: diff --git a/compat/windows/mscl.c b/compat/windows/mscl.c index 8f30abfcb2..156c5bd05c 100644 --- a/compat/windows/mscl.c +++ b/compat/windows/mscl.c @@ -328,10 +328,14 @@ static char *host_path(const char *path) return NULL; /* the root keeps its slash, the host form lost it */ - out = xrealloc(NULL, strlen(host) + strlen(path + r) + 2); - strcpy(out, host); - strcat(out, "/"); - strcat(out, path + r); + { + size_t host_len = strlen(host); + size_t suffix_len = strlen(path + r); + out = xrealloc(NULL, host_len + suffix_len + 2); + memcpy(out, host, host_len); + out[host_len] = '/'; + memcpy(out + host_len + 1, path + r, suffix_len + 1); + } return out; } @@ -577,9 +581,10 @@ static int run(char **args) * listed in WSLENV */ wslenv = getenv("WSLENV"); if (wslenv && *wslenv) { - char *e = xrealloc(NULL, strlen(wslenv) + sizeof(":VSLANG")); - strcpy(e, wslenv); - strcat(e, ":VSLANG"); + size_t wslenv_len = strlen(wslenv); + char *e = xrealloc(NULL, wslenv_len + sizeof(":VSLANG")); + memcpy(e, wslenv, wslenv_len); + memcpy(e + wslenv_len, ":VSLANG", sizeof(":VSLANG")); setenv("WSLENV", e, 1); free(e); } else { @@ -639,7 +644,7 @@ static int write_depfile(void) memcpy(path, obj, len + 1); if (len > 2 && !strcmp(path + len - 2, ".o")) len -= 2; - strcpy(path + len, ".d"); + memcpy(path + len, ".d", sizeof(".d")); if (unmapped) { fprintf(stderr, "mscl: cannot map %s to a host path, " @@ -649,7 +654,7 @@ static int write_depfile(void) tmp = xrealloc(NULL, len + 7); memcpy(tmp, path, len + 2); - strcpy(tmp + len + 2, ".tmp"); + memcpy(tmp + len + 2, ".tmp", sizeof(".tmp")); f = open_wb(tmp); if (f) { diff --git a/libavformat/avio.c b/libavformat/avio.c index f60bc0eae3..366797c3fa 100644 --- a/libavformat/avio.c +++ b/libavformat/avio.c @@ -149,7 +149,7 @@ static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up, } uc->av_class = &url_context_class; uc->filename = (char *)&uc[1]; - strcpy(uc->filename, filename); + memcpy(uc->filename, filename, strlen(filename) + 1); uc->prot = up; uc->flags = flags; uc->is_streamed = 0; /* default = not streamed */ @@ -326,7 +326,7 @@ static const struct URLProtocol *url_find_protocol(const char *filename) if (filename[proto_len] != ':' && (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) || is_dos_path(filename)) - strcpy(proto_str, "file"); + av_strlcpy(proto_str, "file", sizeof(proto_str)); else av_strlcpy(proto_str, filename, FFMIN(proto_len + 1, sizeof(proto_str))); -- 2.53.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
