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

Key sharing for bcachefs and ecryptfs #32279

Open
hyperfekt opened this issue Dec 3, 2017 · 30 comments
Open

Key sharing for bcachefs and ecryptfs #32279

hyperfekt opened this issue Dec 3, 2017 · 30 comments
Labels
0.kind: regression Something that worked before working no longer

Comments

@hyperfekt
Copy link
Contributor

hyperfekt commented Dec 3, 2017

Issue description

Since systemd version 233 each system service gets their own kernel keyring. This prevents disk decryption in cryptsetup, ecryptfs and bcachefs from sharing the keys with other services/instances that need them, making the volumes unmountable.
(A bcachefs ([GUID]): error requesting encryption key message is added to the kernel message buffer when attempting to do so.)
In version 235 (which we would need to update to) this is fixed by making available a new setting 'KeyringMode' for unit files, which can be set to 'shared' to achieve the previous behavior. A fix for cryptsetup is already included in systemd's cryptsetup-generator. Now it needs to be added to the other filesystems in order to restore proper decryption functionality.
Unfortunately my expertise with systemd is practically nil, so I could use some help - first of all I was not able to determine where or if the unit files for these two are actually generated.

Relevant:
Arch Linux discussion
the KeyringMode change
the cryptsetup-generator change

Steps to reproduce

  1. add boot.supportedFilesystems = [ "bcachefs" ]; to configuration.nix and nixos-rebuild
  2. create new encrypted bcachefs volume with bcachefs format --encrypted /dev/sdXX
  3. enter a passphrase twice
  4. unlock the volume with bcachefs unlock /dev/sdXX
  5. enter the passphrase given earlier
  6. attempt to mount with mount -t bcachefs /dev/sdXX /mnt

Technical details

  • system: "x86_64-linux"
  • host os: Linux 4.11.0, NixOS, 18.03pre121094.9c048f4fb6 (Impala)
  • multi-user?: no
  • sandbox: yes
  • version: nix-env (Nix) 1.11.15
  • channels(root): "nixos-18.03pre121094.9c048f4fb6"
  • nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs
@hyperfekt
Copy link
Contributor Author

hyperfekt commented Dec 3, 2017

I stumbled upon this discussion of the same issue by @intelfx .
Apparently there is not much we can do here, since mount can be launched in any context and will not have the KeyringMode=shared property?
Meaning the fix here for bcachefs is to wait for an update to bcachefs-tools and then switch to that, if I'm not mistaken. /cc @koverstreet
Not sure what the ideal solution for ecryptfs is, however, although I understand Arch Linux implemented something to fix it.

@hyperfekt
Copy link
Contributor Author

Turns out the analogue issue for ecryptfs was #27574, and the fix for that (#30333) also fixes the issue for bcachefs (tested with 17.09).
If anyone who understands more about the issue confirms that the fix is the correct solution for this problem, even with systemd v235, this can probably be closed as a duplicate.

@eternaleye
Copy link

I suspect it's not the correct solution, as systemd/systemd#6342 reverted linking the user keyring to the session keyring for all services (as making pam_keyinit.so default does) on security grounds.

I've filed upstream issue systemd/systemd#8159 for a more targeted solution, matching the approach taken in systemd/systemd#6832 for services.

@Chiiruno
Copy link
Contributor

This should be fixed now by #37647 and 361bd59...2d2ab94 which upgraded systemd to 238.

@Chiiruno
Copy link
Contributor

Chiiruno commented Mar 26, 2018

Or so I thought, but upon further attempts at a new install, it appears the same old problem is appearing for bcachefs.
dmesg | grep bcachefs
[ 1411.490679] bcachefs: bch2_fs_journal_init()
[ 1411.490684] bcachefs: bch2_fs_journal_init() ret 0
[ 1411.490685] bcachefs: bch2_fs_btree_cache_init()
[ 1411.490785] bcachefs: bch2_fs_btree_cache_init() ret 0
[ 1411.490786] bcachefs: bch2_fs_encryption_init()
[ 1411.542552] bcachefs (1808534f-da16-4882-ac6e-8d4925673f0e): error requesting encryption key: -126
[ 1411.542555] bcachefs: bch2_fs_encryption_init() ret -126

All the needed keys ARE in the keyring
keyctl list @u
8 keys in keyring:
565092892: --alsw-v 0 0 logon: bcachefs:1808534f-da16-4882-ac6e-8d4925673f0e
1045826354: --alswrv 0 0 user: bcachefs:adfedea3-0f2a-41be-a692-44fdb47db68a
146276675: --alsw-v 0 0 logon: bcachefs:adfedea3-0f2a-41be-a692-44fdb47db68a
1072420447: --alswrv 0 0 user: bcachefs:d8ded406-2657-41fe-b07b-f4317a3b442c
194219927: --alsw-v 0 0 logon: bcachefs:d8ded406-2657-41fe-b07b-f4317a3b442c
351605712: --alswrv 0 0 user: bcachefs:eacfd316-7c70-4cf6-9352-1a7ee8a7e27e
369762748: --alsw-v 0 0 logon: bcachefs:eacfd316-7c70-4cf6-9352-1a7ee8a7e27e
835897310: --alswrv 0 0 user: bcachefs:1808534f-da16-4882-ac6e-8d4925673f0e

I double checked, and I am in fact using systemd version 238.

I created the bcachefs partition with mkfs.bcachefs --compression_type lz4 --encrypted -L root --discard -t 0 /dev/nvme0n1p4 < /root/crypto/root.key
I unlocked it with bcachefs unlock /dev/nvme0n1p4 < /root/crypto/root.key
I attempted to mount it with mount -t bcachefs /dev/nvme0n1p4 /mnt
The error message I got was mount: /mnt: mount(2) system call failed: Cannot allocate memory.
I do have a separate dmcrypt partition open, but I tested this without it open and the same problem exists.

@fpletz fpletz added this to the 18.03 milestone Mar 28, 2018
@fpletz fpletz added the 0.kind: regression Something that worked before working no longer label Mar 28, 2018
@fpletz fpletz closed this as completed Apr 1, 2018
fpletz referenced this issue Apr 1, 2018
This fixes a bug with changed qemu network interface names and also generally
should be preferred to using a release tag.
@vcunat
Copy link
Member

vcunat commented Apr 2, 2018

Eh, after skimming this and linked tickets, I don't know if the problem got solved (by systemd-238 bump) or not :-/ EDIT: from comments I'd think it is not really solved, but fpletz closed this.

@Chiiruno
Copy link
Contributor

Chiiruno commented Apr 3, 2018

...Why is this closed?
I'll test out bcachefs on a USB I guess, but I didn't see anything there that would have fixed this.

@Chiiruno
Copy link
Contributor

Chiiruno commented Apr 3, 2018

Yeah, that didn't work. Still the same problem, can't request the encryption key and it runs out of memory.
That was on a staging USB with systemd 238.
New issue here: #38366

@vcunat vcunat reopened this Apr 3, 2018
@Chiiruno
Copy link
Contributor

Chiiruno commented Apr 3, 2018

Closed the new issue since this is back up.

@matthewbauer matthewbauer modified the milestones: 18.03, 18.09 Apr 17, 2018
@Chiiruno
Copy link
Contributor

Chiiruno commented Aug 30, 2018

Still getting the same error, even though we're on 239. (or at least should be, for tests)
mount: /tmp/mnt: mount(2) system call failed: Cannot allocate memory.
Related to #45774

@Chiiruno
Copy link
Contributor

Chiiruno commented Oct 8, 2018

cc @vcunat @fpletz
Systemd should be updated for this, but it's still not working.
Is there something in the repo blocking this?

@vcunat
Copy link
Member

vcunat commented Oct 8, 2018

I'm not aware of anything, but I don't think I know the related stuff well.

@hyperfekt
Copy link
Contributor Author

hyperfekt commented Nov 5, 2018

After talking to Kent Overstreet, the author of bcachefs, I know that there will not be a fix in the forseeable future.
I suggest using #49506 to replicate the change from Arch Linux for systems with bcachefs enabled only, possibly constrained to single user installations only to limit the security implications (whose accuracy I frankly don't entirely understand).

