public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Sami Tolvanen <samitolvanen@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] driver core: platform: use the correct callback type for bus_find_device
Date: Tue, 12 Nov 2019 14:33:03 -0800	[thread overview]
Message-ID: <201911121422.DD3A022@keescook> (raw)
In-Reply-To: <20191112214156.3430-1-samitolvanen@google.com>

On Tue, Nov 12, 2019 at 01:41:56PM -0800, Sami Tolvanen wrote:
> platform_find_device_by_driver calls bus_find_device and passes
> platform_match as the callback function. Casting the function to a
> mismatching type trips indirect call Control-Flow Integrity (CFI) checking.

Specifically, the mismatch is between these two places:

struct device *bus_find_device(struct bus_type *bus,
                               struct device *start, const void *data,
                               int (*match)(struct device *dev, const void *data))

struct bus_type {
	...
        int (*match)(struct device *dev, struct device_driver *drv);

against the function itself, which needs to match the prototype for the
initializer assignment:

static int platform_match(struct device *dev, struct device_driver *drv)

I'm surprised this is the only place in the kernel where this happens,
but a grep for other bus_find_device() users shows that they don't also
have struct bus_type helpers; everything else uses the "const void
*data" second argument.

> This change adds a callback function with the correct type and instead
> of casting the function, explicitly casts the second parameter to struct
> device_driver* as expected by platform_match.
> 
> Fixes: 36f3313d6bff9 ("platform: Add platform_find_device_by_driver() helper")
> Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
> ---
>  drivers/base/platform.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> index b230beb6ccb4..3c0cd20925b7 100644
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -1278,6 +1278,11 @@ struct bus_type platform_bus_type = {
>  };
>  EXPORT_SYMBOL_GPL(platform_bus_type);
>  
> +static inline int __platform_match(struct device *dev, const void *drv)
> +{
> +	return platform_match(dev, (struct device_driver *)drv);
> +}
> +

So, this makes sense to me. :)

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

>  /**
>   * platform_find_device_by_driver - Find a platform device with a given
>   * driver.
> @@ -1288,7 +1293,7 @@ struct device *platform_find_device_by_driver(struct device *start,
>  					      const struct device_driver *drv)
>  {
>  	return bus_find_device(&platform_bus_type, start, drv,
> -			       (void *)platform_match);
> +			       __platform_match);
>  }
>  EXPORT_SYMBOL_GPL(platform_find_device_by_driver);
>  
> 
> base-commit: 100d46bd72ec689a5582c2f5f4deadc5bcb92d60
> -- 
> 2.24.0.rc1.363.gb1bccd3e3d-goog
> 

-- 
Kees Cook

      reply	other threads:[~2019-11-12 22:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-12 21:41 [PATCH] driver core: platform: use the correct callback type for bus_find_device Sami Tolvanen
2019-11-12 22:33 ` Kees Cook [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=201911121422.DD3A022@keescook \
    --to=keescook@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=samitolvanen@google.com \
    --cc=suzuki.poulose@arm.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