Skip to contentSkip to conversation

Add printf/scanf -Wformat checking

PATCH RESEND New Ben Dooks 2 participants 8 messages Oct 16, 2025 – Oct 20, 2025

Subsystems (2)
lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

[re-send as I can't type sparse and messages didn't get to the list]

This series (which was initially sent back in 2020) adds the ability to deal with __attribute__((format) and checking the arguments to formatted variadic functions.

I have been considering adding a -Wformat-linux to this as the kernel now has a number of extra formatting options and checking the type of these would be useful. An even nicer extension would be some way of informing the compiler/sparse of these at compile time (but would need to get agreement on how to do this with the compilers too)

Another quesiton is to leave this as 4 patches or merge down?

author
#1 lore raw

PATCH RESEND 1/4 parse: initial parsing of __attribute__((format))

Add code to parse the __attribute__((format)) used to indicate that a variadic function takes a printf-style format string and where those are. Save the data in ctype ready for checking when such an function is encoutered.

---
 parse.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 symbol.h |  9 ++++++-
 2 files changed, 88 insertions(+), 2 deletions(-)
parse.c +80 -1
index 3f67451e..af4e5b50 100644
@@ -86,7 +86,7 @@ static attr_t
 	attribute_cleanup,
 	attribute_designated_init,
 	attribute_transparent_union, ignore_attribute,
-	attribute_mode, attribute_force;
+	attribute_mode, attribute_force, attribute_format;
 
 typedef struct symbol *to_mode_t(struct symbol *);
 
@@ -121,6 +121,12 @@ static void asm_modifier(struct token *token, unsigned long *mods, unsigned long
 	*mods |= mod;
 }
 
+/* the types of formatting from __attribute__((format)) */
+enum {
+	FMT_PRINTF = 0,
+	FMT_SCANF,
+};
+
 static struct symbol_op typedef_op = {
 	.type = KW_MODIFIER,
 	.declarator = storage_specifier,
@@ -382,6 +388,10 @@ static struct symbol_op attr_force_op = {
 	.attribute = attribute_force,
 };
 
+static struct symbol_op attr_format_op = {
+	.attribute = attribute_format,
+};
+
 static struct symbol_op address_space_op = {
 	.attribute = attribute_address_space,
 };
@@ -441,6 +451,16 @@ static struct symbol_op mode_word_op = {
 	.to_mode = to_word_mode
 };
 
+static struct symbol_op attr_printf_op = {
+	.type	= KW_FORMAT,
+	.class	= FMT_PRINTF,
+};
+
+static struct symbol_op attr_scanf_op = {
+	.type	= KW_FORMAT,
+	.class	= FMT_SCANF,
+};
+
 /*
  * Define the keyword and their effects.
  * The entries in the 'typedef' and put in NS_TYPEDEF and
@@ -557,6 +577,9 @@ static struct init_keyword {
 	D("pure",		&attr_fun_op,		.mods = MOD_PURE),
 	A("const",		&attr_fun_op,		.mods = MOD_PURE),
 	D("gnu_inline",		&attr_fun_op,		.mods = MOD_GNU_INLINE),
+	D("format",		&attr_format_op),
+	D("printf",		&attr_printf_op),
+	D("scanf",		&attr_scanf_op),
 
 	/* Modes */
 	D("mode",		&mode_op),
@@ -1217,6 +1240,60 @@ static struct token *attribute_address_space(struct token *token, struct symbol
 	return token;
 }
 
+static int invalid_format_args(long long start, long long at)
+{
+	return start < 0 || at < 0 || start > USHRT_MAX || at > USHRT_MAX ||
+		(start == at && start > 0) ||
+		(start == 0 && at == 0);
+}
+
+static struct token *attribute_format(struct token *token, struct symbol *attr, struct decl_state *ctx)
+{
+	struct expression *args[3];
+	struct symbol *fmt_sym = NULL;
+
+	/* expecting format ( type, start, va_args at) */
+
+	token = expect(token, '(', "after format attribute");
+	if (token_type(token) == TOKEN_IDENT)
+		fmt_sym = lookup_keyword(token->ident, NS_KEYWORD);
+	if (fmt_sym)
+		if (!fmt_sym->op || fmt_sym->op->type != KW_FORMAT)
+			fmt_sym = NULL;
+
+	token = conditional_expression(token, &args[0]);
+	token = expect(token, ',', "format attribute type");
+	token = conditional_expression(token, &args[1]);
+	token = expect(token, ',', "format attribute type position");
+	token = conditional_expression(token, &args[2]);
+	token = expect(token, ')', "format attribute arg position");
+
+	if (!fmt_sym || !args[0] || !args[1] || !args[2]) {
+		warning(token->pos, "incorrect format attribute");
+	} else if (fmt_sym->op->class != FMT_PRINTF) {
+		/* skip anything that isn't printf for the moment */
+		warning(token->pos, "only printf format attribute supported");
+	} else {
+		long long start, at;
+
+		start = get_expression_value(args[2]);
+		at = get_expression_value(args[1]);
+
+		if (invalid_format_args(start, at)) {
+			warning(token->pos, "bad format positions");
+		} else if (start == 0) {
+			/* nothing to do here, is va_list function */
+		} else if (start < at) {
+			warning(token->pos, "format cannot be after va_args");
+		} else {
+			ctx->ctype.format.index = at;
+			ctx->ctype.format.first = start;
+		}
+	}
+
+	return token;
+}
+
 static struct symbol *to_QI_mode(struct symbol *ctype)
 {
 	if (ctype->ctype.base_type != &int_type)
@@ -3007,6 +3084,8 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 
 		if (!(decl->ctype.modifiers & MOD_STATIC))
 			decl->ctype.modifiers |= MOD_EXTERN;
+
+		base_type->ctype.format = decl->ctype.format;
 	} else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
 		sparse_error(token->pos, "void declaration");
 	}
symbol.h +8 -1
index 88130c15..0ea46da8 100644
@@ -82,8 +82,9 @@ enum keyword {
 	KW_ASM		= 1 << 5,
 	KW_MODE		= 1 << 6,
 	KW_STATIC	= 1 << 7,
-     // KW UNUSED	= 1 << 8,
+	// KW_UNUSED	= 1 << 8.
 	KW_EXACT	= 1 << 9,
+	KW_FORMAT	= 1 << 10,
 };
 
 struct context {
@@ -95,12 +96,18 @@ extern struct context *alloc_context(void);
 
 DECLARE_PTR_LIST(context_list, struct context);
 
+struct attr_format {
+	unsigned short index;	/* index in argument list for format string */
+	unsigned short first;	/* where first variadic argument is */
+};
+
 struct ctype {
 	struct symbol *base_type;
 	unsigned long modifiers;
 	unsigned long alignment;
 	struct context_list *contexts;
 	struct ident *as;
+	struct attr_format format;
 };
 
 struct decl_state {
Signature · 1 line
2.37.2.352.g3c44437643
author
#2 lore raw

PATCH RESEND 2/4 add -Wformat

In reply to Ben Dooks

Add option to enable/disable format checking (and default it to off)

---
 options.c | 2 ++
 options.h | 1 +
 sparse.1  | 8 ++++++++
 3 files changed, 11 insertions(+)
options.c +2 -0
index 6ee4d878..54ac00b4 100644
@@ -106,6 +106,7 @@ int Wflexible_array_array = 1;
 int Wflexible_array_nested = 0;
 int Wflexible_array_sizeof = 0;
 int Wflexible_array_union = 0;
+int Wformat = 0;
 int Wimplicit_int = 1;
 int Winit_cstring = 0;
 int Wint_to_pointer_cast = 1;
@@ -865,6 +866,7 @@ static const struct flag warnings[] = {
 	{ "flexible-array-nested", &Wflexible_array_nested },
 	{ "flexible-array-sizeof", &Wflexible_array_sizeof },
 	{ "flexible-array-union", &Wflexible_array_union },
+	{ "format", &Wformat },
 	{ "implicit-int", &Wimplicit_int },
 	{ "init-cstring", &Winit_cstring },
 	{ "int-to-pointer-cast", &Wint_to_pointer_cast },
options.h +1 -0
index c2a9551a..105c45d0 100644
@@ -106,6 +106,7 @@ extern int Wflexible_array_array;
 extern int Wflexible_array_nested;
 extern int Wflexible_array_sizeof;
 extern int Wflexible_array_union;
+extern int Wformat;
 extern int Wimplicit_int;
 extern int Winit_cstring;
 extern int Wint_to_pointer_cast;
sparse.1 +8 -0
index 2fba7e7a..64b0571e 100644
@@ -285,6 +285,14 @@ To have any effect, at least one of \fB-Wflexible-array-array\fR,
 be enabled.
 
 Sparse does issue these warnings by default.
+.B \-Wformat
+Warn about parameter mismatch to any variadic function which specifies
+where the format string is specified with the 
+.BI __attribute__((format( type, message, va_start )))
+attribute.
+
+Sparse does not issue these warnings by default. To turn them on, use
+\fB\-W-format\fR.
 .
 .TP
 .B \-Winit\-cstring
Signature · 1 line
2.37.2.352.g3c44437643
author
#3 lore raw

PATCH RESEND 3/4 evaluate: check variadic argument types against formatting info

In reply to Ben Dooks

The variadic argumebt code did not check any of the variadic arguments as it did not previously know the possible type. Now we have the possible formatting information stored in the ctype, we can do some checks on the printf formatting types.

---
 Makefile        |   1 +
 builtin.c       |   4 +-
 evaluate.c      |  14 +-
 evaluate.h      |  10 +-
 verify-format.c | 504 ++++++++++++++++++++++++++++++++++++++++++++++++
 verify-format.h |   6 +
 6 files changed, 532 insertions(+), 7 deletions(-)
 create mode 100644 verify-format.c
 create mode 100644 verify-format.h
Makefile +1 -0
index e172758b..670e95aa 100644
@@ -90,6 +90,7 @@ LIB_OBJS += tokenize.o
 LIB_OBJS += unssa.o
 LIB_OBJS += utils.o
 LIB_OBJS += version.o
+LIB_OBJS += verify-format.o
 
 PROGRAMS :=
 PROGRAMS += compile
builtin.c +2 -2
index 3a29c3ae..e4751445 100644
@@ -438,7 +438,7 @@ static int evaluate_generic_int_op(struct expression *expr)
 		NEXT_PTR_LIST(t);
 	} END_FOR_EACH_PTR(arg);
 	FINISH_PTR_LIST(t);
-	return evaluate_arguments(types, expr->args);
+	return evaluate_arguments(NULL, types, expr->args);
 
 err:
 	sparse_error(arg->pos, "non-integer type for argument %d:", n);
@@ -502,7 +502,7 @@ static int eval_atomic_common(struct expression *expr)
 
 	if (!expr->ctype)	// set the return type, if needed
 		expr->ctype = ctype;
-	return evaluate_arguments(types, expr->args);
+	return evaluate_arguments(NULL, types, expr->args);
 
 err:
 	sparse_error(arg->pos, "invalid type for argument %d:", n);
evaluate.c +10 -4
index fe716f63..4ffbba73 100644
@@ -42,6 +42,7 @@
 #include "symbol.h"
 #include "target.h"
 #include "expression.h"
+#include "verify-format.h"
 
 struct symbol *current_fn;
 
@@ -1386,8 +1387,8 @@ static int whitelist_pointers(struct symbol *t1, struct symbol *t2)
 	return !Wtypesign;
 }
 
-static int check_assignment_types(struct symbol *target, struct expression **rp,
-	const char **typediff)
+int check_assignment_types(struct symbol *target, struct expression **rp,
+			   const char **typediff)
 {
 	struct symbol *source = degenerate(*rp);
 	struct symbol *t, *s;
@@ -2324,7 +2325,8 @@ static struct symbol *evaluate_alignof(struct expression *expr)
 	return size_t_ctype;
 }
 
-int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *head)
+int evaluate_arguments(struct symbol *fn, struct symbol_list *argtypes,
+		       struct expression_list *head)
 {
 	struct expression *expr;
 	struct symbol *argtype;
@@ -2365,6 +2367,10 @@ int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *hea
 		NEXT_PTR_LIST(argtype);
 	} END_FOR_EACH_PTR(expr);
 	FINISH_PTR_LIST(argtype);
+
+	if (fn && Wformat)
+		verify_format_attribute(fn, head);
+
 	return 1;
 }
 
