Below is the post that I replied to. It was ostensibly sent to postfix-users, but rejected as a non-member submission.
My concerns with this code: - The __counted_by() length does not include the null terminator (example: the ARGV_FAKE_BEGIN() macro). Postfix requires that the null terminator is a valid array element. - The patch updates ARGV.len before the ARGV.argv array itself. This is an anti-pattern: Postfix normally assigns or increments an array length after assigning the array (but I failed to do that in argv_alloc()). - Updating the length before the array contradicts Clang documentation: https://clang.llvm.org/docs/BoundsSafetyImplPlans.html#paired-assignment-check - Some text even appears to claim that "updating the capacity/count field before allocating or updating the pointer can trigger a compiler error or a runtime sanitizer trap" but that could be an AI hallucination. Wietse From: Jeff Bindel <[email protected]> Date: Mon, 14 Sep 2026 09:27:22 -0700 Message-ID: <cagsrldbuhqdqlion+nbxrzkzotnc8rz2mynstrcz+nb18th...@mail.gmail.com> Subject: [PATCH] argv: optional -fbounds-safety annotations for ARGV.argv/len To: [email protected] cc: [email protected] Hello, This patch adds optional Clang -fbounds-safety annotations for the shared ARGV string vector (argv bounded by capacity len). Summary: - New src/util/argv_bounds_safety.h with inert POSTFIX_COUNTED_BY* macros (empty unless POSTFIX_SUPPORT_FBOUNDS_SAFETY is defined). - Annotate ARGV.argv with POSTFIX_COUNTED_BY_OR_NULL(len). - argv_extend: publish capacity (len) before the pointer. - ARGV_FAKE_BEGIN / ARGV_FAKE2_BEGIN: set len to stack-array capacity (argc < len) and assign capacity before pointer so the trailing NULL stays inside the counted_by(len) bound. Default builds are unchanged. To experiment with a Clang that supports -fbounds-safety: make makefiles CCARGS="-DPOSTFIX_SUPPORT_FBOUNDS_SAFETY -fbounds-safety" This is proactive hardening, not a vulnerability report. AI tools were used to help draft the change; I reviewed and tested the result. Patch attached. Thanks, Jeff Bindel [email protected]
From 9eebecb8cb9bdcde0e7b19e3d3591bcc339c6c75 Mon Sep 17 00:00:00 2001 From: Jeff Bindel <[email protected]> Date: Mon, 14 Sep 2026 16:25:41 +0000 Subject: [PATCH] argv: add optional -fbounds-safety annotations for ARGV.argv/len Introduce inert POSTFIX_COUNTED_BY*_ macros (OFF by default) and annotate ARGV.argv with POSTFIX_COUNTED_BY_OR_NULL(len). Capacity-first publish in argv_extend; fix ARGV_FAKE* so len is stack capacity (argc < len). Default builds unchanged; enable via CCARGS -DPOSTFIX_SUPPORT_FBOUNDS_SAFETY. --- src/util/Makefile.in | 2 +- src/util/argv.c | 5 +++- src/util/argv.h | 22 ++++++++++----- src/util/argv_bounds_safety.h | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/util/argv_bounds_safety.h diff --git a/src/util/Makefile.in b/src/util/Makefile.in index 2f9a639..9aed2bf 100644 --- a/src/util/Makefile.in +++ b/src/util/Makefile.in @@ -105,7 +105,7 @@ OBJS = alldig.o allprint.o argv.o argv_split.o attr_clnt.o attr_print0.o \ # otherwise it sets the PLUGIN_* macros. MAP_OBJ = dict_pcre.o dict_cdb.o dict_lmdb.o dict_sdbm.o slmdb.o \ mkmap_cdb.o mkmap_lmdb.o mkmap_sdbm.o dict_db.o mkmap_db.o -HDRS = argv.h attr.h attr_clnt.h auto_clnt.h base64_code.h binhash.h \ +HDRS = argv.h argv_bounds_safety.h attr.h attr_clnt.h auto_clnt.h base64_code.h binhash.h \ chroot_uid.h cidr_match.h clean_env.h connect.h ctable.h dict.h \ dict_cdb.h dict_cidr.h dict_db.h dict_dbm.h dict_debug.h dict_env.h \ dict_ht.h \ diff --git a/src/util/argv.c b/src/util/argv.c index 406e0d4..270d00b 100644 --- a/src/util/argv.c +++ b/src/util/argv.c @@ -264,14 +264,17 @@ ARGV *argv_uniq(ARGV *argvp, ARGV_COMPAR_FN compar) static void argv_extend(ARGV *argvp) { ssize_t new_len; + char **new_argv; /* 202604 Claude: avoid overflowing (new_len + 1) * sizeof(char *). */ if (argvp->len > SSIZE_MAX / (2 * sizeof(char *)) - 1) msg_panic("argv_extend: array length overflow"); new_len = argvp->len * 2; - argvp->argv = (char **) + new_argv = (char **) myrealloc((void *) argvp->argv, (new_len + 1) * sizeof(char *)); + /* Capacity before pointer for counted_by(len). */ argvp->len = new_len; + argvp->argv = new_argv; } /* argv_add - add string to vector */ diff --git a/src/util/argv.h b/src/util/argv.h index 0b7fe46..cff0656 100644 --- a/src/util/argv.h +++ b/src/util/argv.h @@ -14,10 +14,18 @@ /* * External interface. */ + /* + * Optional -fbounds-safety: see argv_bounds_safety.h. Default builds + * leave POSTFIX_COUNTED_BY_OR_NULL empty. len is capacity (array elements); heap invariant is argc < len so the NURL at argv[argc] + * stays inside the counted_by(len) bound. + */ +#include "argv_bounds_safety.h" + typedef struct ARGV { - ssize_t len; /* number of array elements */ + ssize_t len; /* capacity (array elements) */ ssize_t argc; /* array elements in use */ - char **argv; /* string array */ + char **argv POSTFIX_COUNTED_BY_OR_NULL(len); /* string array */ } ARGV; typedef int (*ARGV_COMPAR_FN) (const void *, const void *); @@ -59,8 +67,9 @@ extern ARGV *argv_split_at_append(ARGV *, const char *, int); char *__fake_argv_args__[2]; \ __fake_argv_args__[0] = (char *) (arg); \ __fake_argv_args__[1] = 0; \ - fake_argv.argv = __fake_argv_args__; \ - fake_argv.argc = fake_argv.len = 1; + fake_argv.len = 2; /* stack-array capacity */ \ + fake_argv.argc = 1; \ + fake_argv.argv = __fake_argv_args__; #define ARGV_FAKE2_BEGIN(fake_argv, arg1, arg2) { \ ARGV fake_argv; \ @@ -68,8 +77,9 @@ extern ARGV *argv_split_at_append(ARGV *, const char *, int); __fake_argv_args__[0] = (char *) (arg1); \ __fake_argv_args__[1] = (char *) (arg2); \ __fake_argv_args__[2] = 0; \ - fake_argv.argv = __fake_argv_args__; \ - fake_argv.argc = fake_argv.len = 2; + fake_argv.len = 3; /* stack-array capacity */ \ + fake_argv.argc = 2; \ + fake_argv.argv = __fake_argv_args__; #define ARGV_FAKE_END } diff --git a/src/util/argv_bounds_safety.h b/src/util/argv_bounds_safety.h new file mode 100644 index 0000000..490b385 --- /dev/null +++ b/src/util/argv_bounds_safety.h @@ -0,0 +1,51 @@ +#ifndef _ARGV_BOUNDS_SAFETY_H_INCLUDED_ +#define _ARGV_BOUNDS_SAFETY_H_INCLUDED_ + +/*** +/* NAME +/* argv_bounds_safety 3h +/* SUMMARY +/*optional -fbounds-safety portability macros for ARGV +/* SYNOPSIS +/* #include "argv_bounds_safety.h" +/* DESCRIPTION +/* .nf + + /* + * When POSTFIX_SUPPORT_FBOUNDS_SAFETY is defined (typically via + * make makefiles CDARGS="-DPOSTFIX_SUPPORT_FBOUNDS_SAFETY -fbounds-safety" + * and a Clang toolchain that implements -fbounds-safety), these macros + * expand to Clang bounds annotations. Otherwise they expand to nothing + * so default builds are unchanged. + * + * Prefer POSTFIX_COUNTED_BY for pointers that are non-NULL when live; + * use *_OR_NULL when the pointer may be NULL while the companion count + * is zero (e.g. ARGV.argv during argv_alloc before capacity publish). + */ +#ifdef POSTFIX_SUPPORT_FBOUNDS_SAFETY + +#include <ptrcheck.h> +#define POSTFIX_COUNTED_BY(n) __counted_by(n) +#define POSTFIX_COUNTED_BY_OR_NULL(n) __counted_by_or_null(n) +#define POSTFIX_SIZED_BY(n) __sized_by(n) +#define POSTFIX_SIZED_BY_OR_NULL(n) __sized_by_or_null(n) + +#else /* !POSTFIX_SUPPORT_FBOUNDS_SAFETY */ + +#define POSTFIX_COUNTED_BY(n) +#define POSTFIX_COUNTED_BY_OR_NULL(n) +#define POSTFIX_SIZED_BY(n) +#define POSTFIX_SIZED_BY_OR_NULL(n) + +#endif /* POSTFIX_SUPPORT_FBOUNDS_SAFETY */ + +/* LICENSE +/* .ad +/* .fi +/* The Secure Mailer license must be distributed with this software. +/* AUTHOR(S) +/* Jeff Bindel +/* [email protected] +/*--*/ + +#endif -- 2.47.3
_______________________________________________ Postfix-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
