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

Uninitialized next pointer for pci device may crash kernel #25

Closed
rick-masters opened this issue Feb 24, 2023 · 6 comments
Closed

Uninitialized next pointer for pci device may crash kernel #25

rick-masters opened this issue Feb 24, 2023 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@rick-masters
Copy link
Contributor

When a pci device structure is added to the new linked list implementation its next pointer may be uninitialized. It should be set to NULL.

A new struct pci_device is initialized from a passed in variable pci_dev:

*pdt = *pci_dev;

However, that structure is allocated on the stack in scan_bus so some members may be uninitialized:

Fiwix/drivers/pci/pci.c

Lines 112 to 117 in d1a53ab

static void scan_bus(void)
{
int b, d, f;
unsigned int vendor_id, device_id, class;
unsigned char header, irq, prog_if;
struct pci_device pci_dev;

If pci_dev has a non-NULL next pointer, the kernel may attempt to access that memory which may be invalid and may crash the kernel.

Whether the crash is reproducible depends on the contents of stack memory which depends on many factors, so I have no easy way of providing a test case that triggers the problem. It always crashes for me and setting next to NULL fixes it.

@mikaku
Copy link
Owner

mikaku commented Feb 24, 2023

Good point.

I think that this line:

memset_b(pdt, 0, sizeof(struct pci_device));

is useless because pdt is clobbered by the contents of pci_dev some lines below.

I think that instead of setting pdt->next = NULL; we could remove the line 91 and put a similar line inside the scan_bus() loop. So, we make sure that pci_dev is properly initialized from the beginning.

What do you think?

@mikaku
Copy link
Owner

mikaku commented Feb 24, 2023

BTW, It's amazing how you are viewing this code at the same time I was. Today I've been precisely touching these PCI functions. 😃

@rick-masters
Copy link
Contributor Author

Yes, initializing pci_dev sounds like a better solution.

@mikaku
Copy link
Owner

mikaku commented Feb 24, 2023

I think there are still other bugs because pci_get_device() enters in an infinite loop when it doesn't found the vendor and device ids.

@rick-masters
Copy link
Contributor Author

Hmm, looks to me like it should exit the while loop when it reaches the end of the list.

@mikaku mikaku closed this as completed in 4362046 Feb 25, 2023
@mikaku
Copy link
Owner

mikaku commented Feb 25, 2023

Thank you.

@mikaku mikaku self-assigned this Feb 28, 2023
@mikaku mikaku added the bug Something isn't working label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants