From: Baokun Li <libaokun1@huawei.com>
To: Alexey Dobriyan <adobriyan@gmail.com>
Cc: <linux-ext4@vger.kernel.org>, <tytso@mit.edu>,
<adilger.kernel@dilger.ca>, <jack@suse.cz>,
<ritesh.list@gmail.com>, <linux-kernel@vger.kernel.org>,
<yi.zhang@huawei.com>, <yangerkun@huawei.com>,
<chengzhihao1@huawei.com>, <yukuai3@huawei.com>,
Baokun Li <libaokun1@huawei.com>
Subject: Re: [PATCH 1/7] ext4: avoid overflow when setting values via sysfs
Date: Sat, 27 Jan 2024 18:44:29 +0800 [thread overview]
Message-ID: <ad5c3487-e847-7ab9-41bc-f13329265abc@huawei.com> (raw)
In-Reply-To: <1da0649d-812f-4dee-9c1b-af567afa2e46@p183>
On 2024/1/27 17:44, Alexey Dobriyan wrote:
> Baokun Li wrote:
>
>> @@ -463,6 +463,8 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
>> ret = kstrtoul(skip_spaces(buf), 0, &t);
>> if (ret)
>> return ret;
>> + if (t != (unsigned int)t)
>> + return -EINVAL;
> kstrto*() interface has variants for all standard types.
> It should be changed to kstrtou32() or kstrtouint();
>
> If you check if kstrto*() result fits into another type,
> you're probably doing it wrong.
Thanks for your comments!
The reason for not using those helper functions directly is as follows:
1)Those functions are also based on kstrtoull() wrappers, and if we
use kstrtou32() or kstrtouint(), we'd need to declare u32 t or int t,
which would leave us with a lot of declared but unused variables,
and an unsigned long t would be enough to cover our scenario.
2)Moreover, the actual range of a uint type sysfs interface may not
be 0-UINT_MAX, but 0-INT_MAX or 0-s_clusters_per_group, and we
need to limit the range of the variable according to the actual
meaning of the variable.
3)In addition, by declaring only an unsigned long type and then
uniformly parsing it with kstrtoul() and then restricting the range
of the variable according to the actual meaning of the variable,
we can reduce a lot of repetitive code.
>> if (a->attr_ptr == ptr_ext4_super_block_offset)
>> *((__le32 *) ptr) = cpu_to_le32(t);
--
With Best Regards,
Baokun Li
.
next prev parent reply other threads:[~2024-01-27 10:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-27 9:44 Alexey Dobriyan
2024-01-27 10:44 ` Baokun Li [this message]
-- strict thread matches above, loose matches on Subject: below --
2024-01-26 8:57 [PATCH 0/7] ext4: avoid sysfs variables overflow causing BUG_ON/SOOB Baokun Li
2024-01-26 8:57 ` [PATCH 1/7] ext4: avoid overflow when setting values via sysfs Baokun Li
2024-01-26 9:28 ` Zhang Yi
2024-02-13 16:05 ` Jan Kara
2024-02-17 7:09 ` Baokun Li
2024-02-23 11:54 ` Jan Kara
2024-02-24 1:59 ` Baokun Li
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=ad5c3487-e847-7ab9-41bc-f13329265abc@huawei.com \
--to=libaokun1@huawei.com \
--cc=adilger.kernel@dilger.ca \
--cc=adobriyan@gmail.com \
--cc=chengzhihao1@huawei.com \
--cc=jack@suse.cz \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai3@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 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®