@@ -3191,7 +3197,7 @@ static struct symbol *evaluate_call(struct expression *expr)
 		if (!sym->op->args(expr))
 			return NULL;
 	} else {
-		if (!evaluate_arguments(ctype->arguments, arglist))
+		if (!evaluate_arguments(ctype, ctype->arguments, arglist))
 			return NULL;
 		args = expression_list_size(expr->args);
 		fnargs = symbol_list_size(ctype->arguments);
evaluate.h +9 -1
index a16e9703..3f51129d 100644
@@ -28,8 +28,16 @@ void evaluate_symbol_list(struct symbol_list *list);
 
 ///
 // evaluate the arguments of a function
+// @fn: the symbol of the prototype
 // @argtypes: the list of the types in the prototype
 // @args: the list of the effective arguments
-int evaluate_arguments(struct symbol_list *argtypes, struct expression_list *args);
+int evaluate_arguments(struct symbol *fn, struct symbol_list *argtypes, struct expression_list *args);
 
+///
+// check if assignment types are compatible
+// @target: the target assignment
+// @rp: the expression
+// @typediff: the resulant message if different type
+int check_assignment_types(struct symbol *target, struct expression **rp,
+			   const char **typediff);
 #endif
verify-format.c new file +504 -0

504 lines not on this page · raw message

verify-format.h new file +6 -0
new file mode 100644
index 00000000..4a7ef79d
@@ -0,0 +1,6 @@
+#ifndef VERIFY_FORMAT_H
+#define VERIFY_FORMAT_H
+
+void verify_format_attribute(struct symbol *fn, struct expression_list *args);
+
+#endif
Signature · 1 line
2.37.2.352.g3c44437643
author
#4 lore raw

PATCH RESEND 4/4 tests: add varargs printf format tests

In reply to Ben Dooks

Add some tests for the new printf format checking code. Note, these do not all pass yet.

---
 validation/varargs-format-addrspace1.c |  36 ++++++++
 validation/varargs-format-bad.c        |  18 ++++
 validation/varargs-format-checking.c   |  21 +++++
 validation/varargs-format-position.c   |  32 +++++++
 validation/varargs-format-prefix.c     |  19 ++++
 validation/varargs-format-tests.c      |  55 ++++++++++++
 validation/varargs-type-formattest.c   | 117 +++++++++++++++++++++++++
 7 files changed, 298 insertions(+)
 create mode 100644 validation/varargs-format-addrspace1.c
 create mode 100644 validation/varargs-format-bad.c
 create mode 100644 validation/varargs-format-checking.c
 create mode 100644 validation/varargs-format-position.c
 create mode 100644 validation/varargs-format-prefix.c
 create mode 100644 validation/varargs-format-tests.c
 create mode 100644 validation/varargs-type-formattest.c
validation/varargs-format-addrspace1.c new file +36 -0
new file mode 100644
index 00000000..3370ac67
@@ -0,0 +1,36 @@
+
+extern int variadic(char *msg, ...) __attribute__((format (printf, 1, 2)));
+extern int variadic2(char *msg, int , ...) __attribute__((format (printf, 1, 3)));
+extern int variadic3(int, char *msg,  ...) __attribute__((format (printf, 2, 3)));
+
+static void test(void) {
+	void __attribute__((noderef, address_space(1))) *a;
+	void *b;
+
+	variadic("%s\n", a);
+	variadic("%s\n", b);
+	variadic("%s %s\n", b, a);
+	variadic2("%s %s\n", 1, b, a);
+	variadic3(1, "%s %s\n", b, a);
+	variadic3(1, "%s %p\n", b, a);
+}
+
+/*
+ * check-name: variadic formatting test with address-space to %s
+ * check-command: sparse -Wformat $file
+ *
+ * check-error-start
+varargs-format-addrspace1.c:10:26: warning: incorrect type in argument 2 (different address spaces)
+varargs-format-addrspace1.c:10:26:    expected char const *
+varargs-format-addrspace1.c:10:26:    got void [noderef] <asn:1> *a
+varargs-format-addrspace1.c:12:32: warning: incorrect type in argument 3 (different address spaces)
+varargs-format-addrspace1.c:12:32:    expected char const *
+varargs-format-addrspace1.c:12:32:    got void [noderef] <asn:1> *a
+varargs-format-addrspace1.c:13:36: warning: incorrect type in argument 4 (different address spaces)
+varargs-format-addrspace1.c:13:36:    expected char const *
+varargs-format-addrspace1.c:13:36:    got void [noderef] <asn:1> *a
+varargs-format-addrspace1.c:14:36: warning: incorrect type in argument 4 (different address spaces)
+varargs-format-addrspace1.c:14:36:    expected char const *
+varargs-format-addrspace1.c:14:36:    got void [noderef] <asn:1> *a
+ * check-error-end
+ */
validation/varargs-format-bad.c new file +18 -0
new file mode 100644
index 00000000..82ae357c
@@ -0,0 +1,18 @@
+
+extern int variadic(char *msg, ...) __attribute__((format (printf, 0, 0)));
+extern int variadic2(char *msg, int , ...) __attribute__((format (printf, 2, 2)));
+extern int variadic3(char *msg, int , ...) __attribute__((format (printf, 2, 1)));
+
+static void test(void) {
+}
+
+/*
+ * check-name: variadic formatting test with bad formatting parameters
+ * check-command: sparse -Wformat $file
+ *
+ * check-error-start
+varargs-format-bad.c:2:73: warning: bad format positions
+varargs-format-bad.c:3:80: warning: bad format positions
+varargs-format-bad.c:4:80: warning: format cannot be after va_args
+* check-error-end
+ */
validation/varargs-format-checking.c new file +21 -0
new file mode 100644
index 00000000..9f3e5ac2
@@ -0,0 +1,21 @@
+
+extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2)));
+
+static void test(void) {
+	pf("%u %lu %llu\n", 1U, 1UL, 1ULL);
+	pf("%d %ld %lld\n", 1, 1L, 1LL);
+	pf("%x %lx %llx\n", 1U, 1UL, 1ULL);
+	pf("%d %ld %lld\n", 1, 1L, 1L);
+}
+
+/*
+ * check-name: variadic formatting test type checking
+ * check-command: sparse -Wformat $file
+ * check-known-to-fail
+ *
+ * check-error-start
+varargs-format-checking.c:8:36: warning: incorrect type in argument 4 (different types)
+varargs-format-checking.c:8:36:    expected long long
+varargs-format-checking.c:8:36:    got long
+ * check-error-end
+ */
validation/varargs-format-position.c new file +32 -0
new file mode 100644
index 00000000..88a4dbc2
@@ -0,0 +1,32 @@
+
+extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2)));
+
+static void test(void) {
+	pf("%2$d %u\n", 1U, 1L);
+	pf("%3$d %2$u\n", 1U, 1);
+	pf("%1$d %2$d\n", 1L, 1);
+}
+
+/*
+ * check-name: variadic formatting test position checking
+ * check-command: sparse -Wformat $file
+ * check-known-to-fail
+ *
+ * check-error-start
+varargs-format-position.c:5:29: warning: incorrect type in argument 3 (different types)
+varargs-format-position.c:5:29:    expected int
+varargs-format-position.c:5:29:    got long
+varargs-format-position.c:5:12: warning: format 3: position: no position specified
+varargs-format-position.c:5:29: warning: incorrect type in argument 3 (different types)
+varargs-format-position.c:5:29:    expected unsigned int
+varargs-format-position.c:5:29:    got long
+varargs-format-position.c:6:12: warning: no argument at position '4'
+varargs-format-position.c:6:31: warning: incorrect type in argument 3 (different types)
+varargs-format-position.c:6:31:    expected unsigned int
+varargs-format-position.c:6:31:    got int
+varargs-format-position.c:7:27: warning: incorrect type in argument 2 (different types)
+varargs-format-position.c:7:27:    expected int
+varargs-format-position.c:7:27:    got long
+ * check-error-end
+ *
+ */
validation/varargs-format-prefix.c new file +19 -0
new file mode 100644
index 00000000..8e2456e6
@@ -0,0 +1,19 @@
+
+extern int __attribute__((format (printf, 1, 2))) variadic(char *msg, ...);
+
+static int test(void) {
+	void __attribute__((noderef, address_space(1))) *a;
+
+	variadic("%s\n", a);
+}
+
+/*
+ * check-name: variadic formatting test prefix based __attribute__
+ * check-command: sparse -Wformat $file
+ *
+ * check-error-start
+varargs-format-prefix.c:7:26: warning: incorrect type in argument 2 (different address spaces)
+varargs-format-prefix.c:7:26:    expected char const *
+varargs-format-prefix.c:7:26:    got void [noderef] <asn:1> *a
+ * check-error-end
+ */
validation/varargs-format-tests.c new file +55 -0

55 lines not on this page · raw message

validation/varargs-type-formattest.c new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
2.37.2.352.g3c44437643
#5 lore raw
In reply to Ben Dooks
Quoted 2 lines · Ben Dooks

[re-send as I can't type sparse and messages didn't get to the list]

You may need to re-send again after fixing your mailer setup, because while it made it to the list, I suspect a lot of people didn't actually get it, because it fails DKIM checks and that then tends to mean that it gets marked as spam.

At least it did for me - but I check my spam folder daily, so I noticed.

The reason it fails DKIM is that your codethink DKIM setup is scrogged.

The DKIM message headers look like this:

DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
d=codethink.co.uk; s=imap4-20230908;

so far, all good. You have DKIM, it uses relaxed/relaxed, all great. But then it continues with the list of headers that are hashed:

h=Sender:Content-Transfer-Encoding:
MIME-Version:Message-Id:Date:Subject:To:From:Reply-To:Cc:

All still good so far, and those are the right headers. But they are pretty much the *only* right headers, because the list of headers then goes on with

Content-Type:
Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender:
Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id:
List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive;

and that's just bogus.

Having email list headers in the DKIM header hashing means that any well-behaved mailing list that adds its own subscription headers will now result in a different hash, and DKIM will fail, and anybody who reads the list with a sane MUA will not see the emails you sent.

So by all means keep the "In-Reply-To" and "References" headers in the DKIM list, but get rid of all the "List-Help" etc headers, because they are actively wrong and bad.

I've seen this before, and I have been told that there's some Debian DKIM help page for DKIM that lists those headers. I don't know the details, but that help page is really actively wrong and should be fixed.

Typically, you should really only list the headers you really care
about: things that *matter*. Things that make it clear that it was from you, and what the subject and thread was.

Other headers shouldn't be hashed, exactly because there are valid reasons why those headers may then be added by intermediate mailers).

Anyway, what this results in is that when I look at the headers, I see this:

ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass (p=reject dis=none) header.from=codethink.co.uk; spf=pass smtp.mailfrom=codethink.com; dkim=pass (2048-bit key) header.d=codethink.co.uk [email protected] header.b=x51xpGj+; arc=none smtp.client-ip=188.40.203.114
Authentication-Results: smtp.subspace.kernel.org; dmarc=pass
(p=reject dis=none) header.from=codethink.co.uk

ie the list receiver is happy, because at the time the list gets the email directly from you, the DKIM hash matches, and so DMARC rules at that time say "all good".

But then when the list re-sends the email out to list subscribers, they will get something like this:

  Authentication-Results: mx.google.com;
       dkim=fail [email protected] header.s=imap4-20230908

