rust-lang / rust-lang/rust

on some BSD / Darwin, Rust std couldn't bind a UNIX domain socket if path is longer than 104 bytes (regardless of actual limit)

Open
#160,684 8 comments 0 reactions 1 assignee View on GitHub

@asder8215 is already working on this.

Since Aug 10, 2026.

C-bug needs-triage T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code on macOS 26.6 (build 25G72):

const HEADER: &'static str = "/tmp/long-uds-test-";
const FOOTER: &'static str = ".sock";

fn main() {
    let target_length = 104;
    let path = format!(
        "{}{}{}",
        HEADER,
        "0".repeat(target_length - (HEADER.len() + FOOTER.len())),
        FOOTER
    );
    println!("Trying to listen on {}", path);
    _ = std::os::unix::net::UnixListener::bind(path).unwrap()
}

I expected to successfully binds a UNIX domain socket.

Instead, this happened:

Trying to listen on /tmp/long-uds-test-00000000000000000000000000000000000000000000000000000000000000000000000000000000.sock

thread 'main' (25410034) panicked at src/main.rs:13:54:
called `Result::unwrap()` on an `Err` value: Error { kind: InvalidInput, message: "path must be shorter than SUN_LEN" }
Details

I think this error comes from here:
https://github.com/rust-lang/rust/blob/88f7399cb4912680976e1c342ea7661b9dc84940/library/std/src/os/unix/net/addr.rs#L39-L44

while sockaddr_un's sun_path is 104 bytes in header file (and in Rust's libc), actually some BSD and/or Darwin allow longer path by allocating a larger space for sockaddr_un and ignoring sun_path length.

ref. (both links are for Darwin)

I wrote a small C program that binds a UNIX domain socket at argv[1]:

source
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    if (argc < 2) {
        fprintf(stderr, "Usage: %s /path/to/create.sock\n", argc ? argv[0] : "<command>");
        return 1;
    }

    size_t size = strlen(argv[1]);
    if (size > 254) {
        fprintf(stderr, "argv[1] is longer than 254: %zu\n", size);
        return 1;
    }

    size_t struct_size = size + 1 /* terminator*/ + 2;
    void* ptr = malloc(struct_size);
    if (ptr == NULL) return 2;
    struct sockaddr_un* addr = (struct sockaddr_un*) ptr;
    addr->sun_len = (unsigned char) size;
    addr->sun_family = AF_UNIX;
    memcpy(addr->sun_path, argv[1], size + 1);

    int res;
    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd == -1) {
        fprintf(stderr, "socket failed with %d\n", errno);
        return errno;
    }
    fprintf(stderr, "trying to bind at %s\n", addr->sun_path);
    res = bind(fd, (struct sockaddr*) addr, struct_size);
    if (res != 0) {
        fprintf(stderr, "bind failed with %d\n", errno);
        return errno;
    }
    fprintf(stderr, "close...\n");
    close(fd);
    
    return 0;
} 

and test results are:

environment max path length (with last NUL)
Darwin 25.6.0 (macOS 26.6, build 25G72) 253 bytes
NetBSD 11.0 (amd64) 253 bytes
FreeBSD 15.1-RELEASE-p2 (amd64) 104 bytes (didnt change)
DragonFlyBSD v6.4.2-RELEASE (amd64) 253 bytes

(OpenBSD wasn't tested because installer failed on my machine)

so it would be nice to allow longer paths (than 104 bytes) on such systems.

How I found this

on iOS, apps are normally sandboxed (unless jailbreak) and they can only write to, e.g. /private/var/mobile/Containers/Data/Application/00000000-0000-0000-0000-000000000000/Library (93 chars!).
so if you add filename e.g. rust-backend.sock, it can easily exceed current 104 bytes limit.

Meta

rustc --version --verbose:

rustc 1.97.1 (8bab26f4f 2026-07-14)
binary: rustc
commit-hash: 8bab26f4f68e0e26f0bb7960be334d5b520ea452
commit-date: 2026-07-14
host: aarch64-apple-darwin
release: 1.97.1
LLVM version: 22.1.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.