[PATCH v2 08/27] selftests/landlock: Handle a missing control message in recv_fd()

From: Günther Noack

Date: Wed Nov 04 2026 - 16:55:45 EST


CMSG_FIRSTHDR() returns NULL when recvmsg(2) did not receive any
ancillary data, e.g. when the peer closed the socket without sending an
FD. recv_fd() dereferences the result unconditionally and crashes
instead of returning an error.

Return -EIO in that case.

Signed-off-by: Günther Noack <gnoack3000@xxxxxxxxx>
---
tools/testing/selftests/landlock/common.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/landlock/common.h b/tools/testing/selftests/landlock/common.h
index e29406900c04..6c2aa913ff11 100644
--- a/tools/testing/selftests/landlock/common.h
+++ b/tools/testing/selftests/landlock/common.h
@@ -171,7 +171,7 @@ static int __maybe_unused recv_fd(int usock)
return -errno;

cmsg = CMSG_FIRSTHDR(&msg);
- if (cmsg->cmsg_len != CMSG_LEN(sizeof(fd_rx)))
+ if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(fd_rx)))
return -EIO;

memcpy(&fd_rx, CMSG_DATA(cmsg), sizeof(fd_rx));

--
2.56.0