Skip to content

make encryption stream wrapper seekable - #8390

Closed
jknockaert wants to merge 5 commits into
owncloud:masterfrom
jknockaert:master
Closed

jknockaert wants to merge 5 commits into
owncloud:masterfrom
jknockaert:master

Conversation

@jknockaert

Copy link
Copy Markdown
Contributor

I refactored the encryption stream wrapper to make it seekable. This is necessary to handle http range requests efficiently. With the old code the Sabre webdav server would set the beginning of the range by applying fseek on the unencrypted stream, which resulted in the pointer of the encrypted streem set in the middle of a block and subsequently an error in decrypting the subsequent part of the stream. After making the stream seekable (with fseek applying on the unencrypted rather than the encrypted string) the dav server can much more efficiently handle http get operations on parts of large files.

The general concept is to add a position variable as a pointer for the unencrypted stream. This pointer can be moved around. To read from the stream the current block is stored in cache. Writing is also done on the cache. If necessary, read and write spill over in the next block(s). When moving to another block the cash is always flushed (with a writeflag determining if write is necessary).

The getFileSize function is updated along (making use of the seekable nature of the stream).

I refactored the encryption stream wrapper to make it seekable. This is
necessary to handel http range requests efficiently. With the old code
the Sabre webdav server would set the beginning of the range by
applying fseek on the unencrypted stream, which resulted in the pointer
set in the middle of an encrypted block and subsequently an error in
uncrypting the subsequent part of the string. After making the stream
seekable (with fseek applying on the unencrypted rather than the
encrypted string) the dav server can much more efficiently handle http
get operations on parts of large files.
@ghost

ghost commented Apr 28, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/about/contributor-agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

@karlitschek

Copy link
Copy Markdown
Contributor

@schiesbn What do you think?

@PVince81

Copy link
Copy Markdown
Contributor

Wow, nice idea. I can't evaluate your solution and I'd be curious to hear what @schiesbn will say.
It would be good if you could also add unit tests to test the seeking.

Note that in some cases seeking will not be possible when the encrypted files are stored on an unseekable external storage (ex: SMB), does your solution also have a fallback for that ?

@jknockaert

Copy link
Copy Markdown
Contributor Author

@PVince81 Not sure how to do a unit test, sorry.
I didn't think of the possibility that the encrypted stream might be unseekable. I will fix that later today. It won't solve the problem of serving http range requests though, as there's a bug in the Sabre DAV code dealing with non-seekable streams (for which I submitted a pull request already).

@PVince81

Copy link
Copy Markdown
Contributor

@jknockaert

Copy link
Copy Markdown
Contributor Author

@PVince81 Thanks, I will have a look at the unit test after I fix the nonseekable stream support. It's already clear that I will have to rework my patch thoroughly for that, but I do have an idea how to fix it.

@PVince81

Copy link
Copy Markdown
Contributor

Sounds great, thanks 😄

Comment thread apps/files_encryption/lib/stream.php Outdated

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.

in lib/proxy.php we have a postFileSize proxy which should already take care that you get the unencrypted size. No need to calculate it here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will check it out; but we must be sure that nothing is called that depends on the stream wrapper, which could result in an infinite loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I could not yet fix this (see comment below).

@DeepDiver1975

Copy link
Copy Markdown
Contributor

@owncloud-bot ok to test

getFileSize is restored to allow for non-seekable encrypted streams.
Also an irritating typo's fixed.
@jknockaert

Copy link
Copy Markdown
Contributor Author

Ok; being new to github I can't make much sense of all these automated tests and inspections. I will post a message when I'm done reworking the code; I suggest to suspend review & testing till then.

@PVince81

Copy link
Copy Markdown
Contributor

No worries. Let us know when your code is ready to review.

Save for the unencrypted size calc I think now most code allows for
non-seekable encrypted streams.
@jknockaert

Copy link
Copy Markdown
Contributor Author

OK, pull request is updated. I got pretty far in supporting non-seekable encrypted streams. fseek on the unencrypted stream will return -1 when the encrypted stream is non-seekable, fread will work as normal, and fwrite will only work for modifications at the end of the encrypted file and if that file ends with a complete block. For seekable encrypted streams pretty much all operations will be transparent (such as overwriting in the middle of the stream).

Only the hack to get the unencrypted size when opening the stream is still seekable-only. @schiesbn suggested using postFileSize, but I couldn't understand how it works. I'm happy to take any code suggestions here.

After I manage to fix the unencrypted size code I will check out the unit test.

@PVince81

Copy link
Copy Markdown
Contributor

Sounds great, thanks

@jknockaert

Copy link
Copy Markdown
Contributor Author

Hmm. I can't make much of this test report. What is the statement actually saying? Did the test fail to run? Or did the code fail the test? Any help welcome.

@PVince81

Copy link
Copy Markdown
Contributor

Better wait for the result of the banner at the bottom to be either green or red, then look into the last failure. It looks like Jenkins is still running and that fail was an older one.

@ghost

ghost commented Apr 29, 2014

Copy link
Copy Markdown

@jknockaert

Copy link
Copy Markdown
Contributor Author

The result is now red, it reports 4 fails (although to me it seems to be four times the same issue). I checked test/proxy.php; but I can't make much sense of testPostFileSize (why should the filesize in storage be the same encrypted and unencrypted?) and how it relates to my changes. Sorry.

@PVince81

PVince81 commented May 2, 2014

Copy link
Copy Markdown
Contributor

I'm also not sure. You can maybe try running the unit tests locally, it might help debugging it:
./autotest.sh sqlite ../apps/files_encryption/tests/proxy.php

Else we'll have to wait for @schiesbn's feedback.

@jknockaert

Copy link
Copy Markdown
Contributor Author

