All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Bertram via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Cc: Stephen Bertram <sbertram@redhat.com>
Subject: [LTP] [PATCH v2] network/sockets: Add handshake01
Date: Wed, 18 Nov 2026 09:32:54 -0500	[thread overview]
Message-ID: <20261118143256.195124-1-sbertram@redhat.com> (raw)
In-Reply-To: <20261117232036.1228445-1-sbertram@redhat.com>

From: Stephen Bertram <sbertram@redhat.com>

Add a regression test for CVE-2026-74270 (81246a65303d "handshake:
Require admin permission for DONE command"). Verify that
HANDSHAKE_CMD_DONE returns -EPERM for a sender without CAP_NET_ADMIN,
while root reaches the handler and gets -EINVAL for the missing sockfd.

Register it as cve-2026-74270 in runtest/cve.

Reviewed and tested before submission: builds, passes check-handshake01,
fails on the unfixed kernel and passes on the fixed kernel.

Assisted-by: ClaudeCode:DeepSeek-V4.1-Flash
Signed-off-by: Stephen Bertram <sbertram@redhat.com>
---
 runtest/cve                             |   1 +
 testcases/network/sockets/.gitignore    |   1 +
 testcases/network/sockets/handshake01.c | 230 ++++++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 testcases/network/sockets/handshake01.c

diff --git a/runtest/cve b/runtest/cve
index 0fe25cd83..fb31cd76f 100644
--- a/runtest/cve
+++ b/runtest/cve
@@ -105,3 +105,4 @@ cve-2026-46300 xfrm02
 cve-2026-46300-skb-segment xfrm03
 cve-2026-46331 cve-2026-46331
 cve-2026-64564 sctphantom
+cve-2026-74270 handshake01
diff --git a/testcases/network/sockets/.gitignore b/testcases/network/sockets/.gitignore
index 3e8962bbf..c7d784d36 100644
--- a/testcases/network/sockets/.gitignore
+++ b/testcases/network/sockets/.gitignore
@@ -1,3 +1,4 @@
+/handshake01
 /xfrm01
 /xfrm02
 /xfrm03
diff --git a/testcases/network/sockets/handshake01.c b/testcases/network/sockets/handshake01.c
new file mode 100644
index 000000000..a4d04b745
--- /dev/null
+++ b/testcases/network/sockets/handshake01.c
@@ -0,0 +1,230 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Red Hat, Inc.
+ */
+
+/*\
+ * Test for CVE-2026-74270 fixed by:
+ * 81246a65303d ("handshake: Require admin permission for DONE command")
+ *
+ * The handshake generic netlink family exposes the ACCEPT and DONE downcalls
+ * for the trusted handshake agent (tlshd). ACCEPT requires GENL_ADMIN_PERM;
+ * DONE had no privilege check. An unprivileged process that knows a socket
+ * with a pending handshake could send a forged DONE and make the kernel
+ * consumer proceed as if the handshake succeeded, or tear down a legitimate
+ * handshake with a non-zero status.
+ *
+ * The test resolves the ``handshake`` family and sends ``HANDSHAKE_CMD_DONE``
+ * without attributes. The generic netlink permission check runs before
+ * attribute parsing, so no pending handshake is needed: the kernel rejects
+ * the request with ``-EPERM`` for a sender without ``CAP_NET_ADMIN``. A root
+ * control must instead reach the handler and fail with ``-EINVAL`` because
+ * ``HANDSHAKE_A_DONE_SOCKFD`` is missing.
+ *
+ * On an unfixed kernel the unprivileged request reaches the handler and
+ * returns ``-EINVAL`` as well, so the test fails.
+ */
+
+#include <linux/genetlink.h>
+#include <pwd.h>
+
+#include "tst_test.h"
+#include "tst_netlink.h"
+
+/*
+ * Values from include/uapi/linux/handshake.h. Define them locally so the
+ * test builds against kernel headers older than 6.7.
+ */
+#ifndef HANDSHAKE_FAMILY_NAME
+# define HANDSHAKE_FAMILY_NAME "handshake"
+#endif
+#ifndef HANDSHAKE_FAMILY_VERSION
+# define HANDSHAKE_FAMILY_VERSION 1
+#endif
+#ifndef HANDSHAKE_CMD_DONE
+# define HANDSHAKE_CMD_DONE 3
+#endif
+
+static int nl_family_id;
+static uid_t nobody_uid;
+static gid_t nobody_gid;
+
+static int resolve_family(const char *name);
+
+static void setup(void)
+{
+	struct passwd *pw = SAFE_GETPWNAM("nobody");
+
+	nobody_uid = pw->pw_uid;
+	nobody_gid = pw->pw_gid;
+
+	nl_family_id = resolve_family(HANDSHAKE_FAMILY_NAME);
+}
+
+static int resolve_family(const char *name)
+{
+	struct tst_netlink_context *ctx;
+	struct tst_netlink_message *msgs, *m;
+	struct nlmsghdr nlh = {
+		.nlmsg_type = GENL_ID_CTRL,
+		.nlmsg_flags = NLM_F_REQUEST,
+	};
+	struct genlmsghdr gh = {
+		.cmd = CTRL_CMD_GETFAMILY,
+		.version = 1,
+	};
+	char *p;
+	int id = -1, rem;
+	uint16_t fid;
+
+	ctx = NETLINK_CREATE_CONTEXT(NETLINK_GENERIC);
+	NETLINK_ADD_MESSAGE(ctx, &nlh, &gh, sizeof(gh));
+	NETLINK_ADD_ATTR_STRING(ctx, CTRL_ATTR_FAMILY_NAME, name);
+
+	if (NETLINK_SEND(ctx) < 0)
+		tst_brk(TBROK | TERRNO, "send(CTRL_CMD_GETFAMILY) failed");
+
+	if (NETLINK_WAIT(ctx) <= 0)
+		tst_brk(TBROK, "no response to CTRL_CMD_GETFAMILY");
+
+	msgs = NETLINK_RECV(ctx);
+	if (!msgs)
+		tst_brk(TBROK, "no response to CTRL_CMD_GETFAMILY");
+
+	for (m = msgs; m->header; m++) {
+		if (m->err) {
+			if (m->err->error == -ENOENT)
+				tst_brk(TCONF, "family '%s' is not available", name);
+			if (m->err->error)
+				tst_brk(TBROK, "CTRL_CMD_GETFAMILY failed: %s",
+					strerror(-m->err->error));
+			continue;
+		}
+
+		if (m->header->nlmsg_type != GENL_ID_CTRL)
+			continue;
+		if (m->payload_size < GENL_HDRLEN)
+			continue;
+
+		p = (char *)m->payload + GENL_HDRLEN;
+		rem = (int)m->payload_size - GENL_HDRLEN;
+
+		while (rem >= (int)sizeof(struct nlattr)) {
+			struct nlattr *a = (struct nlattr *)p;
+			int alen;
+
+			if (a->nla_len < NLA_HDRLEN)
+				break;
+
+			alen = NLA_ALIGN(a->nla_len);
+			if (alen > rem)
+				break;
+
+			if ((a->nla_type & NLA_TYPE_MASK) == CTRL_ATTR_FAMILY_ID) {
+				if (a->nla_len < NLA_HDRLEN + (int)sizeof(fid))
+					break;
+
+				memcpy(&fid, (char *)a + NLA_HDRLEN, sizeof(fid));
+				id = fid;
+				break;
+			}
+
+			p += alen;
+			rem -= alen;
+		}
+
+		if (id >= 0)
+			break;
+	}
+
+	NETLINK_FREE_MESSAGE(msgs);
+	NETLINK_DESTROY_CONTEXT(ctx);
+
+	if (id < 0)
+		tst_brk(TBROK, "CTRL_ATTR_FAMILY_ID not found for '%s'", name);
+
+	return id;
+}
+
+static int send_done(void)
+{
+	struct tst_netlink_context *ctx;
+	struct nlmsghdr nlh = {
+		.nlmsg_type = nl_family_id,
+		.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
+	};
+	struct genlmsghdr gh = {
+		.cmd = HANDSHAKE_CMD_DONE,
+		.version = HANDSHAKE_FAMILY_VERSION,
+	};
+	int ret;
+
+	ctx = NETLINK_CREATE_CONTEXT(NETLINK_GENERIC);
+	NETLINK_ADD_MESSAGE(ctx, &nlh, &gh, sizeof(gh));
+
+	ret = NETLINK_SEND_VALIDATE(ctx);
+	NETLINK_DESTROY_CONTEXT(ctx);
+
+	if (ret)
+		return 0;
+
+	/* No ACK and no error means the kernel never replied. */
+	if (!tst_netlink_errno)
+		return -ETIMEDOUT;
+
+	return -tst_netlink_errno;
+}
+
+static void unprivileged_done(void)
+{
+	int ret;
+
+	if (SAFE_FORK())
+		return;
+
+	SAFE_SETGROUPS(0, NULL);
+	SAFE_SETGID(nobody_gid);
+	SAFE_SETUID(nobody_uid);
+
+	ret = send_done();
+
+	if (ret == -EPERM)
+		tst_res(TPASS, "unprivileged HANDSHAKE_CMD_DONE rejected with EPERM");
+	else
+		tst_res(TFAIL,
+			"unprivileged HANDSHAKE_CMD_DONE returned %d (%s), expected EPERM",
+			ret, strerror(-ret));
+
+	exit(0);
+}
+
+static void run(void)
+{
+	int root_err;
+
+	root_err = send_done();
+	if (root_err == -EPERM)
+		tst_brk(TBROK, "root was denied HANDSHAKE_CMD_DONE");
+	if (root_err != -EINVAL)
+		tst_brk(TBROK,
+			"root HANDSHAKE_CMD_DONE returned %d (%s), expected EINVAL",
+			root_err, strerror(-root_err));
+
+	unprivileged_done();
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.needs_root = 1,
+	.forks_child = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_NET_HANDSHAKE",
+		NULL
+	},
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "81246a65303d9635266b1334490142caaf86a11f"},
+		{"CVE", "2026-74270"},
+		{}
+	},
+};
-- 
2.56.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2026-11-18 14:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 23:20 [LTP] [PATCH v1] network/sockets: Add handshake01 Stephen Bertram via ltp
2026-11-18  4:51 ` [LTP] " linuxtestproject.agent
2026-11-18 14:32 ` Stephen Bertram via ltp [this message]
2026-11-18 21:41   ` linuxtestproject.agent

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118143256.195124-1-sbertram@redhat.com \
    --to=ltp@lists.linux.it \
    --cc=sbertram@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.