[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CACSyD1M8EZ-qEhSGsLTAs+i+YMGPMw-+39Es1SLg-OxEbgg9yA@mail.gmail.com>
Date: Thu, 1 May 2025 09:56:57 +0800
From: Zhongkun He <hezhongkun.hzk@...edance.com>
To: Dan Carpenter <dan.carpenter@...aro.org>
Cc: akpm@...ux-foundation.org, hannes@...xchg.org, mhocko@...e.com,
yosry.ahmed@...ux.dev, muchun.song@...ux.dev, yuzhao@...gle.com,
linux-mm@...ck.org, linux-kernel@...r.kernel.org
Subject: Re: [External] Re: [PATCH V3 2/3] mm: add max swappiness arg to
lru_gen for anonymous memory only
Hi Dan
On Wed, Apr 30, 2025 at 3:59 PM Dan Carpenter <dan.carpenter@...aro.org> wrote:
>
> On Wed, Apr 09, 2025 at 03:06:19PM +0800, Zhongkun He wrote:
> > + /* set by userspace for anonymous memory only */
> > + if (!strncmp("max", swap_string, sizeof("max"))) {
>
> This pattern of strncmp("foo", str, sizeof("foo")) is exactly the same
> as strcmp(). It doesn't provide any additional security. The strncmp()
> function is meant for matching string prefixes and it's a relatively
> common bug to do this:
>
> intended: if (strcmp(string, "prefix", sizeof("prefix") - 1) == 0) {
> actual: if (strcmp(string, "prefix", sizeof("prefix")) == 0) {
>
Yes, I understand the difference.
> I have a static checker warning for these:
> https://lore.kernel.org/all/30210ed77b40b4b6629de659cb56b9ec7832c447.1744452787.git.dan.carpenter@linaro.org/
>
> If people deliberately misuse the function then it makes it trickier
> to tell accidental mistakes from deliberate mistakes.
>
if (!strncmp("max", swap_string, sizeof("max"))) {
The length of swap_string is 5 because it's read using sscanf, which
will add the null terminator \0
at the end of the string. If we input max into the interface,
swap_string will contain max\0, which is
equivalent to the string "max". Since we only need to compare the
first few characters(There are other
possible inputs as well.) — effectively treating it as a prefix match
— I used strncmp.
Thank you for the reminder, Dan.
> regards,
> dan carpenter
>
Powered by blists - more mailing lists