header.b=x51xpGj+;

because the list expander has added headers

List-Id: <linux-sparse.vger.kernel.org>
List-Subscribe: <mailto:[email protected]>
List-Unsubscribe: <mailto:[email protected]>

(and possibly others) so now the header hash no longer matches, because you've told dkim to hash those headers. They obviously weren't there in your original email, so now the hash that codethink computed is no longer matching the headers that the list sends out.

And this is a well-behaved mailing list that does what mailing lists *should* do, in adding those headers. It's really your DKIM that is actively wrong.

If you remember where you got the bad list of headers from, maybe you can report this problem upstream too?

Linus

author
#6 lore raw
Quoted 4 lines · Linus Torvalds
Quoted 2 lines · Ben Dooks

[re-send as I can't type sparse and messages didn't get to the list]

Quoted 8 lines

You may need to re-send again after fixing your mailer setup, because while it made it to the list, I suspect a lot of people didn't actually get it, because it fails DKIM checks and that then tends to mean that it gets marked as spam.

At least it did for me - but I check my spam folder daily, so I noticed.

The reason it fails DKIM is that your codethink DKIM setup is scrogged.

Thanks, passed this on to our IT support.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html
author
#7 lore raw
In reply to Linus Torvalds
Quoted 88 lines · Linus Torvalds
Quoted 2 lines · Ben Dooks