@ZoomRmc
Copy link

ZoomRmc commented Mar 14, 2019

Bumped into this issue while installing a new system from custom-built image. Systemd v239. Tried with lz4 compression and reformatted without, both times getting the same error.

@matthewbauer matthewbauer modified the milestones: 19.03, 19.09 May 27, 2019
@hyperfekt
Copy link
Contributor Author

Here is a workaround, with unevaluated security implications:

options.security.pam.services = with lib; mkOption {
    type = types.loaOf (types.submodule {
      config.text = mkDefault (mkAfter "session required pam_keyinit.so force revoke");
    });
  };

@Chiiruno
Copy link
Contributor

How does this have security implications?
I heard something about limiting it to single user installs, but I'm not entirely sure why this would be an issue.

@stale
Copy link

stale bot commented Jun 2, 2020

Thank you for your contributions.

This has been automatically marked as stale because it has had no activity for 180 days.

If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse.
  3. Ask on the #nixos channel on irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 2, 2020
@yorickvP
Copy link
Contributor

yorickvP commented Jun 3, 2020

Still a thing

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 3, 2020
@stale
Copy link

stale bot commented Nov 30, 2020

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 30, 2020
@Chiiruno
Copy link
Contributor

Still important to me.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 30, 2020
@veprbl veprbl removed this from the 19.09 milestone May 31, 2021
@Madouura
Copy link
Contributor

Why is this a problem in a nixos vm but not on real hardware?

@Madouura
Copy link
Contributor

I think we can now safely close this as encryption for bcachefs works on bare metal without any workarounds, and our tests can now call keyctl to work around a particular peculiarity.

@Madouura
Copy link
Contributor

Never mind, this has managed to rear it's ugly head again.
Related: koverstreet/bcachefs#374
It would appear that on bare-metal (non-usb) everything works fine, however on a nixos installation usb, I get this issue.
20220329_150415

@Madouura Madouura reopened this Mar 29, 2022
@Madouura
Copy link
Contributor

Madouura commented Apr 9, 2022

The issue is a bad interaction between nixos stable (21.11) and the parts of nixos unstable (master) from using an override for the unstable version of bcachefs pulls.
I do need to do keyctl link @u @s in the usb installation environment to get it to work, however.

@Madouura Madouura closed this as completed Apr 9, 2022
@obadz
Copy link
Contributor

obadz commented Apr 29, 2022

Here is a workaround, with unevaluated security implications:

options.security.pam.services = with lib; mkOption {
    type = types.loaOf (types.submodule {
      config.text = mkDefault (mkAfter "session required pam_keyinit.so force revoke");
    });
  };

This issue and proposed solution seems related to #27574 (comment)

@obadz obadz reopened this Apr 29, 2022
@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 13, 2022
@clonejo
Copy link

clonejo commented Apr 5, 2023

Still an issue for me, i am running into this while trying to do a manual install with a freshly built unstable boot image. @Madouura's workaround worked out, luckily:

env -f '<nixpkgs>' -iA keyutils
keyctl link @u @s
bcachefs unlock …
mount …
nixos-install
reboot
# profit :)

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 5, 2023
@onny
Copy link
Contributor

onny commented Jul 27, 2023

Bug still present in NixOS 23.05. Added a note about this to the NixOS wiki https://nixos.wiki/wiki/Bcachefs#NixOS_installation_on_bcachefs

@kennystrawnmusic
Copy link

kennystrawnmusic commented Feb 10, 2024

Workaround works for me, however I need to hit "Esc" a bunch of times when the Plymouth boot screen appears; is there a Nix-specific way to have plymouth ask-for-password run at startup?

@onny
Copy link
Contributor

onny commented Jun 23, 2024

Bug still present in NixOS 24.05. Added a note about this to the NixOS wiki https://nixos.wiki/wiki/Bcachefs#NixOS_installation_on_bcachefs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: regression Something that worked before working no longer
Projects
Status: To triage/investigate
Development

No branches or pull requests