Go to file
Lasse Collin 0d42217123 crt: Avoid best-fit mapping when constructing argv for main()
__getmainargs() parses the command line from the return value of
GetCommandLineA() which uses best-fit mapping when converting the
native wide-char command line to the process code page. This can
create security issues. For example, fullwidth quotation mark (U+FF02)
may get converted to ASCII quotation mark (U+0022), which will break
argument quoting and can result in argument injection, for example,
if malicious filenames are passed as an argument to a program. There
are other security issues with best-fit mapping too.

Call __wgetmainargs() to get wide-argv and convert it to narrow-argv
without best-fit mapping. If conversion isn't lossless, print an
error message and _exit(255) without calling main() at all. While
this might not be ideal with every application, with most applications
a lossy conversion would be a "garbage in, garbage out" situation.
For example, lossy conversion of filenames doesn't make any sense.

Note that if _dowildcard is set, then filenames from wildcard expansion
can prevent the application from running if those filenames contain
characters that cannot be converted losslessly.

Setting the process code page to UTF-8 using an application manifest
would also fix the issue (apart from unpaired surrogates which are
invalid UTF-16 but legal on Windows command line and in filenames).
Setting UTF-8 in a manifest is only supported on Windows 10 version 1903
and later, and switching to UTF-8 could create new issues in some apps.
The method in this commit works on old Windows versions too. Even with
UTF-8, this commit matters because it blocks unpaired surrogates on the
command line.

The best-fit conversion issue affects a large number of applications
that use main() instead of wmain(). It's better to fix the issue at
toolchain level instead of trying to fix every application separately.
Examples of applications where this has already been reported:

  - The report about the issue in curl has more technical details:
    https://hackerone.com/reports/2550951

  - In XZ Utils the issue was already solved by setting UTF-8 code page:
    https://tukaani.org/xz/#_argument_injection_on_windows
    (CVE-2024-47611)

Thanks to Orange Tsai and splitline from DEVCORE Research Team
for discovering this issue.

Signed-off-by: Lasse Collin <lasse.collin@tukaani.org>
Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-10-30 01:38:42 +08:00
build-aux Regenerate configure with autoconf 2.72 2024-01-09 22:26:23 +08:00
COPYING.MinGW-w64 Remove Fixme about csq.h 2010-02-23 15:17:58 +00:00
COPYING.MinGW-w64-runtime Add 2011-2013 to copyright years 2013-02-26 20:28:23 +00:00
mingw-w64-crt crt: Avoid best-fit mapping when constructing argv for main() 2024-10-30 01:38:42 +08:00
mingw-w64-doc doc: ucrt-vs-msvcrt.txt: Update information about CRT options and links how to obtain UCRT 2024-09-02 23:17:51 +03:00
mingw-w64-headers headers: Add the _InterlockedCompareExchange8 intrinsic 2024-10-29 21:19:51 +08:00
mingw-w64-libraries winpthreads: also use SetThreadDescription to set thread name 2024-10-29 22:39:01 +08:00
mingw-w64-tools widl: Import Wine version wine-9.20. 2024-10-18 23:16:01 +02:00
.gitignore winpthreads: Ignore MSVC object files and Autotools generated files 2023-12-19 15:25:13 +02:00
aclocal.m4 Regenerate configure with autoconf 2.72 2024-01-09 22:26:23 +08:00
AUTHORS Merge commit '954b97bcfc515152cb5e666411d3ca0b256d589b' 2019-06-03 10:37:03 +02:00
config.h.in Add top level configury system 2010-02-24 17:55:02 +00:00
configure Regenerate configure with autoconf 2.72 2024-01-09 22:26:23 +08:00
configure.ac build: enable specific tools and libraries 2017-11-06 23:05:00 +00:00
COPYING Add 2013 as copyright year. 2013-02-26 20:29:09 +00:00
DISCLAIMER removed svn:executable property. 2010-01-15 20:22:39 +00:00
DISCLAIMER.PD Replace 'w64 mingw-runtime' by 'mingw-w64 runtime' 2012-06-28 15:40:59 +00:00
Makefile.am build: autoconf: support winpthreads library 2017-10-07 23:36:26 +00:00
Makefile.in Regenerate autotools scripts with automake 1.16.5 2023-11-17 13:14:02 +08:00