* [PATCH net-next v3] selftests: net: add tests for the QRTR socket family
@ 2026-12-02 12:03 Murali Krishna
2026-12-03 2:01 ` Hangbin Liu
2026-12-03 12:48 ` netdev-bot+sashiko
0 siblings, 2 replies; 3+ messages in thread
From: Murali Krishna @ 2026-12-02 12:03 UTC (permalink / raw)
To: mani, shuah
Cc: hangbin.liu, davem, edumazet, kuba, pabeni, horms, netdev,
linux-kselftest, linux-arm-msm, linux-kernel, Murali Krishna
net/qrtr has no test coverage in tools/testing/selftests, although the
behaviour it exposes to userspace is well defined and testable without
Qualcomm hardware: the local node always exists, and CONFIG_QRTR_TUN lets a
test act as a QRTR endpoint.
Add tools/testing/selftests/net/qrtr with two programs.
qrtr_sock covers the AF_QIPCRTR socket contract: the ephemeral port range
and the CAP_NET_ADMIN boundary documented above qrtr_port_assign(), the
bind() rule that an address must name the socket's own node, rebinding,
EADDRINUSE on a taken port, auto-bind on send, and local datagram delivery.
qrtr_endpoint covers frame validation in qrtr_endpoint_post() by writing
crafted frames to /dev/qrtr-tun: length alignment, unknown protocol
versions, truncated headers, bad declared sizes, control types whose
payload is too small or which are addressed to an ordinary port, and the
legacy 16-bit control port. Each test malforms a frame in exactly one way,
so a failure names the check that regressed.
qrtr.sh loads the qrtr and qrtr-tun modules and runs both programs. The
tests still skip on their own if the socket family or /dev/qrtr-tun is
genuinely unavailable, so the suite is harmless on non-Qualcomm configs.
The tests were written with the help of an LLM coding assistant, from a
prompt asking for selftests covering the AF_QIPCRTR socket contract and
qrtr_endpoint_post() frame validation. Both C files and this changelog were
drafted that way, then reviewed, corrected and run by hand. Writing them
corrected two assumptions about the interface: that bind() treats
sq_node == 0 as "any node", and that rebinding an already bound socket
fails.
Signed-off-by: Murali Krishna <muralikrishna2444b@gmail.com>
---
v3:
- shorten the commit message (Hangbin Liu)
- add qrtr.sh, which loads the qrtr and qrtr-tun modules and then runs both
programs, so the suite does not report skips as passes when the modules
are simply not loaded (Hangbin Liu); the two binaries move to
TEST_GEN_PROGS_EXTENDED so the script is the test entry point
- no change to the tests themselves
Link: https://lore.kernel.org/r/20261201104515.45339-1-muralikrishna2444b@gmail.com [v2]
Link: https://lore.kernel.org/r/20261124150753.115914-1-muralikrishna2444b@gmail.com [v1]
Before qrtr.sh, on a host where qrtr-tun exists but is not loaded:
# PASSED: 13 / 13 tests passed.
# Totals: pass:0 fail:0 xfail:0 xpass:0 skip:13 error:0
With qrtr.sh, same host, run as root (x86_64, 7.0.0-44-generic):
# PASSED: 12 / 12 tests passed.
# Totals: pass:12 fail:0 xfail:0 xpass:0 skip:0 error:0
# PASSED: 13 / 13 tests passed.
# Totals: pass:13 fail:0 xfail:0 xpass:0 skip:0 error:0
Note that /dev/qrtr-tun is mode 0600 root, so qrtr_endpoint needs privilege
regardless of the config; loading the module moves the skip from
"no /dev/qrtr-tun" to "needs root".
MAINTAINERS | 1 +
tools/testing/selftests/Makefile | 1 +
tools/testing/selftests/net/qrtr/.gitignore | 3 +
tools/testing/selftests/net/qrtr/Makefile | 13 +
tools/testing/selftests/net/qrtr/config | 2 +
tools/testing/selftests/net/qrtr/qrtr.sh | 26 ++
.../selftests/net/qrtr/qrtr_endpoint.c | 350 ++++++++++++++++++
tools/testing/selftests/net/qrtr/qrtr_sock.c | 298 +++++++++++++++
8 files changed, 694 insertions(+)
create mode 100644 tools/testing/selftests/net/qrtr/.gitignore
create mode 100644 tools/testing/selftests/net/qrtr/Makefile
create mode 100644 tools/testing/selftests/net/qrtr/config
create mode 100755 tools/testing/selftests/net/qrtr/qrtr.sh
create mode 100644 tools/testing/selftests/net/qrtr/qrtr_endpoint.c
create mode 100644 tools/testing/selftests/net/qrtr/qrtr_sock.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 97308edec53d..cce309b30308 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23174,6 +23174,7 @@ S: Maintained
F: include/trace/events/qrtr.h
F: include/uapi/linux/qrtr.h
F: net/qrtr/
+F: tools/testing/selftests/net/qrtr/
QUALCOMM IPCC MAILBOX DRIVER
M: Manivannan Sadhasivam <mani@kernel.org>
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index c55d7792b27c..3d0916b60f45 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -97,6 +97,7 @@ TARGETS += net/openvswitch
TARGETS += net/ovpn
TARGETS += net/packetdrill
TARGETS += net/ppp
+TARGETS += net/qrtr
TARGETS += net/rds
TARGETS += net/tcp_ao
TARGETS += nfsd
diff --git a/tools/testing/selftests/net/qrtr/.gitignore b/tools/testing/selftests/net/qrtr/.gitignore
new file mode 100644
index 000000000000..cc2ea8a2b665
--- /dev/null
+++ b/tools/testing/selftests/net/qrtr/.gitignore
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+qrtr_endpoint
+qrtr_sock
diff --git a/tools/testing/selftests/net/qrtr/Makefile b/tools/testing/selftests/net/qrtr/Makefile
new file mode 100644
index 000000000000..f9814a5c3b4d
--- /dev/null
+++ b/tools/testing/selftests/net/qrtr/Makefile
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+top_srcdir := ../../../../..
+
+CFLAGS += $(KHDR_INCLUDES) -Wall -Wextra -Wno-unused-parameter
+
+TEST_PROGS := qrtr.sh
+
+TEST_GEN_PROGS_EXTENDED := \
+ qrtr_endpoint \
+ qrtr_sock \
+# end of TEST_GEN_PROGS_EXTENDED
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/net/qrtr/config b/tools/testing/selftests/net/qrtr/config
new file mode 100644
index 000000000000..0bdb8a4c5211
--- /dev/null
+++ b/tools/testing/selftests/net/qrtr/config
@@ -0,0 +1,2 @@
+CONFIG_QRTR=m
+CONFIG_QRTR_TUN=m
diff --git a/tools/testing/selftests/net/qrtr/qrtr.sh b/tools/testing/selftests/net/qrtr/qrtr.sh
new file mode 100755
index 000000000000..031cadd842a1
--- /dev/null
+++ b/tools/testing/selftests/net/qrtr/qrtr.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Run the QRTR selftests.
+#
+# CONFIG_QRTR and CONFIG_QRTR_TUN are modules in this directory's config, so
+# load them before running: a test that skips because a module was not loaded
+# reports the same "not run" as a test with nothing to check, which hides a
+# regression instead of showing it. Both modprobes are quiet and their failure
+# is ignored, so a built-in or absent QRTR still reaches the tests, which skip
+# on their own if the socket family or /dev/qrtr-tun really is unavailable.
+#
+# Only failure is propagated. kselftest_harness exits KSFT_PASS even when
+# every test skipped, so a skip cannot be detected from the exit status here.
+
+modprobe -q qrtr 2>/dev/null
+modprobe -q qrtr-tun 2>/dev/null
+
+ret=0
+
+for t in qrtr_sock qrtr_endpoint; do
+ echo "# $t"
+ ./"$t" || ret=$?
+done
+
+exit $ret
diff --git a/tools/testing/selftests/net/qrtr/qrtr_endpoint.c b/tools/testing/selftests/net/qrtr/qrtr_endpoint.c
new file mode 100644
index 000000000000..c38bc69cb4ce
--- /dev/null
+++ b/tools/testing/selftests/net/qrtr/qrtr_endpoint.c
@@ -0,0 +1,350 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Tests for QRTR frame validation in qrtr_endpoint_post().
+ *
+ * /dev/qrtr-tun (CONFIG_QRTR_TUN) lets userspace act as a QRTR endpoint:
+ * a write() is handed straight to qrtr_endpoint_post(), so every length and
+ * header check in that function is reachable from here with no Qualcomm
+ * hardware. A rejected frame makes write() fail with EINVAL; an accepted one
+ * returns the full length.
+ *
+ * The frames below are deliberately malformed in one way each, so a failure
+ * names the specific check that regressed.
+ *
+ * On-wire header fields are little endian, and qrtr_endpoint_post() reads the
+ * version from the first byte of the frame and decodes the rest with
+ * le32_to_cpu()/le16_to_cpu(), so every multi-byte field is built with
+ * htole32()/htole16() rather than in host order.
+ */
+
+#include <endian.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/qrtr.h>
+#include <sys/socket.h>
+
+#include "../../kselftest_harness.h"
+
+/* From net/qrtr/af_qrtr.c */
+#define QRTR_PROTO_VER_1 1
+#define QRTR_PROTO_VER_2 3
+#define QRTR_PORT_CTRL_LEGACY 0xffff
+
+/* A node id for the test endpoint, distinct from the local node (1). */
+#define TEST_SRC_NODE 5
+
+/* An ordinary (non-control) port, inside the unprivileged ephemeral range. */
+#define TEST_NORMAL_PORT 0x4321
+
+struct qrtr_hdr_v1 {
+ __le32 version;
+ __le32 type;
+ __le32 src_node_id;
+ __le32 src_port_id;
+ __le32 confirm_rx;
+ __le32 size;
+ __le32 dst_node_id;
+ __le32 dst_port_id;
+} __attribute__((packed));
+
+struct qrtr_hdr_v2 {
+ uint8_t version;
+ uint8_t type;
+ uint8_t flags;
+ uint8_t optlen;
+ __le32 size;
+ __le16 src_node_id;
+ __le16 src_port_id;
+ __le16 dst_node_id;
+ __le16 dst_port_id;
+};
+
+/* One header plus one control packet: the smallest realistic valid frame. */
+struct v1_frame {
+ struct qrtr_hdr_v1 hdr;
+ struct qrtr_ctrl_pkt pkt;
+} __attribute__((packed));
+
+static void v1_init(struct v1_frame *f, uint32_t type)
+{
+ memset(f, 0, sizeof(*f));
+ f->hdr.version = htole32(QRTR_PROTO_VER_1);
+ f->hdr.type = htole32(type);
+ f->hdr.src_node_id = htole32(TEST_SRC_NODE);
+ f->hdr.src_port_id = htole32(QRTR_PORT_CTRL);
+ f->hdr.size = htole32(sizeof(f->pkt));
+ f->hdr.dst_node_id = htole32(0);
+ f->hdr.dst_port_id = htole32(QRTR_PORT_CTRL);
+ f->pkt.cmd = htole32(type);
+}
+
+/*
+ * Bind a fresh socket to a given port on the local node. Returns the fd, or
+ * -1 with errno set. bind() insists the address name the socket's own node,
+ * so that is read back from the socket rather than assumed.
+ */
+static int qrtr_bind_local(int *fdp, uint32_t port)
+{
+ struct sockaddr_qrtr sq = {};
+ socklen_t len = sizeof(sq);
+ int fd;
+
+ fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+ if (fd < 0)
+ return -1;
+
+ if (getsockname(fd, (struct sockaddr *)&sq, &len)) {
+ close(fd);
+ return -1;
+ }
+
+ sq.sq_family = AF_QIPCRTR;
+ sq.sq_port = port;
+
+ if (bind(fd, (struct sockaddr *)&sq, sizeof(sq))) {
+ close(fd);
+ return -1;
+ }
+
+ *fdp = fd;
+ return 0;
+}
+
+FIXTURE(tun)
+{
+ int fd;
+};
+
+FIXTURE_SETUP(tun)
+{
+ self->fd = open("/dev/qrtr-tun", O_RDWR | O_NONBLOCK);
+ if (self->fd < 0) {
+ if (errno == ENOENT)
+ SKIP(return,
+ "no /dev/qrtr-tun: modprobe qrtr-tun, or CONFIG_QRTR_TUN=n");
+ if (errno == EACCES || errno == EPERM)
+ SKIP(return, "/dev/qrtr-tun needs root");
+ ASSERT_LE(0, self->fd);
+ }
+}
+
+FIXTURE_TEARDOWN(tun)
+{
+ if (self->fd >= 0)
+ close(self->fd);
+}
+
+/* Baseline: a well formed HELLO to the control port is accepted. */
+TEST_F(tun, valid_hello)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+
+ EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
+}
+
+/* A length that is not a multiple of four is rejected before anything else. */
+TEST_F(tun, unaligned_length)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f) - 1));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/* Only protocol versions 1 and 3 exist. */
+TEST_F(tun, bad_version)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ f.hdr.version = htole32(2);
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ f.hdr.version = htole32(0xff);
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/* A frame shorter than the version 1 header cannot carry one. */
+TEST_F(tun, truncated_v1_header)
+{
+ struct v1_frame f;
+ size_t len;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+
+ for (len = 4; len < sizeof(struct qrtr_hdr_v1); len += 4) {
+ EXPECT_EQ(-1, write(self->fd, &f, len));
+ EXPECT_EQ(EINVAL, errno);
+ }
+}
+
+/* A frame shorter than the version 2 header cannot carry one. */
+TEST_F(tun, truncated_v2_header)
+{
+ struct qrtr_hdr_v2 hdr = {};
+ size_t len;
+
+ hdr.version = QRTR_PROTO_VER_2;
+ hdr.type = QRTR_TYPE_HELLO;
+
+ for (len = 4; len < sizeof(hdr); len += 4) {
+ EXPECT_EQ(-1, write(self->fd, &hdr, len));
+ EXPECT_EQ(EINVAL, errno);
+ }
+}
+
+/* A declared payload size of zero is rejected. */
+TEST_F(tun, zero_size)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ f.hdr.size = htole32(0);
+
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/* A declared size larger than the frame is rejected: it would over-read. */
+TEST_F(tun, size_exceeds_frame)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ f.hdr.size = htole32(sizeof(f) + 64);
+
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/* The frame length must be exactly the header plus the padded payload. */
+TEST_F(tun, size_does_not_match_length)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ /* Fits in the frame, but leaves four unaccounted trailing bytes. */
+ f.hdr.size = htole32(sizeof(f.pkt) - 4);
+
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/*
+ * NEW_SERVER and RESUME_TX are parsed as a struct qrtr_ctrl_pkt, so a
+ * payload too small to hold one must be rejected rather than read past.
+ */
+TEST_F(tun, control_payload_too_small)
+{
+ struct {
+ struct qrtr_hdr_v1 hdr;
+ uint32_t body;
+ } __attribute__((packed)) f;
+
+ memset(&f, 0, sizeof(f));
+ f.hdr.version = htole32(QRTR_PROTO_VER_1);
+ f.hdr.src_node_id = htole32(TEST_SRC_NODE);
+ f.hdr.src_port_id = htole32(QRTR_PORT_CTRL);
+ f.hdr.size = htole32(sizeof(f.body));
+ f.hdr.dst_port_id = htole32(QRTR_PORT_CTRL);
+
+ f.hdr.type = htole32(QRTR_TYPE_NEW_SERVER);
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+
+ f.hdr.type = htole32(QRTR_TYPE_RESUME_TX);
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/*
+ * DATA addressed to an ordinary port is accepted and delivered. This is the
+ * positive half of control_type_to_normal_port below: it shows that a frame
+ * to TEST_NORMAL_PORT does reach qrtr_port_lookup() and succeeds, so the
+ * EINVAL that test expects can only come from the control-type check.
+ */
+TEST_F(tun, data_to_normal_port)
+{
+ struct v1_frame f;
+ int peer;
+
+ if (qrtr_bind_local(&peer, TEST_NORMAL_PORT))
+ SKIP(return, "cannot bind AF_QIPCRTR port (CONFIG_QRTR=n?)");
+
+ v1_init(&f, QRTR_TYPE_DATA);
+ f.hdr.dst_port_id = htole32(TEST_NORMAL_PORT);
+
+ EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
+
+ close(peer);
+}
+
+/*
+ * Only DATA and RESUME_TX may be addressed to an ordinary port. A control
+ * message aimed at a normal port is rejected.
+ *
+ * A socket must be bound to the destination port for this to mean anything:
+ * with nothing bound, qrtr_port_lookup() fails and qrtr_endpoint_post()
+ * returns -EINVAL from there instead, so the test would still pass if the
+ * control-type check were removed.
+ */
+TEST_F(tun, control_type_to_normal_port)
+{
+ struct v1_frame f;
+ int peer;
+
+ if (qrtr_bind_local(&peer, TEST_NORMAL_PORT))
+ SKIP(return, "cannot bind AF_QIPCRTR port (CONFIG_QRTR=n?)");
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ f.hdr.dst_port_id = htole32(TEST_NORMAL_PORT);
+
+ EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
+ EXPECT_EQ(EINVAL, errno);
+
+ close(peer);
+}
+
+/* The legacy 16-bit control port number is still understood. */
+TEST_F(tun, legacy_control_port)
+{
+ struct v1_frame f;
+
+ v1_init(&f, QRTR_TYPE_HELLO);
+ f.hdr.dst_port_id = htole32(QRTR_PORT_CTRL_LEGACY);
+
+ EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
+}
+
+/* A version 2 header is accepted on its own terms. */
+TEST_F(tun, valid_v2_hello)
+{
+ struct {
+ struct qrtr_hdr_v2 hdr;
+ struct qrtr_ctrl_pkt pkt;
+ } f;
+
+ memset(&f, 0, sizeof(f));
+ f.hdr.version = QRTR_PROTO_VER_2;
+ f.hdr.type = QRTR_TYPE_HELLO;
+ f.hdr.size = htole32(sizeof(f.pkt));
+ f.hdr.src_node_id = htole16(TEST_SRC_NODE);
+ f.hdr.src_port_id = htole16((uint16_t)QRTR_PORT_CTRL);
+ f.hdr.dst_port_id = htole16((uint16_t)QRTR_PORT_CTRL);
+ f.pkt.cmd = htole32(QRTR_TYPE_HELLO);
+
+ EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/net/qrtr/qrtr_sock.c b/tools/testing/selftests/net/qrtr/qrtr_sock.c
new file mode 100644
index 000000000000..592e4e429b97
--- /dev/null
+++ b/tools/testing/selftests/net/qrtr/qrtr_sock.c
@@ -0,0 +1,298 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Tests for the AF_QIPCRTR socket address family.
+ *
+ * These exercise the port assignment contract documented above
+ * qrtr_port_assign() in net/qrtr/af_qrtr.c, the bind() address rules in
+ * qrtr_bind(), and local datagram delivery between two sockets on the local
+ * node. No QRTR endpoint and no Qualcomm hardware is required: the local
+ * node always exists.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/qrtr.h>
+#include <sys/socket.h>
+
+#include "../../kselftest_harness.h"
+
+/* From net/qrtr/af_qrtr.c. Ports below the ephemeral range are privileged. */
+#define QRTR_MIN_EPH_SOCKET 0x4000u
+#define QRTR_MAX_EPH_SOCKET 0x7fffu
+
+FIXTURE(qrtr)
+{
+ int fd;
+ __u32 node; /* this socket's own node id */
+};
+
+FIXTURE_SETUP(qrtr)
+{
+ struct sockaddr_qrtr sq = {};
+ socklen_t len = sizeof(sq);
+
+ self->fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+ if (self->fd < 0 &&
+ (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT))
+ SKIP(return, "AF_QIPCRTR not supported (CONFIG_QRTR=n?)");
+ ASSERT_LE(0, self->fd);
+
+ /*
+ * A fresh socket already knows which node it lives on; bind() insists
+ * on being given that same value, so every test needs it.
+ */
+ ASSERT_EQ(0, getsockname(self->fd, (struct sockaddr *)&sq, &len));
+ self->node = sq.sq_node;
+}
+
+FIXTURE_TEARDOWN(qrtr)
+{
+ if (self->fd >= 0)
+ close(self->fd);
+}
+
+static int qrtr_bind_port(int fd, __u32 node, __u32 port)
+{
+ struct sockaddr_qrtr sq = {
+ .sq_family = AF_QIPCRTR,
+ .sq_node = node,
+ .sq_port = port,
+ };
+
+ return bind(fd, (struct sockaddr *)&sq, sizeof(sq));
+}
+
+static __u32 qrtr_port(struct __test_metadata *_metadata, int fd)
+{
+ struct sockaddr_qrtr sq = {};
+ socklen_t len = sizeof(sq);
+
+ ASSERT_EQ(0, getsockname(fd, (struct sockaddr *)&sq, &len));
+ return sq.sq_port;
+}
+
+/* A socket is unbound until it is bound: getsockname() reports port 0. */
+TEST_F(qrtr, getsockname_unbound)
+{
+ struct sockaddr_qrtr sq = {};
+ socklen_t len = sizeof(sq);
+
+ ASSERT_EQ(0, getsockname(self->fd, (struct sockaddr *)&sq, &len));
+ EXPECT_EQ(sizeof(sq), (size_t)len);
+ EXPECT_EQ(AF_QIPCRTR, sq.sq_family);
+ EXPECT_EQ(0u, sq.sq_port);
+}
+
+/*
+ * bind() requires the address to name the socket's own node. Unlike most
+ * families, zero does not mean "any node".
+ */
+TEST_F(qrtr, bind_foreign_node_rejected)
+{
+ EXPECT_EQ(-1, qrtr_bind_port(self->fd, self->node + 1, 0));
+ EXPECT_EQ(EINVAL, errno);
+
+ if (self->node != 0) {
+ EXPECT_EQ(-1, qrtr_bind_port(self->fd, 0, 0));
+ EXPECT_EQ(EINVAL, errno);
+ }
+}
+
+/* A too-short or wrong-family address is rejected. */
+TEST_F(qrtr, bind_bad_addr)
+{
+ struct sockaddr_qrtr sq = {
+ .sq_family = AF_QIPCRTR,
+ .sq_node = self->node,
+ .sq_port = 0,
+ };
+
+ EXPECT_EQ(-1, bind(self->fd, (struct sockaddr *)&sq, sizeof(sq) - 1));
+ EXPECT_EQ(EINVAL, errno);
+
+ sq.sq_family = AF_INET;
+ EXPECT_EQ(-1, bind(self->fd, (struct sockaddr *)&sq, sizeof(sq)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+/* Binding port 0 assigns an ephemeral port inside the documented range. */
+TEST_F(qrtr, bind_ephemeral_in_range)
+{
+ __u32 port;
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, 0));
+
+ port = qrtr_port(_metadata, self->fd);
+ EXPECT_LE(QRTR_MIN_EPH_SOCKET, port);
+ EXPECT_GE(QRTR_MAX_EPH_SOCKET, port);
+}
+
+/* A port at or above the ephemeral range may be claimed by any user. */
+TEST_F(qrtr, bind_unprivileged_port)
+{
+ const __u32 want = QRTR_MIN_EPH_SOCKET + 0x1000;
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, want));
+ EXPECT_EQ(want, qrtr_port(_metadata, self->fd));
+}
+
+/*
+ * A port below the ephemeral range requires CAP_NET_ADMIN. As a normal user
+ * this must fail with EACCES; as root it must succeed. Both directions are
+ * checked so the test says something either way.
+ */
+TEST_F(qrtr, bind_privileged_port)
+{
+ int ret = qrtr_bind_port(self->fd, self->node, 0x100);
+
+ if (geteuid() == 0) {
+ EXPECT_EQ(0, ret);
+ } else {
+ EXPECT_EQ(-1, ret);
+ EXPECT_EQ(EACCES, errno);
+ }
+}
+
+/* Two sockets cannot hold the same port. */
+TEST_F(qrtr, bind_port_in_use)
+{
+ const __u32 port = QRTR_MIN_EPH_SOCKET + 0x2000;
+ int other;
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port));
+
+ other = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+ ASSERT_LE(0, other);
+
+ EXPECT_EQ(-1, qrtr_bind_port(other, self->node, port));
+ EXPECT_EQ(EADDRINUSE, errno);
+
+ close(other);
+}
+
+/*
+ * Rebinding is permitted: the socket moves to the new port and releases the
+ * old one, which another socket may then take.
+ */
+TEST_F(qrtr, rebind_moves_port)
+{
+ const __u32 port_a = QRTR_MIN_EPH_SOCKET + 0x2100;
+ const __u32 port_b = QRTR_MIN_EPH_SOCKET + 0x2200;
+ int other;
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port_a));
+ ASSERT_EQ(port_a, qrtr_port(_metadata, self->fd));
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port_b));
+ EXPECT_EQ(port_b, qrtr_port(_metadata, self->fd));
+
+ /* The first port must have been released. */
+ other = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+ ASSERT_LE(0, other);
+ EXPECT_EQ(0, qrtr_bind_port(other, self->node, port_a));
+ close(other);
+}
+
+/* Binding the port a socket already holds is a no-op, not an error. */
+TEST_F(qrtr, rebind_same_port)
+{
+ const __u32 port = QRTR_MIN_EPH_SOCKET + 0x2300;
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port));
+ EXPECT_EQ(0, qrtr_bind_port(self->fd, self->node, port));
+ EXPECT_EQ(port, qrtr_port(_metadata, self->fd));
+}
+
+/* A datagram sent to a port on the local node is delivered to that socket. */
+TEST_F(qrtr, local_datagram)
+{
+ struct sockaddr_qrtr peer_addr = {};
+ static const char msg[] = "qrtr";
+ struct sockaddr_qrtr from = {};
+ char buf[sizeof(msg)] = {};
+ socklen_t fromlen;
+ __u32 self_port;
+ int peer;
+
+ peer_addr.sq_family = AF_QIPCRTR;
+ peer_addr.sq_node = self->node;
+ fromlen = sizeof(from);
+
+ ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, 0));
+ self_port = qrtr_port(_metadata, self->fd);
+
+ peer = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+ ASSERT_LE(0, peer);
+ ASSERT_EQ(0, qrtr_bind_port(peer, self->node, 0));
+ peer_addr.sq_port = qrtr_port(_metadata, peer);
+
+ ASSERT_EQ((ssize_t)sizeof(msg),
+ sendto(self->fd, msg, sizeof(msg), 0,
+ (struct sockaddr *)&peer_addr, sizeof(peer_addr)));
+
+ ASSERT_EQ((ssize_t)sizeof(msg),
+ recvfrom(peer, buf, sizeof(buf), 0,
+ (struct sockaddr *)&from, &fromlen));
+
+ EXPECT_EQ(0, memcmp(msg, buf, sizeof(msg)));
+ EXPECT_EQ(sizeof(from), (size_t)fromlen);
+ EXPECT_EQ(AF_QIPCRTR, from.sq_family);
+ EXPECT_EQ(self->node, from.sq_node);
+ EXPECT_EQ(self_port, from.sq_port);
+
+ close(peer);
+}
+
+/* An unbound sender is auto-bound by sendto(), inside the ephemeral range. */
+TEST_F(qrtr, autobind_on_send)
+{
+ struct sockaddr_qrtr peer_addr = {};
+ static const char msg[] = "auto";
+ __u32 port;
+ int peer;
+
+ peer_addr.sq_family = AF_QIPCRTR;
+ peer_addr.sq_node = self->node;
+
+ peer = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
+ ASSERT_LE(0, peer);
+ ASSERT_EQ(0, qrtr_bind_port(peer, self->node, 0));
+ peer_addr.sq_port = qrtr_port(_metadata, peer);
+
+ /* self->fd was never bound. */
+ ASSERT_EQ(0u, qrtr_port(_metadata, self->fd));
+
+ ASSERT_EQ((ssize_t)sizeof(msg),
+ sendto(self->fd, msg, sizeof(msg), 0,
+ (struct sockaddr *)&peer_addr, sizeof(peer_addr)));
+
+ port = qrtr_port(_metadata, self->fd);
+ EXPECT_LE(QRTR_MIN_EPH_SOCKET, port);
+ EXPECT_GE(QRTR_MAX_EPH_SOCKET, port);
+
+ close(peer);
+}
+
+/* sendto() with a wrong-size or wrong-family address is rejected. */
+TEST_F(qrtr, sendto_bad_addr)
+{
+ static const char msg[] = "x";
+ struct sockaddr_qrtr sq = {
+ .sq_family = AF_QIPCRTR,
+ .sq_node = self->node,
+ .sq_port = QRTR_MIN_EPH_SOCKET,
+ };
+
+ EXPECT_EQ(-1, sendto(self->fd, msg, sizeof(msg), 0,
+ (struct sockaddr *)&sq, sizeof(sq) - 1));
+ EXPECT_EQ(EINVAL, errno);
+
+ sq.sq_family = AF_INET;
+ EXPECT_EQ(-1, sendto(self->fd, msg, sizeof(msg), 0,
+ (struct sockaddr *)&sq, sizeof(sq)));
+ EXPECT_EQ(EINVAL, errno);
+}
+
+TEST_HARNESS_MAIN
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net-next v3] selftests: net: add tests for the QRTR socket family
2026-12-02 12:03 [PATCH net-next v3] selftests: net: add tests for the QRTR socket family Murali Krishna
@ 2026-12-03 2:01 ` Hangbin Liu
2026-12-03 12:48 ` netdev-bot+sashiko
1 sibling, 0 replies; 3+ messages in thread
From: Hangbin Liu @ 2026-12-03 2:01 UTC (permalink / raw)
To: Murali Krishna
Cc: mani, shuah, davem, edumazet, kuba, pabeni, horms, netdev,
linux-kselftest, linux-arm-msm, linux-kernel
Hi,
On Wed, Dec 02, 2026 at 05:33:02PM +0530, Murali Krishna wrote:
> net/qrtr has no test coverage in tools/testing/selftests, although the
> behaviour it exposes to userspace is well defined and testable without
> Qualcomm hardware: the local node always exists, and CONFIG_QRTR_TUN lets a
> test act as a QRTR endpoint.
>
> Add tools/testing/selftests/net/qrtr with two programs.
>
> qrtr_sock covers the AF_QIPCRTR socket contract: the ephemeral port range
> and the CAP_NET_ADMIN boundary documented above qrtr_port_assign(), the
> bind() rule that an address must name the socket's own node, rebinding,
> EADDRINUSE on a taken port, auto-bind on send, and local datagram delivery.
>
> qrtr_endpoint covers frame validation in qrtr_endpoint_post() by writing
> crafted frames to /dev/qrtr-tun: length alignment, unknown protocol
> versions, truncated headers, bad declared sizes, control types whose
> payload is too small or which are addressed to an ordinary port, and the
> legacy 16-bit control port. Each test malforms a frame in exactly one way,
> so a failure names the check that regressed.
>
> qrtr.sh loads the qrtr and qrtr-tun modules and runs both programs. The
> tests still skip on their own if the socket family or /dev/qrtr-tun is
> genuinely unavailable, so the suite is harmless on non-Qualcomm configs.
>
> The tests were written with the help of an LLM coding assistant, from a
> prompt asking for selftests covering the AF_QIPCRTR socket contract and
> qrtr_endpoint_post() frame validation. Both C files and this changelog were
> drafted that way, then reviewed, corrected and run by hand. Writing them
> corrected two assumptions about the interface: that bind() treats
> sq_node == 0 as "any node", and that rebinding an already bound socket
> fails.
>
> Signed-off-by: Murali Krishna <muralikrishna2444b@gmail.com>
> ---
>
> v3:
> - shorten the commit message (Hangbin Liu)
> - add qrtr.sh, which loads the qrtr and qrtr-tun modules and then runs both
> programs, so the suite does not report skips as passes when the modules
> are simply not loaded (Hangbin Liu); the two binaries move to
> TEST_GEN_PROGS_EXTENDED so the script is the test entry point
> - no change to the tests themselves
>
> Link: https://lore.kernel.org/r/20261201104515.45339-1-muralikrishna2444b@gmail.com [v2]
> Link: https://lore.kernel.org/r/20261124150753.115914-1-muralikrishna2444b@gmail.com [v1]
>
> Before qrtr.sh, on a host where qrtr-tun exists but is not loaded:
>
> # PASSED: 13 / 13 tests passed.
> # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:13 error:0
>
> With qrtr.sh, same host, run as root (x86_64, 7.0.0-44-generic):
>
> # PASSED: 12 / 12 tests passed.
> # Totals: pass:12 fail:0 xfail:0 xpass:0 skip:0 error:0
> # PASSED: 13 / 13 tests passed.
> # Totals: pass:13 fail:0 xfail:0 xpass:0 skip:0 error:0
>
> Note that /dev/qrtr-tun is mode 0600 root, so qrtr_endpoint needs privilege
> regardless of the config; loading the module moves the skip from
> "no /dev/qrtr-tun" to "needs root".
>
> MAINTAINERS | 1 +
> tools/testing/selftests/Makefile | 1 +
> tools/testing/selftests/net/qrtr/.gitignore | 3 +
> tools/testing/selftests/net/qrtr/Makefile | 13 +
> tools/testing/selftests/net/qrtr/config | 2 +
> tools/testing/selftests/net/qrtr/qrtr.sh | 26 ++
> .../selftests/net/qrtr/qrtr_endpoint.c | 350 ++++++++++++++++++
> tools/testing/selftests/net/qrtr/qrtr_sock.c | 298 +++++++++++++++
> 8 files changed, 694 insertions(+)
> create mode 100644 tools/testing/selftests/net/qrtr/.gitignore
> create mode 100644 tools/testing/selftests/net/qrtr/Makefile
> create mode 100644 tools/testing/selftests/net/qrtr/config
> create mode 100755 tools/testing/selftests/net/qrtr/qrtr.sh
> create mode 100644 tools/testing/selftests/net/qrtr/qrtr_endpoint.c
> create mode 100644 tools/testing/selftests/net/qrtr/qrtr_sock.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 97308edec53d..cce309b30308 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -23174,6 +23174,7 @@ S: Maintained
> F: include/trace/events/qrtr.h
> F: include/uapi/linux/qrtr.h
> F: net/qrtr/
> +F: tools/testing/selftests/net/qrtr/
>
> QUALCOMM IPCC MAILBOX DRIVER
> M: Manivannan Sadhasivam <mani@kernel.org>
> diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> index c55d7792b27c..3d0916b60f45 100644
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -97,6 +97,7 @@ TARGETS += net/openvswitch
> TARGETS += net/ovpn
> TARGETS += net/packetdrill
> TARGETS += net/ppp
> +TARGETS += net/qrtr
> TARGETS += net/rds
> TARGETS += net/tcp_ao
> TARGETS += nfsd
> diff --git a/tools/testing/selftests/net/qrtr/.gitignore b/tools/testing/selftests/net/qrtr/.gitignore
> new file mode 100644
> index 000000000000..cc2ea8a2b665
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/.gitignore
> @@ -0,0 +1,3 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +qrtr_endpoint
> +qrtr_sock
> diff --git a/tools/testing/selftests/net/qrtr/Makefile b/tools/testing/selftests/net/qrtr/Makefile
> new file mode 100644
> index 000000000000..f9814a5c3b4d
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/Makefile
> @@ -0,0 +1,13 @@
> +# SPDX-License-Identifier: GPL-2.0
> +top_srcdir := ../../../../..
> +
> +CFLAGS += $(KHDR_INCLUDES) -Wall -Wextra -Wno-unused-parameter
> +
> +TEST_PROGS := qrtr.sh
> +
> +TEST_GEN_PROGS_EXTENDED := \
> + qrtr_endpoint \
> + qrtr_sock \
> +# end of TEST_GEN_PROGS_EXTENDED
> +
> +include ../../lib.mk
> diff --git a/tools/testing/selftests/net/qrtr/config b/tools/testing/selftests/net/qrtr/config
> new file mode 100644
> index 000000000000..0bdb8a4c5211
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/config
> @@ -0,0 +1,2 @@
> +CONFIG_QRTR=m
> +CONFIG_QRTR_TUN=m
> diff --git a/tools/testing/selftests/net/qrtr/qrtr.sh b/tools/testing/selftests/net/qrtr/qrtr.sh
> new file mode 100755
> index 000000000000..031cadd842a1
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/qrtr.sh
> @@ -0,0 +1,26 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# Run the QRTR selftests.
> +#
> +# CONFIG_QRTR and CONFIG_QRTR_TUN are modules in this directory's config, so
> +# load them before running: a test that skips because a module was not loaded
> +# reports the same "not run" as a test with nothing to check, which hides a
> +# regression instead of showing it. Both modprobes are quiet and their failure
> +# is ignored, so a built-in or absent QRTR still reaches the tests, which skip
> +# on their own if the socket family or /dev/qrtr-tun really is unavailable.
> +#
> +# Only failure is propagated. kselftest_harness exits KSFT_PASS even when
> +# every test skipped, so a skip cannot be detected from the exit status here.
> +
> +modprobe -q qrtr 2>/dev/null
> +modprobe -q qrtr-tun 2>/dev/null
> +
> +ret=0
> +
> +for t in qrtr_sock qrtr_endpoint; do
> + echo "# $t"
> + ./"$t" || ret=$?
> +done
> +
> +exit $ret
> diff --git a/tools/testing/selftests/net/qrtr/qrtr_endpoint.c b/tools/testing/selftests/net/qrtr/qrtr_endpoint.c
> new file mode 100644
> index 000000000000..c38bc69cb4ce
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/qrtr_endpoint.c
> @@ -0,0 +1,350 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Tests for QRTR frame validation in qrtr_endpoint_post().
> + *
> + * /dev/qrtr-tun (CONFIG_QRTR_TUN) lets userspace act as a QRTR endpoint:
> + * a write() is handed straight to qrtr_endpoint_post(), so every length and
> + * header check in that function is reachable from here with no Qualcomm
> + * hardware. A rejected frame makes write() fail with EINVAL; an accepted one
> + * returns the full length.
> + *
> + * The frames below are deliberately malformed in one way each, so a failure
> + * names the specific check that regressed.
> + *
> + * On-wire header fields are little endian, and qrtr_endpoint_post() reads the
> + * version from the first byte of the frame and decodes the rest with
> + * le32_to_cpu()/le16_to_cpu(), so every multi-byte field is built with
> + * htole32()/htole16() rather than in host order.
> + */
> +
> +#include <endian.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdint.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +#include <linux/qrtr.h>
> +#include <sys/socket.h>
> +
> +#include "../../kselftest_harness.h"
> +
> +/* From net/qrtr/af_qrtr.c */
> +#define QRTR_PROTO_VER_1 1
> +#define QRTR_PROTO_VER_2 3
> +#define QRTR_PORT_CTRL_LEGACY 0xffff
> +
> +/* A node id for the test endpoint, distinct from the local node (1). */
> +#define TEST_SRC_NODE 5
> +
> +/* An ordinary (non-control) port, inside the unprivileged ephemeral range. */
> +#define TEST_NORMAL_PORT 0x4321
> +
> +struct qrtr_hdr_v1 {
> + __le32 version;
> + __le32 type;
> + __le32 src_node_id;
> + __le32 src_port_id;
> + __le32 confirm_rx;
> + __le32 size;
> + __le32 dst_node_id;
> + __le32 dst_port_id;
> +} __attribute__((packed));
> +
> +struct qrtr_hdr_v2 {
> + uint8_t version;
> + uint8_t type;
> + uint8_t flags;
> + uint8_t optlen;
> + __le32 size;
> + __le16 src_node_id;
> + __le16 src_port_id;
> + __le16 dst_node_id;
> + __le16 dst_port_id;
> +};
> +
> +/* One header plus one control packet: the smallest realistic valid frame. */
> +struct v1_frame {
> + struct qrtr_hdr_v1 hdr;
> + struct qrtr_ctrl_pkt pkt;
> +} __attribute__((packed));
> +
> +static void v1_init(struct v1_frame *f, uint32_t type)
> +{
> + memset(f, 0, sizeof(*f));
> + f->hdr.version = htole32(QRTR_PROTO_VER_1);
> + f->hdr.type = htole32(type);
> + f->hdr.src_node_id = htole32(TEST_SRC_NODE);
> + f->hdr.src_port_id = htole32(QRTR_PORT_CTRL);
> + f->hdr.size = htole32(sizeof(f->pkt));
> + f->hdr.dst_node_id = htole32(0);
> + f->hdr.dst_port_id = htole32(QRTR_PORT_CTRL);
> + f->pkt.cmd = htole32(type);
> +}
> +
> +/*
> + * Bind a fresh socket to a given port on the local node. Returns the fd, or
> + * -1 with errno set. bind() insists the address name the socket's own node,
> + * so that is read back from the socket rather than assumed.
> + */
> +static int qrtr_bind_local(int *fdp, uint32_t port)
> +{
> + struct sockaddr_qrtr sq = {};
> + socklen_t len = sizeof(sq);
> + int fd;
> +
> + fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + if (fd < 0)
> + return -1;
> +
> + if (getsockname(fd, (struct sockaddr *)&sq, &len)) {
> + close(fd);
> + return -1;
> + }
> +
> + sq.sq_family = AF_QIPCRTR;
> + sq.sq_port = port;
> +
> + if (bind(fd, (struct sockaddr *)&sq, sizeof(sq))) {
> + close(fd);
> + return -1;
> + }
> +
> + *fdp = fd;
> + return 0;
> +}
> +
> +FIXTURE(tun)
> +{
> + int fd;
> +};
> +
> +FIXTURE_SETUP(tun)
> +{
> + self->fd = open("/dev/qrtr-tun", O_RDWR | O_NONBLOCK);
> + if (self->fd < 0) {
> + if (errno == ENOENT)
> + SKIP(return,
> + "no /dev/qrtr-tun: modprobe qrtr-tun, or CONFIG_QRTR_TUN=n");
> + if (errno == EACCES || errno == EPERM)
> + SKIP(return, "/dev/qrtr-tun needs root");
> + ASSERT_LE(0, self->fd);
> + }
> +}
> +
> +FIXTURE_TEARDOWN(tun)
> +{
> + if (self->fd >= 0)
> + close(self->fd);
> +}
> +
> +/* Baseline: a well formed HELLO to the control port is accepted. */
> +TEST_F(tun, valid_hello)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> +
> + EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
> +}
> +
> +/* A length that is not a multiple of four is rejected before anything else. */
> +TEST_F(tun, unaligned_length)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> +
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f) - 1));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/* Only protocol versions 1 and 3 exist. */
> +TEST_F(tun, bad_version)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + f.hdr.version = htole32(2);
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + f.hdr.version = htole32(0xff);
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/* A frame shorter than the version 1 header cannot carry one. */
> +TEST_F(tun, truncated_v1_header)
> +{
> + struct v1_frame f;
> + size_t len;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> +
> + for (len = 4; len < sizeof(struct qrtr_hdr_v1); len += 4) {
> + EXPECT_EQ(-1, write(self->fd, &f, len));
> + EXPECT_EQ(EINVAL, errno);
> + }
> +}
> +
> +/* A frame shorter than the version 2 header cannot carry one. */
> +TEST_F(tun, truncated_v2_header)
> +{
> + struct qrtr_hdr_v2 hdr = {};
> + size_t len;
> +
> + hdr.version = QRTR_PROTO_VER_2;
> + hdr.type = QRTR_TYPE_HELLO;
> +
> + for (len = 4; len < sizeof(hdr); len += 4) {
> + EXPECT_EQ(-1, write(self->fd, &hdr, len));
> + EXPECT_EQ(EINVAL, errno);
> + }
> +}
> +
> +/* A declared payload size of zero is rejected. */
> +TEST_F(tun, zero_size)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + f.hdr.size = htole32(0);
> +
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/* A declared size larger than the frame is rejected: it would over-read. */
> +TEST_F(tun, size_exceeds_frame)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + f.hdr.size = htole32(sizeof(f) + 64);
> +
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/* The frame length must be exactly the header plus the padded payload. */
> +TEST_F(tun, size_does_not_match_length)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + /* Fits in the frame, but leaves four unaccounted trailing bytes. */
> + f.hdr.size = htole32(sizeof(f.pkt) - 4);
> +
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/*
> + * NEW_SERVER and RESUME_TX are parsed as a struct qrtr_ctrl_pkt, so a
> + * payload too small to hold one must be rejected rather than read past.
> + */
> +TEST_F(tun, control_payload_too_small)
> +{
> + struct {
> + struct qrtr_hdr_v1 hdr;
> + uint32_t body;
> + } __attribute__((packed)) f;
> +
> + memset(&f, 0, sizeof(f));
> + f.hdr.version = htole32(QRTR_PROTO_VER_1);
> + f.hdr.src_node_id = htole32(TEST_SRC_NODE);
> + f.hdr.src_port_id = htole32(QRTR_PORT_CTRL);
> + f.hdr.size = htole32(sizeof(f.body));
> + f.hdr.dst_port_id = htole32(QRTR_PORT_CTRL);
> +
> + f.hdr.type = htole32(QRTR_TYPE_NEW_SERVER);
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +
> + f.hdr.type = htole32(QRTR_TYPE_RESUME_TX);
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/*
> + * DATA addressed to an ordinary port is accepted and delivered. This is the
> + * positive half of control_type_to_normal_port below: it shows that a frame
> + * to TEST_NORMAL_PORT does reach qrtr_port_lookup() and succeeds, so the
> + * EINVAL that test expects can only come from the control-type check.
> + */
> +TEST_F(tun, data_to_normal_port)
> +{
> + struct v1_frame f;
> + int peer;
> +
> + if (qrtr_bind_local(&peer, TEST_NORMAL_PORT))
> + SKIP(return, "cannot bind AF_QIPCRTR port (CONFIG_QRTR=n?)");
> +
> + v1_init(&f, QRTR_TYPE_DATA);
> + f.hdr.dst_port_id = htole32(TEST_NORMAL_PORT);
> +
> + EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
> +
> + close(peer);
> +}
> +
> +/*
> + * Only DATA and RESUME_TX may be addressed to an ordinary port. A control
> + * message aimed at a normal port is rejected.
> + *
> + * A socket must be bound to the destination port for this to mean anything:
> + * with nothing bound, qrtr_port_lookup() fails and qrtr_endpoint_post()
> + * returns -EINVAL from there instead, so the test would still pass if the
> + * control-type check were removed.
> + */
> +TEST_F(tun, control_type_to_normal_port)
> +{
> + struct v1_frame f;
> + int peer;
> +
> + if (qrtr_bind_local(&peer, TEST_NORMAL_PORT))
> + SKIP(return, "cannot bind AF_QIPCRTR port (CONFIG_QRTR=n?)");
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + f.hdr.dst_port_id = htole32(TEST_NORMAL_PORT);
> +
> + EXPECT_EQ(-1, write(self->fd, &f, sizeof(f)));
> + EXPECT_EQ(EINVAL, errno);
> +
> + close(peer);
> +}
> +
> +/* The legacy 16-bit control port number is still understood. */
> +TEST_F(tun, legacy_control_port)
> +{
> + struct v1_frame f;
> +
> + v1_init(&f, QRTR_TYPE_HELLO);
> + f.hdr.dst_port_id = htole32(QRTR_PORT_CTRL_LEGACY);
> +
> + EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
> +}
> +
> +/* A version 2 header is accepted on its own terms. */
> +TEST_F(tun, valid_v2_hello)
> +{
> + struct {
> + struct qrtr_hdr_v2 hdr;
> + struct qrtr_ctrl_pkt pkt;
> + } f;
> +
> + memset(&f, 0, sizeof(f));
> + f.hdr.version = QRTR_PROTO_VER_2;
> + f.hdr.type = QRTR_TYPE_HELLO;
> + f.hdr.size = htole32(sizeof(f.pkt));
> + f.hdr.src_node_id = htole16(TEST_SRC_NODE);
> + f.hdr.src_port_id = htole16((uint16_t)QRTR_PORT_CTRL);
> + f.hdr.dst_port_id = htole16((uint16_t)QRTR_PORT_CTRL);
> + f.pkt.cmd = htole32(QRTR_TYPE_HELLO);
> +
> + EXPECT_EQ((ssize_t)sizeof(f), write(self->fd, &f, sizeof(f)));
> +}
> +
> +TEST_HARNESS_MAIN
> diff --git a/tools/testing/selftests/net/qrtr/qrtr_sock.c b/tools/testing/selftests/net/qrtr/qrtr_sock.c
> new file mode 100644
> index 000000000000..592e4e429b97
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/qrtr_sock.c
> @@ -0,0 +1,298 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Tests for the AF_QIPCRTR socket address family.
> + *
> + * These exercise the port assignment contract documented above
> + * qrtr_port_assign() in net/qrtr/af_qrtr.c, the bind() address rules in
> + * qrtr_bind(), and local datagram delivery between two sockets on the local
> + * node. No QRTR endpoint and no Qualcomm hardware is required: the local
> + * node always exists.
> + */
> +
> +#include <errno.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +#include <linux/qrtr.h>
> +#include <sys/socket.h>
> +
> +#include "../../kselftest_harness.h"
> +
> +/* From net/qrtr/af_qrtr.c. Ports below the ephemeral range are privileged. */
> +#define QRTR_MIN_EPH_SOCKET 0x4000u
> +#define QRTR_MAX_EPH_SOCKET 0x7fffu
> +
> +FIXTURE(qrtr)
> +{
> + int fd;
> + __u32 node; /* this socket's own node id */
> +};
> +
> +FIXTURE_SETUP(qrtr)
> +{
> + struct sockaddr_qrtr sq = {};
> + socklen_t len = sizeof(sq);
> +
> + self->fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + if (self->fd < 0 &&
> + (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT))
> + SKIP(return, "AF_QIPCRTR not supported (CONFIG_QRTR=n?)");
> + ASSERT_LE(0, self->fd);
> +
> + /*
> + * A fresh socket already knows which node it lives on; bind() insists
> + * on being given that same value, so every test needs it.
> + */
> + ASSERT_EQ(0, getsockname(self->fd, (struct sockaddr *)&sq, &len));
> + self->node = sq.sq_node;
> +}
> +
> +FIXTURE_TEARDOWN(qrtr)
> +{
> + if (self->fd >= 0)
> + close(self->fd);
> +}
> +
> +static int qrtr_bind_port(int fd, __u32 node, __u32 port)
> +{
> + struct sockaddr_qrtr sq = {
> + .sq_family = AF_QIPCRTR,
> + .sq_node = node,
> + .sq_port = port,
> + };
> +
> + return bind(fd, (struct sockaddr *)&sq, sizeof(sq));
> +}
> +
> +static __u32 qrtr_port(struct __test_metadata *_metadata, int fd)
> +{
> + struct sockaddr_qrtr sq = {};
> + socklen_t len = sizeof(sq);
> +
> + ASSERT_EQ(0, getsockname(fd, (struct sockaddr *)&sq, &len));
> + return sq.sq_port;
> +}
> +
> +/* A socket is unbound until it is bound: getsockname() reports port 0. */
> +TEST_F(qrtr, getsockname_unbound)
> +{
> + struct sockaddr_qrtr sq = {};
> + socklen_t len = sizeof(sq);
> +
> + ASSERT_EQ(0, getsockname(self->fd, (struct sockaddr *)&sq, &len));
> + EXPECT_EQ(sizeof(sq), (size_t)len);
> + EXPECT_EQ(AF_QIPCRTR, sq.sq_family);
> + EXPECT_EQ(0u, sq.sq_port);
> +}
> +
> +/*
> + * bind() requires the address to name the socket's own node. Unlike most
> + * families, zero does not mean "any node".
> + */
> +TEST_F(qrtr, bind_foreign_node_rejected)
> +{
> + EXPECT_EQ(-1, qrtr_bind_port(self->fd, self->node + 1, 0));
> + EXPECT_EQ(EINVAL, errno);
> +
> + if (self->node != 0) {
> + EXPECT_EQ(-1, qrtr_bind_port(self->fd, 0, 0));
> + EXPECT_EQ(EINVAL, errno);
> + }
> +}
> +
> +/* A too-short or wrong-family address is rejected. */
> +TEST_F(qrtr, bind_bad_addr)
> +{
> + struct sockaddr_qrtr sq = {
> + .sq_family = AF_QIPCRTR,
> + .sq_node = self->node,
> + .sq_port = 0,
> + };
> +
> + EXPECT_EQ(-1, bind(self->fd, (struct sockaddr *)&sq, sizeof(sq) - 1));
> + EXPECT_EQ(EINVAL, errno);
> +
> + sq.sq_family = AF_INET;
> + EXPECT_EQ(-1, bind(self->fd, (struct sockaddr *)&sq, sizeof(sq)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +/* Binding port 0 assigns an ephemeral port inside the documented range. */
> +TEST_F(qrtr, bind_ephemeral_in_range)
> +{
> + __u32 port;
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, 0));
> +
> + port = qrtr_port(_metadata, self->fd);
> + EXPECT_LE(QRTR_MIN_EPH_SOCKET, port);
> + EXPECT_GE(QRTR_MAX_EPH_SOCKET, port);
> +}
> +
> +/* A port at or above the ephemeral range may be claimed by any user. */
> +TEST_F(qrtr, bind_unprivileged_port)
> +{
> + const __u32 want = QRTR_MIN_EPH_SOCKET + 0x1000;
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, want));
> + EXPECT_EQ(want, qrtr_port(_metadata, self->fd));
> +}
> +
> +/*
> + * A port below the ephemeral range requires CAP_NET_ADMIN. As a normal user
> + * this must fail with EACCES; as root it must succeed. Both directions are
> + * checked so the test says something either way.
> + */
> +TEST_F(qrtr, bind_privileged_port)
> +{
> + int ret = qrtr_bind_port(self->fd, self->node, 0x100);
> +
> + if (geteuid() == 0) {
> + EXPECT_EQ(0, ret);
> + } else {
> + EXPECT_EQ(-1, ret);
> + EXPECT_EQ(EACCES, errno);
> + }
> +}
> +
> +/* Two sockets cannot hold the same port. */
> +TEST_F(qrtr, bind_port_in_use)
> +{
> + const __u32 port = QRTR_MIN_EPH_SOCKET + 0x2000;
> + int other;
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port));
> +
> + other = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + ASSERT_LE(0, other);
> +
> + EXPECT_EQ(-1, qrtr_bind_port(other, self->node, port));
> + EXPECT_EQ(EADDRINUSE, errno);
> +
> + close(other);
> +}
> +
> +/*
> + * Rebinding is permitted: the socket moves to the new port and releases the
> + * old one, which another socket may then take.
> + */
> +TEST_F(qrtr, rebind_moves_port)
> +{
> + const __u32 port_a = QRTR_MIN_EPH_SOCKET + 0x2100;
> + const __u32 port_b = QRTR_MIN_EPH_SOCKET + 0x2200;
> + int other;
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port_a));
> + ASSERT_EQ(port_a, qrtr_port(_metadata, self->fd));
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port_b));
> + EXPECT_EQ(port_b, qrtr_port(_metadata, self->fd));
> +
> + /* The first port must have been released. */
> + other = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + ASSERT_LE(0, other);
> + EXPECT_EQ(0, qrtr_bind_port(other, self->node, port_a));
> + close(other);
> +}
> +
> +/* Binding the port a socket already holds is a no-op, not an error. */
> +TEST_F(qrtr, rebind_same_port)
> +{
> + const __u32 port = QRTR_MIN_EPH_SOCKET + 0x2300;
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, port));
> + EXPECT_EQ(0, qrtr_bind_port(self->fd, self->node, port));
> + EXPECT_EQ(port, qrtr_port(_metadata, self->fd));
> +}
> +
> +/* A datagram sent to a port on the local node is delivered to that socket. */
> +TEST_F(qrtr, local_datagram)
> +{
> + struct sockaddr_qrtr peer_addr = {};
> + static const char msg[] = "qrtr";
> + struct sockaddr_qrtr from = {};
> + char buf[sizeof(msg)] = {};
> + socklen_t fromlen;
> + __u32 self_port;
> + int peer;
> +
> + peer_addr.sq_family = AF_QIPCRTR;
> + peer_addr.sq_node = self->node;
> + fromlen = sizeof(from);
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, 0));
> + self_port = qrtr_port(_metadata, self->fd);
> +
> + peer = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + ASSERT_LE(0, peer);
> + ASSERT_EQ(0, qrtr_bind_port(peer, self->node, 0));
> + peer_addr.sq_port = qrtr_port(_metadata, peer);
> +
> + ASSERT_EQ((ssize_t)sizeof(msg),
> + sendto(self->fd, msg, sizeof(msg), 0,
> + (struct sockaddr *)&peer_addr, sizeof(peer_addr)));
> +
> + ASSERT_EQ((ssize_t)sizeof(msg),
> + recvfrom(peer, buf, sizeof(buf), 0,
> + (struct sockaddr *)&from, &fromlen));
> +
> + EXPECT_EQ(0, memcmp(msg, buf, sizeof(msg)));
> + EXPECT_EQ(sizeof(from), (size_t)fromlen);
> + EXPECT_EQ(AF_QIPCRTR, from.sq_family);
> + EXPECT_EQ(self->node, from.sq_node);
> + EXPECT_EQ(self_port, from.sq_port);
> +
> + close(peer);
> +}
> +
> +/* An unbound sender is auto-bound by sendto(), inside the ephemeral range. */
> +TEST_F(qrtr, autobind_on_send)
> +{
> + struct sockaddr_qrtr peer_addr = {};
> + static const char msg[] = "auto";
> + __u32 port;
> + int peer;
> +
> + peer_addr.sq_family = AF_QIPCRTR;
> + peer_addr.sq_node = self->node;
> +
> + peer = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + ASSERT_LE(0, peer);
> + ASSERT_EQ(0, qrtr_bind_port(peer, self->node, 0));
> + peer_addr.sq_port = qrtr_port(_metadata, peer);
> +
> + /* self->fd was never bound. */
> + ASSERT_EQ(0u, qrtr_port(_metadata, self->fd));
> +
> + ASSERT_EQ((ssize_t)sizeof(msg),
> + sendto(self->fd, msg, sizeof(msg), 0,
> + (struct sockaddr *)&peer_addr, sizeof(peer_addr)));
> +
> + port = qrtr_port(_metadata, self->fd);
> + EXPECT_LE(QRTR_MIN_EPH_SOCKET, port);
> + EXPECT_GE(QRTR_MAX_EPH_SOCKET, port);
> +
> + close(peer);
> +}
> +
> +/* sendto() with a wrong-size or wrong-family address is rejected. */
> +TEST_F(qrtr, sendto_bad_addr)
> +{
> + static const char msg[] = "x";
> + struct sockaddr_qrtr sq = {
> + .sq_family = AF_QIPCRTR,
> + .sq_node = self->node,
> + .sq_port = QRTR_MIN_EPH_SOCKET,
> + };
> +
> + EXPECT_EQ(-1, sendto(self->fd, msg, sizeof(msg), 0,
> + (struct sockaddr *)&sq, sizeof(sq) - 1));
> + EXPECT_EQ(EINVAL, errno);
> +
> + sq.sq_family = AF_INET;
> + EXPECT_EQ(-1, sendto(self->fd, msg, sizeof(msg), 0,
> + (struct sockaddr *)&sq, sizeof(sq)));
> + EXPECT_EQ(EINVAL, errno);
> +}
> +
> +TEST_HARNESS_MAIN
> --
> 2.43.0
>
I'm not familiar with qrtr. I run the selftest on my local side and the
result looks good to me.
Tested-by: Hangbin Liu <liuhangbin@kylinos.cn>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net-next v3] selftests: net: add tests for the QRTR socket family
2026-12-02 12:03 [PATCH net-next v3] selftests: net: add tests for the QRTR socket family Murali Krishna
2026-12-03 2:01 ` Hangbin Liu
@ 2026-12-03 12:48 ` netdev-bot+sashiko
1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 12:48 UTC (permalink / raw)
To: muralikrishna2444b
Cc: mani, shuah, hangbin.liu, davem, edumazet, kuba, pabeni, horms,
netdev, linux-kselftest, linux-arm-msm, linux-kernel
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 4 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] qrtr_endpoint.c gives the fake /dev/qrtr-tun endpoint the
fixed node id TEST_SRC_NODE = 5.
- [Medium] qrtr.sh passes on only the binaries' failure status, so it
exits 0 (PASS) whenever nothing actually runs: - CONFIG_QRTR is not…
- [Medium] The commit message says the suite covers the CAP_NET_ADMIN
boundary, but bind_privileged_port in qrtr_sock.c does not test it…
- [Medium] The qrtr_endpoint.c header comment (lines 11-12) and the
commit message say each frame is malformed in exactly one way, so a…
- [Low] Since commit 3b536db8fb32, qrtr_create() in net/qrtr/af_qrtr.c
returns -EAFNOSUPPORT for any network namespace other than init_net…
--
selftests: net: add tests for the QRTR socket family
This adds tools/testing/selftests/net/qrtr with two programs. qrtr_sock
tests the AF_QIPCRTR bind, port and send rules on the local node.
qrtr_endpoint writes crafted frames to /dev/qrtr-tun to test the frame
validation in qrtr_endpoint_post(), and a qrtr.sh wrapper loads the
modules and runs both.
Link: https://lore.kernel.org/r/20261201104515.45339-1-muralikrishna2444b@gmail.com [v2]
Link: https://lore.kernel.org/r/20261124150753.115914-1-muralikrishna2444b@gmail.com [v1]
> diff --git a/tools/testing/selftests/net/qrtr/qrtr.sh b/tools/testing/selftests/net/qrtr/qrtr.sh
> new file mode 100755
> index 0000000000000..031cadd842a1f
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/qrtr.sh
> @@ -0,0 +1,26 @@
[ ... ]
> +# Only failure is propagated. kselftest_harness exits KSFT_PASS even when
> +# every test skipped, so a skip cannot be detected from the exit status here.
> +
> +modprobe -q qrtr 2>/dev/null
> +modprobe -q qrtr-tun 2>/dev/null
> +
> +ret=0
> +
> +for t in qrtr_sock qrtr_endpoint; do
> + echo "# $t"
> + ./"$t" || ret=$?
> +done
> +
> +exit $ret
[Severity: Medium]
Doesn't this script still report PASS whenever nothing actually ran?
The modprobe results are ignored, and only a non-zero exit from the
binaries is passed on. kselftest_harness ends with ksft_exit(ret == 0),
which exits KSFT_PASS even when every test skipped.
So the suite reports PASS when:
- CONFIG_QRTR is not set and every test skips.
- The script runs as non-root. modprobe then fails silently, and all 13
qrtr_endpoint tests skip with "/dev/qrtr-tun needs root" because the
node is mode 0600.
- The run is outside init_net (see the qrtr_sock.c comment below).
The v3 notes say qrtr.sh was added so the suite does not report skips
as passes. As written, it only handles the case where the module exists
but is not loaded.
A skip can't be seen in the binaries' exit status. Could the script
check its own prerequisites first instead, and exit with ksft_skip (4)
as other net selftest wrappers do? It could check for root, run
[ -c /dev/qrtr-tun ], and confirm that modprobe succeeded.
> diff --git a/tools/testing/selftests/net/qrtr/qrtr_endpoint.c b/tools/testing/selftests/net/qrtr/qrtr_endpoint.c
> new file mode 100644
> index 0000000000000..c38bc69cb4ce9
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/qrtr_endpoint.c
> @@ -0,0 +1,350 @@
[ ... ]
> + * The frames below are deliberately malformed in one way each, so a failure
> + * names the specific check that regressed.
[Severity: Medium]
The commit message makes the same claim:
Each test malforms a frame in exactly one way, so a failure names the
check that regressed.
Does that hold for every test here? qrtr_endpoint_post() ends with:
net/qrtr/af_qrtr.c:qrtr_endpoint_post() {
...
if (!size || size > len || len != ALIGN(size, 4) + hdrlen)
goto err;
...
}
The final length check rejects several of these frames by itself. Those
tests would still pass if the check they are named after were removed.
For reference, sizeof(struct qrtr_hdr_v1) is 32 and
sizeof(struct v1_frame) is 52:
- unaligned_length writes 51 bytes with size=20. Without the len & 3
check, 51 != ALIGN(20, 4) + 32 still rejects it.
- truncated_v1_header and truncated_v2_header use len < hdrlen, which
can never equal ALIGN(size, 4) + hdrlen. Without the len < sizeof(*v1)
and len < sizeof(*v2) guards, the kernel would read past the
kzalloc(len) buffer from qrtr_tun_write_iter() and still return
-EINVAL. Only KASAN would notice.
- zero_size sends 52 bytes with size=0. Without the !size check,
52 != 0 + 32 still rejects it. Only a 32-byte header-only frame would
test that check alone.
- size_exceeds_frame uses size=116 and len=52. Without the size > len
check, 52 != 116 + 32 still rejects it.
Could these frames be built so that the named check is the only one
that fails? Otherwise, could the comment and changelog be changed?
[ ... ]
> +/* A node id for the test endpoint, distinct from the local node (1). */
> +#define TEST_SRC_NODE 5
[Severity: Medium]
Is node 5 safe to use on Qualcomm hardware? The only reason given is
that it differs from the local node. On many Qualcomm SoCs, 5 is a real
remote processor's QRTR node id, commonly the ADSP. Since net/qrtr is
added to the default TARGETS, this suite will also run on Qualcomm
boards.
Suppose the real node already owns nid 5. valid_hello,
legacy_control_port and valid_v2_hello send HELLO frames with
src_node 5 to QRTR_PORT_CTRL. qrtr_node_assign() ignores the
radix_tree_insert() failure, but still sets the tun node's nid to 5:
net/qrtr/af_qrtr.c:qrtr_node_assign() {
...
radix_tree_insert(&qrtr_nodes, nid, node);
if (node->nid == QRTR_EP_NID_AUTO)
WRITE_ONCE(node->nid, nid);
...
}
The HELLO then reaches the name server:
qrtr_endpoint_post()
HELLO queued to the name server socket
ctrl_cmd_hello()
announce_servers()
NEW_SERVER for every local service sent to node 5 by kernel_sendmsg()
Node 5 resolves to the real remote processor, so the selftest sends
unsolicited control traffic to live firmware.
Now suppose the tun node takes nid 5 first, for example while the ADSP
boots or restarts during the test. Traffic for node 5 is then routed to
/dev/qrtr-tun. When the fd closes, qrtr_endpoint_unregister() sends a
BYE for node 5 to the name server. ctrl_cmd_bye() then deletes every
server registered under node 5.
The changelog only says "the suite is harmless on non-Qualcomm
configs". Would a node id that Qualcomm firmware does not use avoid
both cases? It would need to be a high value that still fits in the
16-bit v2 header.
[ ... ]
> diff --git a/tools/testing/selftests/net/qrtr/qrtr_sock.c b/tools/testing/selftests/net/qrtr/qrtr_sock.c
> new file mode 100644
> index 0000000000000..592e4e429b975
> --- /dev/null
> +++ b/tools/testing/selftests/net/qrtr/qrtr_sock.c
> @@ -0,0 +1,298 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Tests for the AF_QIPCRTR socket address family.
> + *
> + * These exercise the port assignment contract documented above
> + * qrtr_port_assign() in net/qrtr/af_qrtr.c, the bind() address rules in
> + * qrtr_bind(), and local datagram delivery between two sockets on the local
> + * node. No QRTR endpoint and no Qualcomm hardware is required: the local
> + * node always exists.
> + */
[ ... ]
> +FIXTURE_SETUP(qrtr)
> +{
> + struct sockaddr_qrtr sq = {};
> + socklen_t len = sizeof(sq);
> +
> + self->fd = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
> + if (self->fd < 0 &&
> + (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT))
> + SKIP(return, "AF_QIPCRTR not supported (CONFIG_QRTR=n?)");
[Severity: Low]
The header comment and the commit message both say "the local node
always exists". Is that still true outside init_net? Since commit
3b536db8fb32, qrtr_create() returns -EAFNOSUPPORT for any namespace
other than init_net:
net/qrtr/af_qrtr.c:qrtr_create() {
...
if (!net_eq(net, &init_net))
return -EAFNOSUPPORT;
...
}
run_kselftest.sh -n/--netns runs each test in its own namespace. In
that mode, this fixture skips all 12 qrtr_sock tests with "AF_QIPCRTR
not supported (CONFIG_QRTR=n?)". data_to_normal_port and
control_type_to_normal_port in qrtr_endpoint.c skip with "cannot bind
AF_QIPCRTR port (CONFIG_QRTR=n?)". qrtr.sh still exits PASS.
The tests were run on a 7.0.0-44-generic distro kernel, which may be
older than this restriction.
Could the skip messages name the init_net restriction? Could qrtr.sh
also detect and report a run outside init_net?
[ ... ]
> +/* A port at or above the ephemeral range may be claimed by any user. */
> +TEST_F(qrtr, bind_unprivileged_port)
> +{
> + const __u32 want = QRTR_MIN_EPH_SOCKET + 0x1000;
> +
> + ASSERT_EQ(0, qrtr_bind_port(self->fd, self->node, want));
> + EXPECT_EQ(want, qrtr_port(_metadata, self->fd));
> +}
> +
> +/*
> + * A port below the ephemeral range requires CAP_NET_ADMIN. As a normal user
> + * this must fail with EACCES; as root it must succeed. Both directions are
> + * checked so the test says something either way.
> + */
> +TEST_F(qrtr, bind_privileged_port)
> +{
> + int ret = qrtr_bind_port(self->fd, self->node, 0x100);
> +
> + if (geteuid() == 0) {
> + EXPECT_EQ(0, ret);
> + } else {
> + EXPECT_EQ(-1, ret);
> + EXPECT_EQ(EACCES, errno);
> + }
> +}
[Severity: Medium]
The changelog says qrtr_sock covers "the CAP_NET_ADMIN boundary
documented above qrtr_port_assign()". Do these two tests actually cover
it?
The kernel checks a capability, not the uid:
net/qrtr/af_qrtr.c:qrtr_port_assign() {
...
} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
rc = -EACCES;
...
}
qrtr_create() only requires init_net, not init_user_ns, so the result
of geteuid() == 0 and capable(CAP_NET_ADMIN) can differ:
- A uid 0 process without CAP_NET_ADMIN gets EACCES, but the test
expects 0. This includes a host-network container with default caps,
unshare -U -r without a new netns, and capsh --drop=cap_net_admin.
- A non-root process with CAP_NET_ADMIN from file or ambient caps
succeeds, but the test expects EACCES.
qrtr.sh needs root for modprobe and /dev/qrtr-tun, and it does not
drop privileges for qrtr_sock. Under the intended runner, the EACCES
branch never runs. bind_unprivileged_port likewise cannot show that the
range is open to non-root users.
If the capable() check were removed, would anything in this suite fail?
The tests also never probe the edge of the range. They use 0x100 and
QRTR_MIN_EPH_SOCKET + 0x1000, but not QRTR_MIN_EPH_SOCKET - 1 or
QRTR_MIN_EPH_SOCKET, so changing < to <= would go unnoticed.
This part is pre-existing and was not introduced by this patch, but the
boundary is also unclear in the kernel. The comment above
qrtr_port_assign() says:
* >QRTR_MIN_EPH_SOCKET: Specified; available to all
while the code treats >= QRTR_MIN_EPH_SOCKET as unprivileged.
Could the test check CAP_NET_ADMIN directly, for example with capget()?
It could then drop the capability for the deny case and probe both
QRTR_MIN_EPH_SOCKET - 1 and QRTR_MIN_EPH_SOCKET.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202120303.257866-1-muralikrishna2444b%40gmail.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 12:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:03 [PATCH net-next v3] selftests: net: add tests for the QRTR socket family Murali Krishna
2026-12-03 2:01 ` Hangbin Liu
2026-12-03 12:48 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox