mingw-w64/mingw-w64-crt
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
cfguard crt: Mark __guard_dispatch_icall_dummy as global 2023-10-31 22:48:43 +02:00
complex catanh: Use approximations for small real part 2022-08-28 23:16:29 +03:00
crt crt: Avoid best-fit mapping when constructing argv for main() 2024-10-30 01:38:42 +08:00
def-include crt: Rename file ucrt_compat.c to ucrt_tzset.c 2024-10-11 11:13:26 +03:00
gdtoa gdtoa: Sync to match the latest version at netlib.org 2023-06-12 23:51:11 +03:00
include crt: Provide a common __mingw_has_sse() 2024-10-23 21:48:32 +08:00
intrincs headers: Add the _InterlockedCompareExchange8 intrinsic 2024-10-29 21:19:51 +08:00
lib32 crt: Add winbrand import library 2024-10-13 19:19:00 +08:00
lib64 crt: Add winbrand import library 2024-10-13 19:19:00 +08:00
lib-common crt: Add winbrand import library 2024-10-13 19:19:00 +08:00
libarm32 crt: Add winbrand import library 2024-10-13 19:19:00 +08:00
libarm64 crt: Add winbrand import library 2024-10-13 19:19:00 +08:00
libce crt: Do not export DllMain from def files 2024-03-02 22:57:10 +02:00
libsrc crt: Use (void*) casts to silence warnings about function pointer casts 2024-09-27 00:27:28 +03:00
math crt: Enable HUGE symbol alias 2024-09-27 23:36:07 +03:00
mingw define WIN32_LEAN_AND_MEAN if not defined already. 2009-08-22 18:11:19 +00:00
misc crt: Provide a common __mingw_has_sse() 2024-10-23 21:48:32 +08:00
profile crt: Fix profil.c compile warning: cast between incompatible function types 2024-08-13 22:26:03 +08:00
secapi crt: Use (void*) casts to silence warnings about function pointer casts 2024-09-27 00:27:28 +03:00
ssp crt: Implement __gets_chk 2023-02-14 00:34:11 +02:00
stdio crt: Split UCRT __ms_fwprintf() into separate file 2024-10-11 11:13:26 +03:00
string crt/string: add parentheses to HASZERO macro to suppress gcc warning 2020-06-12 13:39:55 +03:00
testcases crt: Implement tdestroy 2022-10-23 21:58:24 +08:00
.gitignore crt: Add generated DEFs in .gitignore 2022-12-19 22:24:53 +08:00
aclocal.m4 Regenerate configure with autoconf 2.72 2024-01-09 22:26:23 +08:00
ChangeLog 2014-05-19 Corinna Vinschen <vinschen@redhat.com> 2014-05-19 18:56:41 +02:00
ChangeLog.2009 Use consistent ChangeLog naming 2012-11-16 21:11:30 +00:00
ChangeLog.2010 Changelog move on new year. 2011-01-02 11:04:05 +00:00
ChangeLog.2011 Rotate ChangeLog 2012-01-06 14:52:24 +00:00
ChangeLog.2012 Bump 2012 2013-01-11 16:06:50 +00:00
ChangeLog.2013 Rotate 2013 log 2014-01-25 02:21:20 +00:00
config.h.in Regenerate configure with autoconf 2.72 2024-01-09 22:26:23 +08:00
configure Regenerate configure with autoconf 2.72 2024-01-09 22:26:23 +08:00
configure.ac crt: Remove support for building with genlib 2023-09-08 10:40:27 +03:00
Makefile.am headers: Add the _InterlockedCompareExchange8 intrinsic 2024-10-29 21:19:51 +08:00
Makefile.in crt: Regenerate Makefile.in 2024-10-29 21:27:30 +08:00
revstamp.h