
Merged earlier this year for Linux 7.1 was extended attributes on sockets support as a feature sought after by the likes of GNOME and systemd for helping with Varlink IPC usage and other purposes. A limitation though of the functionality has been no efficient means for a BPF program to read those user extended attribute labels back. But with Linux 7.3 that’s being addressed with the new bpf_sock_read_xattr() kernel function.
Christian Brauner worked out the support for bpf_sock_read_xattr() to allow BPF programs read user.* extended attributes from a socket’s sockfs inode locklessly. He explained of the new functionality in the patch series:
“systemd uses user.* xattrs on sockets to implement socket rate limiting and to tag sockets for other purposes such as implementing a varlink registry. There is currently no efficient way for a BPF program to read those labels back. The new helper allows a listening socket marked with an extended attribute to be read back during bind/connect and then act on the connect()ing socket. Extended attributes make it possible to allow an unprivileged user manager such as systemd –user to mark sockets from userspace and then rediscover them or implement policies.
The kfunc is registered KF_RCU and only for BPF LSM programs. A struct socket is only guaranteed to live in sockfs when an LSM socket hook hands it out, which is what keeps SOCK_INODE() valid. Sockets that embed struct socket outside sockfs (tun, tap) are only reachable from tracing programs and are excluded by the registration. (Btw, for consistency it would be nice to force allocation of struct socket from sockfs instead of simply embedding it in e.g., struct tun_file which makes the SOCKFS_I() pattern a hazard – at least outside of sockfs functions.)
The read never sleeps and takes no lock. For sockfs the value lives in the inode’s in-memory xattr store and simple_xattr_get() resolves it with an RCU-protected rhashtable lookup, taking neither the inode lock nor any xattr lock. The kfunc is therefore usable from both sleepable and non-sleepable LSM hooks.”
That bpf_sock_read_xattr support is now merged via this pull for the Linux 7.3 kernel.