mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	selftests: net: add a test for closing a netlink socket ith dump in progress
Close a socket with dump in progress. We need a dump which generates enough info not to fit into a single skb. Policy dump fits the bill. Use the trick discovered by syzbot for keeping a ref on the socket longer than just close, with mqueue. TAP version 13 1..3 # Starting 3 tests from 1 test cases. # RUN global.test_sanity ... # OK global.test_sanity ok 1 global.test_sanity # RUN global.close_in_progress ... # OK global.close_in_progress ok 2 global.close_in_progress # RUN global.close_with_ref ... # OK global.close_with_ref ok 3 global.close_with_ref # PASSED: 3 / 3 tests passed. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0 Note that this test is not expected to fail but rather crash the kernel if we get the cleanup wrong. Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://patch.msgid.link/20241106015235.2458807-2-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
		
							parent
							
								
									1904fb9ebf
								
							
						
					
					
						commit
						55d42a0c3f
					
				
					 2 changed files with 111 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -78,6 +78,7 @@ TEST_PROGS += test_vxlan_vnifiltering.sh
 | 
			
		|||
TEST_GEN_FILES += io_uring_zerocopy_tx
 | 
			
		||||
TEST_PROGS += io_uring_zerocopy_tx.sh
 | 
			
		||||
TEST_GEN_FILES += bind_bhash
 | 
			
		||||
TEST_GEN_PROGS += netlink-dumps
 | 
			
		||||
TEST_GEN_PROGS += sk_bind_sendto_listen
 | 
			
		||||
TEST_GEN_PROGS += sk_connect_zero_addr
 | 
			
		||||
TEST_GEN_PROGS += sk_so_peek_off
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										110
									
								
								tools/testing/selftests/net/netlink-dumps.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								tools/testing/selftests/net/netlink-dumps.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,110 @@
 | 
			
		|||
// SPDX-License-Identifier: GPL-2.0
 | 
			
		||||
 | 
			
		||||
#define _GNU_SOURCE
 | 
			
		||||
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <sys/socket.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <sys/syscall.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include <linux/genetlink.h>
 | 
			
		||||
#include <linux/netlink.h>
 | 
			
		||||
#include <linux/mqueue.h>
 | 
			
		||||
 | 
			
		||||
#include "../kselftest_harness.h"
 | 
			
		||||
 | 
			
		||||
static const struct {
 | 
			
		||||
	struct nlmsghdr nlhdr;
 | 
			
		||||
	struct genlmsghdr genlhdr;
 | 
			
		||||
	struct nlattr ahdr;
 | 
			
		||||
	__u16 val;
 | 
			
		||||
	__u16 pad;
 | 
			
		||||
} dump_policies = {
 | 
			
		||||
	.nlhdr = {
 | 
			
		||||
		.nlmsg_len	= sizeof(dump_policies),
 | 
			
		||||
		.nlmsg_type	= GENL_ID_CTRL,
 | 
			
		||||
		.nlmsg_flags	= NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
 | 
			
		||||
		.nlmsg_seq	= 1,
 | 
			
		||||
	},
 | 
			
		||||
	.genlhdr = {
 | 
			
		||||
		.cmd		= CTRL_CMD_GETPOLICY,
 | 
			
		||||
		.version	= 2,
 | 
			
		||||
	},
 | 
			
		||||
	.ahdr = {
 | 
			
		||||
		.nla_len	= 6,
 | 
			
		||||
		.nla_type	= CTRL_ATTR_FAMILY_ID,
 | 
			
		||||
	},
 | 
			
		||||
	.val = GENL_ID_CTRL,
 | 
			
		||||
	.pad = 0,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Sanity check for the test itself, make sure the dump doesn't fit in one msg
 | 
			
		||||
TEST(test_sanity)
 | 
			
		||||
{
 | 
			
		||||
	int netlink_sock;
 | 
			
		||||
	char buf[8192];
 | 
			
		||||
	ssize_t n;
 | 
			
		||||
 | 
			
		||||
	netlink_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
 | 
			
		||||
	ASSERT_GE(netlink_sock, 0);
 | 
			
		||||
 | 
			
		||||
	n = send(netlink_sock, &dump_policies, sizeof(dump_policies), 0);
 | 
			
		||||
	ASSERT_EQ(n, sizeof(dump_policies));
 | 
			
		||||
 | 
			
		||||
	n = recv(netlink_sock, buf, sizeof(buf), MSG_DONTWAIT);
 | 
			
		||||
	ASSERT_GE(n, sizeof(struct nlmsghdr));
 | 
			
		||||
 | 
			
		||||
	n = recv(netlink_sock, buf, sizeof(buf), MSG_DONTWAIT);
 | 
			
		||||
	ASSERT_GE(n, sizeof(struct nlmsghdr));
 | 
			
		||||
 | 
			
		||||
	close(netlink_sock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(close_in_progress)
 | 
			
		||||
{
 | 
			
		||||
	int netlink_sock;
 | 
			
		||||
	ssize_t n;
 | 
			
		||||
 | 
			
		||||
	netlink_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
 | 
			
		||||
	ASSERT_GE(netlink_sock, 0);
 | 
			
		||||
 | 
			
		||||
	n = send(netlink_sock, &dump_policies, sizeof(dump_policies), 0);
 | 
			
		||||
	ASSERT_EQ(n, sizeof(dump_policies));
 | 
			
		||||
 | 
			
		||||
	close(netlink_sock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(close_with_ref)
 | 
			
		||||
{
 | 
			
		||||
	char cookie[NOTIFY_COOKIE_LEN] = {};
 | 
			
		||||
	int netlink_sock, mq_fd;
 | 
			
		||||
	struct sigevent sigev;
 | 
			
		||||
	ssize_t n;
 | 
			
		||||
 | 
			
		||||
	netlink_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
 | 
			
		||||
	ASSERT_GE(netlink_sock, 0);
 | 
			
		||||
 | 
			
		||||
	n = send(netlink_sock, &dump_policies, sizeof(dump_policies), 0);
 | 
			
		||||
	ASSERT_EQ(n, sizeof(dump_policies));
 | 
			
		||||
 | 
			
		||||
	mq_fd = syscall(__NR_mq_open, "sed", O_CREAT | O_WRONLY, 0600, 0);
 | 
			
		||||
	ASSERT_GE(mq_fd, 0);
 | 
			
		||||
 | 
			
		||||
	memset(&sigev, 0, sizeof(sigev));
 | 
			
		||||
	sigev.sigev_notify		= SIGEV_THREAD;
 | 
			
		||||
	sigev.sigev_value.sival_ptr	= cookie;
 | 
			
		||||
	sigev.sigev_signo		= netlink_sock;
 | 
			
		||||
 | 
			
		||||
	syscall(__NR_mq_notify, mq_fd, &sigev);
 | 
			
		||||
 | 
			
		||||
	close(netlink_sock);
 | 
			
		||||
 | 
			
		||||
	// give mqueue time to fire
 | 
			
		||||
	usleep(100 * 1000);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST_HARNESS_MAIN
 | 
			
		||||
		Loading…
	
		Reference in a new issue