2
0

mpw_realloc solves some issues with the realloc API that can lead to leaks.

This commit is contained in:
Maarten Billemont 2017-08-05 19:04:42 -04:00
parent 322e056661
commit 9abacaf905
4 changed files with 40 additions and 17 deletions

View File

@ -51,8 +51,7 @@ MPMarshalledSite *mpw_marshall_site(
MPMarshalledUser *marshalledUser, MPMarshalledUser *marshalledUser,
const char *siteName, const MPPasswordType siteType, const uint32_t siteCounter, const MPAlgorithmVersion algorithmVersion) { const char *siteName, const MPPasswordType siteType, const uint32_t siteCounter, const MPAlgorithmVersion algorithmVersion) {
if (!siteName || !(marshalledUser->sites = if (!siteName || !mpw_realloc( &marshalledUser->sites, NULL, sizeof( MPMarshalledSite ) * ++marshalledUser->sites_count ))
realloc( marshalledUser->sites, sizeof( MPMarshalledSite ) * (++marshalledUser->sites_count) )))
return NULL; return NULL;
MPMarshalledSite *site = &marshalledUser->sites[marshalledUser->sites_count - 1]; MPMarshalledSite *site = &marshalledUser->sites[marshalledUser->sites_count - 1];
@ -79,8 +78,7 @@ MPMarshalledSite *mpw_marshall_site(
MPMarshalledQuestion *mpw_marshal_question( MPMarshalledQuestion *mpw_marshal_question(
MPMarshalledSite *marshalledSite, const char *keyword) { MPMarshalledSite *marshalledSite, const char *keyword) {
if (!keyword || !(marshalledSite->questions = if (!keyword || !mpw_realloc( &marshalledSite->questions, NULL, sizeof( MPMarshalledQuestion ) * ++marshalledSite->questions_count ))
realloc( marshalledSite->questions, sizeof( MPMarshalledQuestion ) * (++marshalledSite->questions_count) )))
return NULL; return NULL;
MPMarshalledQuestion *question = &marshalledSite->questions[marshalledSite->questions_count - 1]; MPMarshalledQuestion *question = &marshalledSite->questions[marshalledSite->questions_count - 1];
@ -176,7 +174,8 @@ static bool mpw_marshall_write_flat(
} }
else if (site.type & MPSiteFeatureExportContent && site.content && strlen( site.content )) else if (site.type & MPSiteFeatureExportContent && site.content && strlen( site.content ))
content = mpw_decrypt( masterKey, site.content, site.algorithm ); content = mpw_decrypt( masterKey, site.content, site.algorithm );
} else if (site.type & MPSiteFeatureExportContent && site.content && strlen( site.content )) }
else if (site.type & MPSiteFeatureExportContent && site.content && strlen( site.content ))
// Redacted // Redacted
content = strdup( site.content ); content = strdup( site.content );

View File

@ -47,19 +47,16 @@ bool mpw_push_buf(uint8_t **const buffer, size_t *const bufferSize, const void *
// The buffer was marked as broken, it is missing a previous push. Abort to avoid corrupt content. // The buffer was marked as broken, it is missing a previous push. Abort to avoid corrupt content.
return false; return false;
*bufferSize += pushSize; if (!mpw_realloc( buffer, bufferSize, pushSize )) {
uint8_t *resizedBuffer = realloc( *buffer, *bufferSize );
if (!resizedBuffer) {
// realloc failed, we can't push. Mark the buffer as broken. // realloc failed, we can't push. Mark the buffer as broken.
mpw_free( *buffer, *bufferSize - pushSize ); mpw_free( *buffer, *bufferSize );
*bufferSize = (size_t)ERR; *bufferSize = (size_t)ERR;
*buffer = NULL; *buffer = NULL;
return false; return false;
} }
*buffer = resizedBuffer; uint8_t *bufferOffset = *buffer + *bufferSize - pushSize;
uint8_t *pushDst = *buffer + *bufferSize - pushSize; memcpy( bufferOffset, pushBuffer, pushSize );
memcpy( pushDst, pushBuffer, pushSize );
return true; return true;
} }
@ -94,6 +91,22 @@ bool mpw_push_int(uint8_t **const buffer, size_t *const bufferSize, const uint32
return mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) ); return mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) );
} }
bool mpw_realloc(void **buffer, size_t *bufferSize, const size_t deltaSize) {
if (!buffer)
return false;
void *newBuffer = realloc( *buffer, (bufferSize? *bufferSize: 0) + deltaSize );
if (!newBuffer)
return false;
*buffer = newBuffer;
if (bufferSize)
*bufferSize += deltaSize;
return true;
}
bool mpw_free(const void *buffer, const size_t bufferSize) { bool mpw_free(const void *buffer, const size_t bufferSize) {
if (!buffer) if (!buffer)
@ -257,7 +270,7 @@ const char *mpw_hex(const void *buf, size_t length) {
mpw_hex_buf = calloc( 10, sizeof( char * ) ); mpw_hex_buf = calloc( 10, sizeof( char * ) );
mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10; mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10;
mpw_hex_buf[mpw_hex_buf_i] = realloc( mpw_hex_buf[mpw_hex_buf_i], length * 2 + 1 ); if (mpw_realloc( &mpw_hex_buf[mpw_hex_buf_i], NULL, length * 2 + 1 ))
for (size_t kH = 0; kH < length; kH++) for (size_t kH = 0; kH < length; kH++)
sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] ); sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] );

