Commit graph

29108 commits

Author SHA1 Message Date
Marc Tiehuis
ef5618fcd5 std.hash.crc: simplify api
This removes the two original implementations in favour of the single
generic one based on the Algorithm type. Previously we had three, very
similar implementations which was somewhat confusing when knowing what
one should actually be used.

The previous polynomials all have equivalent variants available when
using the Algorithm type.
2024-04-28 21:12:01 +12:00
antlilja
c231d94960 LLVM: Remove deprecated or soon to be deprecated constant expressions 2024-04-25 22:58:47 -07:00
zhylmzr
3648d7df19 fix: object size error in archive 2024-04-25 18:27:11 +02:00
Andrew Kelley
1b90888f57 migrate langref documentation generation to the build system 2024-04-25 00:09:24 -07:00
Andrew Kelley
9d64332a59
Merge pull request #19698 from squeek502/windows-batbadbut
std.process.Child: Mitigate arbitrary command execution vulnerability on Windows (BatBadBut)
2024-04-24 13:49:43 -07:00
Ryan Liptak
a0f1825c53 windows.GetFinalPathNameByHandle: Support volumes mounted as paths
A volume can be mounted as a NTFS path, e.g. as C:\Mnt\Foo. In that case, IOCTL_MOUNTMGR_QUERY_POINTS gives us a mount point with a symlink value something like `\??\Volume{383da0b0-717f-41b6-8c36-00500992b58d}`. In order to get the `C:\Mnt\Foo` path, we can query the mountmgr again using IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATH.

