Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Niklas Cassel" <cassel@kernel.org>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH 1/2] misc: pci_endpoint_test: Fix doorbell success logic
Date: Thu, 30 Jul 2026 12:30:24 +0000	[thread overview]
Message-ID: <20260730123024.91A541F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260730122045.1382749-5-cassel@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing validation of device-supplied BAR and offset allows arbitrary out-of-bounds memory write and NULL pointer dereference.
- [High] Use-after-free and race condition in device removal due to improper teardown ordering.
--

--- Patch [1]: [PATCH 1/2] misc: pci_endpoint_test: Fix doorbell success logic ---
commit fca8adde489026db6a363f41e32f957940be89f4
Author: Niklas Cassel <cassel@kernel.org>

misc: pci_endpoint_test: Fix doorbell success logic

This patch modifies the pci_endpoint_test_doorbell function to ensure
that if triggering the doorbell fails, the test stores the error code
but continues on to disable the doorbell rather than returning early.

> diff --git a/drivers/misc/pci_endpoint_test.c b/drivers/misc/pci_endpoint_test.c
> index 3635741c3e7a7..26ab9252251f1 100644
> --- a/drivers/misc/pci_endpoint_test.c
> +++ b/drivers/misc/pci_endpoint_test.c
> @@ -1071,6 +1071,7 @@ static int pci_endpoint_test_doorbell(struct pci_endpoint_test *test)
>  	struct pci_dev *pdev = test->pdev;
>  	struct device *dev = &pdev->dev;
>  	int irq_type = test->irq_type;
> +	int ret = 0;
>  	enum pci_barno bar;
>  	u32 data, status;
>  	u32 addr;
> @@ -1119,8 +1120,11 @@ static int pci_endpoint_test_doorbell(struct pci_endpoint_test *test)

[Severity: High]
This is a pre-existing issue, but since we are modifying the error handling
in pci_endpoint_test_doorbell(), is there an out-of-bounds write or NULL
pointer dereference possible just above this diff?

A malicious or malfunctioning PCIe endpoint can supply arbitrary values for
addr and bar. The code checks if bar is within the enum bounds, but does
not check if test->bar[bar] is actually mapped (i.e., non-NULL), nor does
it bounds-check addr against the mapped BAR's size before calling:

    writel(data, test->bar[bar] + addr);

Could this allow an arbitrary MMIO write to kernel space?

>  	status = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
>  
> -	if (!left || !(status & STATUS_DOORBELL_SUCCESS))
> +	if (!left || !(status & STATUS_DOORBELL_SUCCESS)) {
>  		dev_err(dev, "Failed to trigger doorbell in endpoint\n");
> +		/* Store error code, but continue to disable doorbell. */
> +		ret = -EINVAL;
> +	}
>  
>  	pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
>  				 COMMAND_DISABLE_DOORBELL);
> @@ -1134,10 +1138,7 @@ static int pci_endpoint_test_doorbell(struct pci_endpoint_test *test)
>  		return -EINVAL;
>  	}
>  
> -	if (!(status & STATUS_DOORBELL_SUCCESS))
> -		return -EINVAL;
> -
> -	return 0;
> +	return ret;
>  }
>  
>  static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,

[Severity: High]
This isn't a bug introduced by this patch, but looking at how ioctls are
handled here, is there a use-after-free race condition during device
removal?

If a PCIe hot-unplug event or driver unbind occurs while a userspace
process is executing an ioctl, pci_endpoint_test_remove() will execute
concurrently:

    pci_endpoint_test_release_irq(test);
    pci_endpoint_test_free_irq_vectors(test);
    misc_deregister(&test->miscdev);

misc_deregister() does not wait for existing active file operations to
complete. Because the test struct is managed by devm_kzalloc(), it is freed
immediately after remove() returns.

Could the concurrent ioctl handler:

    mutex_lock(&test->mutex);

access the freed test memory or interact with torn-down IRQs or MMIO
regions? Does the driver need to synchronize device teardown with active
ioctls?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260730122045.1382749-4-cassel@kernel.org?part=1

  reply	other threads:[~2026-07-30 12:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-30 12:20 [PATCH 0/2] misc: pci_endpoint_test: Doorbell fix and improvement Niklas Cassel
2026-07-30 12:20 ` [PATCH 1/2] misc: pci_endpoint_test: Fix doorbell success logic Niklas Cassel
2026-07-30 12:30   ` sashiko-bot [this message]
2026-07-30 12:20 ` [PATCH 2/2] misc: pci_endpoint_test: Improve logic to check status for doorbell test case Niklas Cassel
2026-07-30 12:33   ` sashiko-bot
2026-07-30 12:45     ` Niklas Cassel

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=20260730123024.91A541F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cassel@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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