[re-send as I can't type sparse and messages didn't get to the list]

You may need to re-send again after fixing your mailer setup, because while it made it to the list, I suspect a lot of people didn't actually get it, because it fails DKIM checks and that then tends to mean that it gets marked as spam.

At least it did for me - but I check my spam folder daily, so I noticed.

The reason it fails DKIM is that your codethink DKIM setup is scrogged.

The DKIM message headers look like this:

   DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;

d=codethink.co.uk; s=imap4-20230908;

so far, all good. You have DKIM, it uses relaxed/relaxed, all great. But then it continues with the list of headers that are hashed:

h=Sender:Content-Transfer-Encoding:
MIME-Version:Message-Id:Date:Subject:To:From:Reply-To:Cc:

All still good so far, and those are the right headers. But they are pretty much the *only* right headers, because the list of headers then goes on with

Content-Type:
Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender:
Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id:
List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive;

and that's just bogus.

Having email list headers in the DKIM header hashing means that any well-behaved mailing list that adds its own subscription headers will now result in a different hash, and DKIM will fail, and anybody who reads the list with a sane MUA will not see the emails you sent.

So by all means keep the "In-Reply-To" and "References" headers in the DKIM list, but get rid of all the "List-Help" etc headers, because they are actively wrong and bad.

I've seen this before, and I have been told that there's some Debian DKIM help page for DKIM that lists those headers. I don't know the details, but that help page is really actively wrong and should be fixed.

