From: Jakub Kicinski <kuba@kernel.org>
To: Kees Cook <keescook@chromium.org>
Cc: Jijie Shao <shaojijie@huawei.com>,
Leon Romanovsky <leon@kernel.org>,
yisen.zhuang@huawei.com, salil.mehta@huawei.com,
davem@davemloft.net, edumazet@google.com, pabeni@redhat.com,
shenjian15@huawei.com, wangjie125@huawei.com,
liuyonglong@huawei.com, chenhao418@huawei.com,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH net] net: hns3: fix strscpy causing content truncation issue
Date: Thu, 10 Aug 2023 11:47:15 -0700 [thread overview]
Message-ID: <20230810114715.32c8d525@kernel.org> (raw)
In-Reply-To: <202308101103.D0827667B@keescook>
On Thu, 10 Aug 2023 11:23:46 -0700 Kees Cook wrote:
> tldr: use memcpy() instead of strscpy().
>
>
> Okay, I went to go read up on the history here. For my own notes, here's
> the original code, prior to 1cf3d5567f27 ("net: hns3: fix strncpy()
> not using dest-buf length as length issue"):
>
> static void hns3_dbg_fill_content(char *content, u16 len,
> const struct hns3_dbg_item *items,
> const char **result, u16 size)
> {
> char *pos = content;
> u16 i;
>
> memset(content, ' ', len);
> for (i = 0; i < size; i++) {
> if (result)
> strncpy(pos, result[i], strlen(result[i]));
> else
> strncpy(pos, items[i].name, strlen(items[i].name));
>
> pos += strlen(items[i].name) + items[i].interval;
> }
>
> *pos++ = '\n';
> *pos++ = '\0';
> }
>
> The warning to be fixed was:
>
> hclge_debugfs.c:90:25: warning: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
>
> There are a few extra checks added in 1cf3d5567f27, but I'm more curious
> about this original code's intent. It seems very confusing to me.
>
> Firstly, why is "pos" updated based on "strlen(items[i].name)" even when
> "result[i]" is used? Secondly, why is "interval" used? (These concerns
> are mostly addressed in 1cf3d5567f27.)
>
> I guess I'd just like to take a step back and ask, "What is this
> function trying to do?" It seems to be building a series of strings in a
> " "-padding buffer, and it intends that the buffer be newline and %NUL
> terminated.
>
> It looks very much like it wants to _avoid_ adding %NUL termination when
> doing copies, which is why it's using strncpy with a length argument of
> the source string length: it's _forcing_ the copy to not be terminated.
> This is just memcpy.
>
> strtomem() is designed for buffer sizes that can be known at compile
> time, so it's not useful here (as was found), since a string is being
> built up and uses a moving pointer.
>
> I think the correct fix is to use memcpy() instead of strscpy(). No
> %NUL-truncation is desired, the sizes are already determined and bounds
> checked. (And the latter is what likely silenced the compiler warning.)
Got it, thanks!
next prev parent reply other threads:[~2023-08-10 18:47 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-09 2:09 [PATCH net] net: hns3: fix strscpy causing content truncation issue Jijie Shao
2023-08-09 2:14 ` kernel test robot
2023-08-09 7:03 ` Leon Romanovsky
2023-08-10 7:45 ` Jijie Shao
2023-08-10 17:22 ` Jakub Kicinski
2023-08-10 18:23 ` Kees Cook
2023-08-10 18:47 ` Jakub Kicinski [this message]
2023-08-11 2:28 ` Jijie Shao
2023-08-13 9:05 ` Leon Romanovsky
2023-08-10 18:50 ` patchwork-bot+netdevbpf
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=20230810114715.32c8d525@kernel.org \
--to=kuba@kernel.org \
--cc=chenhao418@huawei.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=keescook@chromium.org \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liuyonglong@huawei.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=salil.mehta@huawei.com \
--cc=shaojijie@huawei.com \
--cc=shenjian15@huawei.com \
--cc=stable@vger.kernel.org \
--cc=wangjie125@huawei.com \
--cc=yisen.zhuang@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.