Skip to contentSkip to conversation

net/sched: fix potential stack infoleak in em_text_dump()

PATCH Applied Bernard Ladenthin 4 participants 5 messages Sep 18 – Sep 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/sched: fix potential stack infoleak in em_text_dump()

em_text_dump() allocates struct tcf_em_text on the stack without zeroing it. strscpy() writes the algorithm name and a NUL terminator into conf.algo[], leaving the remaining bytes uninitialised. nla_put_nohdr() then copies the full struct to the netlink response.

KMSAN on Linux 7.2-rc6 reports two kernel-infoleak splats from this path, one triggered via "tc filter show" and one via a raw RTM_GETTFILTER dump:

  BUG: KMSAN: kernel-infoleak in _copy_to_iter+0x1c9/0x2620
    nla_put_nohdr+0x83/0x130
    em_text_dump+0x291/0x550
  Local variable conf created at: em_text_dump+0x5d/0x550
  Bytes 168-179 of 199 are uninitialized

I am not certain whether this constitutes a real security problem in
practice: the test was conducted in a controlled KMSAN environment and the leaked stack bytes may or may not carry sensitive data on actual production kernels. I am reporting it because KMSAN flagged it as a kernel-infoleak and the fix is straightforward. I can provide a userspace reproducer on request.

The original code used strncpy() which zero-pads to the destination size. Commit b04202d6065c ("net/sched: replace strncpy with strscpy") replaced it with strscpy(), which does not pad, creating this condition. Zero-initialising the struct closes it.

Fixes b04202d6065c ("net/sched: replace strncpy with strscpy")
Assisted-by Claude:claude-sonnet-4-6 [KMSAN]
---
 net/sched/em_text.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/em_text.c +1 -1
index 343f1aebeec2..4132f8c3c5fc 100644
@@ -113,7 +113,7 @@ static void em_text_destroy(struct tcf_ematch *m)
 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
 {
 	struct text_match *tm = EM_TEXT_PRIV(m);
-	struct tcf_em_text conf;
+	struct tcf_em_text conf = {};
 
 	strscpy(conf.algo, tm->config->ops->name);
 	conf.from_offset = tm->from_offset;
Signature · 1 line
2.49.0.windows.1
maintainer
#1 lore raw
Quoted 22 lines · Bernard Ladenthin

em_text_dump() allocates struct tcf_em_text on the stack without zeroing it. strscpy() writes the algorithm name and a NUL terminator into conf.algo[], leaving the remaining bytes uninitialised. nla_put_nohdr() then copies the full struct to the netlink response.

KMSAN on Linux 7.2-rc6 reports two kernel-infoleak splats from this path, one triggered via "tc filter show" and one via a raw RTM_GETTFILTER dump:

  BUG: KMSAN: kernel-infoleak in _copy_to_iter+0x1c9/0x2620
    nla_put_nohdr+0x83/0x130
    em_text_dump+0x291/0x550
  Local variable conf created at: em_text_dump+0x5d/0x550
  Bytes 168-179 of 199 are uninitialized

I am not certain whether this constitutes a real security problem in
practice: the test was conducted in a controlled KMSAN environment and the leaked stack bytes may or may not carry sensitive data on actual production kernels. I am reporting it because KMSAN flagged it as a kernel-infoleak and the fix is straightforward. I can provide a userspace reproducer on request.

Quoted 5 lines

The original code used strncpy() which zero-pads to the destination size. Commit b04202d6065c ("net/sched: replace strncpy with strscpy") replaced it with strscpy(), which does not pad, creating this condition. Zero-initialising the struct closes it.

Two things:
1) Don't Cc security@ in the future on trivial things like this.
2) You MUST provide a reproducer - even for something as trivial as this. Since you posted the splat, you must have a reproducer. Share it. This one looks hard to re-create with tdc, so please provide the poc. We only pay attention (prioritize) patches that post repros which our automated system can use to reproduce.

cheers,
jamal

Quoted 24 lines
Fixes b04202d6065c ("net/sched: replace strncpy with strscpy")
Assisted-by Claude:claude-sonnet-4-6 [KMSAN]
---
 net/sched/em_text.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/em_text.c +1 -2
