NeoApplications / Neo-Backup

backup manager for android
GNU Affero General Public License v3.0
2.52k stars 124 forks source link

[Bug] Potential GCM misuse #313

Open yawkat opened 3 years ago

yawkat commented 3 years ago

I've found two potential bugs in the crypto code of this app.

Unfortunately I'm having some trouble testing whether these potential bugs are applicable to the actual app, because the encryption code does not work on OpenJDK (IvParameterSpec does not work for GCM, I assume this works on Android).

machiav3lli commented 3 years ago

And any concrete suggestions to cover up those bugs?

yawkat commented 3 years ago

The first thing should be straight-forward, just extract the full thing (maybe to temp files) before replacing sensitive files. Even better would be just extracting the whole thing as bytes and not touching the tar at all to make it less error-prone, but I don't know if that's feasible. This could be fixed by using a streaming AEAD (like tink does for example) but this would probably break format

I don't know how to fix the second issue without breaking the format.

ioogithub commented 3 years ago

Just to weight in here, rolling your own crypto is a definitely not a best practice. The original oandbackup app had rock solid, proven pgp cencryption support via the actively supported openkeychain app. It would be nice to see this code reintroduced as an encryption option for oandbackupx.

machiav3lli commented 3 years ago

@ioogithub Yeah, I agree it's not the best practice. But gpg's concept/principle has been criticised enough so that I don't really need to spend a word on that.

Its (re-)integration is still not on my short TODO-list. Code contributions to #190 are nevertheless always welcome.

blaueente commented 3 years ago

GPG had the exact same problem, when used in a streaming way as a command line encryption/decryption tool. Although I would not subscribe to the general critique on it. The only real solution is to not trigger any actions until the whole application backup has been verified. Is this "streaming AEAD" a chunked one, where chunks are verified separately? Then the following attack remains: the attacker can just have the update stop (e.g. by corrupting it) at a specific point. Depending on the data structure, replacing half of the files may be enough to make the app's state inconsistent in a way that the attack goal is reached.

yawkat commented 3 years ago

@blaueente Yes, and I believe this has lead to security bugs with GPG in the past.

You can fairly easily defend against this in a backup tool, simply by writing to a temporary file first, and confirming that the full data was received (e.g. with a stop mark), before moving to the restore location. I don't know if tink does this.

blaueente commented 3 years ago

Writing temporary files on flash is something one wants to avoid, for speed and wear reasons. So, if you have direct access to the original file, just rewinding and reading it twice. Might need some access permission trickery to make sure that the original file does not change between the reads.

yawkat commented 3 years ago

Temporary files are not an issue because renaming is basically free. The technique is typically called an atomic file write