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

systemd: backport tpm fixes #214383

Merged
merged 3 commits into from Feb 21, 2023
Merged

Conversation

flokli
Copy link
Contributor

@flokli flokli commented Feb 3, 2023

This backports #210896 and #213182 to staging-22.11.

I still need to check if it actually works :-)

Requested in #210896 (comment).

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@flokli
Copy link
Contributor Author

flokli commented Feb 3, 2023

Hm, this doesn't seem to be sufficient for me. It looks like the systemd patch needs to be updated:

machine # [    6.126209] systemd-cryptsetup[531]: Assertion '(_error) != 0' failed at src/cryptsetup/cryptsetup.c:1327, function attach_luks_or_plain_or_bitlk_by_tpm2(). Aborting.

ping @ElvishJerricco @RaitoBezarius @blitz @ymatsiuk @NickCao

Full build log: https://gist.github.com/flokli/9cdeddd1108eff0c268f3b511fae2a50

@ymatsiuk
Copy link
Contributor

ymatsiuk commented Feb 3, 2023

I don't think this patch is really needed for systemd version in 21.11

@flokli
Copy link
Contributor Author

flokli commented Feb 3, 2023

I don't think this patch is really needed for systemd version in 21.11

It was requested in #210896 (comment).

@ymatsiuk
Copy link
Contributor

ymatsiuk commented Feb 3, 2023

Oh I see, the systemd version in staging-22.11 is too old though, not in 21.11 sorry. 🤔 how was that even possible?

@flokli
Copy link
Contributor Author

flokli commented Feb 3, 2023

Oh I see, the systemd version in staging-22.11 is too old though, not in 21.11 sorry

Not sure I understand. staging-22.11 has 251.11, release-22.11 has 251.10.

This PR doesn't change any of that, it simply adds the ./0019-tpm2_context_init-fix-driver-name-checking.patch patch (which unfortunately is not sufficient).

@ymatsiuk
Copy link
Contributor

ymatsiuk commented Feb 3, 2023

Apologies for misleading I'm a blind fool, confused 21.11 and 22.11 🤦🏻

@NickCao
Copy link
Member

NickCao commented Feb 4, 2023

This looks like a different issue. Need further investigation.

@NickCao
Copy link
Member

NickCao commented Feb 4, 2023

Strange that the commit (systemd/systemd-stable@542dbc6) is only in systemd v252.2 or later. How could the tpm2 init patch apply.
Edit: I was mislead by github, it is here: https://github.com/systemd/systemd-stable/blob/v251.11/src/shared/tpm2-util.c

@NickCao
Copy link
Member

NickCao commented Feb 4, 2023

src/cryptsetup/cryptsetup.c:1327 is https://github.com/systemd/systemd-stable/blob/3402020d17ce9e75167a5415fafb54708edb1ebf/src/cryptsetup/cryptsetup.c#L1327, is our patching mangling with line numbers? A log function can't fail.

@flokli
Copy link
Contributor Author

flokli commented Feb 4, 2023

I mean, we could reflow this patch manually to make sure, and see if it still regresses. Or peek at the patched file during build, by inserting a cat in the build commands...

@NickCao
Copy link
Member

NickCao commented Feb 6, 2023

Added cat (awk actually), still on the same line:
systemd> 1327- log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m");

@NickCao
Copy link
Member

NickCao commented Feb 6, 2023

And yes, there is an assertion in that log function....
https://github.com/systemd/systemd/blob/6823b5bb99b605090d967d08586a4ce8fd5f989f/src/basic/log.h#L223

@NickCao
Copy link
Member

NickCao commented Feb 6, 2023

So this looks like a logic error in the if else clauses.

  1. r = attach_luks2_by_tpm2_via_plugin(cd, name, flags);
  2. handle ENXIO
  3. handle ENOENT
  4. handle anything other than EOPNOTSUPP, EAGAIN, and no error just falls under this case
  5. we cannot log something that's not an error???

@NickCao
Copy link
Member

NickCao commented Feb 6, 2023

The issue is silently fixed in systemd/systemd@35ba2b4

@flokli
Copy link
Contributor Author