View File

@ -112,6 +112,17 @@ bool mpw_string_pushf(
/** Push an integer onto a buffer. reallocs the given buffer and appends the given integer. */ /** Push an integer onto a buffer. reallocs the given buffer and appends the given integer. */
bool mpw_push_int( bool mpw_push_int(
uint8_t **const buffer, size_t *const bufferSize, const uint32_t pushInt); uint8_t **const buffer, size_t *const bufferSize, const uint32_t pushInt);
/** Reallocate the given buffer from the given size by adding the delta size.
* On success, the buffer size pointer will be updated to the buffer's new size
* and the buffer pointer may be updated to a new memory address.
* On failure, the buffer and pointers will remain unaffected.
* @param buffer A pointer to the buffer to reallocate.
* @param bufferSize A pointer to the buffer's actual size.
* @param deltaSize The amount to increase the buffer's size by.
* @return true if successful, false if reallocation failed.
*/
bool mpw_realloc(
void **buffer, size_t *bufferSize, const size_t deltaSize);
/** Free a buffer after zero'ing its contents. */ /** Free a buffer after zero'ing its contents. */
bool mpw_free( bool mpw_free(
const void *buffer, const size_t bufferSize); const void *buffer, const size_t bufferSize);

View File

@ -215,10 +215,10 @@ int main(int argc, char *const argv[]) {
// Read the user's sites file. // Read the user's sites file.
if (mpwSites) { if (mpwSites) {
// Read file. // Read file.
size_t readAmount = 4096, bufSize = 0, bufPointer = 0, readSize = 0; size_t readAmount = 4096, bufSize = 0, bufOffset = 0, readSize = 0;
char *buf = NULL; char *buf = NULL;
while ((buf = realloc( buf, bufSize += readAmount )) && while ((mpw_realloc( &buf, &bufSize, readAmount )) &&
(bufPointer += (readSize = fread( buf + bufPointer, 1, readAmount, mpwSites ))) && (bufOffset += (readSize = fread( buf + bufOffset, 1, readAmount, mpwSites ))) &&
(readSize == readAmount)); (readSize == readAmount));
if (ferror( mpwSites )) if (ferror( mpwSites ))
wrn( "Error while reading configuration file:\n %s: %d\n", mpwSitesPath, ferror( mpwSites ) ); wrn( "Error while reading configuration file:\n %s: %d\n", mpwSitesPath, ferror( mpwSites ) );
@ -227,7 +227,7 @@ int main(int argc, char *const argv[]) {
// Parse file. // Parse file.
MPMarshallError marshallError = { MPMarshallSuccess }; MPMarshallError marshallError = { MPMarshallSuccess };
MPMarshalledUser *user = mpw_marshall_read( buf, mpwSitesFormat, masterPassword, &marshallError ); MPMarshalledUser *user = mpw_marshall_read( buf, mpwSitesFormat, masterPassword, &marshallError );
mpw_free_string( buf ); mpw_free( buf, bufSize );
if (!user || marshallError.type != MPMarshallSuccess) { if (!user || marshallError.type != MPMarshallSuccess) {
if (marshallError.type == MPMarshallErrorMasterPassword) { if (marshallError.type == MPMarshallErrorMasterPassword) {
ftl( "Incorrect master password according to configuration:\n %s: %s\n", mpwSitesPath, marshallError.description ); ftl( "Incorrect master password according to configuration:\n %s: %s\n", mpwSitesPath, marshallError.description );