Skip to content

Uninitialized next pointer for pci device may crash kernel #25

Closed

Description

@rick-masters

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.

Activity

  1. mikaku commented on Feb 24, 2023

    @mikaku
    Owner

    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?

  2. mikaku commented on Feb 24, 2023

    @mikaku
    Owner

    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. 😃

  3. rick-masters commented on Feb 24, 2023

    @rick-masters
    ContributorAuthor

    Yes, initializing pci_dev sounds like a better solution.

  4. mikaku commented on Feb 24, 2023

    @mikaku
    Owner

    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.

  5. rick-masters commented on Feb 24, 2023

    @rick-masters
    ContributorAuthor

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

  6. mikaku commented on Feb 25, 2023

    @mikaku
    Owner

    Thank you.

  7. self-assigned this
    on Feb 28, 2023
  8. added a commit that references this issue on May 27, 2024
    4362046
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions