1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! The batch gate: refuse every program token Windows would resolve to a `.bat`/`.cmd`, because
//! `std::process` hands those to cmd.exe, whose escaping this crate does not implement
//! (CVE-2024-24576).
use crateError;
use is_batch_program;
use win32_effective_file_name;
/// The prefix Win32 acts on: everything before the first interior NUL, where `CreateProcessW` and
/// `PCWSTR` stop. Equal (and borrowed) when there is no NUL, which is how [`reject_batch_path_on`]
/// detects one without a platform-specific byte view at its own call site.
///
/// Host-independent on purpose — it computes the same prefix everywhere, which is what lets a
/// macOS run exercise the Win32 rule.
const BATCH: &str = "cmd.exe batch escaping is not implemented (CVE-2024-24576); \
run it through cmd.exe yourself — .executable(\"cmd.exe\") plus a \
.commandline() you have escaped for cmd.exe";
const NO_FILE: &str = "the path names no file of its own, so which image loads is decided by \
the current directory or PATH — and a .bat-named directory there makes \
std::process substitute cmd.exe (CVE-2024-24576); name the executable";
// A verbatim path resolves against nothing, so NO_FILE's reason does not apply to one.
const VERBATIM_DOTDOT: &str = "a \\\\?\\ path is never normalised, so `..` is a literal file name \
here — and no file may be called that; name the executable";
/// Reject a program token carrying an interior NUL, or naming a `.bat`/`.cmd`: Win32 silently
/// truncates at the NUL (`PCWSTR` has no length), and cmd.exe batch escaping is a distinct,
/// unimplemented vector (CVE-2024-24576 / BatBadBut). Shared by every backend — the std path
/// (`build_std_command`), the raw one (`windows_raw::reject_batch_program`), and the elevated
/// `ShellExecuteEx` launch.
///
/// The rule, and why each half is a fact about Win32, is in [`reject_batch_path_on`]; this asks it
/// for the running host's verdict.
pub
/// PURE given `win32`: the gate's rule with the platform as DATA rather than a `cfg!` buried in
/// it, so one host can ask for either verdict — the same reason `elevation::plan::Host` carries
/// its `Os`. Both are pinned from any host by `batch_gate_tests`, which matters because the Windows
/// branch — the whole `win32_effective_file_name` -> `is_batch_program` composition, where every
/// subtlety lives — would otherwise be covered by the two Windows CI lanes alone. Reading `cfg!`
/// here would have let a regression back to the extension-based reading pass unnoticed off
/// Windows; see `reject_batch_path_on_windows_refuses_every_spelling_that_reaches_a_batch_file`
/// for the cross-platform harness that catches it.
///
/// This gate is LIVE on the std path today, not merely a guard for some future backend. Rust's
/// own `std::process` detects a `.bat`/`.cmd` program, swaps it for `cmd.exe` and builds a batch
/// command line (`sys/process/windows.rs`'s `is_batch_file` -> `make_bat_command_line`), so
/// anything slipping past here is handed to exactly the quoting layer cosca has not implemented.
/// It goes live a second way once `ShellExecuteEx` is gated, which has no such backstop.
///
/// An interior NUL is refused FIRST, under both verdicts, because `\0` is not a path separator
/// and neither `Path::extension()` nor the component walk below stops at one — so the name tested
/// is the INVERSE of what Win32 loads on each of the two NUL/batch shapes:
///
/// - `setup.bat` + NUL + `junk` → the effective name is `setup.bat\0junk`, yet Win32 loads the
/// real batch file `setup.bat`.
/// - `setup` + NUL + `.bat` → the effective name ends in `.bat`, yet Win32 loads `setup`, which is
/// no batch file — so the batch refusal would blame CVE-2024-24576 for a program that does not
/// carry that vector, and interpolate a raw U+0000 into a message bound for logs and terminals.
///
/// Refusing the NUL outright settles both shapes, and makes this gate SELF-SUFFICIENT rather than
/// a rule each caller must order its own NUL check in front of — the std backend, the DEFAULT
/// Windows path, has none to order. The reason given differs by verdict because the facts do: off
/// Win32 nothing truncates, so the token simply names no file.
///
/// The batch half is `win32`-only, because it too is a fact about Win32 rather than the request:
/// Win32 routes a `.bat`/`.cmd` through cmd.exe, which is what CVE-2024-24576 needs. Elsewhere a
/// clean `deploy.bat` is an ordinary executable the host runs, so refusing it would report "not
/// supported on windows" about a Linux or macOS host that runs it fine — and send its caller to
/// audit a batch vector that cannot reach them.
///
/// A path that resolves to NO NAME of its own is refused too, not accepted. std does not test the
/// string it was given: it runs the program through `GetFullPathNameW` (or the PATH search) and
/// applies `has_bat_extension` to the RESULT. A RELATIVE path that pops past its own first
/// component does not vanish — Win32 goes on popping into the ancestors of the current directory,
/// so with a cwd of `C:\w.bat` (a directory, which Windows permits) `x\..` resolves to `C:\w.bat`
/// and std substitutes `cmd.exe`. That is the hole this refusal closes.
///
/// A ROOTED path never reaches the cwd at all, so it is refused for having the same shape rather
/// than for the same danger — and what it clamps at depends on the root. A drive-rooted or
/// drive-relative path clamps at a root with no name of its own (`C:\`, `\`), which is not a
/// loadable image, and nor does a device path, which clamps at `\\.\`. A UNC path clamps at
/// `\\server\share`, which DOES leave a named final component, and that name is judged like any
/// other — see [`win32_effective_file_name`].
///
/// A verbatim (`\\?\`) path is judged by a rule of its own — see [`verbatim_refusal`].
pub
/// Whether a VERBATIM (`\\?\`) program reaches a batch file. std asks a different question of one
/// than it asks of any other path, and this is that question.
///
/// For an ordinary program std runs the string through `GetFullPathNameW` and tests the RESULT;
/// for a verbatim one `is_batch_file` is a literal test of the last four UTF-16 units of the
/// string. std does call `GetFullPathNameW` on a short `\\?\C:\…` program first (`to_user_path`,
/// on the text after the prefix), but drops the prefix only when the result round-trips — and a
/// string that round-trips reads the same under both tests. So `\\?\C:\x.bat.` keeps its prefix,
/// ends in `bat.`, and cmd.exe is not substituted — while the plain `C:\x.bat.` loses its trailing
/// dot on the way through `GetFullPathNameW` and reaches the batch file. The prefix does not merely
/// spell the same file differently; it selects a different resolution.
///
/// std's `is_verbatim` also accepts `\??\`, which this gate does NOT route here: it walks `\??\…`
/// as an ordinary rooted path. No verdict turns on that. A `\??\` string ending in `.bat`/`.cmd`
/// has a final component ending in it, which the walk refuses; the walk's trimming only
/// over-refuses (`\??\C:\x.bat.`, which std's literal test passes).
///
/// Measured on Windows runners, both architectures: `...`, `....`, `" "` and `"x "` are creatable,
/// listable and openable through the prefix, and both `CreateProcessW` and `std::process` spawn
/// them, while the plain spelling fails with access-denied. Refusing those refused a loadable
/// executable, which is why this is not the component machinery below with the trimming disabled.
///
/// The other refusals are a final component that trims or splits to a batch name, below, and `..`,
/// which no collapse turns into a path operation here: it is a literal file name, and no file may
/// be called that (measured: `ERROR_INVALID_NAME`). Other unloadable spellings — a trailing
/// separator, a final `.` — are left to fail with the OS's own error, which says more about why
/// than a security refusal would.
///
/// # What trims or splits to a batch name is refused, conservatively
///
/// The final component is also judged by the plain rule, [`is_batch_program`], and so is the piece
/// after its last `/`. That refuses `\\?\C:\x.bat.` and `\\?\C:\x.bat ` (one trailing space),
/// which std's literal test passes: the stricter verdict wherever the two rules differ. It also
/// refuses `\\?\C:\x.bat:s`, `\\?\C:\x.bat:` and `\\?\C:\x.bat::$DATA` as their plain
/// spellings are. std hands `CreateProcessW` the same string for each stream pair — each
/// round-trips through `GetFullPathNameW`, so `to_user_path` strips the prefix — and substitutes
/// cmd.exe for neither. What is unmeasured is whether `CreateProcessW`, handed any of these names,
/// launches cmd.exe on its own. ReactOS's `CreateProcessInternalW` says no (it tests the last four
/// characters of the name), which would make all of these refusals over-refusals, but Windows' own
/// kernelbase may read the extension differently.
///
/// The measurement that would license narrowing both: on a Windows runner, write `x.bat` whose
/// default stream and an `s` stream both hold a batch script that leaves a marker file, then
/// spawn all six spellings, plain and prefixed, through a direct `CreateProcessW` (which gets the
/// prefix as written) and through `std::process`. The call failing with `ERROR_BAD_EXE_FORMAT`
/// and no marker for every spelling licenses it; any marker means these refusals close a hole.