Skip to contentSkip to conversation

Revert "udata: Store u32 udata values in Big Endian"

PATCH In review Phil Sutter 1 participant 3 messages Jan 29 – Feb 3 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/2 Revert "udata: Store u32 udata values in Big Endian"

This reverts commit f20dfa7824860a9ac14425a3f7ca970a6c981597.

This change to payload (interpretation) is problematic with package updates at run-time: The new version might trip over userdata in the running ruleset, avoid this.

---
 src/udata.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
src/udata.c +2 -5
index 8cf4e7ca61e2f..a1956571ef5fd 100644
@@ -8,7 +8,6 @@
 #include <udata.h>
 #include <utils.h>
 
-#include <arpa/inet.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -101,9 +100,7 @@ EXPORT_SYMBOL(nftnl_udata_put_u32);
 bool nftnl_udata_put_u32(struct nftnl_udata_buf *buf, uint8_t type,
 			 uint32_t data)
 {
-	uint32_t data_be = htonl(data);
-
-	return nftnl_udata_put(buf, type, sizeof(data_be), &data_be);
+	return nftnl_udata_put(buf, type, sizeof(data), &data);
 }
 
 EXPORT_SYMBOL(nftnl_udata_type);
@@ -131,7 +128,7 @@ uint32_t nftnl_udata_get_u32(const struct nftnl_udata *attr)
 
 	memcpy(&data, attr->value, sizeof(data));
 
-	return ntohl(data);
+	return data;
 }
 
 EXPORT_SYMBOL(nftnl_udata_next);
Signature · 1 line
2.51.0
author reviewer
#1 lore raw

PATCH 2/2 src: Do not include userdata content in debug output

This storage in rules and set elements is opaque by design, neither libnftnl nor kernel should deal with its content. Yet nftables enters data in host byte order which will lead to changing output depending on host's byte order. Avoid this problem for test suites checking the debug output by merely printing the number and sum of all the bytes in the buffer. This likely detects changes in userdata but deliberately ignores data reordering.

---
 include/utils.h | 10 ++++++++++
 src/rule.c      | 19 ++++---------------
 src/set_elem.c  | 18 ++++--------------
 3 files changed, 18 insertions(+), 29 deletions(-)
include/utils.h +10 -0
index 10492893e3a16..5dcd287f491c4 100644
@@ -91,4 +91,14 @@ char *nftnl_attr_get_ifname(const struct nlattr *attr);
 int nftnl_parse_str_attr(const struct nlattr *tb, int attr,
 			 const char **field, uint32_t *flags);
 
+static inline uint32_t bytesum(uint8_t *buf, size_t buflen)
+{
+	uint32_t ret = 0;
+
+	while (buflen--)
+		ret += buf[buflen];
+
+	return ret;
+}
+
 #endif
src/rule.c +4 -15
index cd3041e5a399a..f8109969a9183 100644
@@ -509,8 +509,8 @@ static int nftnl_rule_snprintf_default(char *buf, size_t remain,
 				       uint32_t type, uint32_t flags)
 {
 	struct nftnl_expr *expr;
-	int ret, offset = 0, i;
 	const char *sep = "";
+	int ret, offset = 0;
 
 	if (r->flags & (1 << NFTNL_RULE_FAMILY)) {
 		ret = snprintf(buf + offset, remain, "%s%s", sep,
@@ -573,21 +573,10 @@ static int nftnl_rule_snprintf_default(char *buf, size_t remain,
 	}
 
 	if (r->user.len) {
-		ret = snprintf(buf + offset, remain, "\n  userdata = { ");
-		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-
-		for (i = 0; i < r->user.len; i++) {
-			char *c = r->user.data;
-
-			ret = snprintf(buf + offset, remain,
-				       isprint(c[i]) ? "%c" : "\\x%02hhx",
-				       c[i]);
-			SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-		}
-
-		ret = snprintf(buf + offset, remain, " }");
+		ret = snprintf(buf + offset, remain,
+			       "\n  userdata len %d sum 0x%x",
+			       r->user.len, bytesum(r->user.data, r->user.len));
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-
 	}
 
 	return offset;
src/set_elem.c +4 -14
index d22643c44dd71..3e0ab0cf50876 100644
@@ -705,7 +705,7 @@ int nftnl_set_elem_parse_file(struct nftnl_set_elem *e, enum nftnl_parse_type ty
 int nftnl_set_elem_snprintf_default(char *buf, size_t remain,
 				    const struct nftnl_set_elem *e)
 {
-	int ret, dregtype = DATA_NONE, offset = 0, i;
+	int ret, dregtype = DATA_NONE, offset = 0;
 
 	ret = snprintf(buf, remain, "element ");
 	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
@@ -748,19 +748,9 @@ int nftnl_set_elem_snprintf_default(char *buf, size_t remain,
 	}
 
 	if (e->user.len) {
-		ret = snprintf(buf + offset, remain, "  userdata = { ");
-		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-
-		for (i = 0; i < e->user.len; i++) {
-			char *c = e->user.data;
-
-			ret = snprintf(buf + offset, remain,
-				       isprint(c[i]) ? "%c" : "\\x%02hhx",
-				       c[i]);
-			SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-		}
-
-		ret = snprintf(buf + offset, remain, " }");
+		ret = snprintf(buf + offset, remain,
+			       "  userdata len %d sum 0x%x",
+			       e->user.len, bytesum(e->user.data, e->user.len));
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 	}
 
Signature · 1 line
2.51.0
author reviewer
#2 lore raw
In reply to Phil Sutter
Quoted 7 lines · Phil Sutter

This reverts commit f20dfa7824860a9ac14425a3f7ca970a6c981597.

This change to payload (interpretation) is problematic with package updates at run-time: The new version might trip over userdata in the running ruleset, avoid this.

Series applied.