chpock/cookfs

Entering wrong password corrupts archive

Closed this issue · 4 comments

When using encryption with -encyptkey -password "mypassword", entering wrong password at mount time and then unmounting seems to corrupt the archive (or encrypts it with an unknown password).

Steps to reproduce:

1. set fsid [vfs::cookfs::Mount test /local -encryptkey -password "Testing54321"]
2. copy some data into the archive
3. cookfs::Unmount $fsid
4. set fsid [vfs::cookfs::Mount test /local -encryptkey -password "wrongpassword"]
5. cookfs::Unmount $fsid

The archive is no longer usable. Then no matter which password I use, the archive is forever inaccessible.

I'm using TCL 8.6.12 and the latest version of cookfs from this repo

Thank you for this report.

This is the expected behavior when mounting an archive in readwrite mode. If cookfs unable to open archive and readwrite mode is specified, a new archive is assumed to be created at the end of the existing data in the file.

Thus, to avoid this behavior, the switch -readonly should be used. In this case, the error will be reported correctly and the archive will not be mounted.

However, I agree that this behavior is not intuitive for encrypted archives. I added changes to fail with an error in read-write mode when a file that opens looks like an existing cookfs archive and something wrong happened when reading the indexes (e.g. encryption error).

FYI: Opening files in readonly mode is preferable if the archive will not be modified. In this case, Cookfs will attempt to use memory mapped file. Archive will be cached by OS and shared between multiple application instances.

The new commit did not resolve the issue. Same exact issue.

I verified that the new code in the pages.c file before compiling.

@sheckandar please make sure:

  • that you updated the main branch
  • you do make tests. There should be tests cookfsPagesEncrypt-5.2.2 from pagesEncrypt.test and cookfsVfsEncrypt-5.2.2 from vfsEncrypt.test which reproduce the reported issue.
  • you are actually loading a rebuilt package and not a package that was built previously
  • provide the exact steps because it is possible you have encountered something else, and I was no longer able to reproduce it manually.

For example:

> package require cookfs
1.9.0
> set fsid [vfs::cookfs::Mount test /local -encryptkey -password "Testing54321"]
::cookfs::c::vfs::mount0x50e0000a2818
> puts [set fd [open /local/file w]] test; close $fd

> cookfs::Unmount $fsid
285
> set fsid [vfs::cookfs::Mount test /local -encryptkey -password "wrongpassword"]
Unable to create Cookfs object: could not decrypt the encryption key with the specified password
    while executing
"vfs::cookfs::Mount test /local -encryptkey -password "wrongpassword""
> cookfs::Unmount $fsid
given argument "::cookfs::c::vfs::mount0x50e0000a2818" is invalid fsid
    while executing
"cookfs::Unmount $fsid"
> set fsid [vfs::cookfs::Mount test /local -encryptkey -password "Testing54321"]
::cookfs::c::vfs::mount0x50e0000ca6d8
> read [set fd [open /local/file r]]
test

> cookfs::Unmount $fsid
205
> exit

As you can see, mounting with an incorrect password now fails. A subsequent mount with the correct password was successful and the file was accessible.

May apologies. The issue was on my end somewhere.

After clearing the entire directory tree on my computer and re-downloading all the code, it is now working.

Your help is very much appreciated.