public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Bjorn Helgaas <helgaas@kernel.org>
To: Philipp Stanner <phasta@kernel.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
	Bingbu Cao <bingbu.cao@linux.intel.com>
Subject: Re: [PATCH] PCI: Check BAR index for validity
Date: Tue, 4 Mar 2025 16:42:03 -0600	[thread overview]
Message-ID: <20250304224203.GA260968@bhelgaas> (raw)
In-Reply-To: <20250304143112.104190-2-phasta@kernel.org>

On Tue, Mar 04, 2025 at 03:31:13PM +0100, Philipp Stanner wrote:
> Many functions in PCI use accessor macros such as pci_resource_len(),
> which take a BAR index. That index, however, is never checked for
> validity, potentially resulting in undefined behavior by overflowing the
> array pci_dev.resource in the macro pci_resource_n().
> 
> Since many users of those macros directly assign the accessed value to
> an unsigned integer, the macros cannot be changed easily anymore to
> return -EINVAL for invalid indexes. Consequently, the problem has to be
> mitigated in higher layers.
> 
> Add pci_bar_index_valid(). Use it where appropriate.
> 
> Reported-by: Bingbu Cao <bingbu.cao@linux.intel.com>
> Closes: https://lore.kernel.org/all/adb53b1f-29e1-3d14-0e61-351fd2d3ff0d@linux.intel.com/
> Signed-off-by: Philipp Stanner <phasta@kernel.org>

Applied to pci/devres for v6.15, thanks.

I reversed this:

> +static inline bool pci_bar_index_is_valid(int bar)
> +{
> +	if (bar < 0 || bar >= PCI_NUM_RESOURCES)
> +		return false;
> +
> +	return true;
> +}

so the test describes valid indexes, not invalid ones:

  if (bar >= 0 && bar < PCI_NUM_RESOURCES)
    return true;

  return false;

  reply	other threads:[~2025-03-04 22:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-04 14:31 [PATCH] PCI: Check BAR index for validity Philipp Stanner
2025-03-04 22:42 ` Bjorn Helgaas [this message]
2025-03-05  7:03 ` Philipp Stanner
2025-03-05  7:14   ` Krzysztof Wilczyński
2025-03-10  2:43 ` kernel test robot

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=20250304224203.GA260968@bhelgaas \
    --to=helgaas@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=bingbu.cao@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=phasta@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