From: Dan Carpenter <dan.carpenter@linaro.org>
To: Su Hui <suhui@nfschina.com>
Cc: chuck.lever@oracle.com, jlayton@kernel.org, neilb@suse.de,
kolga@netapp.com, Dai.Ngo@oracle.com, tom@talpey.com,
trond.myklebust@hammerspace.com, anna@kernel.org,
nathan@kernel.org, ndesaulniers@google.com, trix@redhat.com,
bfields@fieldses.org, linux-nfs@vger.kernel.org,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] fs: lockd: avoid possible wrong NULL parameter
Date: Wed, 2 Aug 2023 13:25:47 +0300 [thread overview]
Message-ID: <531df8ee-ba09-49df-8201-4221df5853c6@kadam.mountain> (raw)
In-Reply-To: <20230802080544.3239967-1-suhui@nfschina.com>
On Wed, Aug 02, 2023 at 04:05:45PM +0800, Su Hui wrote:
> clang's static analysis warning: fs/lockd/mon.c: line 293, column 2:
> Null pointer passed as 2nd argument to memory copy function.
>
> Assuming 'hostname' is NULL and calling 'nsm_create_handle()', this will
> pass NULL as 2nd argument to memory copy function 'memcpy()'. So return
> NULL if 'hostname' is invalid.
>
> Fixes: 77a3ef33e2de ("NSM: More clean up of nsm_get_handle()")
> Signed-off-by: Su Hui <suhui@nfschina.com>
> ---
> fs/lockd/mon.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/lockd/mon.c b/fs/lockd/mon.c
> index 1d9488cf0534..eebab013e063 100644
> --- a/fs/lockd/mon.c
> +++ b/fs/lockd/mon.c
> @@ -358,6 +358,9 @@ struct nsm_handle *nsm_get_handle(const struct net *net,
>
> spin_unlock(&nsm_lock);
>
> + if (!hostname)
> + return NULL;
> +
> new = nsm_create_handle(sap, salen, hostname, hostname_len);
It's weird that this bug is from 2008 and we haven't found it in
testing. Presumably if hostname is NULL then hostname_len would be zero
and in that case, it's not actually a bug. It's allowed in the kernel
to memcpy zero bytes from a NULL pointer.
memcpy(dst, NULL, 0);
Outside the kernel it's not allowed though.
I noticed a related bug which Smatch doesn't find, because of how Smatch
handles the dprintk macro.
fs/lockd/host.c
truct nlm_host *nlmclnt_lookup_host(const struct sockaddr *sap,
217 const size_t salen,
218 const unsigned short protocol,
219 const u32 version,
220 const char *hostname,
221 int noresvport,
222 struct net *net,
223 const struct cred *cred)
224 {
225 struct nlm_lookup_host_info ni = {
226 .server = 0,
227 .sap = sap,
228 .salen = salen,
229 .protocol = protocol,
230 .version = version,
231 .hostname = hostname,
232 .hostname_len = strlen(hostname),
^^^^^^^^
Dereferenced
233 .noresvport = noresvport,
234 .net = net,
235 .cred = cred,
236 };
237 struct hlist_head *chain;
238 struct nlm_host *host;
239 struct nsm_handle *nsm = NULL;
240 struct lockd_net *ln = net_generic(net, lockd_net_id);
241
242 dprintk("lockd: %s(host='%s', vers=%u, proto=%s)\n", __func__,
243 (hostname ? hostname : "<none>"), version,
^^^^^^^^
Checked too late.
244 (protocol == IPPROTO_UDP ? "udp" : "tcp"));
245
regards,
dan carpenter
next prev parent reply other threads:[~2023-08-02 10:32 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-02 8:05 Su Hui
2023-08-02 10:25 ` Dan Carpenter [this message]
2023-08-02 10:41 ` Dan Carpenter
2023-08-03 3:08 ` Su Hui
2023-08-02 21:40 ` Nick Desaulniers
2023-08-03 4:16 ` Su Hui
2023-08-03 9:10 ` Dan Carpenter
2023-08-03 9:08 ` Dan Carpenter
[not found] ` <5066885c-3ac9-adbd-6852-eba89657470c@nfschina.com>
2023-08-03 16:38 ` Nick Desaulniers
2023-08-03 16:56 ` Jeff Layton
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=531df8ee-ba09-49df-8201-4221df5853c6@kadam.mountain \
--to=dan.carpenter@linaro.org \
--cc=Dai.Ngo@oracle.com \
--cc=anna@kernel.org \
--cc=bfields@fieldses.org \
--cc=chuck.lever@oracle.com \
--cc=jlayton@kernel.org \
--cc=kernel-janitors@vger.kernel.org \
--cc=kolga@netapp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=neilb@suse.de \
--cc=suhui@nfschina.com \
--cc=tom@talpey.com \
--cc=trix@redhat.com \
--cc=trond.myklebust@hammerspace.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
all inboxes | Powered by JetHome®