All of lore.kernel.org
 help / color / mirror / Atom feed
From: Martin Doucha <mdoucha@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] cve-2017-16939: Improve and fix test
Date: Fri,  2 Dec 2022 12:42:05 +0100	[thread overview]
Message-ID: <20221202114205.11077-1-mdoucha@suse.cz> (raw)

The test was unable to reproduce the kernel bug because it needs to
send then netlink message twice and then close the socket. Sending it
just once is not enough.

Also remove unnecessary structures and code and use taint checks.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/cve/cve-2017-16939.c | 63 ++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/testcases/cve/cve-2017-16939.c b/testcases/cve/cve-2017-16939.c
index e41fb274a..fc3500d57 100644
--- a/testcases/cve/cve-2017-16939.c
+++ b/testcases/cve/cve-2017-16939.c
@@ -29,50 +29,63 @@
 
 #define BUFSIZE 2048
 
-static int fd;
-static struct sockaddr_nl addr;
-
-struct msg_policy {
-	struct nlmsghdr msg;
-	char buf[BUFSIZE];
-};
-static struct msg_policy *p;
+static int fd = -1;
+static struct sockaddr_nl nl_addr;
+static struct nlmsghdr xfrm_msg;
 
 static void setup(void)
 {
 	tst_setup_netns();
 
-	fd = SAFE_SOCKET(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
-	memset(&addr, 0, sizeof(struct sockaddr_nl));
-	addr.nl_family = AF_NETLINK;
-	addr.nl_pid = 0; /* packet goes into the kernel */
-	addr.nl_groups = XFRMNLGRP_NONE; /* no need for multicast group */
+	nl_addr.nl_family = AF_NETLINK;
+	nl_addr.nl_pid = 0; /* packet goes into the kernel */
+	nl_addr.nl_groups = XFRMNLGRP_NONE; /* no need for multicast group */
 
-	p = SAFE_MALLOC(sizeof(struct msg_policy));
-	memset(p, 0, sizeof(struct msg_policy));
+	xfrm_msg.nlmsg_len = NLMSG_LENGTH(0);
+	xfrm_msg.nlmsg_type = XFRM_MSG_GETPOLICY;
+	xfrm_msg.nlmsg_flags = NLM_F_MATCH | NLM_F_MULTI | NLM_F_REQUEST;
+	xfrm_msg.nlmsg_seq = 0x1;
+	xfrm_msg.nlmsg_pid = 2;
+}
 
-	p->msg.nlmsg_len = 0x10;
-	p->msg.nlmsg_type = XFRM_MSG_GETPOLICY;
-	p->msg.nlmsg_flags = NLM_F_MATCH | NLM_F_MULTI |  NLM_F_REQUEST;
-	p->msg.nlmsg_seq = 0x1;
-	p->msg.nlmsg_pid = 2;
+static void send_nlmsg(int fd, struct nlmsghdr *msg, struct sockaddr_nl *addr)
+{
+	SAFE_SENDTO(1, fd, (void *)msg, msg->nlmsg_len, 0,
+		    (struct sockaddr *)addr, sizeof(struct sockaddr_nl));
 }
 
 static void run(void)
 {
-	int var = 0x100;
+	fd = SAFE_SOCKET(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
+	SAFE_SETSOCKOPT_INT(fd, SOL_SOCKET, SO_RCVBUF, 0x100);
 
-	SAFE_SETSOCKOPT(fd, SOL_SOCKET, SO_RCVBUF, &var, sizeof(int));
-	SAFE_SENDTO(1, fd, (void *) &p->msg, p->msg.nlmsg_len, 0,
-		    (struct sockaddr *) &addr,
-		    sizeof(struct sockaddr_nl));
+	/* message must be sent twice to trigger the bug */
+	send_nlmsg(fd, &xfrm_msg, &nl_addr);
+	send_nlmsg(fd, &xfrm_msg, &nl_addr);
+	SAFE_CLOSE(fd);
+
+	/* wait for socket close callback to crash */
+	usleep(100000);
+
+	if (tst_taint_check()) {
+		tst_res(TFAIL, "Kernel is vulnerable");
+		return;
+	}
 
 	tst_res(TPASS, "Kernel seems to have survived");
 }
 
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
 static struct tst_test test = {
 	.setup = setup,
 	.test_all = run,
+	.cleanup = cleanup,
+	.taint_check = TST_TAINT_W | TST_TAINT_D,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS=y",
 		"CONFIG_NET_NS=y",
-- 
2.38.1


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

             reply	other threads:[~2022-12-02 11:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-02 11:42 Martin Doucha [this message]
2022-12-02 12:36 ` [LTP] [PATCH] cve-2017-16939: Improve and fix test Petr Vorel
2022-12-02 12:37 ` Petr Vorel
2022-12-02 13:09   ` Petr Vorel
2022-12-02 13:22     ` Martin Doucha

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=20221202114205.11077-1-mdoucha@suse.cz \
    --to=mdoucha@suse.cz \
    --cc=ltp@lists.linux.it \
    /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.