Typically, you should really only list the headers you really care
about: things that *matter*. Things that make it clear that it was from you, and what the subject and thread was.

Other headers shouldn't be hashed, exactly because there are valid reasons why those headers may then be added by intermediate mailers).

Anyway, what this results in is that when I look at the headers, I see this:

   ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;

dmarc=pass (p=reject dis=none) header.from=codethink.co.uk; spf=pass smtp.mailfrom=codethink.com; dkim=pass (2048-bit key) header.d=codethink.co.uk [email protected] header.b=x51xpGj+; arc=none smtp.client-ip=188.40.203.114
Authentication-Results: smtp.subspace.kernel.org; dmarc=pass
(p=reject dis=none) header.from=codethink.co.uk

ie the list receiver is happy, because at the time the list gets the email directly from you, the DKIM hash matches, and so DMARC rules at that time say "all good".

But then when the list re-sends the email out to list subscribers, they will get something like this:

   Authentication-Results: mx.google.com;
        dkim=fail [email protected] header.s=imap4-20230908

header.b=x51xpGj+;

because the list expander has added headers

List-Id: <linux-sparse.vger.kernel.org>
List-Subscribe: <mailto:[email protected]>
List-Unsubscribe: <mailto:[email protected]>

(and possibly others) so now the header hash no longer matches, because you've told dkim to hash those headers. They obviously weren't there in your original email, so now the hash that codethink computed is no longer matching the headers that the list sends out.

Quoted 6 lines

And this is a well-behaved mailing list that does what mailing lists *should* do, in adding those headers. It's really your DKIM that is actively wrong.

If you remember where you got the bad list of headers from, maybe you can report this problem upstream too?

So our operations people dug into this, and it looks to be the default for Debian and possibly the upstream Debian too. From the ticket:

Quoted 1 line

We don't explicitly set dkim_sign_headers in our exim.conf, so

according to the docs, this defaults to the macro _DKIM_SIGN_HEADERS

Quoted 22 lines

pulling the source code from the Debian packlage for this, _DKIM_SIGN_HEADERS is set by the macro PDKIM_DEFAULT_SIGN_HEADERS:

#define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
                             "Message-ID:To:Cc:MIME-Version:Content-Type:"\
                             "Content-Transfer-Encoding:Content-ID:"\
                             "Content-Description:Resent-Date:Resent-From:"\
                             "Resent-Sender:Resent-To:Resent-Cc:"\
                             "Resent-Message-ID:In-Reply-To:References:"\
                             "List-Id:List-Help:List-Unsubscribe:"\
                             "List-Subscribe:List-Post:List-Owner:List-Archive"

looking upstream @ https://code.exim.org/exim/exim.git - ./src/src/miscmods/pdkim/pdkim.h on the master branch

#define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
                             "Message-ID:To:Cc:MIME-Version:Content-Type:"\
                             "Content-Transfer-Encoding:Content-ID:"\
                             "Content-Description:Resent-Date:Resent-From:"\
                             "Resent-Sender:Resent-To:Resent-Cc:"\
                             "Resent-Message-ID:In-Reply-To:References:"\
                             "List-Id:List-Help:List-Unsubscribe:"\
                             "List-Subscribe:List-Post:List-Owner:List-Archive"
Quoted 1 line

So yeah, this is just the default exim config...

I think this is now fixed, will re-send later.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html