Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel crashing in spi_submit_prepare #35

Closed
tchen61 opened this issue Jul 18, 2018 · 6 comments
Closed

kernel crashing in spi_submit_prepare #35

tchen61 opened this issue Jul 18, 2018 · 6 comments

Comments

@tchen61
Copy link

tchen61 commented Jul 18, 2018

in cmp2210-core.c submit_urbs, driver calls spi_submit_prepare with a pointer to
(struct mcp2210_cmd *),
but in the spi_submit_prepare, it CASTs that pointer to (struct mcp2210_cmd_spi_msg *)
and then subsequently tries to load up an element xfer....

and then later use xfer to access the tx_buf, and len....

kernel crashes when tx_buf is NULL, but len is not....
not sure if the casting of mcp2210_cmd to mcp2210_cmd_spi_msg is correct

@tchen61
Copy link
Author

tchen61 commented Jul 18, 2018

this is the cast that is questionalble ?

static int spi_submit_prepare(struct mcp2210_cmd *cmd_head)
{
struct mcp2210_cmd_spi_msg *cmd = (void *)cmd_head;

@daniel-santos
Copy link
Owner

Hello. This cast is C polymorphism. If you look at the definition of struct mcp2210_cmd_spi_msg you will see that it's first member is struct mcp2210_cmd head. This is how you do polymorphism in C and you can think of them as classes. The second field of the base class, struct mcp2210_cmd defines the subtype so it is never cast incorrectly (aside from memory corruption, incorrect code, etc.).

But if there's a NULL dereference then something is obviously wrong. Post the stack trace.

@tchen61
Copy link
Author

tchen61 commented Jul 18, 2018 via email

@dhilst
Copy link

dhilst commented Jul 18, 2018

I don't really have a grasp of what is happening, but about spi communication, usually there is a spi_transfer struct that has a tx_buf pointer, this need to be initialized or you will face a crash by NULL pointer dereferization.

Here is where it is defined:
https://elixir.bootlin.com/linux/latest/source/include/linux/spi/spi.h#L776

And here are some examples of it's use:
https://elixir.bootlin.com/linux/latest/source/drivers/char/tpm/st33zp24/spi.c#L119
https://elixir.bootlin.com/linux/latest/source/drivers/char/tpm/tpm_tis_spi.c#L78

From the description of the problem I guess that mcp2210_cmd_spi_msg is not properly initialized.

--

okay I take a deeper look, this is a master driver, not a protocol driver. I just forgot it, the examples I give won't help here. Anyway, I still think the problem is in up tiers of the stack. I just can't remember how this is interfaced to the user space, libusb?

@daniel-santos
Copy link
Owner

tchen61, were you able to solve this problem? I think I should modify this driver to check for such problems, at least when debug features are enabled, then we can get a backtrace that includes the faulty code. In the backtrace you gave, it didn't try to dereference it until some later time.

daniel-santos added a commit that referenced this issue Nov 8, 2018
First this fixes a logical flaw that fails to verify that the tx_buf of
a struct spi_transfer is non-null.  The check:

	if (!(xfer->tx_buf || xfer->rx_buf))

is only true if BOTH of the buffers are NULL.

Second, the driver should probably tollerate a situation where the
rx_buf is NULL, so this fixes that for cases where CONFIG_MCP2210_DEBUG
is enabled.

This fixes issue #35.
daniel-santos added a commit that referenced this issue Nov 8, 2018
First this fixes a logical flaw that fails to verify that the tx_buf of
a struct spi_transfer is non-null.  The check:

	if (!(xfer->tx_buf || xfer->rx_buf))

is only true if BOTH of the buffers are NULL.

Second, the driver should probably tollerate a situation where the
rx_buf is NULL, so this fixes that for cases where CONFIG_MCP2210_DEBUG
is enabled.

This fixes issue #35.
@daniel-santos
Copy link
Owner

I've added an appropriate check for a tx_buf == NULL and actually modified it so that it should always tolerate rx_buf == NULL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants