Skip to contentSkip to conversation

Improve handling of errors from mnl* functions"

PATCH Applied Eugene Crosser 2 participants 7 messages Dec 9, 2021 – Jan 27, 2022 netfilter

lore
Share & tools
lore
#0 lore raw

v2 cover Improve handling of errors from mnl* functions"

Version 2 of the patchset

Libnftables does not handle errors that libmnl functions may return properly:

1. Retriable errors indicated by errno=EINTR are not retried, but rather treted as fatal.
2. Instead of reporting the error to the caller, functions call exit() on error, which terminates the caller process.

This patch set partly addresses the second point, by calling abort() instead of exit() on ABI error, that will at least give more information to the sysadmin than quiet termination of a process.

It attempts to properly address the first point, by introducing retry logic when mnl_socket_recvfrom() or mnl_cb_run() return -1 with errno=EINTR.

It would be desirable to fully address the second point at some future time, though it requires some redesign of the code structure.

[PATCH nft v2 1/2] Use abort() in case of netlink_abi_error [PATCH nft v2 2/2] Handle retriable errors from mnl functions

#1 lore raw

PATCH v2 1/2 Use abort() in case of netlink_abi_error

Library functions should not use exit(), application that uses the library may contain error handling path, that cannot be executed if library functions calls exit(). For truly fatal errors, using abort() is more acceptable than exit().

---
 src/netlink.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/netlink.c +1 -1
index 359d801c..c25a7e79 100644
@@ -59,7 +59,7 @@ void __noreturn __netlink_abi_error(const char *file, int line,
 {
 	fprintf(stderr, "E: Contact urgently your Linux kernel vendor. "
 		"Netlink ABI is broken: %s:%d %s\n", file, line, reason);
-	exit(NFT_EXIT_FAILURE);
+	abort();
 }
 
 int netlink_io_error(struct netlink_ctx *ctx, const struct location *loc,
Signature · 1 line
2.32.0
#3 lore raw

PATCH v2 2/2 Handle retriable errors from mnl functions

In reply to Eugene Crosser

rc == -1 and errno == EINTR mean:

mnl_socket_recvfrom() - blindly rerun the function
mnl_cb_run() - restart dump request from scratch

This commit introduces handling of both these conditions

---
 src/iface.c | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)
src/iface.c +24 -13
index d0e1834c..5ecc087d 100644
@@ -59,14 +59,14 @@ static int data_cb(const struct nlmsghdr *nlh, void *data)
 	return MNL_CB_OK;
 }
 
-void iface_cache_update(void)
+static int __iface_cache_update(void)
 {
 	char buf[MNL_SOCKET_BUFFER_SIZE];
 	struct mnl_socket *nl;
 	struct nlmsghdr *nlh;
 	struct rtgenmsg *rt;
 	uint32_t seq, portid;
-	int ret;
+	int ret = -1;
 
 	nlh = mnl_nlmsg_put_header(buf);
 	nlh->nlmsg_type	= RTM_GETLINK;
@@ -77,28 +77,39 @@ void iface_cache_update(void)
 
 	nl = mnl_socket_open(NETLINK_ROUTE);
 	if (nl == NULL)
-		netlink_init_error();
+		return -1;
 
 	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
-		netlink_init_error();
+		goto close_and_return;
 
 	portid = mnl_socket_get_portid(nl);
 
 	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0)
-		netlink_init_error();
+		goto close_and_return;
 
-	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	while (ret > 0) {
-		ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
-		if (ret <= MNL_CB_STOP)
+	do {
+		do ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+		while (ret == -1 && errno == EINTR);
+		if (ret == -1)
 			break;
-		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	}
-	if (ret == -1)
-		netlink_init_error();
+		ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
+	} while (ret > MNL_CB_STOP);
 
+close_and_return:
 	mnl_socket_close(nl);
 
+	return ret;
+}
+
+void iface_cache_update(void)
+{
+	int ret;
+
+	do ret = __iface_cache_update();
+	while (ret == -1 && errno == EINTR);
+	if (ret == -1)
+		netlink_init_error();
+
 	iface_cache_init = true;
 }
 
Signature · 1 line
2.32.0
maintainer Applied
#2 lore raw

Re: [PATCH nft v2 1/2] Use abort() in case of netlink_abi_error

In reply to Eugene Crosser
Quoted 4 lines · Eugene Crosser

Library functions should not use exit(), application that uses the library may contain error handling path, that cannot be executed if library functions calls exit(). For truly fatal errors, using abort() is more acceptable than exit().

Applied, thanks.

#4 lore raw

Re: [PATCH nft v2 2/2] Handle retriable errors from mnl functions

In reply to Eugene Crosser
Quoted 6 lines · Eugene Crosser

rc == -1 and errno == EINTR mean:

mnl_socket_recvfrom() - blindly rerun the function
mnl_cb_run() - restart dump request from scratch

This commit introduces handling of both these conditions

Sorry it took me a while to come back to this.

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

This follows the same approach as src/mnl.c, no need to close the reopen the socket to drop the existing messages.

#5 lore raw

Re: [PATCH nft v2 2/2] Handle retriable errors from mnl functions

Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines · Eugene Crosser

rc == -1 and errno == EINTR mean:

mnl_socket_recvfrom() - blindly rerun the function
mnl_cb_run() - restart dump request from scratch

Quoted 8 lines
Quoted 1 line

This commit introduces handling of both these conditions

Sorry it took me a while to come back to this.

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

This follows the same approach as src/mnl.c, no need to close the reopen the socket to drop the existing messages.

Thanks for getting back to it and producing the fix!

I think it is slightly less clean, because if some (not EINTR) error happens while it is draining the queue (the second call to mnl_socket_recvfrom() with eintr == true), errno will be overwritten and the error misrepresented. This should not be a _practical_ problem because presumably the same error will be raised upon retry, and this time it will be reported correctly.

Thanks again,

Eugene

#6 lore raw

Re: [PATCH nft v2 2/2] Handle retriable errors from mnl functions

Quoted 16 lines · Eugene Crosser
Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines · Eugene Crosser

rc == -1 and errno == EINTR mean:

mnl_socket_recvfrom() - blindly rerun the function
mnl_cb_run() - restart dump request from scratch

Quoted 8 lines
Quoted 1 line

This commit introduces handling of both these conditions

Sorry it took me a while to come back to this.

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

This follows the same approach as src/mnl.c, no need to close the reopen the socket to drop the existing messages.

Quoted 7 lines

Thanks for getting back to it and producing the fix!

I think it is slightly less clean, because if some (not EINTR) error happens while it is draining the queue (the second call to mnl_socket_recvfrom() with eintr == true), errno will be overwritten and the error misrepresented. This should not be a _practical_ problem because presumably the same error will be raised upon retry, and this time it will be reported correctly.

Good catch, sending v2.