From: "Leizhen (ThunderTown)" <thunder.leizhen@huawei.com>
To: Tejun Heo <tj@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kernfs: fix potential null-ptr-deref in kernfs_path_from_node_locked()
Date: Thu, 24 Nov 2022 10:52:43 +0800 [thread overview]
Message-ID: <55041efe-7443-d576-287b-49d1221fced2@huawei.com> (raw)
In-Reply-To: <ba46adff-3604-9ccf-b1c5-83411f6652d9@huawei.com>
On 2022/11/24 10:28, Leizhen (ThunderTown) wrote:
>
>
> On 2022/11/24 10:24, Leizhen (ThunderTown) wrote:
>>
>>
>> On 2022/11/24 0:55, Tejun Heo wrote:
>>> On Wed, Nov 23, 2022 at 10:04:19AM +0800, Zhen Lei wrote:
>>>> Ensure that the 'buf' is not empty before strlcpy() uses it.
>>>>
>>>> Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences
>>>> in kernfs_path_from_node_locked()") first noticed this, but it didn't
>>>> fix it completely.
>>>>
>>>> Fixes: 9f6df573a404 ("kernfs: Add API to generate relative kernfs path")
>>>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>>>
>>> I think the right thing to do is removing that if. It makes no sense to call
>>> that function with NULL buf and the fact that nobody reported crashes on
>>> NULL buf indicates that we in fact never do.
>>
>> OK.
>>
>> How about I remove "buf[0] = '\0';" too? It seems to be a useless operation.
>> When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from
>> 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly,
>> even if the buf is too small to save the first path node.
>
> Sorry, I misanalyzed. The length used by "len < buflen ? buflen - len : 0" may
> be zero.
Ah, my brain is unstable today. The initial value of len is 0. So "buf[0] = '\0';"
can still be safely removed.
>
>>
>> static void test(void)
>> {
>> char buf[4];
>> int i, n, buflen;
>>
>> buflen = 1;
>> n = strlcpy(buf, "string", buflen);
>> for (i = 0; i < buflen; i++)
>> printk("%d: %02x\n", i, buf[i]);
>> printk("n=%d\n\n", n);
>>
>> buflen = sizeof(buf);
>> n = strlcpy(buf, "string", buflen);
>> for (i = 0; i < buflen; i++)
>> printk("%d: %02x\n", i, buf[i]);
>> printk("n=%d\n", n);
>> }
>>
>> Output:
>> [ 33.691497] 0: 00
>> [ 33.691569] n=6
>>
>> [ 33.691595] 0: 73
>> [ 33.691622] 1: 74
>> [ 33.691630] 2: 72
>> [ 33.691637] 3: 00
>> [ 33.691650] n=6
>>
>>>
>>> Thanks.
>>>
>>
>
--
Regards,
Zhen Lei
next prev parent reply other threads:[~2022-11-24 2:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-23 2:04 Zhen Lei
2022-11-23 16:55 ` Tejun Heo
2022-11-24 2:24 ` Leizhen (ThunderTown)
2022-11-24 2:28 ` Leizhen (ThunderTown)
2022-11-24 2:52 ` Leizhen (ThunderTown) [this message]
2022-11-26 9:49 ` Leizhen (ThunderTown)
2022-11-26 10:25 ` Greg Kroah-Hartman
2022-11-28 1:15 ` Leizhen (ThunderTown)
2022-11-26 10:46 ` Leizhen (ThunderTown)
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=55041efe-7443-d576-287b-49d1221fced2@huawei.com \
--to=thunder.leizhen@huawei.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tj@kernel.org \
/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®