From: Bjorn Helgaas <helgaas@kernel.org>
To: "Krzysztof Wilczyński" <kwilczynski@kernel.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>, linux-pci@vger.kernel.org
Subject: Re: [PATCH] PCI/sysfs: Use kstrtobool() to parse the ROM attribute input
Date: Fri, 12 Jun 2026 16:22:54 -0500 [thread overview]
Message-ID: <20260612212254.GA659826@bhelgaas> (raw)
In-Reply-To: <20260612182448.552406-1-kwilczynski@kernel.org>
On Fri, Jun 12, 2026 at 06:24:48PM +0000, Krzysztof Wilczyński wrote:
> pci_write_rom() controls access to the ROM content through the
> corresponding sysfs attribute, and treats the input as a request to
> disable only when it matches the string "0\n" exactly:
>
> if ((off == 0) && (*buf == '0') && (count == 2))
>
> The count == 2 condition encodes the trailing newline that echo(1)
> appends. This was found when software in userspace wrote "0" without
> a trailing newline aiming to disable access, which failed to match the
> condition above and enabled access instead. For example:
>
> $ echo 0 > rom <- "0\n", count 2, access disabled
> $ echo -n 0 > rom <- "0", count 1, access enabled
> $ echo > rom <- "", count 1, access enabled (likely not be desirable)
>
> Thus, parse the input with kstrtobool(), which handles common boolean
> inputs such as "0", "1", "n", "y" or "off", "on", with or without a
> trailing newline, so both of the above disable access, and update the
> now stale comment.
>
> As a side effect, input that does not parse as a boolean is rejected
> with -EINVAL rather than enabling access. The documented "0" and "1"
> continue to work as before, and rejecting malformed input brings the
> attribute in line with how sysfs attributes typically handle it.
>
> Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
Applied to pci/sysfs for v7.2, thanks!
> ---
> drivers/pci/pci-sysfs.c | 11 ++++++-----
> 1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index d37860841260..1ddd6be25d95 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1309,18 +1309,19 @@ void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
> * @off: file offset
> * @count: number of byte in input
> *
> - * writing anything except 0 enables it
> + * Writing a boolean value enables or disables the ROM display.
> */
> static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj,
> const struct bin_attribute *bin_attr, char *buf,
> loff_t off, size_t count)
> {
> struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
> + bool enable;
>
> - if ((off == 0) && (*buf == '0') && (count == 2))
> - pdev->rom_attr_enabled = 0;
> - else
> - pdev->rom_attr_enabled = 1;
> + if (kstrtobool(buf, &enable))
> + return -EINVAL;
> +
> + pdev->rom_attr_enabled = enable;
>
> return count;
> }
> --
> 2.54.0
>
next prev parent reply other threads:[~2026-06-12 21:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-12 18:24 [PATCH] PCI/sysfs: Use kstrtobool() to parse the ROM attribute input Krzysztof Wilczyński
2026-06-12 18:35 ` sashiko-bot
2026-06-12 21:22 ` Bjorn Helgaas [this message]
2026-06-13 15:03 ` Krzysztof Wilczyński
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=20260612212254.GA659826@bhelgaas \
--to=helgaas@kernel.org \
--cc=bhelgaas@google.com \
--cc=kwilczynski@kernel.org \
--cc=linux-pci@vger.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