Fixes #19731
2024-04-24 13:44:09 -07:00
IntegratedQuantum
1a6485d111
Clarify the blocking behavior of RwLock.lockShared(). (#19752) 2024-04-24 10:41:13 -05:00
Sean
f6f7a47aad Update fmt.zig tests
Changed uses of `std.testing.expect` to `std.testing.expectEqual`, `std.testing.expectError`, and `std.testing.expectEqualStrings` where appropriate
2024-04-23 17:57:56 -07:00
Eric Joldasov
857c1d4ff2 std.zig.system: fix ELF file search
* Adjust buffer length a bit.
 * Fix detecting if file is a script. Logic below was unreachable,
 because 99% of scripts failed "At least 255 bytes long" check and were detected as ELF files.
 It should be "At least 4" instead (minimum value of "ELF magic length" and "smallest possible interpreter path length").
 * Fix parsing interpreter path, when text after shebang:
     1. does not have newline,
     2. has leading spaces and tabs,
     3. separates interpreter and arguments by tab or NUL.
 * Remove empty error set from `defaultAbiAndDynamicLinker`.

Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>
2024-04-23 17:21:01 -07:00
clickingbuttons
7cf3167e98
std.crypto: make ff.ct_unprotected.limbsCmpLt compile (#19741)
* std.crypto: make ff.ct_unprotected.limbsCmpLt compile

* std.crypto: add ff.ct test

* fix testCt to work on x86

* disable test on stage2-c

---------

Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com>
2024-04-23 20:29:36 +00:00
David Rubin
b87baad0ff error on undefined end index 2024-04-23 19:25:49 +03:00
Ryan Liptak
422464d540 std.process.Child: Mitigate arbitrary command execution vulnerability on Windows (BatBadBut)
> Note: This first part is mostly a rephrasing of https://flatt.tech/research/posts/batbadbut-you-cant-securely-execute-commands-on-windows/
> See that article for more details

On Windows, it is possible to execute `.bat`/`.cmd` scripts via CreateProcessW. When this happens, `CreateProcessW` will (under-the-hood) spawn a `cmd.exe` process with the path to the script and the args like so:

    cmd.exe /c script.bat arg1 arg2

This is a problem because:

- `cmd.exe` has its own, separate, parsing/escaping rules for arguments
- Environment variables in arguments will be expanded before the `cmd.exe` parsing rules are applied

Together, this means that (1) maliciously constructed arguments can lead to arbitrary command execution via the APIs in `std.process.Child` and (2) escaping according to the rules of `cmd.exe` is not enough on its own.

A basic example argv field that reproduces the vulnerability (this will erroneously spawn `calc.exe`):

    .argv = &.{ "test.bat", "\"&calc.exe" },

And one that takes advantage of environment variable expansion to still spawn calc.exe even if the args are properly escaped for `cmd.exe`:

    .argv = &.{ "test.bat", "%CMDCMDLINE:~-1%&calc.exe" },

(note: if these spawned e.g. `test.exe` instead of `test.bat`, they wouldn't be vulnerable; it's only `.bat`/`.cmd` scripts that are vulnerable since they go through `cmd.exe`)

Zig allows passing `.bat`/`.cmd` scripts as `argv[0]` via `std.process.Child`, so the Zig API is affected by this vulnerability. Note also that Zig will search `PATH` for `.bat`/`.cmd` scripts, so spawning something like `foo` may end up executing `foo.bat` somewhere in the PATH (the PATH searching of Zig matches the behavior of cmd.exe).

> Side note to keep in mind: On Windows, the extension is significant in terms of how Windows will try to execute the command. If the extension is not `.bat`/`.cmd`, we know that it will not attempt to be executed as a `.bat`/`.cmd` script (and vice versa). This means that we can just look at the extension to know if we are trying to execute a `.bat`/`.cmd` script.

---

This general class of problem has been documented before in 2011 here:

https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way

and the course of action it suggests for escaping when executing .bat/.cmd files is:

- Escape first using the non-cmd.exe rules
- Then escape all cmd.exe 'metacharacters' (`(`, `)`, `%`, `!`, `^`, `"`, `<`, `>`, `&`, and `|`) with `^`

However, escaping with ^ on its own is insufficient because it does not stop cmd.exe from expanding environment variables. For example:

```
args.bat %PATH%
```

escaped with ^ (and wrapped in quotes that are also escaped), it *will* stop cmd.exe from expanding `%PATH%`:

```
> args.bat ^"^%PATH^%^"
"%PATH%"
```

but it will still try to expand `%PATH^%`:

```
set PATH^^=123
> args.bat ^"^%PATH^%^"
"123"
```

The goal is to stop *all* environment variable expansion, so this won't work.

Another problem with the ^ approach is that it does not seem to allow all possible command lines to round trip through cmd.exe (as far as I can tell at least).

One known example:

```
args.bat ^"\^"key^=value\^"^"
```

where args.bat is:

```
@echo %1 %2 %3 %4 %5 %6 %7 %8 %9
```

will print

```
"\"key value\""
```

(it will turn the `=` into a space for an unknown reason; other minor variations do roundtrip, e.g. `\^"key^=value\^"`, `^"key^=value^"`, so it's unclear what's going on)

It may actually be possible to escape with ^ such that every possible command line round trips correctly, but it's probably not worth the effort to figure it out, since the suggested mitigation for BatBadBut has better roundtripping and leads to less garbled command lines overall.

---

Ultimately, the mitigation used here is the same as the one suggested in:

https://flatt.tech/research/posts/batbadbut-you-cant-securely-execute-commands-on-windows/

The mitigation steps are reproduced here, noted with one deviation that Zig makes (following Rust's lead):

1. Replace percent sign (%) with %%cd:~,%.
2. Replace the backslash (\) in front of the double quote (") with two backslashes (\\).
3. Replace the double quote (") with two double quotes ("").
4. ~~Remove newline characters (\n).~~
  - Instead, `\n`, `\r`, and NUL are disallowed and will trigger `error.InvalidBatchScriptArg` if they are found in `argv`. These three characters do not roundtrip through a `.bat` file and therefore are of dubious/no use. It's unclear to me if `\n` in particular is relevant to the BatBadBut vulnerability (I wasn't able to find a reproduction with \n and the post doesn't mention anything about it except in the suggested mitigation steps); it just seems to act as a 'end of arguments' marker and therefore anything after the `\n` is lost (and same with NUL). `\r` seems to be stripped from the command line arguments when passed through a `.bat`/`.cmd`, so that is also disallowed to ensure that `argv` can always fully roundtrip through `.bat`/`.cmd`.
5. Enclose the argument with double quotes (").

The escaped command line is then run as something like:

    cmd.exe /d /e:ON /v:OFF /c "foo.bat arg1 arg2"

Note: Previously, we would pass `foo.bat arg1 arg2` as the command line and the path to `foo.bat` as the app name and let CreateProcessW handle the `cmd.exe` spawning for us, but because we need to pass `/e:ON` and `/v:OFF` to cmd.exe to ensure the mitigation is effective, that is no longer tenable. Instead, we now get the full path to `cmd.exe` and use that as the app name when executing `.bat`/`.cmd` files.

---

A standalone test has also been added that tests two things:

1. Known reproductions of the vulnerability are tested to ensure that they do not reproduce the vulnerability
2. Randomly generated command line arguments roundtrip when passed to a `.bat` file and then are passed from the `.bat` file to a `.exe`. This fuzz test is as thorough as possible--it tests that things like arbitrary Unicode codepoints and unpaired surrogates roundtrip successfully.

Note: In order for the `CreateProcessW` -> `.bat` -> `.exe` roundtripping to succeed, the .exe must split the arguments using the post-2008 C runtime argv splitting implementation, see https://github.com/ziglang/zig/pull/19655 for details on when that change was made in Zig.
2024-04-23 03:21:51 -07:00
Ryan Liptak
84f4c5d9cc std.unicode: Fix ArrayList functions when using populated ArrayLists
ensureTotalCapacityPrecise only satisfies the assumptions made in the ArrayListImpl functions (that there's already enough capacity for the entire converted string if it's all ASCII) when the ArrayList has no items, otherwise it would hit illegal behavior.
2024-04-23 03:20:38 -07:00
Carl Åstholm
e8f28cda9e std.Build: Install Windows DLLs to <prefix>/bin/ by default
Windows does not support RPATH and only searches for DLLs in a small
number of predetermined paths by default, with one of them being the
directory from which the application loaded.

Installing both executables and DLLs to `bin/` by default helps ensure
that the executable can find any DLL artifacts it has linked to.
DLL import libraries are still installed to `lib/`.

These defaults match CMake's behavior.
2024-04-22 17:12:18 -07:00
clickingbuttons
c947e79d73 std.meta: give TagPayloadByName unreachable a better @compileError message 2024-04-23 01:50:10 +03:00
Travis Staloch
8af59d1f98 ComptimeStringMap: return a regular struct and optimize
this patch renames ComptimeStringMap to StaticStringMap, makes it
accept only a single type parameter, and return a known struct type
instead of an anonymous struct.  initial motivation for these changes
was to reduce the 'very long type names' issue described here
https://github.com/ziglang/zig/pull/19682.

this breaks the previous API.  users will now need to write:
`const map = std.StaticStringMap(T).initComptime(kvs_list);`

* move `kvs_list` param from type param to an `initComptime()` param
* new public methods
  * `keys()`, `values()` helpers
  * `init(allocator)`, `deinit(allocator)` for runtime data
  * `getLongestPrefix(str)`, `getLongestPrefixIndex(str)` - i'm not sure
     these belong but have left in for now incase they are deemed useful
* performance notes:
  * i posted some benchmarking results here:
    https://github.com/travisstaloch/comptime-string-map-revised/issues/1
  * i noticed a speedup reducing the size of the struct from 48 to 32
    bytes and thus use u32s instead of usize for all length fields
  * i noticed speedup storing KVs as a struct of arrays
  * latest benchmark shows these wall_time improvements for
    debug/safe/small/fast builds: -6.6% / -10.2% / -19.1% / -8.9%. full
    output in link above.
2024-04-22 15:31:41 -07:00
Frank Denis
fefdbca6e6 Fix WASI threads, again
Properly call the entrypoint when it doesn't return an optional error,
and use the per-thread copy of the arguments list.
2024-04-22 15:27:05 -07:00
Jacob Young
5d745d94fb x86_64: fix C abi for unions
Closes #19721
2024-04-22 15:24:29 -07:00
Jakub Konka
6fd09f8d2d link/macho: make --verbose-link represent the actual linker line 2024-04-22 15:14:56 +02:00
Jakub Konka
a7e4d17226 link/macho: introduce Atom extras for out-of-band data 2024-04-22 12:21:37 +02:00
Jakub Konka
3c5e840732
Merge pull request #19714 from ziglang/elf-merge-strings
link/elf: implement string merging
2024-04-21 10:37:49 +02:00
Jakub Konka
e0d7a98b72
Merge pull request #19710 from jacobly0/elf-segment-align
Elf: fix unaligned segments on non-linux
2024-04-21 09:56:26 +02:00
Jared Baur
c352845e88 Fix usage of unexpectedErrno
`unexpectedErrno` comes from `std.posix`, not `std.os`.
2024-04-20 19:37:41 -07:00
Marco F
40118c769f update list of missing features in no-LLVM built zig2 2024-04-20 17:26:56 -07:00
Frank Denis
d8764ec770 Rename der_encoded_max_length to der_encoded_length_max
The `length_min`/`length_max` convention is used everywhere else in
`std.crypto.*` so be consistent.
2024-04-20 16:27:56 -07:00
Linus Groh
7789e87230 std.fs.Dir.openDir: use wasi libc API when -lc
Same as #19680 but for directories.
2024-04-20 15:42:44 -07:00
Jakub Konka
da55af1cae link/elf: fix 32bit build 2024-04-21 00:27:16 +02:00
Jakub Konka
bc46a4d51e link/elf: create .comment section for build-lib and no-llvm 2024-04-20 23:36:42 +02:00
Jakub Konka
66253e5b51 link/elf: populate current Zig version in .comment; test 2024-04-20 23:36:42 +02:00
Jakub Konka
bc0d84be0b test/link/elf: add merge strings test for -r mode 2024-04-20 23:36:42 +02:00
Jakub Konka
342235e5eb test/link/elf: enhance merge strings tests 2024-04-20 23:36:42 +02:00
Jakub Konka
4931a291f8 link/elf: keep track of sh_entsize per MergeSubsection 2024-04-20 23:36:42 +02:00
Jakub Konka
457d84f45a link/elf: remove link.link.build as unused; add some merge strings tests 2024-04-20 23:36:42 +02:00
Jakub Konka
2cc1623925 link/elf: fix parsing SHF_STRINGS section 2024-04-20 23:36:42 +02:00
Jakub Konka
63a40bff47 link/elf: actually commit merge sections 2024-04-20 23:36:42 +02:00
Jakub Konka
b5a781d19d link/elf: fix generating invalid section symbol index for merged sections 2024-04-20 23:36:41 +02:00
Jakub Konka
65492b3d52 link/elf: skip empty merge sections when resolving 2024-04-20 23:36:41 +02:00
Jakub Konka
9e0bca73e2 link/elf: implement string merging 2024-04-20 23:36:41 +02:00
Jakub Konka
09820a96b6 link/elf: move relocs indexes into Atom extras 2024-04-20 23:36:41 +02:00
Jakub Konka
13b403cbf7 link/elf: move fde record indexes into Atom extras 2024-04-20 23:36:41 +02:00
Jakub Konka
d5fdb7315f link/elf: port macho symbol extras handling 2024-04-20 23:36:41 +02:00
Jakub Konka
700644d35d link/elf: introduce Atom extras for out-of-band storage 2024-04-20 23:36:41 +02:00
Jacob Young
4daeffab4a Elf: fix missing dynrelocs on haiku 2024-04-20 15:40:01 -04:00
Jacob Young
de68a3ad43 Elf: fix unaligned segments on non-linux
Linux isn't the only OS that `mmap`s segments.
2024-04-20 14:48:06 -04:00
Jacob Young
c7ffdbcd41 Revert "disable flaky std.fs test"
This reverts commit d080622cc3.

Workaround applied to CI.
2024-04-20 06:08:26 -04:00
Andrew Kelley
dddddcffd0 disable RISC-V CI testing due to LLVM's O(N^2) codegen
tracked by #18872
2024-04-19 20:24:57 -07:00
Andrew Kelley
d080622cc3 disable flaky std.fs test
Tracked by #17134
2024-04-19 19:01:13 -07:00
Andrew Kelley
7c0e1cc913 start the 0.13.0 release cycle 2024-04-19 14:01:04 -07:00
Andrew Kelley
a685ab1499 Release 0.12.0 2024-04-19 14:00:35 -07:00
GethDW
db96ad4a16 std: fix Thread.Pool.spawn
`@alignCast` was required for args with greater alignment than that of a pointer.
2024-04-19 13:49:06 -07:00