mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bradley Morgan <include@grrlz.net>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: oleg.deomi@gmail.com, bsingharora@gmail.com,
	linux-kernel@vger.kernel.org
Subject: Re: [BUG] taskstats: REGISTER_CPUMASK silently truncates the last byte of the cpumask string
Date: Thu, 23 Jul 2026 22:11:43 +0100	[thread overview]
Message-ID: <EC49FE41-7F5F-41E0-A07A-ABEB8ECA514D@grrlz.net> (raw)
In-Reply-To: <20260723122825.4626f7e9efb30b22e2ac9d9b@linux-foundation.org>

On 23 July 2026 20:28:25 BST, Andrew Morton <akpm@linux-foundation.org>
wrote:
>On Thu, 23 Jul 2026 14:04:25 +0100 Bradley Morgan <include@grrlz.net>
>wrote:
>
>> Hi Andrew,
>> 
>> I dont think this actually fixes it... :(
>> 
>
>Thanks.  Obviously I did't try very hard.
>
>> 
>> > -	data = kmalloc(len, GFP_KERNEL);
>> > +	data = kmalloc(len + 1, GFP_KERNEL);
>> >  	if (!data)
>> >  		return -ENOMEM;
>> >  	nla_strscpy(data, na, len);
>> 
>> nla_strscpy() copies at most dstsize - 1 bytes, and dstsize is still
>> len here. When the attr payload comes in without a trailing NUL,
>> srclen == len >= dstsize and the last byte still gets cut off. The
>> bigger buffer just adds a byte nothing ever writes to (the zero pad
>> in nla_strscpy() only goes up to dstsize).
>> 
>> Olegs report even spells this out in its suggested fix direction:
>> allocate len + 1 *and* pass len + 1 as dstsize. The patch only
>> picked up the first half. The fix is something like:
>> 
>> 	data = kmalloc(len + 1, GFP_KERNEL);
>> 	if (!data)
>> 		return -ENOMEM;
>> 	nla_strscpy(data, na, len + 1);
>> 
>> or skip the dance entirely, nla_strdup() already does exactly this:
>> 
>> 	data = nla_strdup(na, GFP_KERNEL);
>> 	if (!data)
>> 		return -ENOMEM;
>> 
>> Btw the bug only bites when the sender doesnt NUL terminate the
>> attr payload; senders that include the NUL (srclen gets decremented
>> for the trailing NUL, so srclen < dstsize) were always fine. Thats
>> probably why this survived 20 years. Might be worth a line in
>> the changelog. And as the report already notes, the policy is
>> NLA_STRING, not NLA_NUL_STRING, so a payload without the trailing
>> NUL is legit input here.
>
>OK, I'll drop this.  Please send along a fix sometime.
>


Hi Andrew, let me be honest, I CBA to do a new thread, here's my suggestion


From 0d5d963d0497e3d2c739e8b5c1403f707054e543 Mon Sep 17 00:00:00 2001
From: Bradley Morgan <include@grrlz.net>
Date: Thu, 23 Jul 2026 21:09:22 +0000
Subject: [PATCH] taskstats: fix cpumask parsing cutting off the last character

parse() hands nla_strscpy() len as dstsize, and nla_strscpy() copies
at most dstsize - 1 bytes. When the attr payload comes in without a
trailing NUL, srclen == len >= dstsize and the last character of the
cpumask string gets cut off. Register "0-15" and you are silently
listening on "0-1", exit data for the rest never shows up.

The bug only bites when the sender doesnt NUL terminate the payload;
senders that include the NUL were always fine (srclen gets decremented
for the trailing NUL, so srclen < dstsize). Thats probably why this
survived 20 years. And the policy is NLA_STRING, not NLA_NUL_STRING,
so a payload without the trailing NUL is legit input here.

Skip the kmalloc/nla_strscpy dance entirely and use nla_strdup(),
which already allocates srclen + 1 and terminates. The nla_len()
bounds checks stay as they were.

Fixes: f9fd8914c1ac ("[PATCH] per-task delay accounting taskstats interface: control exit data through cpumasks")
Reported-by: Oleg Deomi <oleg.deomi@gmail.com>
Closes: https://lore.kernel.org/CAByWkfZ6b1=3H9pwkz-dDQOs9cZaF-HYQ6b9Yb0=Hq2r1Vv_Pw@mail.gmail.com
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Bradley Morgan <include@grrlz.net>
---
 kernel/taskstats.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/kernel/taskstats.c b/kernel/taskstats.c
index d19bff9..632ca95 100644
--- a/kernel/taskstats.c
+++ b/kernel/taskstats.c
@@ -371,10 +371,9 @@ static int parse(struct nlattr *na, struct cpumask *mask)
 		return -E2BIG;
 	if (len < 1)
 		return -EINVAL;
-	data = kmalloc(len, GFP_KERNEL);
+	data = nla_strdup(na, GFP_KERNEL);
 	if (!data)
 		return -ENOMEM;
-	nla_strscpy(data, na, len);
 	ret = cpulist_parse(data, mask);
 	kfree(data);
 	return ret;
-- 
2.49.0


I better hope my mail client doesn't break the hell out of this! :)

Thanks!

  reply	other threads:[~2026-07-23 21:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-21 12:52 Oleg Deomi
2026-07-21 23:38 ` Andrew Morton
2026-07-23 13:04 ` Bradley Morgan
2026-07-23 19:28   ` Andrew Morton
2026-07-23 21:11     ` Bradley Morgan [this message]
2026-07-23 23:17       ` Andrew Morton
2026-07-23 23:19         ` Bradley Morgan
2026-07-24 15:32         ` Bradley Morgan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=EC49FE41-7F5F-41E0-A07A-ABEB8ECA514D@grrlz.net \
    --to=include@grrlz.net \
    --cc=akpm@linux-foundation.org \
    --cc=bsingharora@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg.deomi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®