eBPF-based security platform
proofs: tie the value btf_integer() reads to the integer's bits
Daniel Borkmann291b482194bf
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex e05be211..d03b88ff 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -677,8 +677,9 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.* - ``upatch-btf-int``- ``btf_integer()``- Every encoding and every value of up to 8 bytes- - The bits stay inside the integer's bytes; the value is zero- or- sign-extended from them+ - The bits stay inside the integer's bytes; each bit of the value+ is the integer's bit at its place, least significant first, then+ zero- or sign-extended* - ``upatch-writer-header``- ``pack_header()`` of :src:`tools/shirudo-build/package.c`, readback by ``parse_header()``diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex e6ad0d5f..cc6c3f25 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -25,7 +25,7 @@ upatch-prog-kprobes --refine-arithmetic --max-field-sensitivity-array-size 210 -upatch-loader --unwind 50 LEN=4,5,25,49upatch-rodata-btf --refine-arithmetic --object-bits 10 --unwind 16 LEN=4,10,15upatch-btf --unwind 6 --unwindset btf_decl_printable.0:33,btf_name_printable.0:7,btf_unwrap.0:9-upatch-btf-int --unwind 2 --unwindset btf_integer.0:65+upatch-btf-int --unwind 2 --unwindset btf_integer.0:65,value_of_bits.0:65upatch-rodata --refine-arithmetic --object-bits 12 --max-field-sensitivity-array-size 32 --unwind 25 --unwindset memcpy.0:49,strlen.0:18,strcmp.0:18,strnlen.0:18 LEN=6,8,24string-number-model --unwind 6measured-fold-model --unwind 7diff --git a/testing/proofs/cbmc/upatch-btf-int.c b/testing/proofs/cbmc/upatch-btf-int.cindex dbb3c37f..299ba93e 100644--- a/testing/proofs/cbmc/upatch-btf-int.c+++ b/testing/proofs/cbmc/upatch-btf-int.c@@ -4,12 +4,33 @@** The integers util/upatch_rodata.c reads from the .rodata of a .upatch by* their BTF encoding, for every encoding and value of up to 8 bytes: it- * reads only the integer's bytes and returns a value of its bits.+ * reads only the integer's bytes, and the value it returns is the+ * integer's bits, least significant first, zero- or sign-extended.*/#include "proof.h"#include "../../../util/upatch_rodata.c"+/* Bit b of the bytes, counted from the least significant of the first. */+static bool bit_at(const __u8 *bytes, __u32 b)+{+ return bytes[b / 8] >> (b % 8) & 1;+}++static void value_of_bits(__u64 value, const __u8 *bytes, __u32 off,+ __u32 bits, bool is_signed)+{+ __u32 i;++ for (i = 0; i < 64; i++) {+ bool want = i < bits ? bit_at(bytes, off + i) :+ is_signed && bit_at(bytes, off + bits - 1);++ __CPROVER_assert((value >> i & 1) == want,+ "each bit of the value from its place");+ }+}+void harness(void){struct {@@ -29,13 +50,5 @@ void harness(void)__CPROVER_assert(it.t.size && it.t.size <= size && bits &&BTF_INT_OFFSET(it.enc) + bits <= it.t.size * 8,"bits inside the integer's bytes");- if (bits == 64)- return;- if (!is_signed)- __CPROVER_assert(!(value >> bits),- "an unsigned value of its bits");- else- __CPROVER_assert(!(value >> (bits - 1)) ||- !~(value | ((1ULL << (bits - 1)) - 1)),- "a signed value of its bits");+ value_of_bits(value, bytes, BTF_INT_OFFSET(it.enc), bits, is_signed);}