ziglang/zig
148
2.9k
Fork
You've already forked zig
270

UnixAddress.ListenOptions: ability to set perms #30804

Open
blblack wants to merge 1 commit from blblack/zig:unix-listen-perms into master
pull from: blblack/zig:unix-listen-perms
merge into: ziglang:master
ziglang:master
ziglang:mmap
ziglang:riscv-ci-2
ziglang:riscv-ci
ziglang:test-no-bin
ziglang:poll
ziglang:io-uring-update
ziglang:llvm22
ziglang:poll-ring
ziglang:debug-file-leaks-differently
ziglang:debug-file-leaks
ziglang:hate-letter-to-std.os
ziglang:i-am-a-foolish-fool
ziglang:ProcessPrng
ziglang:elfv2-dyn
ziglang:jobserver
ziglang:threadtheft
ziglang:io-threaded-no-queue
ziglang:0.15.x
ziglang:Io.net
ziglang:comptime-allocator
ziglang:restricted-function-pointers
ziglang:cli
ziglang:wasm-linker-writer
ziglang:wrangle-writer-buffering
ziglang:sha1-stream
ziglang:async-await-demo
ziglang:fixes
ziglang:0.14.x
ziglang:ast-node-methods
ziglang:spork8
ziglang:macos-debug-info
ziglang:make-vs-configure
ziglang:fuzz-macos
ziglang:main
ziglang:sans-aro
ziglang:ArrayList-reserve
ziglang:incr-bug
ziglang:llvm-ir-nosanitize-metadata
ziglang:ci-tarballs
ziglang:ci-scripts
ziglang:threadpool
ziglang:0.12.x
ziglang:new-pkg-hash
ziglang:json-diagnostics
ziglang:more-doctests
ziglang:rework-comptime-mutation
ziglang:0.11.x
ziglang:ci-perf-comment
ziglang:stage2-async
ziglang:0.10.x
ziglang:autofix
ziglang:0.9.x
ziglang:aro
ziglang:hcs
ziglang:0.8.x
ziglang:0.7.x
First-time contributor
Copy link

This optionally sets the initial permissions on a UnixAddress
listener socket fd before the bind() call creates the file,
avoiding the potential security race if the application did so
after netListenUnix() returns.

This optionally sets the initial permissions on a UnixAddress listener socket fd before the `bind()` call creates the file, avoiding the potential security race if the application did so after `netListenUnix()` returns.
UnixAddress.ListenOptions: ability to set perms
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
9ee240aad3
This optionally sets the initial permissions on a UnixAddress
listener socket fd before the `bind()` call creates the file,
avoiding the potential security race if the application did so
after `netListenUnix()` returns.
blblack force-pushed unix-listen-perms from 9ee240aad3
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
to cc46cb9acf
Some checks failed
ci / x86_64-windows-debug (pull_request) Failing after 6m46s
ci / x86_64-windows-release (pull_request) Failing after 5m41s
ci / aarch64-macos-release (pull_request) Failing after 33m13s
ci / x86_64-freebsd-release (pull_request) Successful in 43m29s
ci / x86_64-freebsd-debug (pull_request) Successful in 55m29s
ci / aarch64-macos-debug (pull_request) Failing after 1h33m4s
ci / aarch64-linux-release (pull_request) Successful in 1h31m12s
ci / aarch64-linux-debug (pull_request) Successful in 2h10m42s
ci / x86_64-openbsd-debug (pull_request) Failing after 1h8m44s
ci / x86_64-openbsd-release (pull_request) Failing after 54m58s
ci / x86_64-linux-debug (pull_request) Successful in 1h43m18s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h54m9s
ci / x86_64-linux-release (pull_request) Successful in 2h42m34s
ci / powerpc64le-linux-release (pull_request) Successful in 1h29m56s
ci / loongarch64-linux-release (pull_request) Successful in 2h30m16s
ci / loongarch64-linux-debug (pull_request) Successful in 3h13m28s
ci / s390x-linux-release (pull_request) Successful in 1h57m46s
ci / s390x-linux-debug (pull_request) Successful in 2h25m34s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h6m4s
2026年01月12日 23:33:44 +01:00
Compare
andrewrk left a comment
Owner
Copy link

