Skip to content

Reset ownership of keys shipped with var_files - #4

Open
jetm wants to merge 2 commits into
jschneck/authorized-keys-on-varfrom
tmpfiles-key-ownership
Open

jetm wants to merge 2 commits into
jschneck/authorized-keys-on-varfrom
tmpfiles-key-ownership

Conversation

@jetm

@jetm jetm commented Nov 19, 2026

Copy link
Copy Markdown

@nicksinas pointed out on #3 that sshd's StrictModes will refuse keys shipped with a runtime's var_files: avocado-cli copies them with cp -a and packs the var image with mkfs.btrfs -r, which keep the host file's owner and mode, so a key owned by the developer or a group-writable directory is rejected with only "bad ownership or modes" in the journal.

This applies his suggested fix: a tmpfiles.d entry that creates /var/lib/ssh/authorized_keys.d as root 0755 and resets its files to root 0644, and a systemd-tmpfiles call in on_merge so it takes effect without a reboot. I stacked it on #3 and changed nothing else.

Checked: avocado config validate on the manifest. Not checked: nothing was built, provisioned or booted, and I did not reproduce the original problem on a device. Nick's comment cites avocado-cli line numbers that no longer match main, so the cp -a behavior is as he described it, not confirmed here.

@mobileoverlord, merge this into #3 or close it, as you prefer.

Keys shipped with a runtime's var_files can keep the build host's owner
and mode, and sshd's StrictModes then refuses the key file or a group
writable parent directory, which logs only "bad ownership or modes".

Ship a tmpfiles.d entry that creates /var/lib/ssh/authorized_keys.d as
root 0755 and resets its files to root 0644, and run it on merge so the
fix applies without a reboot. The ~ in the Z line keeps files at 0644
instead of forcing the directory mode onto them.

Signed-off-by: Javier Tia <javier@peridio.com>

@mobileoverlord mobileoverlord left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tmpfiles lines are right. d fixes the directory's owner and mode if it already exists. Z ~0755 masks per file, so a key that came in as 0664 or 0600 ends up root 0644, and the directory 0755. Running it from on_merge covers the current boot as well.

One gap, same failure mode: StrictModes checks every directory on the AuthorizedKeysFile path up to / (OpenSSH safe_path()), not just authorized_keys.d. If var_files brings var/lib/ssh/ along with the key, cp -a keeps the build host's owner on /var/lib/ssh too, and sshd still refuses the key. Please add a line for the parent, before the other two:

d /var/lib/ssh 0755 root root -

With that line added, approve from me. Nothing here was run on a device, so I'd still like the shipped-key case proven once on a board before 0.1.1 is tagged.

sshd's StrictModes checks every directory on the AuthorizedKeysFile path up
to the user's home or /, and root's home is /root, so the walk covers
/var/lib/ssh as well as authorized_keys.d. If var_files ships var/lib/ssh/
with the key, cp -a keeps the build host's owner on it and sshd refuses the
key even though authorized_keys.d is reset.

Add a d line for /var/lib/ssh before the authorized_keys.d entries, so a
directory that already exists with the wrong owner or mode is fixed too.

Signed-off-by: Javier Tia <javier@peridio.com>
@jetm

jetm commented Nov 20, 2026

Copy link
Copy Markdown
Author

Added in 4b58777: d /var/lib/ssh 0755 root root - now comes before the two authorized_keys.d lines, with a comment saying why. I checked the claim against OpenSSH safe_path() in misc.c: it walks every directory of the canonical key path up to the home directory or /, and root's home is /root, so /var/lib/ssh is checked too. I did not confirm in avocado-cli that cp -a keeps the host owner on that parent, and nothing was run on a device. The shipped-key case still needs one board run before 0.1.1 is tagged.

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.

2 participants