Skip to content

dtoverlay security audit - #234

Merged
pelwell merged 8 commits into
raspberrypi:masterfrom
pelwell:dtosecure
Nov 19, 2026
Merged

pelwell merged 8 commits into
raspberrypi:masterfrom
pelwell:dtosecure

Conversation

@pelwell

@pelwell pelwell commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Fix a number of issues found by a security audit of dtoverlay/dtparam.

pelwell and others added 8 commits November 19, 2026 11:15
The state directory, /tmp/.dtoverlays, has a fixed name in a world-writable
directory. Any local user can create it first (running "dtoverlay -l" as an
unprivileged user was enough), and root then replays the .dtbo files it
contains into the kernel and writes files through any symlinks planted
there.

Move it to /run/dtoverlays, which only root can write to and which is
cleared at boot (as the configfs state is). Only create it when running as
root, and refuse to use it unless it is a real directory owned and only
writable by root. If it doesn't exist, non-root users listing overlays just
see that none are loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nothing stops two instances of dtoverlay/dtparam from running at once, and
the results can be bad:
 - Both read the same overlay count and give their overlays the same
   sequence number, breaking the numbering that removal relies on.
 - A removal deletes and replays the later overlays, and an add that runs
   during it corrupts the sequence.
 - Concurrent dtparam invocations share base.dtb, and can take a snapshot
   of /proc/device-tree while another overlay is being applied.
 - Listing can read a .dtbo that is still being written.

Take an flock() on /run/dtoverlays/lock before reading the state and hold
it until exit, exclusively for adds and removals, shared for listing. The
kernel releases it however the process exits, including through
fatal_error(). Rather than block forever, give up after 30s.

The dtoverlay-pre/post hooks run with the lock held, so a hook that tried
to add or remove an overlay would deadlock (or recurse). Mark the hooks'
environment and make that an error; listing from a hook is still allowed,
and skips the lock because the parent already holds it exclusively.

Also check for root before touching the state when adding or removing,
rather than after reading it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The .dtbo files in the state directory were written in place, so killing
dtoverlay part-way through a write left a truncated file that a later
removal would try to replay. Write to a temporary file and rename it into
place. The temporary name starts with '.', so a leftover one isn't mistaken
for an overlay. Also fail the add if the file can't be written, instead of
applying whatever is there.

dtoverlay_list passed the result of dtoverlay_load_dtb to
dtoverlay_dtb_trailer without checking it for NULL, so an unreadable file
crashed it; report the overlay as unreadable instead.

In the library, dtoverlay_save_dtb leaked the FILE when a write failed, and
ignored the result of fclose, which is where errors in buffered data are
reported. dtoverlay_save_dtb is also used to write to configfs, so it still
writes directly to the named file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two instances starting at once could both find /config missing, and the
loser's mkdir failed with EEXIST, which was fatal; accept EEXIST. They
could also both find configfs unmounted and mount it twice. The mount now
happens under the exclusive lock; listing only takes a shared lock, so it
upgrades the lock before mounting.

When applying an overlay failed, dtoverlay_add moved it to error.dtb and
freed error_file without clearing it. If a later step then succeeded, as
can happen while replaying overlays during a removal, main() passed the
freed string to unlink. Clear error_file, which also keeps the error.dtb
that has just been written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
If configfs wasn't at /sys/kernel/config, dtoverlay created /config and
then tried to mount configfs on /config/device-tree, which can't exist
until configfs is mounted at /config - so the mount always failed. It
also ran mount via the shell and then reported errno, which said nothing
about why mount had failed.

Mount configfs on /run/dtoverlays/configfs instead, which is on a tmpfs
and in a directory already checked to belong to root, using mount(2).
Then check that it has a device-tree folder, unmounting it and failing if
it doesn't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Removing an overlay deletes the configfs directories of it and every later
overlay, then renumbers and reapplies the survivors. Being interrupted
part-way (Ctrl-C, kill, or the terminal hanging up) left configfs and the
state directory out of step, with no way to recover short of a reboot.

Block SIGHUP, SIGINT, SIGQUIT, SIGTERM and SIGPIPE while an overlay is
being added or removed, and unblock them afterwards, at which point any
that arrived are acted on (so the dtoverlay-post hook doesn't run). A
signal handler couldn't clean up safely, and the only consistent state to
clean up to is the finished one anyway. SIGKILL still can't be stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When removing an overlay, the later overlays are renumbered and reapplied.
If one of them failed to apply, its renumbered .dtbo was left in the state
directory without the count being advanced, so the next overlay was given
the same number, or the next run believed the failed overlay was loaded.
The same happened in dtoverlay_add when a failure had already used up
error.dtb.

Discard the file of an overlay that fails to apply: move the first one to
error.dtb as before, and delete any others. It then drops out of the
sequence and the numbering stays contiguous. Name the overlay in the error
message (it always said "dtparam"), and exit with an error if any overlay
was lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signals from a terminal or from timeout are sent to the whole process
group. While dtoverlay was deferring them during a removal, the dtc that
regenerates a dtparam still received them, and testing on a Pi 5 showed
that a SIGINT or SIGTERM at the wrong moment killed it. The state stayed
consistent, but the dtparam was dropped with "Failed to read active DTB".

Run dtc with fork/exec rather than system(), and while the parent is
deferring signals make the child ignore them, which survives exec. This
also stops the filename being passed through a shell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pelwell
pelwell merged commit 5e9e2da into raspberrypi:master Nov 19, 2026
4 checks passed
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