Skip to content

Commit dc1de2f

Browse files
committed
windows: clarify windowsCreateProcess, add errors of CreateProcessW, LoadLibraryW
These are the non-controversial parts of implementing ziglang#14251 from https://github.com/matu3ba/win32k-mitigation. NOACCESS can occur, if the process is not permitted to access memory provided to UpdateProcThreadAttribute. DLL_INIT_FAILED occurs on first failure to load restricted libraries with win32k mitigation with followup failing loads returning nonsensical NOT_ENOUGH_MEMORY.
1 parent e025ad7 commit dc1de2f

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib/std/child_process.zig

+13-17
Original file line numberDiff line numberDiff line change
@@ -1134,23 +1134,19 @@ fn windowsCreateProcessPathExt(
11341134
}
11351135

11361136
fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u16, cwd_ptr: ?[*:0]u16, lpStartupInfo: *windows.STARTUPINFOW, lpProcessInformation: *windows.PROCESS_INFORMATION) !void {
1137-
// TODO the docs for environment pointer say:
1138-
// > A pointer to the environment block for the new process. If this parameter
1139-
// > is NULL, the new process uses the environment of the calling process.
1140-
// > ...
1141-
// > An environment block can contain either Unicode or ANSI characters. If
1142-
// > the environment block pointed to by lpEnvironment contains Unicode
1143-
// > characters, be sure that dwCreationFlags includes CREATE_UNICODE_ENVIRONMENT.
1144-
// > If this parameter is NULL and the environment block of the parent process
1145-
// > contains Unicode characters, you must also ensure that dwCreationFlags
1146-
// > includes CREATE_UNICODE_ENVIRONMENT.
1147-
// This seems to imply that we have to somehow know whether our process parent passed
1148-
// CREATE_UNICODE_ENVIRONMENT if we want to pass NULL for the environment parameter.
1149-
// Since we do not know this information that would imply that we must not pass NULL
1150-
// for the parameter.
1151-
// However this would imply that programs compiled with -DUNICODE could not pass
1152-
// environment variables to programs that were not, which seems unlikely.
1153-
// More investigation is needed.
1137+
// See https://stackoverflow.com/a/4207169/9306292
1138+
// One can manually write in unicode even if one doesn't compile in unicode
1139+
// (-DUNICODE).
1140+
// Thus CREATE_UNICODE_ENVIRONMENT, according to how one constructed the
1141+
// environment block, is necessary, since CreateProcessA and CreateProcessW
1142+
// may work with either Ansi or Unicode.
1143+
// * The environment variables can still be inherited from parent process,
1144+
// if set to NULL
1145+
// * The OS can for an unspecified environment block not figure out, if it
1146+
// is Unicode or ANSI.
1147+
// * Applications may break without specification of the environment
1148+
// variable due to inability of Windows to check and translate the
1149+
// character encodings.
11541150
return windows.CreateProcessW(
11551151
app_name,
11561152
cmd_line,

lib/std/os/windows.zig

+5
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,7 @@ pub fn CreateProcessW(
17551755
.PATH_NOT_FOUND => return error.FileNotFound,
17561756
.ACCESS_DENIED => return error.AccessDenied,
17571757
.INVALID_PARAMETER => unreachable,
1758+
.NOACCESS => unreachable,
17581759
.INVALID_NAME => return error.InvalidName,
17591760
.FILENAME_EXCED_RANGE => return error.NameTooLong,
17601761
// These are all the system errors that are mapped to ENOEXEC by
@@ -1789,6 +1790,8 @@ pub fn CreateProcessW(
17891790
pub const LoadLibraryError = error{
17901791
FileNotFound,
17911792
Unexpected,
1793+
InitFailed,
1794+
SystemResources,
17921795
};
17931796

17941797
pub fn LoadLibraryW(lpLibFileName: [*:0]const u16) LoadLibraryError!HMODULE {
@@ -1797,6 +1800,8 @@ pub fn LoadLibraryW(lpLibFileName: [*:0]const u16) LoadLibraryError!HMODULE {
17971800
.FILE_NOT_FOUND => return error.FileNotFound,
17981801
.PATH_NOT_FOUND => return error.FileNotFound,
17991802
.MOD_NOT_FOUND => return error.FileNotFound,
1803+
.DLL_INIT_FAILED => return error.InitFailed,
1804+
.NOT_ENOUGH_MEMORY => return error.SystemResources,
18001805
else => |err| return unexpectedError(err),
18011806
}
18021807
};

0 commit comments

Comments
 (0)