Skip to content

rp1-pio security audit - #7771

Open
pelwell wants to merge 7 commits into
raspberrypi:rpi-6.18.yfrom
pelwell:piosecure
Open

pelwell wants to merge 7 commits into
raspberrypi:rpi-6.18.yfrom
pelwell:piosecure

Conversation

@pelwell

@pelwell pelwell commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Fix a number of issues found by a security audit of the rp1-pio driver.

pelwell and others added 7 commits November 18, 2026 10:58
A positive return value from an ioctl handler is used as the number of
bytes of the argument buffer to copy back to the caller. However, some
handlers return a value rather than a length - READ_HW returns the
number of bytes read (up to 127 for a 16-byte argument), and SM_CLAIM
returns the index of the claimed state machine (up to 3 for a 2-byte
argument).

In those cases the copy runs beyond the part of the on-stack argument
buffer that was initialised from the caller's arguments, leaking
uninitialised kernel stack and overwriting user memory beyond the
argument structure.

Limit the copy to the size of the ioctl argument.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
The RP1 firmware is built without parameter assertions, so it uses
state machine indices, state machine masks, GPIO numbers and interrupt
numbers without checking them, and the driver passes the values from
user space through unchanged. As a result, an out-of-range value can:

- hang the firmware (SM_DRAIN_TX on a non-existent state machine never
  sees its TX FIFO empty, and the loop runs in the mailbox interrupt
  handler),
- read and write registers outside the PIO block, such as the RIO
  registers that follow it (SM_PUT, SM_GET, SM_SET_CONFIG, SM_EXEC and
  SM_SET_DMACTRL index register arrays by state machine),
- reconfigure pins in banks 1 and 2, which PIO can't even drive,
- modify the firmware memory after its state machine claim mask.

Reject them in the driver instead, along with zero-length programs
(for which the mask calculations are undefined, and which can end up
giving a client ownership of other clients' instructions), GPIO
function numbers that would spill into other fields of the GPIO
control register, and hardware accesses where the address range wraps.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
Identical programs are shared between clients, with a per-instruction
reference count that is only 8 bits wide. Adding the same program 256
times wraps the count, after which a single removal frees instructions
that another client is still executing. Refuse to share a program whose
count is already at its maximum.

Also check that an offset returned by the firmware fits in the
instruction memory before using it to index the driver's own state.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
SM_GET_FLAGS takes a timeout in microseconds that is passed straight to
the firmware, which waits by polling in its mailbox interrupt handler.
The driver gives up on a firmware message after one second, so a longer
timeout fails with -ETIMEDOUT, blocks all other firmware users for as
long as it lasts (potentially over an hour), and results in a late
reply that overwrites the header of whichever request comes next.

Only ask the firmware to wait for up to a millisecond at a time, and do
the rest of the waiting in the driver, where it can sleep and be
interrupted. Short timeouts behave exactly as before.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
Nothing stops a client from issuing ioctls on the same file from
several threads at once, and the DMA state has no protection against
that. In particular, reconfiguring a channel frees its buffers and
releases the DMA channel while a transfer in another thread may be
sleeping with pointers into them, or be part way through copying user
data. The transfer then reads from or writes to freed memory, the
cyclic paths can write beyond a new, smaller buffer using the old
period size, and two concurrent configurations free everything twice.

Add a mutex to each channel, and hold it for the duration of every
configuration and user transfer. Decide whether there is an old
configuration to free from the state of the channel, under the mutex,
rather than from the claim bits, and make sure dma->chan never holds an
error pointer. The transfer paths sleep interruptibly, so a
reconfiguration waits for a blocked transfer to finish or be
interrupted.

A channel can be configured with no bounce buffers, for the benefit of
kernel clients that supply their own DMA addresses. That is no use to
user space, so fail user transfers on such a channel instead of taking
the remainder of a division by zero.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
The interrupt handler for user space clients dereferences the client
that owns the interrupt. When a client is closed, its interrupts are
only disabled synchronously if they are still enabled. If the client
has already disabled an interrupt, which is done with
disable_irq_nosync(), a handler that was running at the time can still
be running when the owner is cleared and the client is freed.

Always wait for the handler when releasing an interrupt. Do it without
holding the device spinlock, because disable_irq() and
synchronize_irq() can sleep.

While here, check the ownership of an interrupt under the lock when
enabling or disabling it.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
The arguments to an ioctl are only copied back to the caller if the
handler returns a positive number of bytes. The handler for
IRQ_IS_ENABLED stored the state in its arguments but then returned
zero, so user space never saw it and the interrupt always appeared to
be disabled.

Return the size of the arguments instead, as the other handlers with
results do, and read the state under the lock.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
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

Successfully merging this pull request may close these issues.

1 participant