From: Petr Pavlu <petr.pavlu@suse.com>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Luis Chamberlain <mcgrof@kernel.org>,
Sami Tolvanen <samitolvanen@google.com>,
Daniel Gomez <da.gomez@samsung.com>, Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH 2/4] module: sysfs: Simplify section attribute allocation
Date: Wed, 18 Dec 2024 12:28:45 +0100 [thread overview]
Message-ID: <e65c9cc2-fbb5-464e-99bd-1c9f47ce0f2e@suse.com> (raw)
In-Reply-To: <20241216-sysfs-const-bin_attr-module-v1-2-f81e49e54ce4@weissschuh.net>
On 12/16/24 20:16, Thomas Weißschuh wrote:
> The existing allocation logic manually stuffs two allocations into one.
> This is hard to understand and of limited value, given that all the
> section names are allocated on their own anyways.
> Une one allocation per datastructure.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> kernel/module/sysfs.c | 18 ++++++++++--------
> 1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/module/sysfs.c b/kernel/module/sysfs.c
> index b7841f76a933114e6dbd0fc2d32a60b66b7966b6..935629ac21fa16504ddb5f3762af5539572c3bf1 100644
> --- a/kernel/module/sysfs.c
> +++ b/kernel/module/sysfs.c
> @@ -65,34 +65,37 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
>
> for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++)
> kfree((*bin_attr)->attr.name);
> + kfree(sect_attrs->grp.bin_attrs);
> kfree(sect_attrs);
> }
>
> static int add_sect_attrs(struct module *mod, const struct load_info *info)
> {
> - unsigned int nloaded = 0, i, size[2];
> struct module_sect_attrs *sect_attrs;
> struct module_sect_attr *sattr;
> struct bin_attribute **gattr;
> + unsigned int nloaded = 0, i;
> int ret;
>
> /* Count loaded sections and allocate structures */
> for (i = 0; i < info->hdr->e_shnum; i++)
> if (!sect_empty(&info->sechdrs[i]))
> nloaded++;
> - size[0] = ALIGN(struct_size(sect_attrs, attrs, nloaded),
> - sizeof(sect_attrs->grp.bin_attrs[0]));
> - size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]);
> - sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
> + sect_attrs = kzalloc(struct_size(sect_attrs, attrs, nloaded), GFP_KERNEL);
> if (!sect_attrs)
> return -ENOMEM;
>
> + gattr = kcalloc(nloaded + 1, sizeof(*gattr), GFP_KERNEL);
> + if (!gattr) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
Member sect_attrs->grp.bin_attrs is NULL at this point. If the above
kcalloc() call fails, the control goes to the out label which invokes
free_sect_attrs() and its code
"for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; ..."
results in a NULL dereference.
> /* Setup section attributes. */
> sect_attrs->grp.name = "sections";
> - sect_attrs->grp.bin_attrs = (void *)sect_attrs + size[0];
> + sect_attrs->grp.bin_attrs = gattr;
>
> sattr = §_attrs->attrs[0];
> - gattr = §_attrs->grp.bin_attrs[0];
> for (i = 0; i < info->hdr->e_shnum; i++) {
> Elf_Shdr *sec = &info->sechdrs[i];
>
> @@ -111,7 +114,6 @@ static int add_sect_attrs(struct module *mod, const struct load_info *info)
> sattr->battr.attr.mode = 0400;
> *(gattr++) = &(sattr++)->battr;
> }
> - *gattr = NULL;
>
> ret = sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp);
> if (ret)
>
--
Thanks,
Petr
next prev parent reply other threads:[~2024-12-18 11:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-16 19:16 [PATCH 0/4] module: sysfs: Two cleanups and preparation for const struct bin_attribute Thomas Weißschuh
2024-12-16 19:16 ` [PATCH 1/4] module: sysfs: Drop member 'nsections' Thomas Weißschuh
2024-12-16 19:16 ` [PATCH 2/4] module: sysfs: Simplify section attribute allocation Thomas Weißschuh
2024-12-18 11:28 ` Petr Pavlu [this message]
2024-12-16 19:16 ` [PATCH 3/4] module: sysfs: Add notes attributes through attribute_group Thomas Weißschuh
2024-12-18 13:13 ` Petr Pavlu
2024-12-16 19:16 ` [PATCH 4/4] module: sysfs: Use const 'struct bin_attribute' Thomas Weißschuh
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=e65c9cc2-fbb5-464e-99bd-1c9f47ce0f2e@suse.com \
--to=petr.pavlu@suse.com \
--cc=da.gomez@samsung.com \
--cc=gustavoars@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=mcgrof@kernel.org \
--cc=samitolvanen@google.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