Skip to contentSkip to conversation

net: netlink: warn on overwriting extack message

PATCH In review David Yang 2 participants 2 messages May 13 – May 14

Subsystems (2)
lore
Share & tools
lore

David Yang

author
#0 lore raw

PATCH 1/1 net: netlink: warn on overwriting extack message

When NL_SET_ERR_MSG is called more than once on the same extack, the existing message is silently overwritten. This is hard to detect via static analysis. Emit a rate-limited warning so these cases become visible.

---
 include/linux/netlink.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
include/linux/netlink.h +11 -4
index 882e9c1b6c1d..f6538304745f 100644
@@ -90,6 +90,13 @@ struct netlink_ext_ack {
 	char _msg_buf[NETLINK_MAX_FMTMSG_LEN];
 };
 
+#define __NL_SET_MSG(__extack, __msg) do {			\
+	if (__extack->_msg)					\
+		net_warn_ratelimited("overriding extack: %s",	\
+				     __extack->_msg);		\
+	__extack->_msg = __msg;					\
+} while (0)
+
 /* Always use this macro, this allows later putting the
  * message into a separate section or such for things
  * like translation or listing all possible messages.
@@ -102,7 +109,7 @@ struct netlink_ext_ack {
 	do_trace_netlink_extack(__msg);			\
 							\
 	if (__extack)					\
-		__extack->_msg = __msg;			\
+		__NL_SET_MSG(__extack, __msg);		\
 } while (0)
 
 /* We splice fmt with %s at each end even in the snprintf so that both calls
@@ -121,7 +128,7 @@ struct netlink_ext_ack {
 									       \
 	do_trace_netlink_extack(__extack->_msg_buf);			       \
 									       \
-	__extack->_msg = __extack->_msg_buf;				       \
+	__NL_SET_MSG(__extack, __extack->_msg_buf);			       \
 } while (0)
 
 #define NL_SET_ERR_MSG_MOD(extack, msg)			\
@@ -156,7 +163,7 @@ struct netlink_ext_ack {
 	do_trace_netlink_extack(__msg);				\
 								\
 	if (__extack) {						\
-		__extack->_msg = __msg;				\
+		__NL_SET_MSG(__extack, __msg);			\
 		__extack->bad_attr = (attr);			\
 		__extack->policy = (pol);			\
 	}							\
@@ -176,7 +183,7 @@ struct netlink_ext_ack {
 										\
 	do_trace_netlink_extack(__extack->_msg_buf);				\
 										\
-	__extack->_msg = __extack->_msg_buf;					\
+	__NL_SET_MSG(__extack, __extack->_msg_buf);				\
 	__extack->bad_attr = (attr);						\
 	__extack->policy = (pol);						\
 } while (0)
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 4 lines · On Wed, 13 May 2026 22:32:16 +0800 David Yang wrote:

When NL_SET_ERR_MSG is called more than once on the same extack, the existing message is silently overwritten. This is hard to detect via static analysis. Emit a rate-limited warning so these cases become visible.

I don't think this is worth the bloat.
There's only so much we can do for poorly written code.

Signature · 1 line
pw-bot: reject