mirror of
https://github.com/vgough/encfs.git
synced 2024-11-22 07:53:31 +01:00
Merge pull request #149 from jetwhiz/fork-origin-15
add buffer size assertions when handling names
This commit is contained in:
commit
0602d89e03
@ -135,16 +135,18 @@ int BlockNameIO::maxDecodedNameLen(int encodedNameLen) const {
|
||||
}
|
||||
|
||||
int BlockNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
char *encodedName) const {
|
||||
// copy the data into the encoding buffer..
|
||||
memcpy(encodedName + 2, plaintextName, length);
|
||||
char *encodedName, int bufferLength) const {
|
||||
|
||||
// Pad encryption buffer to block boundary..
|
||||
int padding = _bs - length % _bs;
|
||||
if (padding == 0) padding = _bs; // padding a full extra block!
|
||||
|
||||
rAssert(bufferLength >= length + 2 + padding);
|
||||
memset(encodedName + length + 2, (unsigned char)padding, padding);
|
||||
|
||||
// copy the data into the encoding buffer..
|
||||
memcpy(encodedName + 2, plaintextName, length);
|
||||
|
||||
// store the IV before it is modified by the MAC call.
|
||||
uint64_t tmpIV = 0;
|
||||
if (iv && _interface >= 3) tmpIV = *iv;
|
||||
@ -182,7 +184,7 @@ int BlockNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
}
|
||||
|
||||
int BlockNameIO::decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const {
|
||||
char *plaintextName, int bufferLength) const {
|
||||
int decLen256 =
|
||||
_caseSensitive ? B32ToB256Bytes(length) : B64ToB256Bytes(length);
|
||||
int decodedStreamLen = decLen256 - 2;
|
||||
@ -225,6 +227,7 @@ int BlockNameIO::decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
}
|
||||
|
||||
// copy out the result..
|
||||
rAssert(finalSize < bufferLength);
|
||||
memcpy(plaintextName, tmpBuf + 2, finalSize);
|
||||
plaintextName[finalSize] = '\0';
|
||||
|
||||
|
@ -55,9 +55,9 @@ class BlockNameIO : public NameIO {
|
||||
|
||||
protected:
|
||||
virtual int encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
char *encodedName) const;
|
||||
char *encodedName, int bufferLength) const;
|
||||
virtual int decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const;
|
||||
char *plaintextName, int bufferLength) const;
|
||||
|
||||
private:
|
||||
int _interface;
|
||||
|
@ -142,7 +142,7 @@ bool NameIO::getReverseEncryption() const { return reverseEncryption; }
|
||||
std::string NameIO::recodePath(const char *path,
|
||||
int (NameIO::*_length)(int) const,
|
||||
int (NameIO::*_code)(const char *, int,
|
||||
uint64_t *, char *) const,
|
||||
uint64_t *, char *, int) const,
|
||||
uint64_t *iv) const {
|
||||
string output;
|
||||
|
||||
@ -166,11 +166,12 @@ std::string NameIO::recodePath(const char *path,
|
||||
// figure out buffer sizes
|
||||
int approxLen = (this->*_length)(len);
|
||||
if (approxLen <= 0) throw ERROR("Filename too small to decode");
|
||||
int bufSize = 0;
|
||||
|
||||
BUFFER_INIT(codeBuf, 32, (unsigned int)approxLen + 1)
|
||||
BUFFER_INIT_S(codeBuf, 32, (unsigned int)approxLen + 1, bufSize)
|
||||
|
||||
// code the name
|
||||
int codedLen = (this->*_code)(path, len, iv, codeBuf);
|
||||
int codedLen = (this->*_code)(path, len, iv, codeBuf, bufSize);
|
||||
rAssert(codedLen <= approxLen);
|
||||
rAssert(codeBuf[codedLen] == '\0');
|
||||
path += len;
|
||||
@ -217,21 +218,22 @@ std::string NameIO::decodePath(const char *path, uint64_t *iv) const {
|
||||
return getReverseEncryption() ? _encodePath(path, iv) : _decodePath(path, iv);
|
||||
}
|
||||
|
||||
int NameIO::encodeName(const char *input, int length, char *output) const {
|
||||
return encodeName(input, length, (uint64_t *)0, output);
|
||||
int NameIO::encodeName(const char *input, int length, char *output, int bufferLength) const {
|
||||
return encodeName(input, length, (uint64_t *)0, output, bufferLength);
|
||||
}
|
||||
|
||||
int NameIO::decodeName(const char *input, int length, char *output) const {
|
||||
return decodeName(input, length, (uint64_t *)0, output);
|
||||
int NameIO::decodeName(const char *input, int length, char *output, int bufferLength) const {
|
||||
return decodeName(input, length, (uint64_t *)0, output, bufferLength);
|
||||
}
|
||||
|
||||
std::string NameIO::_encodeName(const char *plaintextName, int length) const {
|
||||
int approxLen = maxEncodedNameLen(length);
|
||||
int bufSize = 0;
|
||||
|
||||
BUFFER_INIT(codeBuf, 32, (unsigned int)approxLen + 1)
|
||||
BUFFER_INIT_S(codeBuf, 32, (unsigned int)approxLen + 1, bufSize)
|
||||
|
||||
// code the name
|
||||
int codedLen = encodeName(plaintextName, length, 0, codeBuf);
|
||||
int codedLen = encodeName(plaintextName, length, 0, codeBuf, bufSize);
|
||||
rAssert(codedLen <= approxLen);
|
||||
rAssert(codeBuf[codedLen] == '\0');
|
||||
|
||||
@ -245,11 +247,12 @@ std::string NameIO::_encodeName(const char *plaintextName, int length) const {
|
||||
|
||||
std::string NameIO::_decodeName(const char *encodedName, int length) const {
|
||||
int approxLen = maxDecodedNameLen(length);
|
||||
int bufSize = 0;
|
||||
|
||||
BUFFER_INIT(codeBuf, 32, (unsigned int)approxLen + 1)
|
||||
BUFFER_INIT_S(codeBuf, 32, (unsigned int)approxLen + 1, bufSize)
|
||||
|
||||
// code the name
|
||||
int codedLen = decodeName(encodedName, length, 0, codeBuf);
|
||||
int codedLen = decodeName(encodedName, length, 0, codeBuf, bufSize);
|
||||
rAssert(codedLen <= approxLen);
|
||||
rAssert(codeBuf[codedLen] == '\0');
|
||||
|
||||
|
@ -83,19 +83,19 @@ class NameIO {
|
||||
|
||||
protected:
|
||||
virtual int encodeName(const char *plaintextName, int length,
|
||||
char *encodedName) const;
|
||||
char *encodedName, int bufferLength) const;
|
||||
virtual int decodeName(const char *encodedName, int length,
|
||||
char *plaintextName) const;
|
||||
char *plaintextName, int bufferLength) const;
|
||||
|
||||
virtual int encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
char *encodedName) const = 0;
|
||||
char *encodedName, int bufferLength) const = 0;
|
||||
virtual int decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const = 0;
|
||||
char *plaintextName, int bufferLength) const = 0;
|
||||
|
||||
private:
|
||||
std::string recodePath(const char *path, int (NameIO::*codingLen)(int) const,
|
||||
int (NameIO::*codingFunc)(const char *, int,
|
||||
uint64_t *, char *) const,
|
||||
uint64_t *, char *, int) const,
|
||||
uint64_t *iv) const;
|
||||
|
||||
std::string _encodePath(const char *plaintextPath, uint64_t *iv) const;
|
||||
@ -120,6 +120,16 @@ class NameIO {
|
||||
if (sizeof(Name##_Raw) < Size) Name = new char[Size]; \
|
||||
memset(Name, 0, Size);
|
||||
|
||||
#define BUFFER_INIT_S(Name, OptimizedSize, Size, BufSize) \
|
||||
char Name##_Raw[OptimizedSize]; \
|
||||
BufSize = sizeof(Name##_Raw); \
|
||||
char *Name = Name##_Raw; \
|
||||
if (sizeof(Name##_Raw) < Size) { \
|
||||
Name = new char[Size]; \
|
||||
BufSize = Size; \
|
||||
} \
|
||||
memset(Name, 0, Size);
|
||||
|
||||
#define BUFFER_RESET(Name) \
|
||||
do { \
|
||||
if (Name != Name##_Raw) { \
|
||||
|
@ -56,16 +56,20 @@ int NullNameIO::maxDecodedNameLen(int encodedNameLen) const {
|
||||
}
|
||||
|
||||
int NullNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
char *encodedName) const {
|
||||
char *encodedName, int bufferLength) const {
|
||||
(void)iv;
|
||||
|
||||
rAssert(length <= bufferLength);
|
||||
memcpy(encodedName, plaintextName, length);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int NullNameIO::decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const {
|
||||
char *plaintextName, int bufferLength) const {
|
||||
(void)iv;
|
||||
|
||||
rAssert(length <= bufferLength);
|
||||
memcpy(plaintextName, encodedName, length);
|
||||
|
||||
return length;
|
||||
|
@ -23,6 +23,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "rlog/Error.h"
|
||||
#include "rlog/rlog.h"
|
||||
#include "Interface.h"
|
||||
#include "NameIO.h"
|
||||
|
||||
@ -44,9 +46,9 @@ class NullNameIO : public NameIO {
|
||||
|
||||
protected:
|
||||
virtual int encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
char *encodedName) const;
|
||||
char *encodedName, int bufferLength) const;
|
||||
virtual int decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const;
|
||||
char *plaintextName, int bufferLength) const;
|
||||
|
||||
private:
|
||||
};
|
||||
|
@ -90,7 +90,7 @@ int StreamNameIO::maxDecodedNameLen(int encodedStreamLen) const {
|
||||
}
|
||||
|
||||
int StreamNameIO::encodeName(const char *plaintextName, int length,
|
||||
uint64_t *iv, char *encodedName) const {
|
||||
uint64_t *iv, char *encodedName, int bufferLength) const {
|
||||
uint64_t tmpIV = 0;
|
||||
if (iv && _interface >= 2) tmpIV = *iv;
|
||||
|
||||
@ -99,6 +99,7 @@ int StreamNameIO::encodeName(const char *plaintextName, int length,
|
||||
|
||||
// add on checksum bytes
|
||||
unsigned char *encodeBegin;
|
||||
rAssert(bufferLength >= length + 2);
|
||||
if (_interface >= 1) {
|
||||
// current versions store the checksum at the beginning
|
||||
encodedName[0] = (mac >> 8) & 0xff;
|
||||
@ -126,10 +127,11 @@ int StreamNameIO::encodeName(const char *plaintextName, int length,
|
||||
}
|
||||
|
||||
int StreamNameIO::decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const {
|
||||
char *plaintextName, int bufferLength) const {
|
||||
rAssert(length > 2);
|
||||
int decLen256 = B64ToB256Bytes(length);
|
||||
int decodedStreamLen = decLen256 - 2;
|
||||
rAssert(decodedStreamLen <= bufferLength);
|
||||
|
||||
if (decodedStreamLen <= 0) throw ERROR("Filename too small to decode");
|
||||
|
||||
|
@ -48,9 +48,9 @@ class StreamNameIO : public NameIO {
|
||||
|
||||
protected:
|
||||
virtual int encodeName(const char *plaintextName, int length, uint64_t *iv,
|
||||
char *encodedName) const;
|
||||
char *encodedName, int bufferLength) const;
|
||||
virtual int decodeName(const char *encodedName, int length, uint64_t *iv,
|
||||
char *plaintextName) const;
|
||||
char *plaintextName, int bufferLength) const;
|
||||
|
||||
private:
|
||||
int _interface;
|
||||
|
Loading…
Reference in New Issue
Block a user