Thread (13 messages) 13 messages, 3 authors, 2025-07-24
STALE322d
Revisions (3)
  1. v1 current
  2. v2 [diff vs current]
  3. v3 [diff vs current]

[PATCH 1/6 net-next] net: uapi: Add __kernel_sockaddr_unspec for sockaddr of unknown length

From: Kees Cook <kees@kernel.org>
Date: 2025-07-23 23:19:26
Subsystem: networking [general], networking [sockets], the rest · Maintainers: "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Kuniyuki Iwashima, Willem de Bruijn, Linus Torvalds

Add flexible sockaddr structure to support addresses longer than the
traditional 14-byte struct sockaddr::sa_data limitation. This allows the
network APIs to pass around a pointer to an object that isn't lying to
the compiler about how big it is.

I added this to UAPI in the hopes that it could also be used for any
future "arbitrarily sized" sockaddr needs. But it may be better to
use a different UAPI with an explicit size member:

struct sockaddr_unspec {
	u16 sa_data_len;
	u16 sa_family;
	u8  sa_data[] __counted_by(sa_data_len);
};

Signed-off-by: Kees Cook <kees@kernel.org>
---
 include/linux/net.h         |  2 ++
 include/uapi/linux/socket.h | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
diff --git a/include/linux/net.h b/include/linux/net.h
index ec09620f40f7..77de581bdd56 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -26,6 +26,8 @@
 
 #include <uapi/linux/net.h>
 
+#define sockaddr_unspec __kernel_sockaddr_unspec
+
 struct poll_table_struct;
 struct pipe_inode_info;
 struct inode;
diff --git a/include/uapi/linux/socket.h b/include/uapi/linux/socket.h
index d3fcd3b5ec53..2667dd64fd0f 100644
--- a/include/uapi/linux/socket.h
+++ b/include/uapi/linux/socket.h
@@ -26,6 +26,21 @@ struct __kernel_sockaddr_storage {
 	};
 };
 
+/**
+ * struct __kernel_sockaddr_unspec - Unspecified size sockaddr for callbacks
+ * @sa_family: Address family (AF_UNIX, AF_INET, AF_INET6, etc.)
+ * @sa_data: Flexible array for address data
+ *
+ * This structure is designed for callback interfaces where the
+ * total size is known via the sockaddr_len parameter. Unlike struct
+ * sockaddr which has a fixed 14-byte sa_data limit, this structure
+ * can accommodate addresses of any size.
+ */
+struct __kernel_sockaddr_unspec {
+	__kernel_sa_family_t	sa_family;	/* address family, AF_xxx */
+	char			sa_data[];	/* flexible address data */
+};
+
 #define SOCK_SNDBUF_LOCK	1
 #define SOCK_RCVBUF_LOCK	2
 
-- 
2.34.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help