syslogd: Fix handling of unix socket modes
Some checks are pending
Cross-build Kernel / ${{ matrix.target_arch }} ${{ matrix.os }} (${{ matrix.compiler }}) (clang-14, /usr/lib/llvm-14/bin, ubuntu-22.04, bmake libarchive-dev clang-14 lld-14, amd64, amd64) (push) Waiting to run
Cross-build Kernel / ${{ matrix.target_arch }} ${{ matrix.os }} (${{ matrix.compiler }}) (clang-14, /usr/lib/llvm-14/bin, ubuntu-22.04, bmake libarchive-dev clang-14 lld-14, arm64, aarch64) (push) Waiting to run
Cross-build Kernel / ${{ matrix.target_arch }} ${{ matrix.os }} (${{ matrix.compiler }}) (clang-18, /opt/homebrew/opt/llvm@18/bin, macos-latest, bmake libarchive llvm@18, amd64, amd64) (push) Waiting to run
Cross-build Kernel / ${{ matrix.target_arch }} ${{ matrix.os }} (${{ matrix.compiler }}) (clang-18, /opt/homebrew/opt/llvm@18/bin, macos-latest, bmake libarchive llvm@18, arm64, aarch64) (push) Waiting to run
Cross-build Kernel / ${{ matrix.target_arch }} ${{ matrix.os }} (${{ matrix.compiler }}) (clang-18, /usr/lib/llvm-18/bin, ubuntu-24.04, bmake libarchive-dev clang-18 lld-18, amd64, amd64) (push) Waiting to run
Cross-build Kernel / ${{ matrix.target_arch }} ${{ matrix.os }} (${{ matrix.compiler }}) (clang-18, /usr/lib/llvm-18/bin, ubuntu-24.04, bmake libarchive-dev clang-18 lld-18, arm64, aarch64) (push) Waiting to run

When bind() is called, the process umask is applied, so one has to
either clear the umask before binding or call chmod() to add permissions
after the fact.  Do the former here to ensure that the socket always has
the correct mode.

Reported by:	Lexi Winter <lexi@le-fay.org>
Fixes:		2b8c3a05e0 ("syslogd: Set unix socket modes atomically")
This commit is contained in:
Mark Johnston 2024-11-05 17:48:37 +00:00
parent d14c38ceb8
commit 88dd055092

View File

@ -3721,12 +3721,24 @@ socksetup(struct addrinfo *ai, const char *name, mode_t mode)
if (ai->ai_family == AF_LOCAL)
unlink(name);
if (ai->ai_family == AF_LOCAL || NoBind == 0 || name != NULL) {
mode_t mask;
int error;
if (ai->ai_family == AF_LOCAL && fchmod(s, mode) < 0) {
dprintf("fchmod %s: %s\n", name, strerror(errno));
close(s);
return (NULL);
}
if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
/*
* For AF_LOCAL sockets, the process umask is applied to the
* mode set above, so temporarily clear it to ensure that the
* socket always has the correct permissions.
*/
mask = umask(0);
error = bind(s, ai->ai_addr, ai->ai_addrlen);
(void)umask(mask);
if (error < 0) {
logerror("bind");
close(s);
return (NULL);