Commit 08ca0c0d54c for php
commit 08ca0c0d54ca64fc51b1376f873b318b3177ddd8
Author: Matyapiro31 <matyapiro31@gmail.com>
Date: Tue Dec 1 15:52:54 2026 +0000
main: Unix socket peer name read from uninitialized memory
php_network_populate_name_from_sockaddr() read sun_path of an AF_UNIX
address without regard to the length (sl) returned by the kernel. For an
unnamed peer (a client that never bind()s) sl == sizeof(sa_family_t), so
sun_path[0] and the subsequent strlen() read uninitialized stack memory.
This is a code-quality fix, not a security issue: all accesses are reads,
and the stale bytes are PHP's own stack contents that the peer cannot
control.
Bound all sun_path accesses by sl and return an empty name when no path
was provided. Replace strlen() with a memchr() bounded by the valid
length.
Fix GH-25989
Close GH-25990
diff --git a/NEWS b/NEWS
index ae8ed7e7fcb..965ad4d166f 100644
--- a/NEWS
+++ b/NEWS
@@ -321,6 +321,8 @@ PHP NEWS
its user filter). (Jakub Zelenka)
. Fixed bug GH-25954 (dns_get_mx() parses MX exchange past the record
RDATA). (lazerg)
+ . Fixed bug GH-25989 (Unix socket peer name of an unnamed client is read from
+ uninitialized memory). (Matyapiro31)
- Streams:
. Fixed bug GH-22063 (Stream filter chain UAF on self-removal during
diff --git a/ext/standard/tests/network/unix_peername_unnamed.phpt b/ext/standard/tests/network/unix_peername_unnamed.phpt
new file mode 100644
index 00000000000..8a973874d5b
--- /dev/null
+++ b/ext/standard/tests/network/unix_peername_unnamed.phpt
@@ -0,0 +1,37 @@
+--TEST--
+Unix domain socket: peer name of an unnamed (unbound) client is empty
+--SKIPIF--
+<?php
+if (array_search("unix", stream_get_transports()) === false)
+ die('SKIP No support for UNIX domain sockets.');
+?>
+--FILE--
+<?php
+$path = sys_get_temp_dir() . "/php_unix_peername_" . uniqid() . ".sock";
+
+$server = stream_socket_server("unix://$path", $errno, $errstr);
+if (!$server) {
+ die("Unable to create AF_UNIX socket: $errstr");
+}
+
+/* The client never bind()s, so the kernel reports an address of length
+ * sizeof(sa_family_t) with no sun_path. */
+$client = stream_socket_client("unix://$path", $errno, $errstr);
+if (!$client) {
+ die("Unable to connect: $errstr");
+}
+
+$peer = null;
+$conn = stream_socket_accept($server, 5, $peer);
+var_dump((bool) $conn);
+/* Some platforms (e.g. FreeBSD) report an unnamed peer as a zeroed sun_path. */
+var_dump(trim((string) $peer, "\0") === "");
+
+fclose($conn);
+fclose($client);
+fclose($server);
+@unlink($path);
+?>
+--EXPECT--
+bool(true)
+bool(true)
diff --git a/main/network.c b/main/network.c
index e2dd6d89c7e..07f9ed47b6e 100644
--- a/main/network.c
+++ b/main/network.c
@@ -692,12 +692,21 @@ PHPAPI void php_network_populate_name_from_sockaddr(
{
struct sockaddr_un *ua = (struct sockaddr_un*)sa;
- if (ua->sun_path[0] == '\0') {
+ /* Only the first sl bytes of sa are initialized by the kernel.
+ * An unnamed peer (e.g. a client that never bound its socket)
+ * reports no sun_path at all, so never read beyond sl. */
+ size_t path_offset = offsetof(struct sockaddr_un, sun_path);
+ size_t max_len = sl > path_offset ? sl - path_offset : 0;
+
+ if (max_len == 0) {
+ /* unnamed */
+ *textaddr = ZSTR_EMPTY_ALLOC();
+ } else if (ua->sun_path[0] == '\0') {
/* abstract name */
- int len = sl - sizeof(sa_family_t);
- *textaddr = zend_string_init((char*)ua->sun_path, len, 0);
+ *textaddr = zend_string_init((char*)ua->sun_path, max_len, 0);
} else {
- int len = strlen(ua->sun_path);
+ const char *nul = memchr(ua->sun_path, '\0', max_len);
+ size_t len = nul ? (size_t)(nul - ua->sun_path) : max_len;
*textaddr = zend_string_init((char*)ua->sun_path, len, 0);
}
}