flokli commented Feb 6, 2023

So this also should have gone into systemd-stable?

@NickCao
Copy link
Member

NickCao commented Feb 6, 2023

The commit message says: This finishes the implementation started in commit https://github.com/systemd/systemd/commit/1f895adac287b5f1b6b854caa586093616ccc172 ("cryptsetup: add libcryptsetup TPM2 PIN support"). Maybe tpm2 with pin is just not supported by older versions of systemd, thus the features was not backported.

@flokli
Copy link
Contributor Author

flokli commented Feb 6, 2023

Thanks for the digging! So certainly not something for us to backport on our own.

@alaviss If you're interested in driving this forward, please open a backport PR in the systemd-stable repo, otherwise I'd propose to only use this in 252 and later.

@alaviss
Copy link
Contributor

alaviss commented Feb 6, 2023

I'm not sure why this is not working in the test VM because it is working on my system...

@hesiod
Copy link
Contributor

hesiod commented Feb 19, 2023

Could this PR (with the current PATCH) be merged independently of fixing the outstanding cryptsetup issues?
I have a server running 22.11 that is affected by the underlying library path issue, and it is fixed by applying the patch. I'm using systemd credentials protected by the TPM for a number of services including systemd-networkd.service and nix-daemon.service, and since systemd fails starting the services which use LoadCredentialEncrypted without the patch the affected server was not accessible anymore.
I'm currently using an overlay to include the patch, but it would be more convenient for me if this PR is merged. OTOH, I understand if my use case is too specific and you'd rather fix the cryptsetup failure as well in this PR.

@flokli
Copy link
Contributor Author

flokli commented Feb 19, 2023

Could this PR (with the current PATCH) be merged independently of fixing the outstanding cryptsetup issues? I have a server running 22.11 that is affected by the underlying library path issue, and it is fixed by applying the patch. I'm using systemd credentials protected by the TPM for a number of services including systemd-networkd.service and nix-daemon.service, and since systemd fails starting the services which use LoadCredentialEncrypted without the patch the affected server was not accessible anymore. I'm currently using an overlay to include the patch, but it would be more convenient for me if this PR is merged. OTOH, I understand if my use case is too specific and you'd rather fix the cryptsetup failure as well in this PR.

Oh, i wasn't aware someone was using that. We currently don't have any tests covering it, so it's hard to see things are broken / would get fixed by it.

Can you provide / extend a VM test with your usecase? We could then backport that and the fix, so it'll serve as a nice régression test.

@hesiod
Copy link
Contributor

hesiod commented Feb 20, 2023

@flokli I created a new NixOS test systemd-credentials-tpm2.
PRs:

Not sure if it makes sense to actually backport the test to stable, but I created the PR anyway so anyone can test it easily.

@flokli
Copy link
Contributor Author

flokli commented Feb 20, 2023

I approved #217255, and once undrafted and merged, would cherry-pick it into this PR. Thanks!

@hesiod
Copy link
Contributor

hesiod commented Feb 20, 2023

@flokli I just realized that the updated commit (with the meta.maintainers entry) won't work on release-22.11 directly since I didn't exist in the maintainers list back then. So you'd need to cherry-pick 62f73c7 (commit adding me to the maintainers list) as well, or you'd have to remove the meta.maintainers entry from the commit.

(cherry picked from commit 62f73c7)
@flokli
Copy link
Contributor Author

flokli commented Feb 20, 2023

I'll do some compiling/testing and will then push here.

hesiod and others added 2 commits February 20, 2023 22:09
Add a test that checks whether systemd can access the TPM in order
access credentials requested via Load/SetCredentialEncrypted.

(cherry picked from commit e83babd)
(cherry picked from commit 568d6fc)
@flokli flokli marked this pull request as ready for review February 20, 2023 21:20
@flokli flokli requested a review from a team as a code owner February 20, 2023 21:20
@flokli
Copy link
Contributor Author

flokli commented Feb 20, 2023

Alright, this now successfully fixes the new nixosTests.systemd-credentials-tpm2 for 22.11, PTAL.

@flokli flokli merged commit 98407d0 into NixOS:staging-22.11 Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants