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

extend fix-key-location to handle cases from broken cross-storage moves #36068

Merged
merged 2 commits into from
Sep 1, 2023

Conversation

icewind1991
Copy link
Member

Fixes files not being decrypted when moved cross-storage due to (#35894) (ab)using the fact that the encrypted flag wasn't cleared for them (#35961) to find these cases.
If a working key can be found for the files they are decrypted in place, leaving the encrypted file as a backup to reduce chances of errors leading to further data loss.

This currently only works if the file hasn't been renamed after being moved because the file name is being used to try and find possible encryption keys. A possible further step would be to add an option to check every available key, covering more cases at the cost of performance.

A special case was also added for when encryption is enabled for the storage after encrypted files have been moved. In those cases the found key is copied to the correct location.

The "fix-key-location" name of the command isn't fully applicable anymore so it might make sense to give it a new name.


To test:

  1. setup an instance with encryption and a groupfolder
  2. move an encrypted file from the home storage to the groupfolder
  3. try to open the file
  4. run occ encryption:fix-key-location <user>
  5. try to open the file again

Alternatively add step 3.5 occ config:app:set groupfolders enable_encryption --value true to enable groupfolder encryption


Backport note: this currently reuses the normal encryption logic to determine which mountpoints are "system" mountpoints, previous nc versions didn't handle groupfolders for that properly. So some extra logic for that will probsbly need to be added for backports.

@icewind1991 icewind1991 added the 3. to review Waiting for reviews label Jan 10, 2023
@icewind1991 icewind1991 added this to the Nextcloud 26 milestone Jan 10, 2023
@icewind1991 icewind1991 requested review from PVince81, a team, ArtificialOwl and blizzz and removed request for a team January 10, 2023 13:40
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Psalm found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

$systemKeyPath = $this->getSystemKeyPath($node);

if ($this->rootView->file_exists($systemKeyPath)) {
// already has a key, reject new key
Copy link
Member

Choose a reason for hiding this comment

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

alternatively we could check if the mtime of the new key is higher than the existing one and then overwrite.
and don't overwrite is mtime is lower

a higher mtime would mean that the file has been overwritten by another user despite being not decryptable (not sure if possible though)

Copy link
Member Author

Choose a reason for hiding this comment

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

As is, this code path shouldn't be reached if a system key already exists, the check is mostly there as an extra safety rail.

Copy link
Member

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

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

nice one 👍

would this also work with primary object store, seeing that there's code that states that keys might not be in filecache, but filecache is needed for primary object store ?

$this->rootView->copy($key, $systemKeyPath);

try {
if (!$storage->instanceOfStorage(Encryption::class)) {
Copy link
Member

Choose a reason for hiding this comment

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

does it mean this command works even with encryption app disabled ? maybe add a comment here to answer

Copy link
Member Author

Choose a reason for hiding this comment

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

The command itself is part of the encryption app, so no

@PVince81
Copy link
Member

might be useful to add more debug logging / verbose output for each step in this complex algorithm, to be able to trace back repair problems and/or cases we haven't found yet

then we can ask people to run with -vvvv

also wondering if we should add a ctrl+c trap that only allow shutdown between two files, to avoid having a mess if an admin abort while we test keys combinations (where we put it in a place then delete again if it doesn't work).
because since this command is going to take a while, the likeliness of an admin wanting to abort might be higher

@blizzz blizzz removed their request for review July 20, 2023 10:19
Signed-off-by: Robin Appelman <[email protected]>
@icewind1991 icewind1991 merged commit 4711c77 into master Sep 1, 2023
39 checks passed
@icewind1991 icewind1991 deleted the gf-encryption-fix branch September 1, 2023 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants