[PATCH 0/2] taskstats: fix cgroupstats invalid fd handling and add selftests

Yiyang Chen posted 2 patches 2 weeks, 1 day ago
kernel/taskstats.c                         |   2 +-
tools/testing/selftests/acct/Makefile      |   1 +
tools/testing/selftests/acct/cgroupstats.c | 354 +++++++++++++++++++++
3 files changed, 356 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/acct/cgroupstats.c
[PATCH 0/2] taskstats: fix cgroupstats invalid fd handling and add selftests
Posted by Yiyang Chen 2 weeks, 1 day ago
This series fixes an issue where cgroupstats mishandles invalid file
descriptors, and introduces a functional kselftest to prevent regressions.

When an invalid file descriptor is passed via CGROUPSTATS_CMD_ATTR_FD,
cgroupstats_user_cmd() returns 0 instead of an error code. This leads to
two broken behaviors depending on netlink flags:
- Callers without NLM_F_ACK block indefinitely on recv().
- Callers with NLM_F_ACK receive a misleading success ACK (errno == 0)
  but no actual statistics payload.

The first patch addresses this by returning -EBADF when the fd cannot be
resolved. The second patch adds a comprehensive kselftest covering both
the valid cgroup v1 query and the invalid fd paths (with and without
NLM_F_ACK) to ensure the fixes work as intended.

Yiyang Chen (2):
  taskstats: return -EBADF when cgroupstats receives an invalid fd
  selftests/acct: add cgroupstats functional test

 kernel/taskstats.c                         |   2 +-
 tools/testing/selftests/acct/Makefile      |   1 +
 tools/testing/selftests/acct/cgroupstats.c | 354 +++++++++++++++++++++
 3 files changed, 356 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/acct/cgroupstats.c

-- 
2.43.0
Re: [PATCH 0/2] taskstats: fix cgroupstats invalid fd handling and add selftests
Posted by Bradley Morgan 1 day ago
not terrible.

can't be bothered to do the quotes

I really like this series.

Reviewed-by: Bradley Morgan <include@grrlz.net>

Thanks!
Re: [PATCH 0/2] taskstats: fix cgroupstats invalid fd handling and add selftests
Posted by Andrew Morton 2 weeks, 1 day ago
On Sat, 11 Jul 2026 04:07:35 +0800 Yiyang Chen <cyyzero16@gmail.com> wrote:

> This series fixes an issue where cgroupstats mishandles invalid file
> descriptors, and introduces a functional kselftest to prevent regressions.
> 
> When an invalid file descriptor is passed via CGROUPSTATS_CMD_ATTR_FD,
> cgroupstats_user_cmd() returns 0 instead of an error code. This leads to
> two broken behaviors depending on netlink flags:
> - Callers without NLM_F_ACK block indefinitely on recv().
> - Callers with NLM_F_ACK receive a misleading success ACK (errno == 0)
>   but no actual statistics payload.
> 
> The first patch addresses this by returning -EBADF when the fd cannot be
> resolved. The second patch adds a comprehensive kselftest covering both
> the valid cgroup v1 query and the invalid fd paths (with and without
> NLM_F_ACK) to ensure the fixes work as intended.

lgtm, thanks.

It's a little odd to group two unrelated(?) changes in one series like
this.  Oh well, let's not redo things for this.

Should the netlink_open() in taskstats_fill_stats_tgid.c also be
setting that timeout?  If so, we can share a single implementation - a
little cleanup.

There's a lot of code duplication in selftests/ :(
Re: [PATCH 0/2] taskstats: fix cgroupstats invalid fd handling and add selftests
Posted by Yiyang Chen 2 weeks ago
On Sat, Jul 11, 2026 at 6:59 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Sat, 11 Jul 2026 04:07:35 +0800 Yiyang Chen <cyyzero16@gmail.com> wrote:
>
> > This series fixes an issue where cgroupstats mishandles invalid file
> > descriptors, and introduces a functional kselftest to prevent regressions.
> >
> > When an invalid file descriptor is passed via CGROUPSTATS_CMD_ATTR_FD,
> > cgroupstats_user_cmd() returns 0 instead of an error code. This leads to
> > two broken behaviors depending on netlink flags:
> > - Callers without NLM_F_ACK block indefinitely on recv().
> > - Callers with NLM_F_ACK receive a misleading success ACK (errno == 0)
> >   but no actual statistics payload.
> >
> > The first patch addresses this by returning -EBADF when the fd cannot be
> > resolved. The second patch adds a comprehensive kselftest covering both
> > the valid cgroup v1 query and the invalid fd paths (with and without
> > NLM_F_ACK) to ensure the fixes work as intended.
>
> lgtm, thanks.
>
> It's a little odd to group two unrelated(?) changes in one series like
> this.  Oh well, let's not redo things for this.

I've put the fix and the corresponding regression test into a patch
series, but since there
didn't seem to be a test suite for cgroupstats before, I also added a
test case for a typical
scenario (test 1).

>
> Should the netlink_open() in taskstats_fill_stats_tgid.c also be
> setting that timeout?  If so, we can share a single implementation - a
> little cleanup.
>
> There's a lot of code duplication in selftests/ :(

Thanks for you advise:)
I'll do some cleanup stuff in the follow-up that extracts the duplicated helpers
into common headers for taskstats selftests