Browse zlib specs (14 of 62)

Proofs / zlib / Rejecting bad input

rj_bad_dictionary

A zlib stream whose header asks for a dictionary makes inflate return Z_NEED_DICT, and a dictionary whose checksum is not the one the header names is refused.

spec
42 lines of C
with its headers
82 lines
status
Proved
14 of 62
On this page

The spec

The spec is the C program harness/rj_bad_dictionary.c, 42 lines, shown whole as it is in the repository at 37189f4. It includes harness/laws_common.h (40 lines). Its first comment states the property in full, with every bound.

C
// Law rj_bad_dictionary: For a zlib stream that starts with any header asking for a dictionary (FDICT set, any DICTID,
// any CINFO the windowBits 8..15 allow, any FLEVEL), followed by any bytes (any length the allocator provides): once
// inflate() offered the stream (at most 2^32 - 1 bytes of it), with any output space and any flush, has returned
// Z_NEED_DICT, inflateSetDictionary with any bytes (up to 2^32 - 1, as many as it takes) whose Adler-32 is not DICTID
// returns Z_DATA_ERROR.
#include "laws_common.h"

int main(void) {
	int window_bits = 8 + (int) upto(7);
	size_t stream_len = 6 + upto(PTRDIFF_MAX - 6);
	size_t room = nondet_u32();
	size_t dict_len = nondet_u32();
	unsigned char *stream = alloc_any_bytes(stream_len);
	unsigned char *out = malloc(room);
	unsigned char *dict = alloc_any_bytes(dict_len);
	if (stream == NULL || out == NULL || dict == NULL)
		return 0;
	// RFC 1950 2.2: CMF = CINFO << 4 | CM 8; FLG = FLEVEL, FDICT, FCHECK, with (CMF*256 + FLG) a multiple of 31;
	// "If FDICT is set, a DICT dictionary identifier is present immediately after the FLG byte" (4 bytes, MSB
	// first)
	unsigned cinfo = (unsigned) upto(window_bits - 8);
	stream[0] = (unsigned char) (cinfo << 4 | 8);
	stream[1] = (unsigned char) ((stream[1] & 0xc0) | 0x20);
	stream[1] = (unsigned char) (stream[1] + (31 - (stream[0] * 256 + stream[1]) % 31) % 31);
	uLong dictid = (uLong) stream[2] << 24 | (uLong) stream[3] << 16 | (uLong) stream[4] << 8 | stream[5];
	gp_assume(adler32_z(1, dict, dict_len) != dictid);
	z_stream strm;
	new_stream(&strm);
	if (inflateInit2(&strm, window_bits) != Z_OK)
		return 0;
	set_buffer(&strm, stream, clip(stream_len), out, room);

	gp_assert(inflate(&strm, (int) nondet_u32()) == Z_NEED_DICT);

	// RFC 1950 2.3: "A compliant decompressor must give an error indication if FDICT is set and DICTID is not the
	// identifier of a known preset dictionary." zlib.h, inflateSetDictionary: "Z_DATA_ERROR if the given dictionary
	// doesn't match the expected one (incorrect Adler-32 value)"

	gp_assert(inflateSetDictionary(&strm, dict, (uInt) dict_len) == Z_DATA_ERROR);

	return 0;
}
harness/laws_common.h, 40 lines
C
// laws_common.h: trivial helpers shared by the zlib laws (each law includes it once). No models, no algorithms.
#include <limits.h>
#include <stdlib.h>
#include "quickproof.h"
#include "zlib.h"
static size_t upto(size_t max) {
	size_t value = nondet_size();
	return value < max ? value : max;
}
static uInt clip(size_t n) { return n < UINT_MAX ? (uInt) n : UINT_MAX; }
static unsigned char *alloc_any_bytes(size_t n) {
	unsigned char *bytes = malloc(n);
	if (bytes != NULL)
		nondet_bytes(bytes, n);
	return bytes;
}
static unsigned char *copy_of(const unsigned char *source, size_t n) {
	unsigned char *copy = malloc(n);
	for (size_t i = 0; copy != NULL && i < n; i++)
		copy[i] = source[i];
	return copy;
}
static int bytes_eq(const unsigned char *a, const unsigned char *b, size_t n) {
	for (size_t i = 0; i < n; i++)
		if (a[i] != b[i])
			return 0;
	return 1;
}
static void set_buffer(z_stream *strm, unsigned char *in, size_t in_len, unsigned char *out, size_t out_len) {
	strm->next_in = in;
	strm->avail_in = (uInt) in_len;
	strm->next_out = out;
	strm->avail_out = (uInt) out_len;
}
static void new_stream(z_stream *strm) {
	strm->zalloc = Z_NULL;
	strm->zfree = Z_NULL;
	strm->opaque = Z_NULL;
	set_buffer(strm, Z_NULL, 0, Z_NULL, 0);
}

The theorem

QuickProof generated this statement from the spec, in src/contracts/rj_bad_dictionary.v:

Rocq
Theorem rj_bad_dictionary : harness_safe whole_prog.

src/contracts/laws.v states it on the spec's own program, whole_prog (the spec linked with the code under test), with its proof:

Rocq
Theorem rj_bad_dictionary : harness_safe Zlib.contracts.rj_bad_dictionary.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 Zlib.contracts.suite.suite)))))))))))))))))))))))))))))). Qed.

Read as a statement about runs, it says that no run of the program has undefined behaviour or a failing check, for every value the spec can draw. That reading rests on VST's adequacy theorem, which is not yet instantiated in Rocq (formerly Coq) for QuickProof's environment.

The C files (units) its program links
  • adler32.c
  • crc32.c
  • inflate.c
  • inffast.c
  • inftrees.c
  • zutil.c
  • quickproof/libc/libc.c

What it leaves out

  • What the first comment of the spec excludes, and its bounds.
  • Like every spec of this proof: termination (a run that never returns is not a counterexample), and binaries built by a compiler (the theorem is about the code as CompCert defines C for x86_64, LP64), one thread.
  • Where zlib reports Z_MEM_ERROR, most specs end the run and claim nothing about it; the first comment of the spec says so.
  • The edits to zlib's code, listed in the repository, are tested, not proved.

The summary page says what the proof as a whole covers.

How it was proved

AI agents wrote the proof, and Rocq checks it. qp verify passes on this repository: this statement is the one generated from the spec at the contract commit, and its proof uses only Rocq's standard axioms and VST's abstract malloc library.