OK thanks for your reaction. I'd prefer to wait for some more feedback, provided that there's a snippet of code left that has to be replaced before final testing can happen.

@jknockaert

Copy link
Copy Markdown
Contributor Author

I updated the unencrypted filesize calculation to work on nonseekable streams. I didn't test it yet, but it's fairly trivial. I'm just wondering if the approach I use may be of any value for getFileSize in util.php, where I noticed the comment "if fseek also fails on the local storage, than there is nothing we can do". I think the approach I implemented is better, as copying the stream to local is probably not faster than reading it in chunks of 8192 bytes, and may be even much slower for files that exceed buffer size so that the local copy would be written to disk. But I am not aware of the motivations for the approach used in getFileSize, so I decided not to change anything there.

@jknockaert

Copy link
Copy Markdown
Contributor Author

Hmm, I'm afraid I'm getting nowhere with the unit test.

@jknockaert

Copy link
Copy Markdown
Contributor Author

OK--forget the last update. Rewind doesn't work on nonseekable streams; I overlooked this limitation. I will come up with a better idea.

@jknockaert

Copy link
Copy Markdown
Contributor Author

OK, I think it should now handle nonseekable streams correctly.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@PVince81 Well, I need it on my own installation of OC7 so I will do that first; just unsure if it makes sense to do the work for 7.0.2 if 7.0.3 is around the corner. I will definitely port my patch to the master branch but that will come after.

@PVince81

PVince81 commented Oct 6, 2014

Copy link
Copy Markdown
Contributor

I think currently the encryption code from the stable7 branch (the future OC 7.0.3) and master should not differ too much.
So if you port your code to work on stable7 it should be possible to make it work on master as well (or the reverse).

@ghost

ghost commented Oct 15, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/contribute/agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

@jknockaert

Copy link
Copy Markdown
Contributor Author

Ok, I started working on updating the patch for OC7. The issue that I keep hitting is to get the unencrypted filesize when the encrypted stream is opened. @schiesbn already suggested postFileSize, but there seems to be a case for recursivity, as postFileSize will call getFileSize when it needs to fix the unencrypted size, and getFileSize will open the encrypted stream.
It would probably be more efficient to use postFileSize in order to avoid recalculating the unencrypted size over and over, but that means that getFileSize should decrypt the last block of the encrypted file without opening it is an encrypted stream. I will fix that first (and at the same time make sure it works with unseekable local storage as well).

@jknockaert jknockaert mentioned this pull request Oct 19, 2014
@jknockaert

Copy link
Copy Markdown
Contributor Author

The new getFileSize is submitted as PR #11659. I will wait some days to see if that works before reworking the encrypted stream for OC7.

@PVince81

Copy link
Copy Markdown
Contributor

The other PR was merged, please rebase.

@jknockaert

Copy link
Copy Markdown
Contributor Author

I think it's better to wait for #11736 (which in turn depends on #11539). I understood it should not take too long before these are merged. After that I will start from the master version of stream.php. I guess it would be better to open a new PR then, right? Sorry, I'm not very experienced in working with these git(hub) tools.

@PVince81

Copy link
Copy Markdown
Contributor

Yes, makes sense. I missed these two.

@PVince81

Copy link
Copy Markdown
Contributor

I mean makes sense to wait.
Please keep this PR open. I can guide you through rebasing when needed.

@jknockaert

Copy link
Copy Markdown
Contributor Author

Ok, we'll come back here after the the two are merged.

@ghost

ghost commented Nov 21, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/contribute/agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

@PVince81

Copy link
Copy Markdown
Contributor

@jknockaert seems the two linked PRs were merged. Can you rebase ? 😄

@jknockaert

Copy link
Copy Markdown
Contributor Author

I will take this up next weekend.

@MorrisJobke

Copy link
Copy Markdown
Contributor

@jknockaert Thanks :)

@jknockaert

Copy link
Copy Markdown
Contributor Author

OK, I ported my code to 7.0.4. It works. I will continue to test it for a couple of days, then it should be rebased to the master. @PVince81 I will need help for rebasing. No clue how this github cms works (I've been struggling with).

@MorrisJobke

Copy link
Copy Markdown
Contributor

@jknockaert Feel free to ping me if @PVince81 is not available in IRC (I'm MorrisJobke in #owncloud-dev at freenode ;)) Big thanks to you :)

@jknockaert

Copy link
Copy Markdown
Contributor Author

@MorrisJobke OK, thanks. Will take some time before the code is ready for this PR, as I need to add comments as well is remove all debugging commands.

@MorrisJobke

Copy link
Copy Markdown
Contributor

@jknockaert no problem :) I just want you to know that you don't need to feel bad about the tooling - we can help there ;)

@jknockaert

Copy link
Copy Markdown
Contributor Author

@MorrisJobke For my understanding: is there any deadline coming up that ideally should be met for this PR? It was once suggested to include it in OC8, but I have no idea if that's still possible.

@schiessle

Copy link
Copy Markdown
Contributor

@jknockaert feature freeze for ownCloud8 is 2. January 2015. Would be great to have it in OC8. But don't worry if it doesn't make it, the next release will come only three months later.

I would also recommend to base your work as soon as possible on master. There are quite some changes and I want to avoid that you have to do stuff twice.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn Avoiding to do stuff over again is exactly why I want to have it in OC8 if remotely possible. I already had to redo it since OC6 for OC7. Let's target rebasing by the end of this week, so that early next week the discussion and review process can start.

@jknockaert

Copy link
Copy Markdown
Contributor Author

Rewritten in #12909.

@jknockaert jknockaert closed this Dec 17, 2014
@jknockaert jknockaert mentioned this pull request Mar 29, 2015
1 task
@lock lock Bot locked as resolved and limited conversation to collaborators Aug 15, 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.

8 participants