connectat()/bindat() or an alternative design
Subsystems (14)
RFC 1/1 connectat()/bindat() or an alternative design
Hello, I am interested in seeing something like the BSD's connectat/bindat in Linux.
The deficiencies with traditional bind/connect for unix sockets are well-known. The advantages of openat2 over openat over open are also well-known, so I won't waste anyone's time restating either.
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/all/CAEnbY+co6YLXANfeMnfBOBs8Ba_Sbdqz0Ahm8RzAhRF7MrxL4Q@mail.gmail.com/
Here are two previous times this was raised, unfortunately with no replies. Hoping this third time is the charm!
To hopefully give the discussion a bit more concreteness, I have taken a stab at two refactors (with an LLM, full disclosure) to get a sense of what the approaches look like. (To be clear, these are not patches I am trying to formally submit, these are just sketches to guide the conversation. I haven't built or tested them, just read them.)
https://github.com/Ericson2314/linux/tree/bindat-connectat
1. 9b72f7e2add657cfd0d755c7ea24e56f1aab7025
The first is a straightforward port of connectat/bindat, except for a (not yet used) flags argument so the likes of AT_SYMLINK_NOFOLLOW and AT_EMPTY_PATH could someday be supported. (Or RESOLVE_NO_SYMLINKS from openat2 for something more stringent than AT_SYMLINK_NOFOLLOW.)
I made some effort to deduplicate things as much as possible, and support io_uring. So the (again WIP) diff is somewhat elegant. Still though, to me at least, a glaring issue with this design is that we are making new syscall and other "entrypoint" machinery for *all* socket types, when only unix sockets stand to benefit from this. For everything else, the other parameters are just nonsense to be ignored or, worse, confused by.
That brings me to the next attempt, which I personally prefer:
https://github.com/Ericson2314/linux/tree/sockaddr_un2
1. 96c45c5cc43799f95a1a90a87cfaccf9377b82da
2. d183785e63d0b224de21fc4f7e505eef99b27592
Here, instead of making a new connect/bind, I've made a new struct sockaddr_un alternative.
Actually, there are two commits. The first commit cleans up an internal data structure to decouple it from struct sockaddr_un and thus the stable syscall ABI. I like this commit in any event, but it is fine to also just view it as prep for the second commit.
In that second commit, a new struct sockaddr_un2 is created, which has a pointer to a path rather than an inline path to get around path length issues, an fd / AT_FDCWD for at-relative argument, and the flags argument.
I very much like how this localizes what is useful for unix sockets to just that case, without bloating the code for the other socket types. Even including the preparatory first commit, this way is still fewer lines of code, too. It feels like the correct approach to me.
Ultimately though, I am happy to go with whichever approach the networking maintainers go with --- either would be preferable to the status quo. If one of those (or something else!) looks promising, let me know, and I am happy to take the time to polish it up into a proper submitted patch.
Thanks,
John
From 9b72f7e2add657cfd0d755c7ea24e56f1aab7025 Mon Sep 17 00:00:00 2001
From: John Ericson <[email protected]>
Date: Mon, 18 May 2026 11:17:03 -0400
Subject: [PATCH] [WIP, no-submit] connectat and bindat like BSD for Linux
A quick exploratory refactor with an LLM.
The flags param is new here. It would allow supporting e.g. AT_SYMLINK_NOFOLLOW and AT_EMPTY_PATH in the future.
N.B. Tests have not been run yet. I don't know if those are good at all. I just read and reviewed the refactors themselves as they are easy to read.
--- arch/x86/entry/syscalls/syscall_32.tbl | 2 + arch/x86/entry/syscalls/syscall_64.tbl | 2 + fs/namei.c | 7 + include/linux/namei.h | 1 + include/linux/net.h | 6 + include/linux/socket.h | 14 +- include/linux/syscalls.h | 2 + include/uapi/asm-generic/unistd.h | 8 +- include/uapi/linux/io_uring.h | 2 + io_uring/net.c | 73 +++-- io_uring/net.h | 6 +- io_uring/opdef.c | 32 ++- net/compat.c | 4 +- net/socket.c | 85 ++++-- net/unix/af_unix.c | 67 ++++- tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/unix_connect_at.c | 265 ++++++++++++++++++ 17 files changed, 513 insertions(+), 64 deletions(-) create mode 100644 tools/testing/selftests/net/af_unix/unix_connect_at.c
arch/x86/entry/syscalls/syscall_32.tbl +2 -0
@@ -477,3 +477,5 @@ 469 i386 file_setattr sys_file_setattr 470 i386 listns sys_listns 471 i386 rseq_slice_yield sys_rseq_slice_yield +472 i386 bindat sys_bindat +473 i386 connectat sys_connectat
arch/x86/entry/syscalls/syscall_64.tbl +2 -0
@@ -396,6 +396,8 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common bindat sys_bindat +473 common connectat sys_connectat # # Due to a historical design error, certain syscalls are numbered differently
fs/namei.c +7 -0
@@ -3045,6 +3045,13 @@ int kern_path(const char *name, unsigned int flags, struct path *path) }EXPORT_SYMBOL (kern_path); +int kern_path_at (int dfd,const char *name,unsigned int flags,struct path *path) +{ +CLASS (filename_kernel, filename)(name); +return filename_lookup (dfd, filename, flags, path,NULL ); +} +EXPORT_SYMBOL (kern_path_at); +/** * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair * @filename: filename structure
include/linux/namei.h +1 -0
@@ -55,6 +55,7 @@ extern int path_pts(struct path *path);extern int user_path_at (int ,const char __user *,unsigned ,struct path *);extern int kern_path (const char *,unsigned ,struct path *); +extern int kern_path_at (int ,const char *,unsigned ,struct path *);struct dentry *kern_path_parent (const char *name,struct path *parent);extern struct dentry *start_creating_path (int ,const char *,struct path *,unsigned int );
include/linux/net.h +6 -0
@@ -185,9 +185,15 @@ struct proto_ops {int (*bind) (struct socket *sock,struct sockaddr_unsized *myaddr,int sockaddr_len); +int (*bind_at) (struct socket *sock,int dfd, +struct sockaddr_unsized *myaddr, +int sockaddr_len,int flags);int (*connect) (struct socket *sock,struct sockaddr_unsized *vaddr,int sockaddr_len,int flags); +int (*connect_at)(struct socket *sock,int dfd, +struct sockaddr_unsized *vaddr, +int sockaddr_len,int flags);int (*socketpair)(struct socket *sock1,struct socket *sock2); int (*accept) (struct socket *sock,
include/linux/socket.h +8 -6
@@ -459,13 +459,15 @@ extern int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen, int flags);extern int __sys_socket (int family,int type,int protocol);extern struct file *__sys_socket_file (int family,int type,int protocol); -extern int __sys_bind (int fd,struct sockaddr __user *umyaddr,int addrlen); -extern int __sys_bind_socket (struct socket *sock,struct sockaddr_storage *address, -int addrlen); -extern int __sys_connect_file (struct file *file,struct sockaddr_storage *addr, +extern int __sys_bindat (int dfd,int fd,struct sockaddr __user *umyaddr, +int addrlen,int flags); +extern int __sys_bind_socket (struct socket *sock,int dfd, +struct sockaddr_storage *address,int addrlen); +extern int __sys_connect_file (struct file *file,int dfd, +struct sockaddr_storage *addr,int addrlen,int file_flags); -extern int __sys_connect (int fd,struct sockaddr __user *uservaddr, -int addrlen); +extern int __sys_connectat (int dfd,int fd,struct sockaddr __user *uservaddr, +int addrlen,int flags);extern int __sys_listen (int fd,int backlog);extern int __sys_listen_socket (struct socket *sock,int backlog); extern int do_getsockname(struct socket *sock, int peer,
include/linux/syscalls.h +2 -0
@@ -772,9 +772,11 @@ asmlinkage long sys_shmdt(char __user *shmaddr);asmlinkage long sys_socket (int ,int ,int );asmlinkage long sys_socketpair (int ,int ,int ,int __user *);asmlinkage long sys_bind (int ,struct sockaddr __user *,int ); +asmlinkage long sys_bindat (int ,int ,struct sockaddr __user *,int ,int );asmlinkage long sys_listen (int ,int );asmlinkage long sys_accept (int ,struct sockaddr __user *,int __user *);asmlinkage long sys_connect (int ,struct sockaddr __user *,int ); +asmlinkage long sys_connectat (int ,int ,struct sockaddr __user *,int ,int );asmlinkage long sys_getsockname (int ,struct sockaddr __user *,int __user *);asmlinkage long sys_getpeername (int ,struct sockaddr __user *,int __user *); asmlinkage long sys_sendto(int, void __user *, size_t, unsigned,
include/uapi/asm-generic/unistd.h +7 -1
@@ -863,8 +863,14 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471__SYSCALL (__NR_rseq_slice_yield, sys_rseq_slice_yield) +/* net/socket.c */ +#define __NR_bindat 472 +__SYSCALL(__NR_bindat, sys_bindat) +#define __NR_connectat 473 +__SYSCALL(__NR_connectat, sys_connectat) +#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_syscalls 474/* * 32 bit systems traditionally used different
include/uapi/linux/io_uring.h +2 -0
@@ -318,6 +318,8 @@ enum io_uring_op {IORING_OP_PIPE ,IORING_OP_NOP128 ,IORING_OP_URING_CMD128 , +IORING_OP_BINDAT , +IORING_OP_CONNECTAT ,/* this goes last, obviously */ IORING_OP_LAST,
io_uring/net.c +56 -17
145 lines not on this page · raw message
io_uring/net.h +4 -2
@@ -48,14 +48,16 @@ int io_socket(struct io_kiocb *req, unsigned int issue_flags);void io_socket_bpf_populate (struct io_uring_bpf_ctx *bctx,struct io_kiocb *req);int io_connect_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); -int io_connect (struct io_kiocb *req,unsigned int issue_flags); +int io_connectat_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); +int io_connectat (struct io_kiocb *req,unsigned int issue_flags);int io_sendmsg_zc (struct io_kiocb *req,unsigned int issue_flags);int io_send_zc_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);void io_send_zc_cleanup (struct io_kiocb *req);int io_bind_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); -int io_bind (struct io_kiocb *req,unsigned int issue_flags); +int io_bindat_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); +int io_bindat (struct io_kiocb *req,unsigned int issue_flags);int io_listen_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);int io_listen (struct io_kiocb *req,unsigned int issue_flags);
io_uring/opdef.c +30 -2
@@ -205,7 +205,7 @@ const struct io_issue_def io_issue_defs[] = {#if defined(CONFIG_NET ) .async_size = sizeof(struct io_async_msghdr), .prep = io_connect_prep, - .issue = io_connect, + .issue = io_connectat, #else .prep = io_eopnotsupp_prep,#endif
@@ -502,7 +502,7 @@ const struct io_issue_def io_issue_defs[] = {#if defined(CONFIG_NET ) .needs_file = 1, .prep = io_bind_prep, - .issue = io_bind, + .issue = io_bindat, .async_size = sizeof(struct io_async_msghdr), #else .prep = io_eopnotsupp_prep,
@@ -589,6 +589,28 @@ const struct io_issue_def io_issue_defs[] = { .prep = io_uring_cmd_prep, .issue = io_uring_cmd, }, + [IORING_OP_BINDAT] = { +#if defined(CONFIG_NET ) + .needs_file = 1, + .prep = io_bindat_prep, + .issue = io_bindat, + .async_size = sizeof(struct io_async_msghdr), +#else + .prep = io_eopnotsupp_prep, +#endif + }, + [IORING_OP_CONNECTAT] = { + .needs_file = 1, + .unbound_nonreg_file = 1, + .pollout = 1, +#if defined(CONFIG_NET ) + .async_size = sizeof(struct io_async_msghdr), + .prep = io_connectat_prep, + .issue = io_connectat, +#else + .prep = io_eopnotsupp_prep, +#endif + }, };const struct io_cold_def io_cold_defs[] = {
@@ -847,6 +869,12 @@ const struct io_cold_def io_cold_defs[] = { .sqe_copy = io_uring_cmd_sqe_copy, .cleanup = io_uring_cmd_cleanup, }, + [IORING_OP_BINDAT] = { + .name = "BINDAT", + }, + [IORING_OP_CONNECTAT] = { + .name = "CONNECTAT", + }, };const char *io_uring_get_opcode (u8 opcode)
net/compat.c +2 -2
@@ -448,10 +448,10 @@ COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args) ret =__sys_socket (a0, a1, a[2 ]);break ;case SYS_BIND : - ret =__sys_bind (a0,compat_ptr (a1), a[2 ]); + ret =__sys_bindat (AT_FDCWD , a0,compat_ptr (a1), a[2 ],0 );break ;case SYS_CONNECT : - ret =__sys_connect (a0,compat_ptr (a1), a[2 ]); + ret =__sys_connectat (AT_FDCWD , a0,compat_ptr (a1), a[2 ],0 );break ;case SYS_LISTEN : ret =__sys_listen (a0, a1);
net/socket.c +65 -20
163 lines not on this page · raw message
net/unix/af_unix.c +53 -14
199 lines not on this page · raw message
tools/testing/selftests/net/af_unix/Makefile +1 -0
@@ -13,6 +13,7 @@ TEST_GEN_PROGS := \ scm_rights \ so_peek_off \ unix_connect \ + unix_connect_at \ unix_connreset \ # end of TEST_GEN_PROGS
tools/testing/selftests/net/af_unix/unix_connect_at.c new file +265 -0
265 lines not on this page · raw message
Signature · 1 line
2.51.2
From 96c45c5cc43799f95a1a90a87cfaccf9377b82da Mon Sep 17 00:00:00 2001
From: John Ericson <[email protected]>
Date: Mon, 18 May 2026 12:51:49 -0400
Subject: [PATCH 1/2] [WIP, no-submit] af_unix: store only the name in
unix_address, not the full sockaddr_un
struct unix_address previously stored a trailing struct sockaddr_un name[], including the redundant sun_family field. Since unix_address is only used within AF_UNIX code, the family is always AF_UNIX and adds no information.
Change unix_address.name to char name[] containing just the path/ abstract name bytes, with addr->len being the name length. This removes the redundant family field and decouples the internal representation from the sockaddr_un wire format, which will make it easier to support alternative improved ways of binding/connecting unix sockets in the future. (By "improved ways", supporting use cases such as longer paths, fd-relative paths, and openat{,2}-style flags.)
--- include/net/af_unix.h | 4 +- net/unix/af_unix.c | 64 ++++++++++++++--------------- net/unix/diag.c | 4 +- security/apparmor/af_unix.c | 40 +++++++++--------- security/apparmor/include/af_unix.h | 9 ++-- security/apparmor/net.c | 29 ++++++------- security/landlock/task.c | 3 +- security/lsm_audit.c | 4 +- 8 files changed, 73 insertions(+), 84 deletions(-)
include/net/af_unix.h +2 -2
@@ -23,8 +23,8 @@ static inline struct unix_sock *unix_get_socket(struct file *filp)struct unix_address {refcount_t refcnt; -int len; -struct sockaddr_un name[]; +int len;/* length of name[] */ +char name[]; };struct scm_stat {
net/unix/af_unix.c +32 -32
179 lines not on this page · raw message
net/unix/diag.c +1 -3
@@ -21,9 +21,7 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)if (!addr)return 0 ; -return nla_put (nlskb,UNIX_DIAG_NAME , - addr->len - offsetof(struct sockaddr_un , sun_path), - addr->name->sun_path); +return nla_put (nlskb,UNIX_DIAG_NAME , addr->len, addr->name); }static int sk_diag_dump_vfs (struct sock *sk,struct sk_buff *nlskb)
security/apparmor/af_unix.c +19 -21
@@ -67,12 +67,11 @@ static int unix_fs_perm(const char *op, u32 mask, const struct cred *subj_cred,#define FS_ADDR "/"/* path addr in fs */ static aa_state_t match_addr (struct aa_dfa *dfa,aa_state_t state, -struct sockaddr_un *addr,int addrlen) +const char *name,int name_len) { -if (addr) +if (name)/* include leading \0 */ - state =aa_dfa_match_len (dfa, state, addr->sun_path, -unix_addr_len (addrlen)); + state =aa_dfa_match_len (dfa, state, name, name_len);else state =aa_dfa_match_len (dfa, state,ANONYMOUS_ADDR ,1 );/* todo: could change to out of band for cleaner separation */
@@ -84,14 +83,14 @@ static aa_state_t match_addr(struct aa_dfa *dfa, aa_state_t state,static aa_state_t match_to_local (struct aa_policydb *policy,aa_state_t state,u32 request,int type,int protocol, -struct sockaddr_un *addr,int addrlen, +const char *name,int name_len,struct aa_perms **p,const char **info) { state =aa_match_to_prot (policy, state, request,PF_UNIX , type, protocol,NULL , info);if (state) { - state =match_addr (policy->dfa, state, addr, addrlen); + state =match_addr (policy->dfa, state, name, name_len);if (state) {/* todo: local label matching */ state =aa_dfa_null_transition (policy->dfa, state);
@@ -105,7 +104,7 @@ static aa_state_t match_to_local(struct aa_policydb *policy,return state; } -struct sockaddr_un *aa_sunaddr (const struct unix_sock *u,int *addrlen) +const char *aa_unix_addr_name (const struct unix_sock *u,int *addrlen) {struct unix_address *addr;
@@ -124,11 +123,11 @@ static aa_state_t match_to_sk(struct aa_policydb *policy, struct unix_sock *u, struct aa_perms **p, const char **info) { - int addrlen; -struct sockaddr_un *addr =aa_sunaddr (u, &addrlen); + int name_len; +const char *name =aa_unix_addr_name (u, &name_len);return match_to_local (policy, state, request, u->sk.sk_type, - u->sk.sk_protocol, addr, addrlen, p, info); + u->sk.sk_protocol, name, name_len, p, info); }#define CMD_ADDR 1
@@ -154,7 +153,7 @@ static aa_state_t match_to_cmd(struct aa_policydb *policy, aa_state_t state,static aa_state_t match_to_peer (struct aa_policydb *policy,aa_state_t state,u32 request,struct unix_sock *u, -struct sockaddr_un *peer_addr,int peer_addrlen, +const char *peer_addr,int peer_addrlen,struct aa_perms **p,const char **info) {AA_BUG (!p);
@@ -271,8 +270,7 @@ static int profile_bind_perm(struct aa_profile *profile, struct sock *sk,/* bind for abstract socket */ state =match_to_local (rules->policy, state,AA_MAY_BIND , sk->sk_type, sk->sk_protocol, -unix_addr (ad->net.addr), - ad->net.addrlen, + ad->net.addr, ad->net.addrlen, &p, &ad->info); return aa_do_perms(profile, rules->policy, state, AA_MAY_BIND,
@@ -387,7 +385,7 @@ static int profile_opt_perm(struct aa_profile *profile, u32 request,/* null peer_label is allowed, in which case the peer_sk label is used */ static int profile_peer_perm (struct aa_profile *profile,u32 request,struct sock *sk,const struct path *path, -struct sockaddr_un *peer_addr, +const char *peer_addr,int peer_addrlen,const struct path *peer_path,struct aa_label *peer_label,struct apparmor_audit_data *ad)
@@ -500,8 +498,8 @@ int aa_unix_bind_perm(struct socket *sock, struct sockaddr *addr,if (!unconfined (label)) {DEFINE_AUDIT_SK (ad,OP_BIND ,current_cred (), sock->sk); - ad.net.addr =unix_addr (addr); - ad.net.addrlen = addrlen; + ad.net.addr =unix_addr (addr)->sun_path; + ad.net.addrlen = addrlen - offsetof(struct sockaddr_un , sun_path); error =fn_for_each_confined (label, profile,profile_bind_perm (profile, sock->sk, &ad));
@@ -600,7 +598,7 @@ int aa_unix_opt_perm(const char *op, u32 request, struct socket *sock,static int unix_peer_perm (const struct cred *subj_cred,struct aa_label *label,const char *op,u32 request,struct sock *sk,const struct path *path, -struct sockaddr_un *peer_addr,int peer_addrlen, +const char *peer_addr,int peer_addrlen,const struct path *peer_path,struct aa_label *peer_label) {struct aa_profile *profile;
@@ -628,7 +626,7 @@ int aa_unix_peer_perm(const struct cred *subj_cred,struct unix_sock *peeru =unix_sk (peer_sk);struct unix_sock *u =unix_sk (sk);int plen; -struct sockaddr_un *paddr =aa_sunaddr (unix_sk (peer_sk), &plen); +const char *paddr =aa_unix_addr_name (unix_sk (peer_sk), &plen);AA_BUG (!label);AA_BUG (!sk);
@@ -710,7 +708,7 @@ int aa_unix_file_perm(const struct cred *subj_cred, struct aa_label *label, const char *op, u32 request, struct file *file) { struct socket *sock = (struct socket *) file->private_data; -struct sockaddr_un *addr, *peer_addr; +const char *addr, *peer_addr;int addrlen, peer_addrlen;struct aa_label *plabel =NULL ;struct sock *peer_sk =NULL ;
@@ -734,7 +732,7 @@ int aa_unix_file_perm(const struct cred *subj_cred, struct aa_label *label,sock_hold (peer_sk); is_sk_fs =is_unix_fs (sock->sk); - addr =aa_sunaddr (unix_sk (sock->sk), &addrlen); + addr =aa_unix_addr_name (unix_sk (sock->sk), &addrlen); path =unix_sk (sock->sk)->path;unix_state_unlock (sock->sk);
@@ -748,7 +746,7 @@ int aa_unix_file_perm(const struct cred *subj_cred, struct aa_label *label,if (!peer_sk) goto out; - peer_addr =aa_sunaddr (unix_sk (peer_sk), &peer_addrlen); + peer_addr =aa_unix_addr_name (unix_sk (peer_sk), &peer_addrlen);struct path peer_path;
security/apparmor/include/af_unix.h +4 -5
@@ -18,20 +18,19 @@#include "label.h" #define unix_addr (A ) ((struct sockaddr_un *)(A)) -#define unix_addr_len (L ) ((L) - sizeof(sa_family_t))#define unix_peer (sk) (unix_sk(sk)->peer)#define is_unix_addr_abstract_name (B ) ((B)[0] == 0) -#define is_unix_addr_anon (A ,L ) ((A) && unix_addr_len(L) <= 0) +#define is_unix_addr_anon (A ,L ) ((A) && (L) <= 0)#define is_unix_addr_fs (A ,L ) (!is_unix_addr_anon(A, L) && \ - !is_unix_addr_abstract_name(unix_addr(A)->sun_path)) + !is_unix_addr_abstract_name(A))#define is_unix_anonymous (U ) (!unix_sk(U)->addr)#define is_unix_fs (U ) (!is_unix_anonymous(U) && \ - unix_sk(U)->addr->name->sun_path[0]) + unix_sk(U)->addr->name[0])#define is_unix_connected (S ) ((S)->state == SS_CONNECTED) -struct sockaddr_un *aa_sunaddr (const struct unix_sock *u,int *addrlen); +const char *aa_unix_addr_name (const struct unix_sock *u,int *addrlen); int aa_unix_peer_perm(const struct cred *subj_cred, struct aa_label *label, const char *op, u32 request, struct sock *sk, struct sock *peer_sk,
security/apparmor/net.c +12 -17
@@ -74,22 +74,19 @@ static const char * const net_mask_names[] = { };static void audit_unix_addr (struct audit_buffer *ab,const char *str, -struct sockaddr_un *addr,int addrlen) +const char *name,int name_len) { -int len =unix_addr_len (addrlen); - -if (!addr || len <=0 ) { +if (!name || name_len <=0 ) {audit_log_format (ab," %s=none" , str); - }else if (addr->sun_path[0 ]) { + }else if (name[0 ]) {audit_log_format (ab," %s=" , str); -audit_log_untrustedstring (ab, addr->sun_path); +audit_log_untrustedstring (ab, name); }else {audit_log_format (ab," %s=\"@" , str); -if (audit_string_contains_control (&addr->sun_path[1 ], len -1 )) -audit_log_n_hex (ab, &addr->sun_path[1 ], len -1 ); +if (audit_string_contains_control (&name[1 ], name_len -1 )) +audit_log_n_hex (ab, &name[1 ], name_len -1 );else -audit_log_format (ab,"%.*s" , len -1 , - &addr->sun_path[1 ]); +audit_log_format (ab,"%.*s" , name_len -1 , &name[1 ]);audit_log_format (ab,"\"" ); } }
@@ -100,13 +97,12 @@ static void audit_unix_sk_addr(struct audit_buffer *ab, const char *str,const struct unix_sock *u =unix_sk (sk);if (u && u->addr) { -int addrlen; -struct sockaddr_un *addr =aa_sunaddr (u, &addrlen); +int name_len; +const char *name =aa_unix_addr_name (u, &name_len); -audit_unix_addr (ab, str, addr, addrlen); +audit_unix_addr (ab, str, name, name_len); }else {audit_unix_addr (ab, str,NULL ,0 ); - } }
@@ -144,13 +140,12 @@ void audit_net_cb(struct audit_buffer *ab, void *va)if (ad->common.u.net->family ==PF_UNIX ) {if (ad->net.addr || !ad->common.u.net->sk)audit_unix_addr (ab,"addr" , -unix_addr (ad->net.addr), - ad->net.addrlen); + ad->net.addr, ad->net.addrlen);else audit_unix_sk_addr (ab,"addr" , ad->common.u.net->sk);if (ad->request &NET_PEER_MASK ) {audit_unix_addr (ab,"peer_addr" , -unix_addr (ad->net.peer.addr), + ad->net.peer.addr, ad->net.peer.addrlen); } }
security/landlock/task.c +1 -2
@@ -255,8 +255,7 @@ static bool is_abstract_socket(struct sock *const sock)if (!addr)return false; -if (addr->len >= offsetof(struct sockaddr_un , sun_path) +1 && - addr->name->sun_path[0 ] =='\0' ) +if (addr->len >=1 && addr->name[0 ] =='\0' )return true;return false;
security/lsm_audit.c +2 -2
@@ -324,8 +324,8 @@ void audit_log_lsm_data(struct audit_buffer *ab,audit_log_d_path (ab," path=" , &u->path);break ; } - len = addr->len-sizeof (short ); - p = &addr->name->sun_path[0 ]; + len = addr->len; + p = addr->name;audit_log_format (ab," path=" );if (*p)audit_log_untrustedstring (ab, p);
Signature · 1 line
2.51.2
From d183785e63d0b224de21fc4f7e505eef99b27592 Mon Sep 17 00:00:00 2001
From: John Ericson <[email protected]>
Date: Mon, 18 May 2026 12:03:37 -0400
Subject: [PATCH 2/2] [WIP, no-submit] AF_UNIX2 alternative to connectat
and bindat
A quick exploratory refactor with an LLM.
The flags param is new here. It would allow supporting e.g. AT_SYMLINK_NOFOLLOW and AT_EMPTY_PATH in the future.
N.B. Tests have not been run yet. I don't know if those are good at all. I just read and reviewed the refactors themselves as they are easy to read.
--- include/linux/socket.h | 13 +- include/uapi/linux/un.h | 7 + net/unix/af_unix.c | 246 +++++++++++++----- tools/testing/selftests/net/af_unix/Makefile | 1 + .../selftests/net/af_unix/unix_connect_at.c | 215 +++++++++++++++ 5 files changed, 411 insertions(+), 71 deletions(-) create mode 100644 tools/testing/selftests/net/af_unix/unix_connect_at.c
include/linux/socket.h +11 -2
@@ -255,8 +255,17 @@ struct ucred {#define AF_MCTP 45/* Management component * transport protocol */ - -#define AF_MAX 46/* For now.. */ +#define AF_UNIX2 46/* AF_UNIX with extended addressing. +* Note this is *not* an actual distinct +* address family, but just here to +* distinguish different initialization +* structs.Any internal non-syscall-API +* usage should normalize AF_UNIX2 to +* AF_UNIX as there is no difference +* between sockets initialized the two +* different ways. */ + +#define AF_MAX 47/* For now.. */ /* Protocol families, same as address families. */ #define PF_UNSPEC AF_UNSPEC
include/uapi/linux/un.h +7 -0
@@ -11,6 +11,13 @@ struct sockaddr_un { char sun_path[UNIX_PATH_MAX]; /* pathname */ }; +struct sockaddr_un2 { +__kernel_sa_family_t sun2_family;/* AF_UNIX2 */ +__s32 sun2_dfd;/* directory fd, or AT_FDCWD */ +const char *sun2_path;/* pointer to null-terminated path */ +__u32 sun2_flags;/* reserved, must be 0 */ +}; +#define SIOCUNIXFILE (SIOCPROTOPRIVATE + 0)/* open a socket file with O_PATH */ #endif /* _LINUX_UN_H */
net/unix/af_unix.c +177 -69
362 lines not on this page · raw message
tools/testing/selftests/net/af_unix/Makefile +1 -0
@@ -13,6 +13,7 @@ TEST_GEN_PROGS := \ scm_rights \ so_peek_off \ unix_connect \ + unix_connect_at \ unix_connreset \ # end of TEST_GEN_PROGS
tools/testing/selftests/net/af_unix/unix_connect_at.c new file +215 -0
215 lines not on this page · raw message
Signature · 1 line
2.51.2
Hi John,
Quoted 10 lines · John Ericson
Hello, I am interested in seeing something like the BSD's connectat/bindat in Linux.
The deficiencies with traditional
bind/connectfor unix sockets are well-known. The advantages of openat2 over openat over open are also well-known, so I won't waste anyone's time restating either.https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/all/CAEnbY+co6YLXANfeMnfBOBs8Ba_Sbdqz0Ahm8RzAhRF7MrxL4Q@mail.gmail.com/
Quoted 9 lines
Here are two previous times this was raised, unfortunately with no replies. Hoping this third time is the charm!
To hopefully give the discussion a bit more concreteness, I have taken a stab at two refactors (with an LLM, full disclosure) to get a sense of what the approaches look like. (To be clear, these are not patches I am trying to formally submit, these are just sketches to guide the conversation. I haven't built or tested them, just read them.)
Thanks for bringing this up.
I have no doubt connectat()/bindat() helps closing TOCTOU for Unix sockets. However, it would be nicer to describe your use case here, especially what the problems are without it. This would help more to jusify your proposal here than just getting aligned with openat() or BSD.
Hope this helps.
Regards,
Cong
Hi Cong,
Quoted 5 lines · Cong Wang
Hi John,
[...]
Thanks for bringing this up.
Sure, thanks for replying to me!
Quoted 10 lines
I have no doubt connectat()/bindat() helps closing TOCTOU for Unix sockets. However, it would be nicer to describe your use case here, especially what the problems are without it. This would help more to jusify your proposal here than just getting aligned with
openat()or BSD.Hope this helps.
Regards,
Cong
Yeah, happy to talk about that. Hope this is not too long a reply!
First, for some background context, I am a developer of the Nix package manager. And this, plus my own personal taste, always has me thinking about ways we can run processes with fewer privileges. The no-ambient-authority capsicum/cloudabi/wasi/whatever dream has lived in my head rent-free for many years :). Now these days, with LLMs, it feels like these nice-to-have yak shaves of mine are finally worth dusting off and striking off the bucket list.
Also in recent months, we Nix developers have been putting a bunch of work into using more openat2 and friends, and I have no doubt that we will continue down this path (even on Windows!). We aim to be an exemplar program for following the "always work relative to a file descriptor" discipline. It's good for security, but also makes for code that --- I believe --- is just more elegant and nicer to read.
----
Nearer term use case: slightly less ugly long path socket opening in Nix:
If you look at [1] you can see a PR I've asked my coworker to draft to improve binding and connecting code to cope with longer file paths, something which does come up in practice when we are running multiple tests with multiple daemons in parallel.
Now, I think it is safe to say that this code was already quite complex, and in this patch only gets *more* complex. The current interfaces make supporting longer paths quite annoying. (Though, once we remove the open and switch to an *at-style interface in the wrapper (if macOS lets us), it will get less bad.)
So the first use case would be getting something nicer than the /proc/self/fd/<N> dance the linked code falls back to. It is good that /proc/self/fd/<N> exists for legacy code, but it is an unergonomic way to do file-descriptor-relative paths, and should be a fallback, never the first choice. A real fd parameter along with a regular path pointer would buy two concrete wins:
1. A clean, separate file descriptor parameter, the way openat has one --- rather than assembling a /proc path by hand.
2. Normal PATH_MAX room for the real pathname, rather than cramming /proc/self/fd/<N> (plus any residual path after it) into the small sun_path field of struct sockaddr_un.
----
Longer term use case: anonymous listening sockets, avoiding advertising sockets to potential clients using ambient authority mechanisms altogether:
Some more background: I think this whole business of listening unix sockets necessarily living in the file system is a bit silly, since there is nothing to put on disk --- it's just a mechanism to communicate to clients where they should connect. Now ostensibly, Linux agrees --- that is why Linux's *abstract* Unix domain sockets were created. But I really don't like this because we have just replaced one ambient authority contraption (the root filesystem) with another (the abstract socket name space in the network namespace). The problems with ambient authority remain all the same (and indeed, our experience with Nix has been that network namespace unsharing when you do want to do some outside world network access is much more work than filesystem namespace unsharing).
What I would really like to do is go further than what I proposed, and separate the binding of a unix socket from the placing in the file system.
Today, with only existing UAPIs, the closest you can get is a scratch path you pin with O_PATH and immediately unlink:
/* server */
int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
struct sockaddr_un a = { .sun_family = AF_UNIX };
strcpy(a.sun_path, "/tmp/scratchXXXXXX");
bind(lfd, (struct sockaddr *)&a, sizeof a);
int addrfd = open(a.sun_path, O_PATH | O_CLOEXEC); /* pin the socket inode */
unlink(a.sun_path); /* nameless now */
listen(lfd, 64);
/* client, handed `addrfd` -- but still has to *name* it, via /proc magic */
struct sockaddr_un c = { .sun_family = AF_UNIX };
sprintf(c.sun_path, "/proc/self/fd/%d", addrfd);
connect(cfd, (struct sockaddr *)&c, sizeof c);So even though I hold the socket by descriptor, I still route a pathname (/proc/self/fd/...) to reach it, and I have to deal with the /tmp/scratchXXXXXX proper temp file usage.
What I'd actually want is to sidestep all those nuisances entirely.
The important piece is a bind variation: like binding an abstract unix socket, except that it publishes no abstract socket name, so the *only* way to connect to the socket is to be given an fd referring to it.
A matching connect variation is more of a nice-to-have: it lets a client connect straight through that fd, rather than having to name it via /proc/self/fd as above.
Put together:
/* server */
int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
int addrfd = bind_anon(lfd, /*flags, for the future*/0); /* proposed: no filesystem or abstract name */
listen(lfd, 64);
/* client, handed `addrfd` -- connect straight to the descriptor */
connectat(addrfd, cfd, NULL, 0, AT_EMPTY_PATH); /* proposed */I would use this *a lot*! First of all, in our testing code, I would use this, and not even bother (on Linux at least) putting the test daemon socket on a (probably quite long) path; I would just rig up the test harness to pass the fd to the client process with an environment variable (local not global naming!) indicating to the process which file descriptor it should connect to.
If that sounds vaguely like systemd socket activation, yes it should. Socket activating *servers*, as we do today, is great, but I would also modify my init system to pass these listening sockets to *client* services. At that point, servers should ditch any sort of getsockopt authentication (which they are likely to implement incorrectly or in an ad-hoc manner), and instead rely on the init system to make sure only services/users which are authorized to connect to a given server have been given its listening socket file descriptor.
----
Misc notes:
[Note 1]: I didn't specify what bind_anon should do for getpeername but frankly, I don't really care. getpeername already doesn't identify unix sockets uniquely, since one can bind using relative paths.
[Note 2]: Insofar as we are designing new interfaces, we might ask whether the division of labor across 3 system calls --- socket, bind/bind_anon, and listen --- is really carrying its weight, but this is orthogonal tech debt.
[Note 3]: As a bonus, bind_anon subsumes the traditional pathname bind, with a nice separation of concerns: bind first, name later (if ever).
/* server, bonus before listening */
linkat(addrfd, "", AT_FDCWD, "/run/myservice.sock", AT_EMPTY_PATH);This needs bind_anon to create the socket O_TMPFILE-style --- nlink == 0 but materializable by linkat, reusing the existing may_linkat() carve-out. (I checked: today you *can* linkat a bound socket that still has a name, but not once it has been unlinked to namelessness --- which is exactly the anonymous case --- so this really does need the new bind.)
[Note 4]: If you look at [2] (another example of one of my old dreams perhaps finally coming true, this time better process spawning), you will see I mention that I would like null namespaces to ratchet down process privileges even further than we can today, and also have a nicer default state for a new process creation UAPI. In the case of a null mount namespace / null root fs, /proc/self/fd/<M> would no longer work, but explicit file-descriptor-relative APIs would.
Finally, I've attached a little test program I used to double-check some of my points, in case that is useful to anyone.
[1]: https://github.com/NixOS/nix/pull/15867
[2]: https://lore.kernel.org/all/[email protected]/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
static int make_listener(const char *path)
{
int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (lfd < 0) { perror("socket lfd"); return -1; }
struct sockaddr_un a = { .sun_family = AF_UNIX };
snprintf(a.sun_path, sizeof a.sun_path, "%s", path);
unlink(path);
if (bind(lfd, (struct sockaddr *)&a, sizeof a) < 0) { perror("bind"); return -1; }
if (listen(lfd, 64) < 0) { perror("listen"); return -1; }
return lfd;
}/* connect to a unix socket by pathname; returns 0 on success */ static int connect_path(const char *path)
{
int cfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
struct sockaddr_un c = { .sun_family = AF_UNIX };
snprintf(c.sun_path, sizeof c.sun_path, "%s", path);
int r = connect(cfd, (struct sockaddr *)&c, sizeof c);
close(cfd);
return r;
}static void try_connect(const char *label, int targetfd, int lfd)
{
char p[64];
snprintf(p, sizeof p, "/proc/self/fd/%d", targetfd);
if (connect_path(p) < 0) {
printf("%-30s via %-18s -> FAILED: %s\n", label, p, strerror(errno));
} else {
int s = accept(lfd, NULL, NULL);
printf("%-30s via %-18s -> SUCCEEDED (accept %s)\n",
label, p, s >= 0 ? "ok" : "FAILED");
if (s >= 0) close(s);
}
}/* linkat the socket referred to by (olddirfd, oldpath, flags) to newpath,
* then prove it by connecting through newpath. */
static void try_link(const char *label, int olddirfd, const char *oldpath,
int flags, const char *newpath)
{
unlink(newpath);
if (linkat(olddirfd, oldpath, AT_FDCWD, newpath, flags) < 0) {
printf("%-30s -> linkat FAILED: %s\n", label, strerror(errno));
return;
}
if (connect_path(newpath) < 0)
printf("%-30s -> linked, but connect FAILED: %s\n", label, strerror(errno));
else
printf("%-30s -> linked AND connectable\n", label);
unlink(newpath);
}int main(void)
{
char pa[64], pb[64], pc[64], pl[80];
snprintf(pa, sizeof pa, "/tmp/sockfdtest.a.%d", getpid());
snprintf(pb, sizeof pb, "/tmp/sockfdtest.b.%d", getpid());
snprintf(pc, sizeof pc, "/tmp/sockfdtest.c.%d", getpid());
snprintf(pl, sizeof pl, "/tmp/sockfdtest.link.%d", getpid());
/* A: pin the bind path's inode with O_PATH, unlink, connect via the pin fd */
int lfd_a = make_listener(pa);
int pin = open(pa, O_PATH | O_CLOEXEC);
if (pin < 0) perror("open O_PATH");
unlink(pa);
try_connect("A: O_PATH pin", pin, lfd_a);
/* B: skip the pin -- connect via the listening socket fd itself */
int lfd_b = make_listener(pb);
/* int pin_b = open(pb, O_PATH | O_CLOEXEC); */ /* <-- skipped on purpose */
try_connect("B: listen fd direct", lfd_b, lfd_b);
unlink(pb);
/* C..E: can we *materialize* a bound socket into the fs via link/linkat? */
int lfd_c = make_listener(pc); /* pc: bound socket file, nlink=1 */
int pin_c = open(pc, O_PATH | O_CLOEXEC); /* fd to that inode */
(void)lfd_c; /* kept open to keep the listener alive */
/* C: plain hardlink of the socket *pathname* (nlink 1 -> 2) */
try_link("C: link(path) hardlink", AT_FDCWD, pc, 0, pl);
/* D: linkat the *fd* via AT_EMPTY_PATH, inode still has a name (nlink=1) */
try_link("D: linkat fd AT_EMPTY_PATH", pin_c, "", AT_EMPTY_PATH, pl);
/* E: now make it nameless (nlink=0, sock still bound), then linkat the fd
* -- this is the O_TMPFILE-style "name an anonymous inode" move. */
unlink(pc);
try_link("E: linkat fd, nlink==0", pin_c, "", AT_EMPTY_PATH, pl);
return 0;
}Quoted 39 lines · John Ericson
Hi Cong,
Quoted 5 lines · Cong Wang
Hi John,
[...]
Thanks for bringing this up.
Sure, thanks for replying to me!
Quoted 10 lines
I have no doubt connectat()/bindat() helps closing TOCTOU for Unix sockets. However, it would be nicer to describe your use case here, especially what the problems are without it. This would help more to jusify your proposal here than just getting aligned with
openat()or BSD.Hope this helps.
Regards,
CongYeah, happy to talk about that. Hope this is not too long a reply!
First, for some background context, I am a developer of the Nix package manager. And this, plus my own personal taste, always has me thinking about ways we can run processes with fewer privileges. The no-ambient-authority capsicum/cloudabi/wasi/whatever dream has lived in my head rent-free for many years :). Now these days, with LLMs, it feels like these nice-to-have yak shaves of mine are finally worth dusting off and striking off the bucket list.
Also in recent months, we Nix developers have been putting a bunch of work into using more
openat2and friends, and I have no doubt that we will continue down this path (even on Windows!). We aim to be an exemplar program for following the "always work relative to a file descriptor" discipline. It's good for security, but also makes for code that --- I believe --- is just more elegant and nicer to read.
Quoted 4 lines
----
Nearer term use case: slightly less ugly long path socket opening in Nix:
"Nix needs it" is a much better justification than "BSD already has it". :) So please add this to your patch description/cover letter.
Quoted 32 lines
If you look at [1] you can see a PR I've asked my coworker to draft to improve binding and connecting code to cope with longer file paths, something which does come up in practice when we are running multiple tests with multiple daemons in parallel.
Now, I think it is safe to say that this code was already quite complex, and in this patch only gets *more* complex. The current interfaces make supporting longer paths quite annoying. (Though, once we remove the
openand switch to an*at-style interface in the wrapper (if macOS lets us), it will get less bad.)So the first use case would be getting something nicer than the
/proc/self/fd/<N>dance the linked code falls back to. It is good that/proc/self/fd/<N>exists for legacy code, but it is an unergonomic way to do file-descriptor-relative paths, and should be a fallback, never the first choice. A real fd parameter along with a regular path pointer would buy two concrete wins:1. A clean, separate file descriptor parameter, the way
openathas one --- rather than assembling a/procpath by hand.2. Normal
PATH_MAXroom for the real pathname, rather than cramming/proc/self/fd/<N>(plus any residual path after it) into the smallsun_pathfield ofstruct sockaddr_un.----
Longer term use case: anonymous listening sockets, avoiding advertising sockets to potential clients using ambient authority mechanisms altogether:
Quoted 12 lines
Some more background: I think this whole business of listening unix sockets necessarily living in the file system is a bit silly, since there is nothing to put on disk --- it's just a mechanism to communicate to clients where they should connect. Now ostensibly, Linux agrees --- that is why Linux's *abstract* Unix domain sockets were created. But I really don't like this because we have just replaced one ambient authority contraption (the root filesystem) with another (the abstract socket name space in the network namespace). The problems with ambient authority remain all the same (and indeed, our experience with Nix has been that network namespace unsharing when you do want to do some outside world network access is much more work than filesystem namespace unsharing).
Indeed, it would be very hard to change since it is coded in UDS API since probably day 1.
Just curious: any reason not to use TCP loopback here?
Quoted 5 lines
What I would really like to do is go further than what I proposed, and separate the binding of a unix socket from the placing in the file system.
Quoted 8 lines
Today, with only existing UAPIs, the closest you can get is a scratch path you pin with
O_PATHand immediately unlink:/* server */ int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); struct sockaddr_un a = { .sun_family = AF_UNIX }; strcpy(a.sun_path, "/tmp/scratchXXXXXX"); bind(lfd, (struct sockaddr *)&a, sizeof a);
Any reason not to use abstract socket?
abstract
an abstract socket address is distinguished (from a pathname socket) by the fact that sun_path[0] is a null byte ('\0'). The socket's address in this namespace is given by the additional bytes in sun_path that are covered by the specified length of the address structure. (Null bytes in the name have no special significance.) The name has no connection with filesystem pathnames. When the address of an abstract socket is returned, the returned addrlen is greater than sizeof(sa_family_t) (i.e., greater than 2), and the name of the socket is contained in the first (addrlen - sizeof(sa_family_t)) bytes of sun_path.
Quoted 40 lines
int addrfd = open(a.sun_path, O_PATH | O_CLOEXEC); /* pin the socket inode */ unlink(a.sun_path); /* nameless now */ listen(lfd, 64); /* client, handed `addrfd` -- but still has to *name* it, via /proc magic */ struct sockaddr_un c = { .sun_family = AF_UNIX }; sprintf(c.sun_path, "/proc/self/fd/%d", addrfd); connect(cfd, (struct sockaddr *)&c, sizeof c);So even though I hold the socket by descriptor, I still route a pathname (
/proc/self/fd/...) to reach it, and I have to deal with the/tmp/scratchXXXXXXproper temp file usage.What I'd actually want is to sidestep all those nuisances entirely.
The important piece is a
bindvariation: like binding an abstract unix socket, except that it publishes no abstract socket name, so the *only* way to connect to the socket is to be given an fd referring to it.A matching
connectvariation is more of a nice-to-have: it lets a client connect straight through that fd, rather than having to name it via/proc/self/fdas above.Put together:
/* server */ int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); int addrfd = bind_anon(lfd, /*flags, for the future*/0); /* proposed: no filesystem or abstract name */ listen(lfd, 64); /* client, handed `addrfd` -- connect straight to the descriptor */ connectat(addrfd, cfd, NULL, 0, AT_EMPTY_PATH); /* proposed */I would use this *a lot*! First of all, in our testing code, I would use this, and not even bother (on Linux at least) putting the test daemon socket on a (probably quite long) path; I would just rig up the test harness to pass the fd to the client process with an environment variable (local not global naming!) indicating to the process which file descriptor it should connect to.
Quoted 9 lines
If that sounds vaguely like systemd socket activation, yes it should. Socket activating *servers*, as we do today, is great, but I would also modify my init system to pass these listening sockets to *client* services. At that point, servers should ditch any sort of
getsockoptauthentication (which they are likely to implement incorrectly or in an ad-hoc manner), and instead rely on the init system to make sure only services/users which are authorized to connect to a given server have been given its listening socket file descriptor.
Thanks,
Cong
Hi Cong,
Sorry I have taken so long to respond. Rest assured, I still wish to put in the work discussing this and (if there is agreement) implementing it.
Quoted 2 lines · Cong Wang
"Nix needs it" is a much better justification than "BSD already has it". :) So please add this to your patch description/cover letter.
Well, to be clear, the motivation goes beyond Nix's immediate needs. I think the Nix ecosystem would be interested in my object capability / "zero trust" experiments that this (and other things) would enable, but this is farther afield.
Quoted 1 line
Just curious: any reason not to use TCP loopback here?
Sending file descriptors over sockets is very important to me, so TCP is ruled out.
Quoted 1 line
Any reason not to use abstract socket?
I wrote a bit about that but perhaps it got buried:
Quoted 7 lines
Quoted 7 lines
But I really don't like this because we have just replaced one ambient authority contraption (the root filesystem) with another (the abstract socket name space in the network namespace). The problems with ambient authority remain all the same (and indeed, our experience with Nix has been that network namespace unsharing when you do want to do some outside world network access is much more work than filesystem namespace unsharing).
To make that more concrete, connecting to an abstract socket by name has similar TOCTOU issues. For example, it is possible that the original server disconnected and something else "stole" the name in the meantime. With file descriptors there is no name reuse issue --- the O_PATH open file must point to the original socket.
I don't want the socket to have to live in the file system *or* the abstract socket namespace. I want it to be truly anonymous, and only referred to by file descriptors.
(Also note, the mechanisms described in my last email go further than the original patch's, but also subsume them. If it would be helpful to illustrate exactly what I mean, I would be happy to share a new patch implementing them instead.)
Quoted 2 lines
Indeed, it would be very hard to change since it is coded in UDS API since probably day 1.
Just to be clear, I don't consider things so hard to change, because the bad UDS UAPI stuff is rather "cosmetic". "Anonymous listening sockets" (let's call them) can be retrofitted fairly easily, just as abstract sockets were retrofitted fairly easily.
Thanks,
John
I'm bumping this and adding new recipients again in light of the discussion happening elsewhere in
<https://lore.kernel.org/all/[email protected]/>.
I don't want to count my chickens before they are hatched, but it is looking to me like a consensus in that thread is building around the ability to opt into intentionally empty/unusable root and working directories (at least with nullfs, maybe but less likely with other mechanisms instead).
That new functionality concretizes the motivation for what I am proposing in this thread: in such a world, there is little to no point binding listening sockets in the file system, because the containing directory would have to be conveyed by file descriptor anyways --- might as well just directly convey the socket to connect to by file descriptor. Likewise, abstract sockets are not appealing, because the abstract socket namespace is either too coarse-grained (leaking info in the same way root/cwd would), or too cumbersome to keep it from leaking.
To recap (with some slight changes, like renames), my latest proposal (a new version, not either of the two variations in the original email) is new syscalls bind_unix_anon and connectat, supporting a workflow like this:
/* server */
int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
int addrfd = bind_unix_anon(
lfd,
/*flags, for the future*/0);
listen(lfd, 64);
/* client, handed `addrfd` */
int cfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
connectat(addrfd, cfd, AT_EMPTY_PATH);Or, more radically, bind_unix_anon and connectat could let one skip the initial socket calls by returning those new sockets directly:
/* server */
int fds[2];
bind_unix_anon(
SOCK_STREAM | SOCK_CLOEXEC,
/*flags, for the future*/0,
fds);
int lfd = fds[0], addrfd = fds[1];
listen(lfd, 64);
/* client, handed `addrfd` */
int cfd = connectat(
addrfd,
SOCK_STREAM | SOCK_CLOEXEC,
AT_EMPTY_PATH);(Note that in this variation bind_unix_anon would return *two* file
descriptors: one for the server, with the permission to listen, and the other for clients, with just the privilege to connectat.) (Maybe bind_unix_anon should furthermore listen right away on lfd too?)
Of course, it would be nice to have io_uring versions of these too. But I don't know what the usual process is for that (regular first? io_uring first? both at the same time?)
Thanks,
John
P.S. For anyone just getting CC'd now, the first message in this thread is
<https://lore.kernel.org/all/[email protected]/>.
Hope that might save people a few keypresses :).
Two small addendums to my previous email:
First, I linked these two unresponded-to emails for prior art in the opening email of the thread:
Quoted 2 lines
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/all/CAEnbY+co6YLXANfeMnfBOBs8Ba_Sbdqz0Ahm8RzAhRF7MrxL4Q@mail.gmail.com/
Since then, I found one more:
https://lore.kernel.org/all/[email protected]/#t
which is an actual patch by the same person that wrote the first linked email, with replies. Hopefully that is useful.
Second, it occurred to me that regarding
Quoted 2 lines
(Maybe
bind_unix_anonshould furthermorelistenright away onlfdtoo?)
a useful thing to first decide is whether bind_unix_anon is helpful and/or should be allowed for SOCK_DGRAM.
John
Quoted 34 lines · John Ericson
I'm bumping this and adding new recipients again in light of the discussion happening elsewhere in
<https://lore.kernel.org/all/[email protected]/>.
I don't want to count my chickens before they are hatched, but it is looking to me like a consensus in that thread is building around the ability to opt into intentionally empty/unusable root and working directories (at least with nullfs, maybe but less likely with other mechanisms instead).That new functionality concretizes the motivation for what I am proposing in this thread: in such a world, there is little to no point binding listening sockets in the file system, because the containing directory would have to be conveyed by file descriptor anyways --- might as well just directly convey the socket to connect to by file descriptor. Likewise, abstract sockets are not appealing, because the abstract socket namespace is either too coarse-grained (leaking info in the same way root/cwd would), or too cumbersome to keep it from leaking.
To recap (with some slight changes, like renames), my latest proposal (a new version, not either of the two variations in the original email) is new syscalls
bind_unix_anonandconnectat, supporting a workflow like this:/* server */ int lfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); int addrfd = bind_unix_anon( lfd, /*flags, for the future*/0); listen(lfd, 64); /* client, handed `addrfd` */ int cfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); connectat(addrfd, cfd, AT_EMPTY_PATH);
Quoted 2 lines
Or, more radically,
bind_unix_anonandconnectatcould let one skip the initialsocketcalls by returning those new sockets directly:
Hm? Why not just setsockopt()? Something like:
struct unix_lookup_ctx {
int dirfd;
__u64 resolve_flags; /* RESOLVE_BENEATH, RESOLVE_NO_SYMLINKS, etc. */
__u64 op_flags; /* maybe future */
};
setsockopt(fd, SOL_UNIX, UNIX_NEXT_LOOKUP,
&ctx, sizeof(ctx));
bind(fd, (struct sockaddr *)&addr, addrlen);
/* or connect(fd, ...) */
Zero new syscall is needed.
Regards,
Cong
Quoted 8 lines · Cong Wang
Hm? Why not just
setsockopt()? Something like:struct
unix_lookup_ctx{
int dirfd;
__u64resolve_flags; /*RESOLVE_BENEATH,RESOLVE_NO_SYMLINKS, etc. */
__u64op_flags; /* maybe future */
};
Quoted 8 lines
setsockopt(fd,
SOL_UNIX,UNIX_NEXT_LOOKUP,&ctx, sizeof(ctx));
bind(fd, (struct sockaddr *)&addr, addrlen);
/* orconnect(fd, ...)*/Zero new syscall is needed.
If this supports the AT_EMPTY_PATH no filesystem at all use-case (which is what I chose to highlight in the previous summary email), I'll take it --- it hits my desiderata. But implementation-side, I don't relish the thought of even more state for socket setup --- since we need to persist the lookup context until the bind.
If setsockopt is the "ioctl of sockets" --- a convenient way to de facto create syscalls without doing so de jure --- I would think it would be better to just make it do the bind in one go so there is no state:
struct unix_bind_anon_ctx {
int *connect_fd; /* fd used to connect to this socket
written to this */
__u64 op_flags; /* future usage */
};
setsockopt(fd, SOL_UNIX, UNIX_BIND_ANON,
&ctx, sizeof(ctx));
Regular linkat can be used to place the "connect fd" in the filesystem for regular file-based connecting (e.g. to cope with long paths), in addition to how the "connect fd" can directly be used in the AT_EMPTY_PATH case.
and for the connecting side:
struct unix_connect_at_ctx {
int dirfd;
const char *path;
__u64 resolve_flags; /* RESOLVE_BENEATH, RESOLVE_NO_SYMLINKS, etc. */
__u64 op_flags; /* SOMETHING_EMPTY_PATH, other future usage */
};
setsockopt(fd, SOL_UNIX, UNIX_CONNECT_AT,
&ctx, sizeof(ctx));This is just what I had in my previous summary email, but with the new system calls twisted into setsockopt variations instead. (I forgot to mention the linkat usage in that email, oops, but it was always intended to be part of the plan.)
Cheers,
John
Christian Brauner
maintainerQuoted 18 lines · John Ericson
I'm bumping this and adding new recipients again in light of the discussion happening elsewhere in
<https://lore.kernel.org/all/[email protected]/>.
I don't want to count my chickens before they are hatched, but it is looking to me like a consensus in that thread is building around the ability to opt into intentionally empty/unusable root and working directories (at least with nullfs, maybe but less likely with other mechanisms instead).That new functionality concretizes the motivation for what I am proposing in this thread: in such a world, there is little to no point binding listening sockets in the file system, because the containing directory would have to be conveyed by file descriptor anyways --- might as well just directly convey the socket to connect to by file descriptor. Likewise, abstract sockets are not appealing, because the abstract socket namespace is either too coarse-grained (leaking info in the same way root/cwd would), or too cumbersome to keep it from leaking.
Quoted 4 lines
To recap (with some slight changes, like renames), my latest proposal (a new version, not either of the two variations in the original email) is new syscalls
bind_unix_anonandconnectat, supporting a workflow like this:
Please stop sending a bunch of disparate patch series that all do slightly related or overlapping things and point back at each other.
It's completely impossible to follow for anyone what's going on without chasing down discussion state across multiple subsystems. The net people have zero insight onto the fs struct discussions and it's completely pointless to try and design all of this based on thin air. Nothing is locked-in yet. This is not how this goes.
This frantic pushing of various features doesn't scale. It is requesting costly review time for multiple RFC series.
Quoted 11 lines · Christian Brauner
Please stop sending a bunch of disparate patch series that all do slightly related or overlapping things and point back at each other.
It's completely impossible to follow for anyone what's going on without chasing down discussion state across multiple subsystems. The net people have zero insight onto the fs struct discussions and it's completely pointless to try and design all of this based on thin air. Nothing is locked-in yet. This is not how this goes.
This frantic pushing of various features doesn't scale. It is requesting costly review time for multiple RFC series
I'm sorry for the confusion and the demands on your review time from concurrently spreading my various Capsicum-like ideas across too many threads. I can hereafter make things more single-threaded, as I'll propose below. I hope that helps.
Just so we are all on the same page, at the moment I believe I have created two RFC discussion threads, and submitted one RFC patch series:
- discussion thread: [RFC] Null Namespaces
URL: https://lore.kernel.org/all/[email protected]/
Current status:
- You (Christian) are writing nullfs/failfs FDs.
- I am submitting nothing else (e.g. the actual null namespaces)
until that is done.
- discussion thread: [RFC] connectat()/bindat() or an alternative design
(this one)
URL: https://lore.kernel.org/all/[email protected]/
Current status: Working on one patch:
- Patch Series: [RFC PATCH 0/3] coredump, net: fix layer violation
with direct connection
URL: https://lore.kernel.org/all/[email protected]/
- I am not submitting any UAPI changes until that is done.
(Note: This thread had some code in the first message, but I never
meant for that to be a formal submission, just a discussion aid, and
the conversation moved on from that original design anyways.)
To make things maximally single-threaded, I could pause working on "coredump, net: fix layer violation" (i.e. preparing a v2 based on what you requested in your review) until you are done implementing nullfs/failfs FDs. I am actually quite happy to do that if you want; I should spend less time on the computer the next few days anyways, so the timing is very good for that.
Either way, I can also henceforth not refer to yet-unwritten and -unmerged patches like nullfs/failfs FDs in the motivation for other things.
John
P.S. There is also my non-RFC bugfix series:
[PATCH net] af_unix: fix listen() succeeding on sockets in the wrong state
URL: https://lore.kernel.org/all/[email protected]/
but I don't believe you were referring to that one, since I think bugfixes are supposed to be submitted separately and immediately. Do correct me if I am wrong.