mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hans de Goede <hdegoede@redhat.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Mark Gross <markgross@kernel.org>,
	kernel test robot <oliver.sang@intel.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>
Subject: Re: [PATCH v1 1/1] platform/x86: p2sb: Fix UAF when caller uses resource name
Date: Thu, 1 Sep 2022 17:51:10 +0200	[thread overview]
Message-ID: <42bb615d-995a-5bcb-d481-f0d6054d4398@redhat.com> (raw)
In-Reply-To: <20220901113406.65876-1-andriy.shevchenko@linux.intel.com>

Hi,

On 9/1/22 13:34, Andy Shevchenko wrote:
> We have to copy only selected fields from the original resource.
> Because a PCI device will be removed immediately after getting
> its resources, we may not use any allocated data, hence we may
> not copy any pointers.
> 
> Consider the following scenario:
> 
>   1/ a caller of p2sb_bar() gets the resource;
> 
>   2/ the resource has been copied by platform_device_add_data()
>      in order to create a platform device;
> 
>   3/ the platform device creation will call for the device driver's
>      ->probe() as soon as a match found;
> 
>   4/ the ->probe() takes given resources (see 2/) and tries to
>      access one of its field, i.e. 'name', in the
>      __devm_ioremap_resource() to create a pretty looking output;
> 
>   5/ but the 'name' is a dangling pointer because p2sb_bar()
>      removed a PCI device, which 'name' had been copied to
>      the caller's memory.
> 
>   6/ UAF (Use-After-Free) as a result.
> 
> Kudos to Mika for the initial analisys of the issue.
> 
> Fixes: 9745fb07474f ("platform/x86/intel: Add Primary to Sideband (P2SB) bridge support")
> Reported-by: kernel test robot <oliver.sang@intel.com>
> Suggested-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> Link: https://lore.kernel.org/linux-i2c/YvPCbnKqDiL2XEKp@xsang-OptiPlex-9020/
> Link: https://lore.kernel.org/linux-i2c/YtjAswDKfiuDfWYs@xsang-OptiPlex-9020/
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Thank you for your patch, I've applied this patch to my review-hans 
branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=review-hans

Note it will show up in my review-hans branch once I've pushed my
local branch there, which might take a while.

Once I've run some tests on this branch the patches there will be
added to the platform-drivers-x86/for-next branch and eventually
will be included in the pdx86 pull-request to Linus for the next
merge-window.

Regards,

Hans



> ---
>  drivers/platform/x86/p2sb.c | 18 ++++++++++++++++--
>  1 file changed, 16 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/platform/x86/p2sb.c b/drivers/platform/x86/p2sb.c
> index fb2e141f3eb8..384d0962ae93 100644
> --- a/drivers/platform/x86/p2sb.c
> +++ b/drivers/platform/x86/p2sb.c
> @@ -42,10 +42,24 @@ static int p2sb_get_devfn(unsigned int *devfn)
>  	return 0;
>  }
>  
> +/* Copy resource from the first BAR of the device in question */
>  static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
>  {
> -	/* Copy resource from the first BAR of the device in question */
> -	*mem = pdev->resource[0];
> +	struct resource *bar0 = &pdev->resource[0];
> +
> +	/* Make sure we have no dangling pointers in the output */
> +	memset(mem, 0, sizeof(*mem));
> +
> +	/*
> +	 * We copy only selected fields from the original resource.
> +	 * Because a PCI device will be removed soon, we may not use
> +	 * any allocated data, hence we may not copy any pointers.
> +	 */
> +	mem->start = bar0->start;
> +	mem->end = bar0->end;
> +	mem->flags = bar0->flags;
> +	mem->desc = bar0->desc;
> +
>  	return 0;
>  }
>  


      reply	other threads:[~2022-09-01 15:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 11:34 Andy Shevchenko
2022-09-01 15:51 ` Hans de Goede [this message]

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=42bb615d-995a-5bcb-d481-f0d6054d4398@redhat.com \
    --to=hdegoede@redhat.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markgross@kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    --cc=oliver.sang@intel.com \
    --cc=platform-driver-x86@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

all inboxes | Powered by JetHome®