index 343f1aebeec2..4132f8c3c5fc 100644
@@ -113,7 +113,7 @@ static void em_text_destroy(struct tcf_ematch *m)
 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
 {
        struct text_match *tm = EM_TEXT_PRIV(m);
-       struct tcf_em_text conf;
+       struct tcf_em_text conf = {};
 
        strscpy(conf.algo, tm->config->ops->name);
        conf.from_offset = tm->from_offset;
--

2.49.0.windows.1

maintainer Tested-by Hybris Acked-by on v1 1/1
#2 lore raw
In reply to Bernard Ladenthin
Quoted 27 lines · Bernard Ladenthin

em_text_dump() allocates struct tcf_em_text on the stack without zeroing it. strscpy() writes the algorithm name and a NUL terminator into conf.algo[], leaving the remaining bytes uninitialised. nla_put_nohdr() then copies the full struct to the netlink response.

KMSAN on Linux 7.2-rc6 reports two kernel-infoleak splats from this path, one triggered via "tc filter show" and one via a raw RTM_GETTFILTER dump:

  BUG: KMSAN: kernel-infoleak in _copy_to_iter+0x1c9/0x2620
    nla_put_nohdr+0x83/0x130
    em_text_dump+0x291/0x550
  Local variable conf created at: em_text_dump+0x5d/0x550
  Bytes 168-179 of 199 are uninitialized

I am not certain whether this constitutes a real security problem in
practice: the test was conducted in a controlled KMSAN environment and the leaked stack bytes may or may not carry sensitive data on actual production kernels. I am reporting it because KMSAN flagged it as a kernel-infoleak and the fix is straightforward. I can provide a userspace reproducer on request.

The original code used strncpy() which zero-pads to the destination size. Commit b04202d6065c ("net/sched: replace strncpy with strscpy") replaced it with strscpy(), which does not pad, creating this condition. Zero-initialising the struct closes it.

Quoted 4 lines
Fixes b04202d6065c ("net/sched: replace strncpy with strscpy")
Assisted-by Claude:claude-sonnet-4-6 [KMSAN]

Thanks for providing the PoC offline.

I reviewed, but I think ACK also means "I reviewed". So:

cheers,
jamal

Quoted 20 lines
---
 net/sched/em_text.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/sched/em_text.c +1 -2
index 343f1aebeec2..4132f8c3c5fc 100644
@@ -113,7 +113,7 @@ static void em_text_destroy(struct tcf_ematch *m)
 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
 {
        struct text_match *tm = EM_TEXT_PRIV(m);
-       struct tcf_em_text conf;
+       struct tcf_em_text conf = {};
 
        strscpy(conf.algo, tm->config->ops->name);
        conf.from_offset = tm->from_offset;
--

2.49.0.windows.1

#3 lore raw
In reply to Bernard Ladenthin
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 (1) · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The Fixes: tag and the commit message body misattribute the stack infoleak to b04202d6065c ("net/sched: replace strncpy with…

Pre-existing issues:
- [High] Out-of-bounds read on the request (parse) side of the same ABI
field: em_text_change() (net/sched/em_text.c:50) passes the raw, user-…

--

net/sched: fix potential stack infoleak in em_text_dump()

This zero-initialises the on-stack struct tcf_em_text in em_text_dump() so that the bytes of conf.algo[] not written by strscpy() are not copied to user space by nla_put_nohdr(). KMSAN reported the leak on both "tc filter show" and a raw RTM_GETTFILTER dump.

A couple of questions below, one about the Fixes attribution and one about the matching parse path.

Quoted 6 lines

The original code used strncpy() which zero-pads to the destination size. Commit b04202d6065c ("net/sched: replace strncpy with strscpy") replaced it with strscpy(), which does not pad, creating this condition. Zero-initialising the struct closes it.

Fixes b04202d6065c ("net/sched: replace strncpy with strscpy")

[Severity: Medium]
Is this attribution accurate? The call site that b04202d6065c replaced was bounded by sizeof(conf.algo) - 1:

net/sched/em_text.c before b04202d6065c:

	strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);

strncpy() zero-pads only up to the supplied bound n, not to the destination size, so with a bound of 15 on the 16-byte algo[] the last byte was never written:

include/uapi/linux/tc_ematch/tc_em_text.h:

	struct tcf_em_text {
		char		algo[TC_EM_TEXT_ALGOSIZ];
		...

Doesn't that mean nla_put_nohdr(skb, sizeof(conf), &conf) already copied one uninitialised stack byte to user space before b04202d6065c, and that b04202d6065c only widened the window (12 bytes for a three character name such as "kmp", which matches the quoted "Bytes 168-179 of 199")?

If so, could the Fixes tag point at the introduction of em_text.c, or the patch carry Cc: [email protected] covering the older trees? As written, stable selection driven by this Fixes tag will skip every tree older than b04202d6065c, which keeps leaking the final byte of algo[] on every dump of a text ematch.

Could the sentence "The original code used strncpy() which zero-pads to the destination size ... creating this condition" also be adjusted, since the old call padded only to the bound of 15?

The code change itself looks correct and complete for the current tree:
the uAPI struct has no interior padding (algo[0..15], from_offset, to_offset, pattern_len, the layer bitfield byte, pad, sizeof == 24), so = {} clears every transmitted byte.

net/sched/em_text.c static void em_text_destroy(struct tcf_ematch *m) · line 118

Quoted 12 lines
net/sched/em_text.c +1 -1
index 343f1aebeec2..4132f8c3c5fc 100644
@@ -113,7 +113,7 @@ static void em_text_destroy(struct tcf_ematch *m)
 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
 {
 	struct text_match *tm = EM_TEXT_PRIV(m);
-	struct tcf_em_text conf;
+	struct tcf_em_text conf = {};
 
 	strscpy(conf.algo, tm->config->ops->name);

[Severity: High]
This is a pre-existing issue and is not introduced by this patch, but since it concerns the same algo[] field it may be worth handling in the same series.

After this change the emitter always produces a NUL-terminated, zero-padded algo[], while the parser accepts an algo[] with no NUL at all. em_text_change() validates only len, the layers and the offsets:

net/sched/em_text.c:em_text_change() {
	if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
		return -EINVAL;
	if (conf->from_layer > conf->to_layer)
		return -EINVAL;
	...
retry:
	ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
				     conf->pattern_len, GFP_KERNEL, flags);
	...
		flags |= TS_AUTOLOAD;
		goto retry;
}

data here points straight into the user-supplied rtnetlink message, with no copy and no termination:

net/sched/ematch.c:tcf_em_validate() {
	void *data = (void *) em_hdr + sizeof(*em_hdr);
	...
}

On the TS_AUTOLOAD retry the unterminated name reaches:

lib/textsearch.c:textsearch_prepare() {
	...
		request_module("ts_%s", algo);
}

and vsnprintf's %s handler scans for a zero byte with no length limit on the source side:

lib/vsprintf.c:string_nocheck() {
	int lim = spec.precision;
	...
	while (lim--) {
		char c = *s++;
		if (!c)
			break;
	...
}

with spec.precision == -1 for a plain %s. If a caller sets every byte of algo[], from_offset, to_offset, pattern_len, the layer bitfield, pad and the pattern to non-zero values, can this read past the end of algo[] and past the end of the netlink message into adjacent kernel memory, and embed those bytes into the modprobe request name? For requests large enough that netlink_alloc_large_skb() uses vmalloc, the scan would be walking towards the guard page.

The reachable path is RTM_NEWTFILTER with an ematch tree containing a TCF_EM_TEXT match whose algo does not resolve to a registered ts_ops, so the first textsearch_prepare() returns -ENOENT and the retry with TS_AUTOLOAD is taken. That needs only CAP_NET_ADMIN in a network namespace, plus CONFIG_MODULES.

Would rejecting an algo[] without a NUL byte in em_text_change() be the right companion fix, so both ends of the uAPI agree on the format?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260918133953.12494-1-bernard.ladenthin%40gmail.com
#4 lore raw
In reply to Bernard Ladenthin
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 18 Sep 2026 15:39:37 +0200 you wrote:

em_text_dump() allocates struct tcf_em_text on the stack without zeroing it. strscpy() writes the algorithm name and a NUL terminator into conf.algo[], leaving the remaining bytes uninitialised. nla_put_nohdr() then copies the full struct to the netlink response.

KMSAN on Linux 7.2-rc6 reports two kernel-infoleak splats from this path, one triggered via "tc filter show" and one via a raw RTM_GETTFILTER dump:

[...]

Here is the summary with links:
- net/sched: fix potential stack infoleak in em_text_dump()

    https://git.kernel.org/netdev/net/c/9c572a83037a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html