Skip to content

eBPF-based security platform

proofs: prove the netlink attribute parsers of util/xdp.c

Daniel Borkmann12d749ca41d7
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index a93621d7..aa05d634 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -1166,6 +1166,15 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
most 255 of them; of a key's lines the last; the root
``roothash=`` names, else ``usrhash=``, in lowercase when it is
64 hex digits, else none
+ * - ``xdp-attrs``
+ - ``attr_find()``, ``attr_string()``, ``attr_number()``,
+ ``xdp_device_parse()`` of :src:`util/xdp.c`
+ - Netlink replies of 0 to 12, 16, 20, 24, 28, 32 and 40 bytes
+ - An attribute exactly in a run of whole attributes that names its
+ type at most once, found where it lies; a string NUL-terminated
+ within its room; a number only of its exact size; a device with
+ an index, a name, terminated strings, and the bus and device of a
+ devlink port
Lean 4 proofs
-------------
@@ -1531,7 +1540,7 @@ a proof covers.
:src:`util/keyring.c`, :src:`util/preflight.c`, :src:`util/seal.c`,
:src:`util/tamper.c`, :src:`util/upatch_maps.c`,
:src:`util/upatch_pins.c`, :src:`util/upatch_resolve.c`,
- :src:`util/upatch_verify.c`, :src:`util/xdp.c`
+ :src:`util/upatch_verify.c`
- Mostly calls into the kernel, which CBMC has no body for: files,
extended attributes, mounts, the device mapper, keyrings,
``bpf()``, netlink, ``/proc`` and the process's own limits
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index a0246518..6c4433b2 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -152,3 +152,4 @@ bpm-tree-state-match --unwind 4 --unwindset read_config_stream.0:7,read_config_s
bpm-tree-state-options --unwind 4
keepalive-pack --unwind 9 --unwindset strcmp.0:18,keepalive_mask.0:514,mask.0:514,mask.1:514 LEN=0-9,512,513
sysinfo-image --unwind 8 --unwindset strcmp.0:8,memcmp.0:10,os_release_unquote.0:258,cmdline_image_root.0:8,cmdline_image_root.1:76,cmdline_image_root.2:5,strlen.0:11,all_hex.0:65,image_root_normalise.0:65,command_line.0:65,command_line.1:65 LEN=0-6
+xdp-attrs --unwind 12 --unwindset memchr.0:129 LEN=0-12,16,20,24,28,32,40
diff --git a/testing/proofs/cbmc/xdp-attrs.c b/testing/proofs/cbmc/xdp-attrs.c
new file mode 100644
index 00000000..bfcdc950
--- /dev/null
+++ b/testing/proofs/cbmc/xdp-attrs.c
@@ -0,0 +1,140 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The netlink attribute parsers of util/xdp.c, which read the kernel's
+ * replies about network devices, on every buffer of LEN bytes: an
+ * attribute found lies inside the buffer with its header, a string is
+ * NUL-terminated within its room and a number of its exact size, and a
+ * device parsed has an index, a name, terminated strings and, with a
+ * devlink port, its bus and device.
+ */
+
+#include "proof.h"
+/* NLA_HDRLEN casts the negative ~(NLA_ALIGNTO - 1) on purpose. */
+#pragma CPROVER check push
+#pragma CPROVER check disable "conversion"
+#include "../../../util/xdp.c"
+#pragma CPROVER check pop
+
+#define ATTR_HEADER sizeof(struct nlattr)
+
+static size_t aligned(size_t n)
+{
+ return (n + 3) / 4 * 4;
+}
+
+/* The attributes a buffer of LEN bytes holds, each taking its aligned length. */
+static bool attributes_end(const __u8 *data, size_t len, __u16 type,
+ size_t *found)
+{
+ size_t off = 0, seen = 0;
+
+ while (off < len) {
+ const struct nlattr *attr = (const void *)(data + off);
+
+ if (len - off < ATTR_HEADER || attr->nla_len < ATTR_HEADER ||
+ attr->nla_len > len - off ||
+ aligned(attr->nla_len) > len - off)
+ return false;
+ if ((attr->nla_type & ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)) ==
+ type) {
+ *found = off;
+ seen++;
+ }
+ off += aligned(attr->nla_len);
+ }
+ return seen <= 1;
+}
+
+static void find(const __u8 *data)
+{
+ const struct nlattr *attr;
+ size_t found = LEN;
+ bool well_formed;
+ __u16 type;
+ int ret;
+
+ ret = attr_find(data, LEN, type, &attr);
+ well_formed = attributes_end(data, LEN, type, &found);
+ __CPROVER_assert(!ret == well_formed,
+ "a run of whole attributes, the type at most once");
+ if (ret)
+ return;
+ __CPROVER_assert(attr ? (const __u8 *)attr == data + found :
+ found == LEN,
+ "the attribute of the type, or none");
+}
+
+static void string(const __u8 *data)
+{
+ char out[8];
+ size_t cap, i;
+ __u16 type;
+
+ __CPROVER_assume(cap <= sizeof(out));
+ for (i = 0; i < sizeof(out); i++)
+ out[i] = 'x';
+ if (attr_string(data, LEN, type, out, cap))
+ return;
+ for (i = 0; i < cap && out[i]; i++)
+ ;
+ __CPROVER_assert(i < cap || out[0] == 'x',
+ "a string terminated within its room, or none");
+}
+
+static void number(const __u8 *data)
+{
+ const struct nlattr *attr;
+ bool present = false;
+ __u64 value = 0;
+ size_t size;
+ __u16 type;
+
+ __CPROVER_assume(size == 2 || size == 4 || size == 8);
+ if (attr_number(data, LEN, type, &value, size, &present))
+ return;
+ if (attr_find(data, LEN, type, &attr) || !attr) {
+ __CPROVER_assert(!present && !value, "nothing without one");
+ return;
+ }
+ __CPROVER_assert(present && attr->nla_len == ATTR_HEADER + size &&
+ !memcmp(&value, (const __u8 *)attr + ATTR_HEADER,
+ size),
+ "a number of exactly its size");
+}
+
+static bool terminated(const char *s, size_t cap)
+{
+ return memchr(s, '\0', cap);
+}
+
+static void device(const __u8 *data)
+{
+ struct xdp_device dev;
+
+ if (xdp_device_parse(data, LEN, &dev))
+ return;
+ __CPROVER_assert(dev.ifindex > 0 && dev.name[0] &&
+ terminated(dev.name, sizeof(dev.name)),
+ "an index and a name");
+ __CPROVER_assert(terminated(dev.kind, sizeof(dev.kind)) &&
+ terminated(dev.parent_bus, sizeof(dev.parent_bus)) &&
+ terminated(dev.devlink_bus, sizeof(dev.devlink_bus)) &&
+ terminated(dev.devlink_device,
+ sizeof(dev.devlink_device)),
+ "terminated strings");
+ __CPROVER_assert(!dev.devlink_port ||
+ (dev.devlink_bus[0] && dev.devlink_device[0]),
+ "a devlink port with its bus and device");
+}
+
+void harness(void)
+{
+ const __u8 *data = any_bytes(LEN);
+
+ find(data);
+ string(data);
+ number(data);
+ device(data);
+}