Have execve() escape double quotes in cmd.exe's preferred style

This makes it possible for us to use system() and popen() with paths
that redirect to filenames that contain spaces, e.g.

    system("echo.com hello >\"hello there.txt\"")

It's difficult to solve this problem, because WIN32 only allows passing
one single argument when launching programs and each program is allowed
to tokenize that however it wants. Most software follows the convention
of cmd.exe which is poorly documented and positively byzantine.

In the future we're going to solve this by not using cmd.exe at all and
instead embedding the cocmd.com interpreter into the system() function.
In the meantime, our documentation has been updated to help recalibrate
any expectation the user might hold regarding the security of using the
Windows command interpreter.

Fixes #644
This commit is contained in:
Justine Tunney 2022-10-02 08:43:25 -07:00
parent acd8900071
commit 3f3cb0650b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
15 changed files with 223 additions and 95 deletions

View file

@ -104,16 +104,14 @@ textwindows noinstrument noasan void FixPath(char *path) {
}
}
/**
* Transcodes NT environment variable block from UTF-16 to UTF-8.
*
* @param env is a double NUL-terminated block of key=values
* @param buf is the new environment which gets double-nul'd
* @param size is the byte capacity of buf
* @param envp stores NULL-terminated string pointer list (optional)
* @param max is the pointer count capacity of envp
* @return number of variables decoded, excluding NULL-terminator
*/
// Transcodes NT environment variable block from UTF-16 to UTF-8.
//
// @param env is a double NUL-terminated block of key=values
// @param buf is the new environment which gets double-nul'd
// @param size is the byte capacity of buf
// @param envp stores NULL-terminated string pointer list (optional)
// @param max is the pointer count capacity of envp
// @return number of variables decoded, excluding NULL-terminator
textwindows noasan noinstrument int GetDosEnviron(const char16_t *env,
char *buf, size_t size,
char **envp, size_t max) {