linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: Zhongkun He <hezhongkun.hzk@bytedance.com>
Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, mhocko@suse.com,
	yosry.ahmed@linux.dev, muchun.song@linux.dev, yuzhao@google.com,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [External] Re: [PATCH V3 2/3] mm: add max swappiness arg to lru_gen for anonymous memory only
Date: Fri, 2 May 2025 09:58:45 +0300	[thread overview]
Message-ID: <aBRtJdD6P1rgTsuA@stanley.mountain> (raw)
In-Reply-To: <CACSyD1M8EZ-qEhSGsLTAs+i+YMGPMw-+39Es1SLg-OxEbgg9yA@mail.gmail.com>

On Thu, May 01, 2025 at 09:56:57AM +0800, Zhongkun He wrote:
> Hi Dan
> 
> On Wed, Apr 30, 2025 at 3:59 PM Dan Carpenter <dan.carpenter@linaro.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.

I'm a not sure I understand.  You say you are treating it as a "prefix
match", but sizeof("max") is 4 so this is not treated as a prefix.  Did
you mean to write strlen("max") which does not include the NUL
terminator?

regards,
dan carpenter




  reply	other threads:[~2025-05-02  6:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-09  7:06 [PATCH V3 0/3] add max arg to swappiness in memory.reclaim and lru_gen Zhongkun He
2025-04-09  7:06 ` [PATCH V3 1/3] mm: add swappiness=max arg to memory.reclaim for only anon reclaim Zhongkun He
2025-04-10  2:09   ` Andrew Morton
2025-04-10  3:48     ` [External] " Zhongkun He
2025-04-09  7:06 ` [PATCH V3 2/3] mm: add max swappiness arg to lru_gen for anonymous memory only Zhongkun He
2025-04-10  2:09   ` Andrew Morton
2025-04-10  4:50     ` [External] " Zhongkun He
2025-04-30  7:59   ` Dan Carpenter
2025-05-01  1:56     ` [External] " Zhongkun He
2025-05-02  6:58       ` Dan Carpenter [this message]
2025-05-07  3:27         ` Zhongkun He
2025-04-09  7:06 ` [PATCH V3 3/3] mm: vmscan: add more comments about cache_trim_mode Zhongkun He

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=aBRtJdD6P1rgTsuA@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=hezhongkun.hzk@bytedance.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=yosry.ahmed@linux.dev \
    --cc=yuzhao@google.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