* [PATCH] mm: page_counter: reject empty string in page_counter_memparse()
@ 2026-08-17 4:26 Tao Cui
2026-08-17 16:16 ` Shakeel Butt
0 siblings, 1 reply; 5+ messages in thread
From: Tao Cui @ 2026-08-17 4:26 UTC (permalink / raw)
To: akpm
Cc: linux-mm, cgroups, linux-kernel, hannes, mhocko, roman.gushchin,
shakeel.butt, muchun.song, cui.tao, Tao Cui
From: Tao Cui <cuitao@kylinos.cn>
memparse() consumes no characters on an empty input and leaves the
end pointer at the terminating NUL. The only validation in
page_counter_memparse() checks for trailing characters, so an empty
input slips through and the limit becomes 0.
All limit write callbacks of the memory controller strstrip() the
input before calling this helper, so a script that writes an unset
variable hits this path:
LIMIT=
echo "$LIMIT" > $CG/memory.max
echo $?
0
cat $CG/memory.max
0
Nothing reports the mistake: the limit is now 0 and the OOM killer
goes after every task in the cgroup. The same happens for
memory.min, memory.low, memory.high, memory.swap.high,
memory.swap.max and memory.zswap.max, where 0 silently removes the
protection or disables swap and zswap.
Reject the input when no characters were consumed, which is the one
case the trailing-character check cannot catch.
Fixes: 3e32cb2e0a12 ("mm: memcontrol: lockless page counters")
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
mm/page_counter.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/page_counter.c b/mm/page_counter.c
index 661e0f2a5127..d14db705b04f 100644
--- a/mm/page_counter.c
+++ b/mm/page_counter.c
@@ -281,7 +281,7 @@ int page_counter_memparse(const char *buf, const char *max,
}
bytes = memparse(buf, &end);
- if (*end != '\0')
+ if (*end != '\0' || end == buf)
return -EINVAL;
*nr_pages = min(bytes / PAGE_SIZE, (u64)PAGE_COUNTER_MAX);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: page_counter: reject empty string in page_counter_memparse()
2026-08-17 4:26 [PATCH] mm: page_counter: reject empty string in page_counter_memparse() Tao Cui
@ 2026-08-17 16:16 ` Shakeel Butt
2026-08-18 7:45 ` Michal Hocko
0 siblings, 1 reply; 5+ messages in thread
From: Shakeel Butt @ 2026-08-17 16:16 UTC (permalink / raw)
To: Tao Cui
Cc: akpm, linux-mm, cgroups, linux-kernel, hannes, mhocko,
roman.gushchin, muchun.song, Tao Cui
On Mon, Aug 17, 2026 at 12:26:52PM +0800, Tao Cui wrote:
> From: Tao Cui <cuitao@kylinos.cn>
>
> memparse() consumes no characters on an empty input and leaves the
> end pointer at the terminating NUL. The only validation in
> page_counter_memparse() checks for trailing characters, so an empty
> input slips through and the limit becomes 0.
>
> All limit write callbacks of the memory controller strstrip() the
> input before calling this helper, so a script that writes an unset
> variable hits this path:
>
> LIMIT=
> echo "$LIMIT" > $CG/memory.max
> echo $?
> 0
> cat $CG/memory.max
> 0
>
> Nothing reports the mistake: the limit is now 0 and the OOM killer
> goes after every task in the cgroup. The same happens for
> memory.min, memory.low, memory.high, memory.swap.high,
> memory.swap.max and memory.zswap.max, where 0 silently removes the
> protection or disables swap and zswap.
>
> Reject the input when no characters were consumed, which is the one
> case the trailing-character check cannot catch.
>
> Fixes: 3e32cb2e0a12 ("mm: memcontrol: lockless page counters")
> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> ---
> mm/page_counter.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/page_counter.c b/mm/page_counter.c
> index 661e0f2a5127..d14db705b04f 100644
> --- a/mm/page_counter.c
> +++ b/mm/page_counter.c
> @@ -281,7 +281,7 @@ int page_counter_memparse(const char *buf, const char *max,
> }
>
> bytes = memparse(buf, &end);
> - if (*end != '\0')
> + if (*end != '\0' || end == buf)
> return -EINVAL;
I wonder if someone started depending on this behavior. In that case it is
better to return error instead of silently ignore, so we will hear complains
loudly. This looks good to me.
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
>
> *nr_pages = min(bytes / PAGE_SIZE, (u64)PAGE_COUNTER_MAX);
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: page_counter: reject empty string in page_counter_memparse()
2026-08-17 16:16 ` Shakeel Butt
@ 2026-08-18 7:45 ` Michal Hocko
2026-08-18 15:06 ` Shakeel Butt
0 siblings, 1 reply; 5+ messages in thread
From: Michal Hocko @ 2026-08-18 7:45 UTC (permalink / raw)
To: Shakeel Butt
Cc: Tao Cui, akpm, linux-mm, cgroups, linux-kernel, hannes,
roman.gushchin, muchun.song, Tao Cui
On Mon 17-08-26 09:16:40, Shakeel Butt wrote:
> On Mon, Aug 17, 2026 at 12:26:52PM +0800, Tao Cui wrote:
> > From: Tao Cui <cuitao@kylinos.cn>
> >
> > memparse() consumes no characters on an empty input and leaves the
> > end pointer at the terminating NUL. The only validation in
> > page_counter_memparse() checks for trailing characters, so an empty
> > input slips through and the limit becomes 0.
> >
> > All limit write callbacks of the memory controller strstrip() the
> > input before calling this helper, so a script that writes an unset
> > variable hits this path:
> >
> > LIMIT=
> > echo "$LIMIT" > $CG/memory.max
> > echo $?
> > 0
> > cat $CG/memory.max
> > 0
> >
> > Nothing reports the mistake: the limit is now 0 and the OOM killer
> > goes after every task in the cgroup. The same happens for
> > memory.min, memory.low, memory.high, memory.swap.high,
> > memory.swap.max and memory.zswap.max, where 0 silently removes the
> > protection or disables swap and zswap.
> >
> > Reject the input when no characters were consumed, which is the one
> > case the trailing-character check cannot catch.
> >
> > Fixes: 3e32cb2e0a12 ("mm: memcontrol: lockless page counters")
> > Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> > ---
> > mm/page_counter.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/mm/page_counter.c b/mm/page_counter.c
> > index 661e0f2a5127..d14db705b04f 100644
> > --- a/mm/page_counter.c
> > +++ b/mm/page_counter.c
> > @@ -281,7 +281,7 @@ int page_counter_memparse(const char *buf, const char *max,
> > }
> >
> > bytes = memparse(buf, &end);
> > - if (*end != '\0')
> > + if (*end != '\0' || end == buf)
> > return -EINVAL;
>
> I wonder if someone started depending on this behavior. In that case it is
> better to return error instead of silently ignore, so we will hear complains
> loudly. This looks good to me.
This is backward incompatible change and I am wondering why should we
even risk regression.
> Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: page_counter: reject empty string in page_counter_memparse()
2026-08-18 7:45 ` Michal Hocko
@ 2026-08-18 15:06 ` Shakeel Butt
2026-08-18 16:43 ` Michal Hocko
0 siblings, 1 reply; 5+ messages in thread
From: Shakeel Butt @ 2026-08-18 15:06 UTC (permalink / raw)
To: Michal Hocko
Cc: Tao Cui, akpm, linux-mm, cgroups, linux-kernel, hannes,
roman.gushchin, muchun.song, Tao Cui
On Tue, Aug 18, 2026 at 09:45:56AM +0200, Michal Hocko wrote:
> On Mon 17-08-26 09:16:40, Shakeel Butt wrote:
> > On Mon, Aug 17, 2026 at 12:26:52PM +0800, Tao Cui wrote:
> > > From: Tao Cui <cuitao@kylinos.cn>
> > >
> > > memparse() consumes no characters on an empty input and leaves the
> > > end pointer at the terminating NUL. The only validation in
> > > page_counter_memparse() checks for trailing characters, so an empty
> > > input slips through and the limit becomes 0.
> > >
> > > All limit write callbacks of the memory controller strstrip() the
> > > input before calling this helper, so a script that writes an unset
> > > variable hits this path:
> > >
> > > LIMIT=
> > > echo "$LIMIT" > $CG/memory.max
> > > echo $?
> > > 0
> > > cat $CG/memory.max
> > > 0
> > >
> > > Nothing reports the mistake: the limit is now 0 and the OOM killer
> > > goes after every task in the cgroup. The same happens for
> > > memory.min, memory.low, memory.high, memory.swap.high,
> > > memory.swap.max and memory.zswap.max, where 0 silently removes the
> > > protection or disables swap and zswap.
> > >
> > > Reject the input when no characters were consumed, which is the one
> > > case the trailing-character check cannot catch.
> > >
> > > Fixes: 3e32cb2e0a12 ("mm: memcontrol: lockless page counters")
> > > Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> > > ---
> > > mm/page_counter.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/mm/page_counter.c b/mm/page_counter.c
> > > index 661e0f2a5127..d14db705b04f 100644
> > > --- a/mm/page_counter.c
> > > +++ b/mm/page_counter.c
> > > @@ -281,7 +281,7 @@ int page_counter_memparse(const char *buf, const char *max,
> > > }
> > >
> > > bytes = memparse(buf, &end);
> > > - if (*end != '\0')
> > > + if (*end != '\0' || end == buf)
> > > return -EINVAL;
> >
> > I wonder if someone started depending on this behavior. In that case it is
> > better to return error instead of silently ignore, so we will hear complains
> > loudly. This looks good to me.
>
> This is backward incompatible change and I am wondering why should we
> even risk regression.
Mainly I was wondering if this is intentional or unintentional. If this us
unintentional, can we fix it without anyone noticing?
However if we are ok with this then let's make is formal and make this a
documented behavior. I don't have any strong opinion either way but I think you
are saying it safer to just assume this is intentional. Fine with me.
Tao, can you send a patch to Documentation/admin-guide/cgroup-v2.rst which
explicitly mention this behavior?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: page_counter: reject empty string in page_counter_memparse()
2026-08-18 15:06 ` Shakeel Butt
@ 2026-08-18 16:43 ` Michal Hocko
0 siblings, 0 replies; 5+ messages in thread
From: Michal Hocko @ 2026-08-18 16:43 UTC (permalink / raw)
To: Shakeel Butt
Cc: Tao Cui, akpm, linux-mm, cgroups, linux-kernel, hannes,
roman.gushchin, muchun.song, Tao Cui
On Tue 18-08-26 08:06:22, Shakeel Butt wrote:
> On Tue, Aug 18, 2026 at 09:45:56AM +0200, Michal Hocko wrote:
> > On Mon 17-08-26 09:16:40, Shakeel Butt wrote:
> > > On Mon, Aug 17, 2026 at 12:26:52PM +0800, Tao Cui wrote:
> > > > From: Tao Cui <cuitao@kylinos.cn>
> > > >
> > > > memparse() consumes no characters on an empty input and leaves the
> > > > end pointer at the terminating NUL. The only validation in
> > > > page_counter_memparse() checks for trailing characters, so an empty
> > > > input slips through and the limit becomes 0.
> > > >
> > > > All limit write callbacks of the memory controller strstrip() the
> > > > input before calling this helper, so a script that writes an unset
> > > > variable hits this path:
> > > >
> > > > LIMIT=
> > > > echo "$LIMIT" > $CG/memory.max
> > > > echo $?
> > > > 0
> > > > cat $CG/memory.max
> > > > 0
> > > >
> > > > Nothing reports the mistake: the limit is now 0 and the OOM killer
> > > > goes after every task in the cgroup. The same happens for
> > > > memory.min, memory.low, memory.high, memory.swap.high,
> > > > memory.swap.max and memory.zswap.max, where 0 silently removes the
> > > > protection or disables swap and zswap.
> > > >
> > > > Reject the input when no characters were consumed, which is the one
> > > > case the trailing-character check cannot catch.
> > > >
> > > > Fixes: 3e32cb2e0a12 ("mm: memcontrol: lockless page counters")
> > > > Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> > > > ---
> > > > mm/page_counter.c | 2 +-
> > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/mm/page_counter.c b/mm/page_counter.c
> > > > index 661e0f2a5127..d14db705b04f 100644
> > > > --- a/mm/page_counter.c
> > > > +++ b/mm/page_counter.c
> > > > @@ -281,7 +281,7 @@ int page_counter_memparse(const char *buf, const char *max,
> > > > }
> > > >
> > > > bytes = memparse(buf, &end);
> > > > - if (*end != '\0')
> > > > + if (*end != '\0' || end == buf)
> > > > return -EINVAL;
> > >
> > > I wonder if someone started depending on this behavior. In that case it is
> > > better to return error instead of silently ignore, so we will hear complains
> > > loudly. This looks good to me.
> >
> > This is backward incompatible change and I am wondering why should we
> > even risk regression.
>
> Mainly I was wondering if this is intentional or unintentional. If this us
> unintentional, can we fix it without anyone noticing?
My guess would be this was just omission. Those happen and over years we
have learned that userspace is quite creative at using those.
> However if we are ok with this then let's make is formal and make this a
> documented behavior. I don't have any strong opinion either way but I think you
> are saying it safer to just assume this is intentional. Fine with me.
My main question is why should we even bother to change this in the
first place? Is that reason stronger than a theoretical breakage of
userspace that we might learn much later?
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-08-18 16:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-17 4:26 [PATCH] mm: page_counter: reject empty string in page_counter_memparse() Tao Cui
2026-08-17 16:16 ` Shakeel Butt
2026-08-18 7:45 ` Michal Hocko
2026-08-18 15:06 ` Shakeel Butt
2026-08-18 16:43 ` Michal Hocko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox