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

Transfer ownership work with masterkey #28107

Merged
merged 1 commit into from
Jul 28, 2017

Conversation

sharidas
Copy link
Contributor

@sharidas sharidas commented Jun 8, 2017

This change helps users to get transfer-ownership
command work when masterkey is enabled as mode
of encryption.

Signed-off-by: Sujith H sharidasan@owncloud.com

Description

This change will help users to transfer files using transfer-ownership command when masterkey mode is enabled for encryption.

Related Issue

#27427

Motivation and Context

This change would help user tranfer files between users using transfer-ownership command when masterkey mode is enabled for encryption.

How Has This Been Tested?

Below are the steps executed for testing:
`sujith@sujith-Inspiron-5567 ~/test/owncloud $ ./occ files:transfer-ownership --path transferred\ from\ user1\ on\ 20170608_115804/1 admin user1
Cannot load Xdebug - it was already loaded
Analysing files of admin ...
1 [->--------------------------]
Check if master key enabled 1
1 [============================]
Collecting all share information for files and folder of admin ...
0 [>---------------------------]
Transferring files to user1/files/transferred from admin on 20170608_032359 ...
Restoring shares ...
0 [>---------------------------]
sujith@sujith-Inspiron-5567 ~/test/owncloud $ ./occ files:transfer-ownership --path transferred\ from\ admin\ on\ 20170608_032359/1 user1 admin
Cannot load Xdebug - it was already loaded
Analysing files of user1 ...
1 [============================]
Collecting all share information for files and folder of user1 ...
0 [>---------------------------]
Transferring files to admin/files/transferred from user1 on 20170608_033618 ...
Restoring shares ...
0 [>---------------------------]
sujith@sujith-Inspiron-5567 ~/test/owncloud $

This has also been tested by running the test transfer-ownership.feature`

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from e8b352c to cd0b99a Compare June 9, 2017 07:55
@sharidas sharidas changed the title Transfer ownership work with masterkey [WIP] Transfer ownership work with masterkey Jun 9, 2017
@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from cd0b99a to fdaae0a Compare June 11, 2017 07:36
@sharidas sharidas changed the title [WIP] Transfer ownership work with masterkey Transfer ownership work with masterkey Jun 11, 2017
@@ -944,6 +944,9 @@ public static function getDirectoryContent($directory, $mimetype_filter = '') {
* @return string
*/
public static function getPath($id) {
if (self::$defaultInstance === null) {
throw new NotFoundException("defaultInstance is null");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is one solution I found to get rid of the fatal error thrown by symfony. Any suggestions how to improve this?

Copy link
Contributor

Choose a reason for hiding this comment

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

thrown by symfony.

when exactly is this happening ? when running CI ?

Copy link
Contributor Author

@sharidas sharidas Jun 20, 2017

Choose a reason for hiding this comment

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

when exactly is this happening ? when running CI ?

When integration test is executed.

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from fdaae0a to 432b6bb Compare June 11, 2017 08:20
@sharidas
Copy link
Contributor Author

Attaching the integration-test result :
integration-test-result.txt

@@ -480,7 +480,9 @@ public function symmetricDecryptFileContent($keyFileContents, $passPhrase, $ciph
private function checkSignature($data, $passPhrase, $expectedSignature) {
$signature = $this->createSignature($data, $passPhrase);
if (!hash_equals($expectedSignature, $signature)) {
throw new HintException('Bad Signature', $this->l->t('Bad Signature'));
if (\OC::$server->getAppConfig()->getValue('encryption', 'useMasterKey', 0) === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I know the master key should still use signatures to detect when encrypted files have been tampered with.

Last time there was another bug that led to "Bad signature" which was because something in the encryption code wasn't properly initialized. I suggest you verify that.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can read from here #27261 (comment).
My solution was here #27261 (comment) but will probably not apply. Still, having a look there and at my PR might give some potential clues.

@@ -194,6 +195,10 @@ function (FileInfo $fileInfo) use ($progress, $self) {
$progress->advance();
$this->allFiles[] = $fileInfo;
if ($fileInfo->isEncrypted()) {

if (\OC::$server->getAppConfig()->getValue('encryption', 'useMasterKey', 0) !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not add it to the array if it's encrypted with master key ? answer in a PHPDoc to de-confuse future devs

try {
$this->shareManager->deleteShare($share);
} catch (\OCP\Files\NotFoundException $e) {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

add error handling or a comment why we don't handle / ignore

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch 2 times, most recently from 172f754 to f032e9f Compare June 20, 2017 06:22
@sharidas
Copy link
Contributor Author

Updated the PR by:

  1. Reverted the change made in apps/encryption/lib/Crypto/Crypt.php
  2. Added phpdoc for the exception in TransferOwnership.php
  3. Tried to write the message to log file when the exception is called. Rather than making it empty
  4. Added phpdoc explaining why the masterkey encryption doesn't need to be added to the array in TransferOwnership.php

* master key. The method getPath from \OC\Files\Filesystem tries
* to call getPath using defaultInstance variable which
* is null. Hence this causes our integration tests to break.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation. I think we shouldn't catch and ignore an exception like this just because of integration tests.

I was fine with your other workaround in the other method when instance is null but here I think it's a bit too much.
Do you know which test fails ? Maybe there's a setupFS missing somewhere to initialize that default instance ?

From what I see the defaultInstance is created when calling Filesystem::init().
Can you debug and compare why that one is properly set when running OCC manually and not when running integration tests ? There shouldn't be any difference...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the PR. This change is not required :( Hence I have removed the code in the latest PR. Here is the output of : OC_TEST_ENCRYPTION_MASTER_KEY_ENABLED=1 ./run.sh features/transfer-ownership.feature &> out.txt ( the out.txt :
out1.txt )

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from f032e9f to 2c38732 Compare June 21, 2017 14:52
@@ -194,6 +195,14 @@ function (FileInfo $fileInfo) use ($progress, $self) {
$progress->advance();
$this->allFiles[] = $fileInfo;
if ($fileInfo->isEncrypted()) {
if (\OC::$server->getAppConfig()->getValue('encryption', 'useMasterKey', 0) !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that in general the transfer ownership app isn't supposed to know about specific encryption implementations.
Some people might implement their own custom encryption schemes, so would need a better abstraction.

But as we likely don't have time for better abstractions let's merge this.

Copy link
Contributor

@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.

👍

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch 2 times, most recently from 0b55ee0 to 1fb8a88 Compare July 3, 2017 07:43
@sharidas
Copy link
Contributor Author

sharidas commented Jul 3, 2017

Updated the PR and the verified the transfer-ownership.feature on my machine:
log.txt

* version will be 1 and the expected version is at 2. And this results in Bad
* Signature. This change helps to fix the issue.
*/
if ($encryptedVersion === 2) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not able to get any better solution. As per my understanding this version is causing "Bad Signature". The problem is when the expected signature is for version 2 and the signature generated is for version 1.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please try creating and overwriting a file multiple times before the transfer. You'll see that the value of encrypted in the database can be higher, so this hard-coded 2 likely will not work.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also suggest to add this as an additional integration test ^

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from 1fb8a88 to 1a717b4 Compare July 3, 2017 09:16
* Instead of the reusing old version, we stick with version 1. This helps
* the data viewed in UI and helps resolve "Bad Signature".
*/
$encryptedVersion = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was right. When the data is updated in the file multiple times or file is edited multiple times, old logic fails. So if we make the version 1, then it looks like the data doesn't hit "Bad Signature".

Copy link
Contributor

Choose a reason for hiding this comment

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

the "Bad signature" was only observed with transfer ownership or also other cases ?

cc @tomneedham

Copy link
Contributor Author

@sharidas sharidas Jul 26, 2017

Choose a reason for hiding this comment

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

One of my observation is that whenever a new file is created the version is set to 0. So when transfer:ownership is called, altogether a new file is being created. Which is resulting in version to set to zero: https://github.com/owncloud/core/blob/master/lib/private/Files/Storage/Wrapper/Encryption.php#L743. Whenever new file is created the version is set: https://github.com/owncloud/encryption/blob/master/lib/Crypto/Encryption.php#L192. Need to figure out how to use the sourceInternalPath's version assigned to targetInternalPath.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did another experiment where I had 2 instances community edition(CE) and EE. Shared a folder of EE with CE. Now without my reset change, tried to create a file and moved to EE folder. It caused "Bad Signature". With my change enabled, created another file and moved to same EE folder. I was able to open the text file and read the contents. Moved the working file back to CE. No BadSignature was observed.

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from 1a717b4 to b985093 Compare July 4, 2017 06:13
@PVince81
Copy link
Contributor

@sharidas strange failures, I suggest rebasing

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from e79cc5f to 5bd426b Compare July 10, 2017 11:46
@sharidas
Copy link
Contributor Author

Rebased the branch with current master.

@sharidas
Copy link
Contributor Author

It looks like the transfer-ownership.feature is run as ./run.sh features/transfer-ownership.feature. Which causes issue when run on the terminal. Where as if ran as OC_TEST_ENCRYPTION_MASTER_KEY_ENABLED=1 ./run.sh features/transfer-ownership.feature doesn't cause any issue. Which also means need further investigation to get it working without setting OC_TEST_ENCRYPTION_MASTER_KEY_ENABLED.

@PVince81
Copy link
Contributor

failing tests, please have a look

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from 5bd426b to b8465f2 Compare July 17, 2017 17:07
@sharidas
Copy link
Contributor Author

The test cases were passing without encryption:
log.txt The file generated using command sujith@sujith-Inspiron-5567 ~/test/owncloud/tests/integration $ ./run.sh features/transfer-ownership.feature &> /tmp/log.txt
and with encryption masterkey:
encrypt.txt The file generated using command : sujith@sujith-Inspiron-5567 ~/test/owncloud/tests/integration $ OC_TEST_ENCRYPTION_MASTER_KEY_ENABLED=1 ./run.sh features/transfer-ownership.feature &> /tmp/encrypt.txt

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch 4 times, most recently from 83f79ad to 33db0ff Compare July 24, 2017 06:07
@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from 33db0ff to 2fa1d6a Compare July 27, 2017 12:04
if($isRename) {
$absSourcePath = Filesystem::normalizePath($sourceStorage->getOwner($sourceInternalPath). '/' . $sourceInternalPath);
$absTargetPath = Filesystem::normalizePath($this->getFullPath($targetInternalPath));
\OC::$server->getSession()->set('renameFile',[$absSourcePath, $absTargetPath]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One mechanism adopted to give a hint that rename action is taking place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And associated changes here: owncloud/encryption#8

* Instead of the reusing old version, we stick with version 1. This helps
* the data viewed in UI and helps resolve "Bad Signature".
*/
$cacheInformation['encryptedVersion'] += 1;
Copy link
Contributor Author

@sharidas sharidas Jul 27, 2017

Choose a reason for hiding this comment

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

No reset to 1. Instead increment the version.

@sharidas
Copy link
Contributor Author

sharidas commented Jul 27, 2017

This change works with transfer-ownership. The transfer of files between external storage found problematic :(

*
* $return array $header contain data as key-value pairs which should be
* written to the header, in case of a write operation
* or if no additional data is needed return a empty array
* @since 8.1.0
*/
public function begin($path, $user, $mode, array $header, array $accessList);
public function begin($path, $user, $mode, array $header, array $accessList, $renameFile);
Copy link
Contributor

Choose a reason for hiding this comment

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

since you are using a string instead of bool I suggest renaming this everywhere to $sourceFileOfRename = null.

@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch 3 times, most recently from efaa8fd to 83e8467 Compare July 28, 2017 13:40
This change helps users to get transfer-ownership
command work when masterkey is enabled as mode
of encryption.

Signed-off-by: Sujith H <sharidasan@owncloud.com>
@sharidas sharidas force-pushed the transfer-ownership-masterkey-task branch from 83e8467 to 9602182 Compare July 28, 2017 14:17
* Rename is a process of creating a new file. Here we try to use the
* incremented version of source file, for the destination file.
*/
$encryptedVersion = $sourceStorage->getCache()->get($sourceInternalPath)['encryptedVersion'];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified this change. Trivial change. There was failure in couple of tests : apps/files_trashbin/tests/StorageTest.php and apps/files_trashbin/tests/TrashbinTest.php

@sharidas sharidas merged commit 082eb95 into master Jul 28, 2017
@PVince81 PVince81 deleted the transfer-ownership-masterkey-task branch July 31, 2017 06:45
@PVince81
Copy link
Contributor

@sharidas please backport to stable10

@lock
Copy link

lock bot commented Aug 3, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants