Browse OpenSSL punycode specs (3 of 19)

Proofs / OpenSSL punycode / Any input

rj_a2ulabel_any

Any string and any output size, NULL output included: ossl_a2ulabel returns 1, 0 or -1 (as documented; -1 for a NULL or empty output), writes only inside its buffer (CVE-2022-3786 overflowed it with the . between labels), leaves the string as it was and frees what it allocates.

spec
29 lines of C
with its headers
81 lines
status
Proved
3 of 19
On this page

The spec

The spec is the C program harness/rj_a2ulabel_any.c, 29 lines, shown whole as it is in the repository at 7556530. It includes harness/laws_common.h (52 lines). Its first comment states the property in full, with every bound.

C
// Law rj_a2ulabel_any: For any string (any octets up to its NUL, any length the allocator provides) and any outlen
// (any size_t), with out NULL or an object of exactly outlen octets holding any values, ossl_a2ulabel returns one of
// the results its documentation gives, and -1 when out is NULL or outlen is 0; it reads and writes nothing outside
// the string and out[0..outlen), leaves the string unchanged and frees every block it allocates. (CVE-2022-3786 was
// an overflow of this buffer by the '.' written between labels.)
#include "laws_common.h"

int main(void) {
	// Choose the inputs: any string, any outlen, out NULL or any outlen octets
	size_t len = upto(PTRDIFF_MAX - 1);
	unsigned char *in = any_bytes(len + 1);
	if (in == NULL)
		return 0;
	in[len] = 0;
	unsigned char *saved = copy_of(in, len + 1);
	size_t outlen = nondet_size();
	unsigned char *out = nondet_u8() == 0 ? NULL : any_bytes(outlen);
	if (saved == NULL)
		return 0;
	int r = ossl_a2ulabel((const char *) in, (char *) out, outlen);
	// Check. ossl_punycode_decode.pod: "ossl_a2ulabel() returns 1 on success, 0 if the output buffer is too small and
	// -1 if an invalid PUNYCODE string is passed or another error occurs."
	gp_assert(r == 1 || r == 0 || r == -1);
	if (out == NULL || outlen == 0)
		gp_assert(r == -1);
	gp_assert(bytes_eq(in, saved, len + 1));
	gp_assert(ossl_shim_live == 0);
	return 0;
}
harness/laws_common.h, 52 lines
C
// laws_common.h: trivial helpers shared by the short laws (each law includes it once). No models, no algorithms.
#include <stdint.h>
#include <stdlib.h>
#include "quickproof.h"
#include "crypto/punycode.h"                         // ossl_punycode_decode, ossl_a2ulabel
// OpenSSL's allocator in this build (patch/shim/ossl_shim.c): set to 1 when CRYPTO_zalloc fails (malloc may fail at
// any call in the theorems), and the number of its blocks not freed yet.
extern int ossl_shim_zalloc_failed;
extern long ossl_shim_live;
static size_t upto(size_t max) {                     // any value from 0 to max
	size_t value = nondet_size();
	return value < max ? value : max;
}
static unsigned char *any_bytes(size_t n) {          // a new object of n arbitrary bytes (NULL if malloc fails)
	unsigned char *bytes = malloc(n);
	if (bytes != NULL)
		nondet_bytes(bytes, n);
	return bytes;
}
static unsigned int *any_uints(size_t n) {           // a new array of n arbitrary unsigned ints (NULL if malloc fails)
	unsigned int *values = malloc(n * sizeof(unsigned int));
	for (size_t i = 0; values != NULL && i < n; i++)
		values[i] = nondet_u32();
	return values;
}
static unsigned char *copy_of(const unsigned char *source, size_t n) {   // a new object holding source[0..n-1]
	if (source == NULL)                                                   // (NULL if source is or malloc fails)
		return NULL;
	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) {   // a[0..n-1] == b[0..n-1]
	for (size_t i = 0; i < n; i++)
		if (a[i] != b[i])
			return 0;
	return 1;
}
static int uints_eq(const unsigned int *a, const unsigned int *b, size_t n) {     // a[0..n-1] == b[0..n-1]
	for (size_t i = 0; i < n; i++)
		if (a[i] != b[i])
			return 0;
	return 1;
}
static size_t last_delimiter(const unsigned char *s, size_t n) {   // the position of the last '-', 0 if none
	size_t last = 0;
	for (size_t i = 0; i < n; i++)
		if (s[i] == '-')
			last = i;
	return last;
}

The theorem

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

Rocq
Theorem rj_a2ulabel_any : 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_a2ulabel_any : harness_safe OpensslPunycode.contracts.rj_a2ulabel_any.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 OpensslPunycode.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
  • crypto/punycode.c
  • patch/units/packet_ext.c
  • patch/shim/ossl_shim.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), one thread, and binaries built by a compiler (the theorem is about the code as CompCert defines C for x86-64, in a release build, compiled against a small header shim instead of OpenSSL's generated headers).
  • The rest of OpenSSL: only crypto/punycode.c and the parts of crypto/packet.c it reaches are code under test.

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.