From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <buaajxlj@163.com>
Cc: <davem@davemloft.net>, <edumazet@google.com>, <horms@kernel.org>,
<kuba@kernel.org>, <kuniyu@amazon.com>, <liangjie@lixiang.com>,
<linux-kernel@vger.kernel.org>, <mhal@rbox.co>,
<netdev@vger.kernel.org>, <pabeni@redhat.com>
Subject: Re: [PATCH net-next] af_unix: Refine UNIX domain sockets autobind identifier length
Date: Thu, 6 Feb 2025 13:01:18 +0900 [thread overview]
Message-ID: <20250206040118.77016-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20250205100904.2534565-1-buaajxlj@163.com>
From: Liang Jie <buaajxlj@163.com>
Date: Wed, 5 Feb 2025 18:09:04 +0800
> The logs from 'netdev/build_allmodconfig_warn' indicate that the patch has
> given rise to the following warning:
>
> - ../net/unix/af_unix.c: In function ‘unix_autobind’:
> - ../net/unix/af_unix.c:1227:48: warning: ‘sprintf’ writing a terminating nul past the end of the destination [-Wformat-overflow=]
> - 1227 | sprintf(addr->name->sun_path + 1, "%0*x", AUTOBIND_LEN - 1, ordernum);
> - | ^
> - ../net/unix/af_unix.c:1227:9: note: ‘sprintf’ output 6 bytes into a destination of size 5
> - 1227 | sprintf(addr->name->sun_path + 1, "%0*x", AUTOBIND_LEN - 1, ordernum);
> - | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> It appears that the 'sprintf' call attempts to write a terminating null
> byte past the end of the 'sun_path' array, potentially causing an overflow.
>
> To address this issue, I am considering the following approach:
>
> char orderstring[6];
>
> sprintf(orderstring, "%05x", ordernum);
> memcpy(addr->name->sun_path + 1, orderstring, 5);
>
> This would prevent the buffer overflow by using 'memcpy' to safely copy the
> formatted string into 'sun_path'.
Finally new hard-coded values are introduced..
I'm not sure this is worth saving just 10 bytes, which is not excessive,
vs extra 5 bytes memcpy(), so I'd rather not touch here.
>
> Before proceeding with a patch submission, I wanted to consult with you to
> see if you have any suggestions for a better or more elegant solution to
> this problem.
An elegant option might be add a variant of snprintf without terminating
string by \0 ?
next prev parent reply other threads:[~2025-02-06 4:01 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-05 6:06 [PATCH net-next] af_unix: Refine UNIX domain sockets autobind identifier length Liang Jie
2025-02-05 8:28 ` Kuniyuki Iwashima
2025-02-05 10:09 ` Liang Jie
2025-02-06 4:01 ` Kuniyuki Iwashima [this message]
2025-02-06 4:27 ` Liang Jie
2025-02-06 19:20 ` David Laight
2025-02-05 18:11 ` Simon Horman
2025-02-06 6:49 ` kernel test robot
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=20250206040118.77016-1-kuniyu@amazon.com \
--to=kuniyu@amazon.com \
--cc=buaajxlj@163.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=liangjie@lixiang.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhal@rbox.co \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.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;
as well as URLs for NNTP newsgroup(s).