Thanks!

Thanks!

(I'll want to have another look after the CI is passing)

(I'll want to have another look after the CI is passing)
blblack force-pushed unix-listen-perms from cc46cb9acf
Some checks failed
ci / x86_64-windows-debug (pull_request) Failing after 6m46s
ci / x86_64-windows-release (pull_request) Failing after 5m41s
ci / aarch64-macos-release (pull_request) Failing after 33m13s
ci / x86_64-freebsd-release (pull_request) Successful in 43m29s
ci / x86_64-freebsd-debug (pull_request) Successful in 55m29s
ci / aarch64-macos-debug (pull_request) Failing after 1h33m4s
ci / aarch64-linux-release (pull_request) Successful in 1h31m12s
ci / aarch64-linux-debug (pull_request) Successful in 2h10m42s
ci / x86_64-openbsd-debug (pull_request) Failing after 1h8m44s
ci / x86_64-openbsd-release (pull_request) Failing after 54m58s
ci / x86_64-linux-debug (pull_request) Successful in 1h43m18s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h54m9s
ci / x86_64-linux-release (pull_request) Successful in 2h42m34s
ci / powerpc64le-linux-release (pull_request) Successful in 1h29m56s
ci / loongarch64-linux-release (pull_request) Successful in 2h30m16s
ci / loongarch64-linux-debug (pull_request) Successful in 3h13m28s
ci / s390x-linux-release (pull_request) Successful in 1h57m46s
ci / s390x-linux-debug (pull_request) Successful in 2h25m34s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h6m4s
to 690997680f
Some checks failed
ci / aarch64-macos-release (pull_request) Failing after 34m3s
ci / x86_64-freebsd-release (pull_request) Successful in 35m41s
ci / x86_64-windows-release (pull_request) Successful in 43m4s
ci / x86_64-freebsd-debug (pull_request) Successful in 44m36s
ci / aarch64-macos-debug (pull_request) Failing after 45m1s
ci / x86_64-windows-debug (pull_request) Successful in 48m12s
ci / aarch64-linux-release (pull_request) Successful in 1h26m49s
ci / x86_64-openbsd-release (pull_request) Failing after 1h2m24s
ci / x86_64-openbsd-debug (pull_request) Failing after 1h11m43s
ci / aarch64-linux-debug (pull_request) Successful in 2h30m24s
ci / x86_64-linux-debug (pull_request) Successful in 1h3m10s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 3h10m55s
ci / x86_64-linux-release (pull_request) Failing after 6h0m36s
ci / powerpc64le-linux-release (pull_request) Successful in 1h36m26s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h26m52s
ci / s390x-linux-release (pull_request) Successful in 1h18m59s
ci / s390x-linux-debug (pull_request) Successful in 3h33m36s
ci / loongarch64-linux-debug (pull_request) Failing after 2h41m13s
ci / loongarch64-linux-release (pull_request) Successful in 2h37m18s
2026年01月13日 13:28:54 +01:00
Compare
Author
First-time contributor
Copy link

@andrewrk - The problem I'm running into here now is that some platforms fail if you try to set permissions this way. It's just not as universally-supported as I expected it to be!

Works: Linux, FreeBSD, apparently Windows (at least, it doesn't error out)
Fails: MacOS, and I now think OpenBSD and NetBSD will too
Unknown: All the rest

They fail because they simply don't support fchmod() on a unix socket descriptor and will return EINVAL or similar. For such platforms, the only way to handle this (AFAIK) is the racy method of doing it after the bind(), probably with a separately-opened regular file descriptor on the same path (or, I guess, you could alter the process-global umask around the bind(), but that's racy for threads on another level).

There's a few potential ways I can see to handle this situation and still have the option. The top two on my mind are:

  1. Leave the code as it is, and in the testsuite only test .permissions = .default_file on select known-good platforms so that CI passes. It will be basically up to the application layer to decide if they care about this and to care about which platforms they handle race-free (via this option) or in a racy way (in their application code).
  2. Use target conditionals inside of netListenUnix(): only handle it race-free where it's known to work, and then do it the racy way for the caller in other cases at the bottom with another few syscalls, and document that the option only makes it race-free where that's even possible.

Thoughts?

@andrewrk - The problem I'm running into here now is that some platforms fail if you try to set permissions this way. It's just not as universally-supported as I expected it to be! Works: Linux, FreeBSD, apparently Windows (at least, it doesn't error out) Fails: MacOS, and I now think OpenBSD and NetBSD will too Unknown: All the rest They fail because they simply don't support `fchmod()` on a unix socket descriptor and will return `EINVAL` or similar. For such platforms, the only way to handle this (AFAIK) is the racy method of doing it after the `bind()`, probably with a separately-opened regular file descriptor on the same path (or, I guess, you could alter the process-global umask around the `bind()`, but that's racy for threads on another level). There's a few potential ways I can see to handle this situation and still have the option. The top two on my mind are: 1. Leave the code as it is, and in the testsuite only test `.permissions = .default_file` on select known-good platforms so that CI passes. It will be basically up to the application layer to decide if they care about this and to care about which platforms they handle race-free (via this option) or in a racy way (in their application code). 2. Use target conditionals inside of `netListenUnix()`: only handle it race-free where it's known to work, and then do it the racy way for the caller in other cases at the bottom with another few syscalls, and document that the option only makes it race-free where that's even possible. Thoughts?
Author
First-time contributor
Copy link

... or of course, I should add:

  1. Just drop this and ignore the problem. In theory, most of the time the default process umask will disable other/group write bits (0o022), meaning the default perms of the socket will only allow connections from the same uid as ourselves, which makes changing it later not be a security race from most perspectives.

Mostly the root of this whole problem is the process-global-umask problem. You could argue that applications which care about unix listening socket perms should enforce an appropriate umask early during main() before any threading issues arise and leave it that way? There will be edge cases where other parts of the code needs a more-permissive umask than the unix listening socket, but those edge cases could be punted as "your case is too-special to support".

There's similar issues around owner/group for the socket file, but changing that is only realistic if running as root, usually, and so again it's kind of out there from the norm.

... or of course, I should add: 3. Just drop this and ignore the problem. In theory, most of the time the default process umask will disable other/group write bits (`0o022`), meaning the default perms of the socket will only allow connections from the same uid as ourselves, which makes changing it later not be a security race from most perspectives. Mostly the root of this whole problem is the process-global-umask problem. You could argue that applications which care about unix listening socket perms should enforce an appropriate umask early during `main()` before any threading issues arise and leave it that way? There will be edge cases where other parts of the code needs a more-permissive umask than the unix listening socket, but those edge cases could be punted as "your case is too-special to support". There's similar issues around owner/group for the socket file, but changing that is only realistic if running as `root`, usually, and so again it's kind of out there from the norm.
Some checks failed
ci / aarch64-macos-release (pull_request) Failing after 34m3s
Required
Details
ci / x86_64-freebsd-release (pull_request) Successful in 35m41s
Required
Details
ci / x86_64-windows-release (pull_request) Successful in 43m4s
Required
Details
ci / x86_64-freebsd-debug (pull_request) Successful in 44m36s
Required
Details
ci / aarch64-macos-debug (pull_request) Failing after 45m1s
Required
Details
ci / x86_64-windows-debug (pull_request) Successful in 48m12s
Required
Details
ci / aarch64-linux-release (pull_request) Successful in 1h26m49s
Required
Details
ci / x86_64-openbsd-release (pull_request) Failing after 1h2m24s
Required
Details
ci / x86_64-openbsd-debug (pull_request) Failing after 1h11m43s
Required
Details
ci / aarch64-linux-debug (pull_request) Successful in 2h30m24s
Required
Details
ci / x86_64-linux-debug (pull_request) Successful in 1h3m10s
Required
Details
ci / x86_64-linux-debug-llvm (pull_request) Successful in 3h10m55s
Required
Details
ci / x86_64-linux-release (pull_request) Failing after 6h0m36s
Required
Details
ci / powerpc64le-linux-release (pull_request) Successful in 1h36m26s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h26m52s
ci / s390x-linux-release (pull_request) Successful in 1h18m59s
ci / s390x-linux-debug (pull_request) Successful in 3h33m36s
ci / loongarch64-linux-debug (pull_request) Failing after 2h41m13s
ci / loongarch64-linux-release (pull_request) Successful in 2h37m18s
Some required checks were not successful.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u unix-listen-perms:blblack-unix-listen-perms
git switch blblack-unix-listen-perms
Sign in to join this conversation.
No reviewers
Labels
Clear labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted

This proposal is planned.
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_16
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc

The web application for interactive documentation and generation of its assets.
backend/c

The C backend outputs C source code.
backend/llvm

The LLVM backend outputs an LLVM bitcode module.
backend/self-hosted

The self-hosted backends produce machine code directly.
binutils

Zig's included binary utilities: zig ar, zig dlltool, zig lib, zig ranlib, zig objcopy, and zig rc.
breaking

Implementing this issue could cause existing code to no longer compile or have different behavior.
build system

The Zig build system - zig build, std.Build, the build runner, and package management.
debug info

An issue related to debug information (e.g. DWARF) produced by the Zig compiler.
docs

An issue with documentation, e.g. the language reference or standard library doc comments.
error message

This issue points out an error message that is unhelpful and should be improved.
frontend

Tokenization, parsing, AstGen, ZonGen, Sema, Legalize, and Liveness.
fuzzing

An issue related to Zig's integrated fuzz testing.
incremental

Reuse of internal compiler state for faster compilation.
lib/c

This issue relates to Zig's libc implementation and/or vendored libcs.
lib/compiler-rt

This issue relates to Zig's compiler-rt library.
lib/cxx

This issue relates to Zig's vendored libc++ and/or libc++abi.
lib/std

This issue relates to Zig's standard library.
lib/tsan

This issue relates to Zig's vendored libtsan.
lib/ubsan-rt

This issue relates to Zig's ubsan-rt library.
lib/unwind

This issue relates to Zig's vendored libunwind.
linking

Zig's integrated object file and incremental linker.
miscompilation

The compiler reports success but produces semantically incorrect code.
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal

This issue suggests modifications. If it also has the "accepted" label then it is planned.
release notes

This issue or pull request should be mentioned in the release notes.
testing

This issue is related to testing the compiler, standard library, or other parts of Zig.
tier system

This issue tracks the support tier for a target.
zig cc

Zig as a drop-in C-family compiler.
zig fmt

The Zig source code formatter.
bounty

https://ziglang.org/news/announcing-donor-bounties
bug

Observed behavior contradicts documented or intended behavior.
contributor-friendly

This issue is limited in scope and/or knowledge of project internals.
downstream

An issue with a third-party project that uses this project.
enhancement

Solving this issue will likely involve adding new logic or components to the codebase.
infra

An issue related to project infrastructure, e.g. continuous integration.
optimization

A task to improve performance and/or resource usage.
question

No questions on the issue tracker; use a community space instead.
regression

A bug that did not occur in a previous version.
upstream

An issue with a third-party project that this project uses.
No labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_16
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal
release notes
testing
tier system
zig cc
zig fmt
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig!30804
Reference in a new issue
ziglang/zig
No description provided.
Delete branch "blblack/zig:unix-listen-perms"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?