Skip to contentSkip to conversation

tst_netdevice: Add strict parameter to netdevice helper functions

PATCH In review Martin Doucha 2 participants 3 messages Sep 14, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 tst_netdevice: Add strict parameter to netdevice helper functions

The strict parameter controls whether errors reported through rtnetlink ack messages will terminate the test. All convenience macros for netdevice helper functions are automatically strict.

---

Changes since v1: New patch

 include/tst_netdevice.h | 100 +++++++++++++-------------
 lib/tst_netdevice.c     | 155 +++++++++++++++++++++-------------------
 2 files changed, 133 insertions(+), 122 deletions(-)
include/tst_netdevice.h +52 -48
index 95544814e..5e62ba065 100644
@@ -20,69 +20,70 @@ int tst_netdev_set_state(const char *file, const int lineno,
 	tst_netdev_set_state(__FILE__, __LINE__, (ifname), (up))
 
 /* Create a connected pair of virtual network devices */
-int tst_create_veth_pair(const char *file, const int lineno,
+int tst_create_veth_pair(const char *file, const int lineno, int strict,
 	const char *ifname1, const char *ifname2);
 #define CREATE_VETH_PAIR(ifname1, ifname2) \
-	tst_create_veth_pair(__FILE__, __LINE__, (ifname1), (ifname2))
+	tst_create_veth_pair(__FILE__, __LINE__, 1, (ifname1), (ifname2))
 
-int tst_netdev_add_device(const char *file, const int lineno,
+int tst_netdev_add_device(const char *file, const int lineno, int strict,
 	const char *ifname, const char *devtype);
 #define NETDEV_ADD_DEVICE(ifname, devtype) \
-	tst_netdev_add_device(__FILE__, __LINE__, (ifname), (devtype))
+	tst_netdev_add_device(__FILE__, __LINE__, 1, (ifname), (devtype))
 
-int tst_netdev_remove_device(const char *file, const int lineno,
+int tst_netdev_remove_device(const char *file, const int lineno, int strict,
 	const char *ifname);
 #define NETDEV_REMOVE_DEVICE(ifname) \
-	tst_netdev_remove_device(__FILE__, __LINE__, (ifname))
+	tst_netdev_remove_device(__FILE__, __LINE__, 1, (ifname))
 
-int tst_netdev_add_address(const char *file, const int lineno,
+int tst_netdev_add_address(const char *file, const int lineno, int strict,
 	const char *ifname, unsigned int family, const void *address,
 	unsigned int prefix, size_t addrlen, unsigned int flags);
 #define NETDEV_ADD_ADDRESS(ifname, family, address, prefix, addrlen, flags) \
-	tst_netdev_add_address(__FILE__, __LINE__, (ifname), (family), \
+	tst_netdev_add_address(__FILE__, __LINE__, 1, (ifname), (family), \
 		(address), (prefix), (addrlen), (flags))
 
-int tst_netdev_add_address_inet(const char *file, const int lineno,
+int tst_netdev_add_address_inet(const char *file, const int lineno, int strict,
 	const char *ifname, in_addr_t address, unsigned int prefix,
 	unsigned int flags);
 #define NETDEV_ADD_ADDRESS_INET(ifname, address, prefix, flags) \
-	tst_netdev_add_address_inet(__FILE__, __LINE__, (ifname), (address), \
-		(prefix), (flags))
+	tst_netdev_add_address_inet(__FILE__, __LINE__, 1, (ifname), \
+		(address), (prefix), (flags))
 
-int tst_netdev_remove_address(const char *file, const int lineno,
+int tst_netdev_remove_address(const char *file, const int lineno, int strict,
 	const char *ifname, unsigned int family, const void *address,
 	size_t addrlen);
 #define NETDEV_REMOVE_ADDRESS(ifname, family, address, addrlen) \
-	tst_netdev_remove_address(__FILE__, __LINE__, (ifname), (family), \
+	tst_netdev_remove_address(__FILE__, __LINE__, 1, (ifname), (family), \
 		(address), (addrlen))
 
 int tst_netdev_remove_address_inet(const char *file, const int lineno,
-	const char *ifname, in_addr_t address);
+	int strict, const char *ifname, in_addr_t address);
 #define NETDEV_REMOVE_ADDRESS_INET(ifname, address) \
-	tst_netdev_remove_address_inet(__FILE__, __LINE__, (ifname), (address))
+	tst_netdev_remove_address_inet(__FILE__, __LINE__, 1, (ifname), \
+		(address))
 
-int tst_netdev_change_ns_fd(const char *file, const int lineno,
+int tst_netdev_change_ns_fd(const char *file, const int lineno, int strict,
 	const char *ifname, int nsfd);
 #define NETDEV_CHANGE_NS_FD(ifname, nsfd) \
-	tst_netdev_change_ns_fd(__FILE__, __LINE__, (ifname), (nsfd))
+	tst_netdev_change_ns_fd(__FILE__, __LINE__, 1, (ifname), (nsfd))
 
-int tst_netdev_change_ns_pid(const char *file, const int lineno,
+int tst_netdev_change_ns_pid(const char *file, const int lineno, int strict,
 	const char *ifname, pid_t nspid);
 #define NETDEV_CHANGE_NS_PID(ifname, nspid) \
-	tst_netdev_change_ns_pid(__FILE__, __LINE__, (ifname), (nspid))
+	tst_netdev_change_ns_pid(__FILE__, __LINE__, 1, (ifname), (nspid))
 
 /*
  * Add new static entry to main routing table. If you specify gateway address,
  * the interface name is optional.
  */
-int tst_netdev_add_route(const char *file, const int lineno,
+int tst_netdev_add_route(const char *file, const int lineno, int strict,
 	const char *ifname, unsigned int family, const void *srcaddr,
 	unsigned int srcprefix, size_t srclen, const void *dstaddr,
 	unsigned int dstprefix, size_t dstlen, const void *gateway,
 	size_t gatewaylen);
 #define NETDEV_ADD_ROUTE(ifname, family, srcaddr, srcprefix, srclen, dstaddr, \
 	dstprefix, dstlen, gateway, gatewaylen) \
-	tst_netdev_add_route(__FILE__, __LINE__, (ifname), (family), \
+	tst_netdev_add_route(__FILE__, __LINE__, 1, (ifname), (family), \
 		(srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \
 		(dstlen), (gateway), (gatewaylen))
 
@@ -91,25 +92,25 @@ int tst_netdev_add_route(const char *file, const int lineno,
  * or dstprefix to 0, the corresponding address will be ignored. Interface
  * name is optional if gateway address is non-zero.
  */
-int tst_netdev_add_route_inet(const char *file, const int lineno,
+int tst_netdev_add_route_inet(const char *file, const int lineno, int strict,
 	const char *ifname, in_addr_t srcaddr, unsigned int srcprefix,
 	in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway);
 #define NETDEV_ADD_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, dstprefix, \
 	gateway) \
-	tst_netdev_add_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \
+	tst_netdev_add_route_inet(__FILE__, __LINE__, 1, (ifname), (srcaddr), \
 		(srcprefix), (dstaddr), (dstprefix), (gateway))
 
 /*
  * Remove static entry from main routing table.
  */
-int tst_netdev_remove_route(const char *file, const int lineno,
+int tst_netdev_remove_route(const char *file, const int lineno, int strict,
 	const char *ifname, unsigned int family, const void *srcaddr,
 	unsigned int srcprefix, size_t srclen, const void *dstaddr,
 	unsigned int dstprefix, size_t dstlen, const void *gateway,
 	size_t gatewaylen);
 #define NETDEV_REMOVE_ROUTE(ifname, family, srcaddr, srcprefix, srclen, \
 	dstaddr, dstprefix, dstlen, gateway, gatewaylen) \
-	tst_netdev_remove_route(__FILE__, __LINE__, (ifname), (family), \
+	tst_netdev_remove_route(__FILE__, __LINE__, 1, (ifname), (family), \
 		(srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \
 		(dstlen), (gateway), (gatewaylen))
 
@@ -117,32 +118,33 @@ int tst_netdev_remove_route(const char *file, const int lineno,
  * Simplified function for removing IPv4 static route.
  */
 int tst_netdev_remove_route_inet(const char *file, const int lineno,
-	const char *ifname, in_addr_t srcaddr, unsigned int srcprefix,
-	in_addr_t dstaddr, unsigned int dstprefix, in_addr_t gateway);
+	int strict, const char *ifname, in_addr_t srcaddr,
+	unsigned int srcprefix, in_addr_t dstaddr, unsigned int dstprefix,
+	in_addr_t gateway);
 #define NETDEV_REMOVE_ROUTE_INET(ifname, srcaddr, srcprefix, dstaddr, \
 	dstprefix, gateway) \
-	tst_netdev_remove_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \
-		(srcprefix), (dstaddr), (dstprefix), (gateway))
+	tst_netdev_remove_route_inet(__FILE__, __LINE__, 1, (ifname), \
+		(srcaddr), (srcprefix), (dstaddr), (dstprefix), (gateway))
 
 /*
  * Add queueing discipline. Network interface name is optional.
  */
-int tst_netdev_add_qdisc(const char *file, const int lineno,
+int tst_netdev_add_qdisc(const char *file, const int lineno, int strict,
 	const char *ifname, unsigned int family, unsigned int parent,
 	unsigned int handle, const char *qd_kind,
 	const struct tst_rtnl_attr_list *config);
 #define NETDEV_ADD_QDISC(ifname, family, parent, handle, qd_kind, config) \
-	tst_netdev_add_qdisc(__FILE__, __LINE__, (ifname), (family), \
+	tst_netdev_add_qdisc(__FILE__, __LINE__, 1, (ifname), (family), \
 		(parent), (handle), (qd_kind), (config))
 
 /*
  * Remove queueing discipline.
  */
-int tst_netdev_remove_qdisc(const char *file, const int lineno,
+int tst_netdev_remove_qdisc(const char *file, const int lineno, int strict,
 	const char *ifname, unsigned int family, unsigned int parent,
 	unsigned int handle, const char *qd_kind);
 #define NETDEV_REMOVE_QDISC(ifname, family, parent, handle, qd_kind) \
-	tst_netdev_remove_qdisc(__FILE__, __LINE__, (ifname), (family), \
+	tst_netdev_remove_qdisc(__FILE__, __LINE__, 1, (ifname), (family), \
 		(parent), (handle), (qd_kind))
 
 /*
@@ -150,17 +152,18 @@ int tst_netdev_remove_qdisc(const char *file, const int lineno,
  * optional.
  */
 int tst_netdev_add_traffic_class(const char *file, const int lineno,
-	const char *ifname, unsigned int parent, unsigned int handle,
-	const char *qd_kind, const struct tst_rtnl_attr_list *config);
+	int strict, const char *ifname, unsigned int parent,
+	unsigned int handle, const char *qd_kind,
+	const struct tst_rtnl_attr_list *config);
 #define NETDEV_ADD_TRAFFIC_CLASS(ifname, parent, handle, qd_kind, config) \
-	tst_netdev_add_traffic_class(__FILE__, __LINE__, (ifname), (parent), \
-		(handle), (qd_kind), (config))
+	tst_netdev_add_traffic_class(__FILE__, __LINE__, 1, (ifname), \
+		(parent), (handle), (qd_kind), (config))
 
 int tst_netdev_remove_traffic_class(const char *file, const int lineno,
-	const char *ifname, unsigned int parent, unsigned int handle,
-	const char *qd_kind);
+	int strict, const char *ifname, unsigned int parent,
+	unsigned int handle, const char *qd_kind);
 #define NETDEV_REMOVE_TRAFFIC_CLASS(ifname, parent, handle, qd_kind) \
-	tst_netdev_remove_traffic_class(__FILE__, __LINE__, (ifname), \
+	tst_netdev_remove_traffic_class(__FILE__, __LINE__, 1, (ifname), \
 		(parent), (handle), (qd_kind))
 
 /*
@@ -168,20 +171,21 @@ int tst_netdev_remove_traffic_class(const char *file, const int lineno,
  * constant in host byte order. Network interface name is optional.
  */
 int tst_netdev_add_traffic_filter(const char *file, const int lineno,
-	const char *ifname, unsigned int parent, unsigned int handle,
-	unsigned int protocol, unsigned int priority, const char *f_kind,
-	const struct tst_rtnl_attr_list *config);
+	int strict, const char *ifname, unsigned int parent,
+	unsigned int handle, unsigned int protocol, unsigned int priority,
+	const char *f_kind, const struct tst_rtnl_attr_list *config);
 #define NETDEV_ADD_TRAFFIC_FILTER(ifname, parent, handle, protocol, priority, \
 	f_kind, config) \
-	tst_netdev_add_traffic_filter(__FILE__, __LINE__, (ifname), (parent), \
-		(handle), (protocol), (priority), (f_kind), (config))
+	tst_netdev_add_traffic_filter(__FILE__, __LINE__, 1, (ifname), \
+		(parent), (handle), (protocol), (priority), (f_kind), (config))
 
 int tst_netdev_remove_traffic_filter(const char *file, const int lineno,
-	const char *ifname, unsigned int parent, unsigned int handle,
-	unsigned int protocol, unsigned int priority, const char *f_kind);
+	int strict, const char *ifname, unsigned int parent,
+	unsigned int handle, unsigned int protocol, unsigned int priority,
+	const char *f_kind);
 #define NETDEV_REMOVE_TRAFFIC_FILTER(ifname, parent, handle, protocol, \
 	priority, f_kind) \
-	tst_netdev_remove_traffic_filter(__FILE__, __LINE__, (ifname), \
+	tst_netdev_remove_traffic_filter(__FILE__, __LINE__, 1, (ifname), \
 		(parent), (handle), (protocol), (priority), (f_kind))
 
 #endif /* TST_NETDEVICE_H */
lib/tst_netdevice.c +81 -74

337 lines not on this page · raw message

Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v2 2/2 tcindex01: Simplify test

The bug can be reproduced simply by adding, removing and adding again the same tcindex traffic filter. If the bug is present, the attempt to add the removed filter again will fail with EEXIST error.

---

Changes since v1: Call tst_netdev_add_traffic_filter() in non-strict mode and report meaningful results according to return/errno value.

 testcases/cve/tcindex01.c | 45 +++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 25 deletions(-)
testcases/cve/tcindex01.c +20 -25
index 9bd7de9dd..91bfafb53 100644
@@ -84,31 +84,26 @@ static void setup(void)
 
 static void run(void)
 {
-	unsigned int i;
-
-	for (i = 0; i < 100; i++) {
-		NETDEV_ADD_QDISC(DEVNAME, AF_UNSPEC, TC_H_ROOT, qd_handle,
-			"htb", qd_config);
-		NETDEV_ADD_TRAFFIC_CLASS(DEVNAME, qd_handle, clsid, "htb",
-			cls_config);
-		NETDEV_ADD_TRAFFIC_FILTER(DEVNAME, qd_handle, 10, ETH_P_IP, 1,
-			"tcindex", f_config);
-		NETDEV_REMOVE_TRAFFIC_FILTER(DEVNAME, qd_handle, 10, ETH_P_IP,
-			1, "tcindex");
-
-		/* Wait at least one jiffy for use-after-free */
-		usleep(10000);
-
-		NETDEV_REMOVE_QDISC(DEVNAME, AF_UNSPEC, TC_H_ROOT, qd_handle,
-			"htb");
-	}
-
-	if (tst_taint_check()) {
-		tst_res(TFAIL, "Kernel is vulnerable");
-		return;
-	}
-
-	tst_res(TPASS, "Nothing bad happened (yet)");
+	int ret;
+
+	NETDEV_ADD_QDISC(DEVNAME, AF_UNSPEC, TC_H_ROOT, qd_handle, "htb",
+		qd_config);
+	NETDEV_ADD_TRAFFIC_CLASS(DEVNAME, qd_handle, clsid, "htb", cls_config);
+	NETDEV_ADD_TRAFFIC_FILTER(DEVNAME, qd_handle, 10, ETH_P_IP, 1,
+		"tcindex", f_config);
+	NETDEV_REMOVE_TRAFFIC_FILTER(DEVNAME, qd_handle, 10, ETH_P_IP,
+		1, "tcindex");
+	ret = tst_netdev_add_traffic_filter(__FILE__, __LINE__, 0, DEVNAME,
+		qd_handle, 10, ETH_P_IP, 1, "tcindex", f_config);
+	TST_ERR = tst_rtnl_errno;
+	NETDEV_REMOVE_QDISC(DEVNAME, AF_UNSPEC, TC_H_ROOT, qd_handle, "htb");
+
+	if (ret)
+		tst_res(TPASS, "Removing tcindex filter works correctly");
+	else if (TST_ERR == EEXIST)
+		tst_res(TFAIL, "Kernel traffic filter list is corrupted");
+	else
+		tst_brk(TBROK | TTERRNO, "Unexpected rtnetlink error");
 }
 
 static void cleanup(void)
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH v2 2/2] tcindex01: Simplify test

Hi!
Patchset pushed, thanks.

Signature · 5 lines
Cyril Hrubis
[email protected]

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