]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
netlink: don't crash if prefix for < byte is requested
authorFlorian Westphal <fw@strlen.de>
Thu, 14 Dec 2023 14:39:27 +0000 (15:39 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 22 Jan 2025 23:41:53 +0000 (00:41 +0100)
commit 0404ff08b3c18052e6689d75fa85275d3cef7e8e upstream.

If prefix is used with a datatype that has less than 8 bits an
assertion is triggered:

src/netlink.c:243: netlink_gen_raw_data: Assertion `len > 0' failed.

This is esoteric, the alternative would be to restrict prefixes
to ipv4/ipv6 addresses.

Simpler fix is to use round_up instead of divide.

Signed-off-by: Florian Westphal <fw@strlen.de>
src/netlink_linearize.c
tests/py/ip/ip.t
tests/py/ip/ip.t.json
tests/py/ip/ip.t.payload
tests/py/ip/ip.t.payload.bridge
tests/py/ip/ip.t.payload.inet
tests/py/ip/ip.t.payload.netdev

index 6b965a8dd06e1bdb28802ab72f1cf8f45d8e55f6..c2cb4f7324987f1eb80012cf1d63282905a74d39 100644 (file)
@@ -391,7 +391,8 @@ static struct expr *netlink_gen_prefix(struct netlink_linearize_ctx *ctx,
        mpz_init(mask);
        mpz_prefixmask(mask, expr->right->len, expr->right->prefix_len);
        netlink_gen_raw_data(mask, expr->right->byteorder,
-                            expr->right->len / BITS_PER_BYTE, &nld);
+                            div_round_up(expr->right->len, BITS_PER_BYTE),
+                            &nld);
        mpz_clear(mask);
 
        zero.len = nld.len;
index 1338a909ee86f35e89d8fc1abef1018c0e4f5381..c962d4b3fc60f43dacb866595eb6c02100cd092b 100644 (file)
@@ -130,3 +130,5 @@ ip saddr . ip daddr vmap { 192.168.5.1-192.168.5.128 . 192.168.6.1-192.168.6.128
 
 ip saddr 1.2.3.4 ip daddr 3.4.5.6;ok
 ip saddr 1.2.3.4 counter ip daddr 3.4.5.6;ok
+
+ip dscp 1/6;ok;ip dscp & 0x3f == lephb
index f019655ed32474196dd81a22918b8549fc871ecf..617cd50639da77b607d5065a9606c0aa24ef47ef 100644 (file)
         }
     }
 ]
+
+# ip dscp 1/6
+[
+    {
+        "match": {
+            "left": {
+                "&": [
+                    {
+                        "payload": {
+                            "field": "dscp",
+                            "protocol": "ip"
+                        }
+                    },
+                    63
+                ]
+            },
+            "op": "==",
+            "right": "lephb"
+        }
+    }
+]
index 4d34f989847abaa3f1f75f358f46f6e4592b59d8..2c150cbadd60d947a4bfb7bfb35ab804e0e19812 100644 (file)
@@ -538,3 +538,11 @@ ip test-ip4 input
   [ counter pkts 0 bytes 0 ]
   [ payload load 4b @ network header + 16 => reg 1 ]
   [ cmp eq reg 1 0x06050403 ]
+
+# ip dscp 1/6
+ip test-ip4 input
+  [ payload load 1b @ network header + 1 => reg 1 ]
+  [ bitwise reg 1 = ( reg 1 & 0x000000fc ) ^ 0x00000000 ]
+  [ bitwise reg 1 = ( reg 1 >> 0x00000002 ) ]
+  [ bitwise reg 1 = ( reg 1 & 0x0000003f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000001 ]
index e8dd3c2fcaeb6e6653039e65ba9657346bd24d4e..e3e0f1d0623df3ed2722d8e8a0464bb4c7630425 100644 (file)
@@ -702,3 +702,13 @@ bridge test-bridge input
   [ counter pkts 0 bytes 0 ]
   [ payload load 4b @ network header + 16 => reg 1 ]
   [ cmp eq reg 1 0x06050403 ]
+
+# ip dscp 1/6
+bridge test-bridge input
+  [ meta load protocol => reg 1 ]
+  [ cmp eq reg 1 0x00000008 ]
+  [ payload load 1b @ network header + 1 => reg 1 ]
+  [ bitwise reg 1 = ( reg 1 & 0x000000fc ) ^ 0x00000000 ]
+  [ bitwise reg 1 = ( reg 1 >> 0x00000002 ) ]
+  [ bitwise reg 1 = ( reg 1 & 0x0000003f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000001 ]
index adcfe124d18d42bfa14daff58dfda9ad62795dde..754e37fff9d36361f1d06238cd953313093fb053 100644 (file)
@@ -702,3 +702,13 @@ inet test-inet input
   [ counter pkts 0 bytes 0 ]
   [ payload load 4b @ network header + 16 => reg 1 ]
   [ cmp eq reg 1 0x06050403 ]
+
+# ip dscp 1/6
+inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x00000002 ]
+  [ payload load 1b @ network header + 1 => reg 1 ]
+  [ bitwise reg 1 = ( reg 1 & 0x000000fc ) ^ 0x00000000 ]
+  [ bitwise reg 1 = ( reg 1 >> 0x00000002 ) ]
+  [ bitwise reg 1 = ( reg 1 & 0x0000003f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000001 ]
index c6652a3d066e456cb0f74e2112b93c84edbf308e..b72233548cf2fa61c20e83c9990a30d3465f9604 100644 (file)
@@ -702,3 +702,13 @@ netdev test-netdev ingress
   [ counter pkts 0 bytes 0 ]
   [ payload load 4b @ network header + 16 => reg 1 ]
   [ cmp eq reg 1 0x06050403 ]
+
+# ip dscp 1/6
+netdev test-netdev ingress
+  [ meta load protocol => reg 1 ]
+  [ cmp eq reg 1 0x00000008 ]
+  [ payload load 1b @ network header + 1 => reg 1 ]
+  [ bitwise reg 1 = ( reg 1 & 0x000000fc ) ^ 0x00000000 ]
+  [ bitwise reg 1 = ( reg 1 >> 0x00000002 ) ]
+  [ bitwise reg 1 = ( reg 1 & 0x0000003f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000001 ]