From 9cb3a68c38ff7c8679ff1047a2f1531b083f23ff Mon Sep 17 00:00:00 2001 From: Garry McNulty Date: Tue, 18 Dec 2018 20:35:41 +0000 Subject: [PATCH] crypt: check for maximum length before decrypting filename The EME Transform() method will panic if the input data is larger than 2048 bytes. Fixes #2826 --- backend/crypt/cipher.go | 4 ++++ backend/crypt/cipher_test.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/backend/crypt/cipher.go b/backend/crypt/cipher.go index 9cf468d89..ab0656ba8 100644 --- a/backend/crypt/cipher.go +++ b/backend/crypt/cipher.go @@ -41,6 +41,7 @@ var ( ErrorBadDecryptControlChar = errors.New("bad decryption - contains control chars") ErrorNotAMultipleOfBlocksize = errors.New("not a multiple of blocksize") ErrorTooShortAfterDecode = errors.New("too short after base32 decode") + ErrorTooLongAfterDecode = errors.New("too long after base32 decode") ErrorEncryptedFileTooShort = errors.New("file is too short to be encrypted") ErrorEncryptedFileBadHeader = errors.New("file has truncated block header") ErrorEncryptedBadMagic = errors.New("not an encrypted file - bad magic string") @@ -284,6 +285,9 @@ func (c *cipher) decryptSegment(ciphertext string) (string, error) { // not possible if decodeFilename() working correctly return "", ErrorTooShortAfterDecode } + if len(rawCiphertext) > 2048 { + return "", ErrorTooLongAfterDecode + } paddedPlaintext := eme.Transform(c.block, c.nameTweak[:], rawCiphertext, eme.DirectionDecrypt) plaintext, err := pkcs7.Unpad(nameCipherBlockSize, paddedPlaintext) if err != nil { diff --git a/backend/crypt/cipher_test.go b/backend/crypt/cipher_test.go index 601d1e744..dc20c97a7 100644 --- a/backend/crypt/cipher_test.go +++ b/backend/crypt/cipher_test.go @@ -194,6 +194,10 @@ func TestEncryptSegment(t *testing.T) { func TestDecryptSegment(t *testing.T) { // We've tested the forwards above, now concentrate on the errors + longName := make([]byte, 3328) + for i := range longName { + longName[i] = 'a' + } c, _ := newCipher(NameEncryptionStandard, "", "", true) for _, test := range []struct { in string @@ -201,6 +205,7 @@ func TestDecryptSegment(t *testing.T) { }{ {"64=", ErrorBadBase32Encoding}, {"!", base32.CorruptInputError(0)}, + {string(longName), ErrorTooLongAfterDecode}, {encodeFileName([]byte("a")), ErrorNotAMultipleOfBlocksize}, {encodeFileName([]byte("123456789abcdef")), ErrorNotAMultipleOfBlocksize}, {encodeFileName([]byte("123456789abcdef0")), pkcs7.ErrorPaddingTooLong},