Sizing and memory related C fixes.
[FIXED] mpw_templatesForType now returns a fully allocated array, was relying on undefined behavior before. [FIXED] mpw_variantWithName was allocating a badly sized standard string.
This commit is contained in:
parent
f80bbff46e
commit
8514a58e64
@ -11,6 +11,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
|
||||||
#define MP_N 32768
|
#define MP_N 32768
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
|
||||||
#define MP_N 32768
|
#define MP_N 32768
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
|
||||||
#define MP_N 32768
|
#define MP_N 32768
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
|
||||||
#define MP_N 32768
|
#define MP_N 32768
|
||||||
|
@ -52,7 +52,7 @@ const MPSiteType mpw_typeWithName(const char *typeName) {
|
|||||||
ftl( "Not a generated type name: %s", stdTypeName );
|
ftl( "Not a generated type name: %s", stdTypeName );
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const char **mpw_templatesForType(MPSiteType type, size_t *count) {
|
const char **mpw_templatesForType(MPSiteType type, size_t *count) {
|
||||||
|
|
||||||
if (!(type & MPSiteTypeClassGenerated)) {
|
if (!(type & MPSiteTypeClassGenerated)) {
|
||||||
ftl( "Not a generated type: %d", type );
|
ftl( "Not a generated type: %d", type );
|
||||||
@ -62,42 +62,42 @@ inline const char **mpw_templatesForType(MPSiteType type, size_t *count) {
|
|||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MPSiteTypeGeneratedMaximum: {
|
case MPSiteTypeGeneratedMaximum: {
|
||||||
*count = 2;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){ "anoxxxxxxxxxxxxxxxxx", "axxxxxxxxxxxxxxxxxno" };
|
"anoxxxxxxxxxxxxxxxxx", "axxxxxxxxxxxxxxxxxno" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedLong: {
|
case MPSiteTypeGeneratedLong: {
|
||||||
*count = 21;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){ "CvcvnoCvcvCvcv", "CvcvCvcvnoCvcv", "CvcvCvcvCvcvno",
|
"CvcvnoCvcvCvcv", "CvcvCvcvnoCvcv", "CvcvCvcvCvcvno",
|
||||||
"CvccnoCvcvCvcv", "CvccCvcvnoCvcv", "CvccCvcvCvcvno",
|
"CvccnoCvcvCvcv", "CvccCvcvnoCvcv", "CvccCvcvCvcvno",
|
||||||
"CvcvnoCvccCvcv", "CvcvCvccnoCvcv", "CvcvCvccCvcvno",
|
"CvcvnoCvccCvcv", "CvcvCvccnoCvcv", "CvcvCvccCvcvno",
|
||||||
"CvcvnoCvcvCvcc", "CvcvCvcvnoCvcc", "CvcvCvcvCvccno",
|
"CvcvnoCvcvCvcc", "CvcvCvcvnoCvcc", "CvcvCvcvCvccno",
|
||||||
"CvccnoCvccCvcv", "CvccCvccnoCvcv", "CvccCvccCvcvno",
|
"CvccnoCvccCvcv", "CvccCvccnoCvcv", "CvccCvccCvcvno",
|
||||||
"CvcvnoCvccCvcc", "CvcvCvccnoCvcc", "CvcvCvccCvccno",
|
"CvcvnoCvccCvcc", "CvcvCvccnoCvcc", "CvcvCvccCvccno",
|
||||||
"CvccnoCvcvCvcc", "CvccCvcvnoCvcc", "CvccCvcvCvccno" };
|
"CvccnoCvcvCvcc", "CvccCvcvnoCvcc", "CvccCvcvCvccno" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedMedium: {
|
case MPSiteTypeGeneratedMedium: {
|
||||||
*count = 2;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){ "CvcnoCvc", "CvcCvcno" };
|
"CvcnoCvc", "CvcCvcno" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedBasic: {
|
case MPSiteTypeGeneratedBasic: {
|
||||||
*count = 3;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){ "aaanaaan", "aannaaan", "aaannaaa" };
|
"aaanaaan", "aannaaan", "aaannaaa" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedShort: {
|
case MPSiteTypeGeneratedShort: {
|
||||||
*count = 1;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){"Cvcn"};
|
"Cvcn" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedPIN: {
|
case MPSiteTypeGeneratedPIN: {
|
||||||
*count = 1;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){ "nnnn" };
|
"nnnn" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedName: {
|
case MPSiteTypeGeneratedName: {
|
||||||
*count = 1;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]) {"cvccvcvcv"};
|
"cvccvcvcv" );
|
||||||
}
|
}
|
||||||
case MPSiteTypeGeneratedPhrase: {
|
case MPSiteTypeGeneratedPhrase: {
|
||||||
*count = 3;
|
return alloc_array( *count, const char *,
|
||||||
return (const char *[]){ "cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" };
|
"cvcc cvc cvccvcv cvc", "cvc cvccvcvcv cvcv", "cv cvccv cvc cvcvccv" );
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
ftl( "Unknown generated type: %d", type );
|
ftl( "Unknown generated type: %d", type );
|
||||||
@ -114,15 +114,19 @@ const char *mpw_templateForType(MPSiteType type, uint8_t seedByte) {
|
|||||||
if (!count)
|
if (!count)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return templates[seedByte % count];
|
char const *template = templates[seedByte % count];
|
||||||
|
free( templates );
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MPSiteVariant mpw_variantWithName(const char *variantName) {
|
const MPSiteVariant mpw_variantWithName(const char *variantName) {
|
||||||
|
|
||||||
char stdVariantName[strlen( variantName )];
|
// Lower-case and trim optionally leading "generated" string from typeName to standardize it.
|
||||||
strcpy( stdVariantName, variantName );
|
size_t stdVariantNameSize = strlen( variantName );
|
||||||
for (char *vN = stdVariantName; *vN; ++vN)
|
char stdVariantName[stdVariantNameSize + 1];
|
||||||
*vN = (char)tolower( *vN );
|
for (size_t c = 0; c < stdVariantNameSize; ++c)
|
||||||
|
stdVariantName[c] = (char)tolower( variantName[c] );
|
||||||
|
stdVariantName[stdVariantNameSize] = '\0';
|
||||||
|
|
||||||
if (0 == strcmp( stdVariantName, "p" ) || 0 == strcmp( stdVariantName, "password" ))
|
if (0 == strcmp( stdVariantName, "p" ) || 0 == strcmp( stdVariantName, "password" ))
|
||||||
return MPSiteVariantPassword;
|
return MPSiteVariantPassword;
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
// Copyright (c) 2014 Lyndir. All rights reserved.
|
// Copyright (c) 2014 Lyndir. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#ifndef _MPW_TYPES_H
|
||||||
|
#define _MPW_TYPES_H
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@ -73,9 +75,10 @@ const char *mpw_scopeForVariant(MPSiteVariant variant);
|
|||||||
const MPSiteType mpw_typeWithName(const char *typeName);
|
const MPSiteType mpw_typeWithName(const char *typeName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return An array of internal strings that express the templates to use for the given type.
|
* @return A newly allocated array of internal strings that express the templates to use for the given type.
|
||||||
* The amount of elements in the array is stored in count.
|
* The amount of elements in the array is stored in count.
|
||||||
* If an unsupported type is given, count will be 0 and will return NULL.
|
* If an unsupported type is given, count will be 0 and will return NULL.
|
||||||
|
* The array needs to be free'ed, the strings themselves must not be free'ed or modified.
|
||||||
*/
|
*/
|
||||||
const char **mpw_templatesForType(MPSiteType type, size_t *count);
|
const char **mpw_templatesForType(MPSiteType type, size_t *count);
|
||||||
/**
|
/**
|
||||||
@ -93,3 +96,4 @@ const char *mpw_charactersInClass(char characterClass);
|
|||||||
*/
|
*/
|
||||||
const char mpw_characterFromClass(char characterClass, uint8_t seedByte);
|
const char mpw_characterFromClass(char characterClass, uint8_t seedByte);
|
||||||
|
|
||||||
|
#endif // _MPW_TYPES_H
|
||||||
|
@ -25,6 +25,14 @@
|
|||||||
|
|
||||||
//// Buffers and memory.
|
//// Buffers and memory.
|
||||||
|
|
||||||
|
#define alloc_array(_count, _type, ...) ({ \
|
||||||
|
_type stackElements[] = (_type[]){ __VA_ARGS__ }; \
|
||||||
|
_count = sizeof( stackElements ) / sizeof( _type ); \
|
||||||
|
_type *allocElements = malloc( sizeof( stackElements ) ); \
|
||||||
|
memcpy( allocElements, stackElements, sizeof( stackElements ) ); \
|
||||||
|
allocElements; \
|
||||||
|
})
|
||||||
|
|
||||||
/** Push a buffer onto a buffer. reallocs the given buffer and appends the given buffer. */
|
/** Push a buffer onto a buffer. reallocs the given buffer and appends the given buffer. */
|
||||||
void mpw_pushBuf(
|
void mpw_pushBuf(
|
||||||
uint8_t **const buffer, size_t *const bufferSize, const void *pushBuffer, const size_t pushSize);
|
uint8_t **const buffer, size_t *const bufferSize, const void *pushBuffer, const size_t pushSize);
|
||||||
|
@ -730,6 +730,7 @@
|
|||||||
BN_add( permutations, permutations, templatePermutations );
|
BN_add( permutations, permutations, templatePermutations );
|
||||||
}
|
}
|
||||||
BN_free( templatePermutations );
|
BN_free( templatePermutations );
|
||||||
|
free( templates );
|
||||||
|
|
||||||
return [self timeToCrack:timeToCrack permutations:permutations forAttacker:attacker];
|
return [self timeToCrack:timeToCrack permutations:permutations forAttacker:attacker];
|
||||||
}
|
}
|
||||||
|
@ -605,7 +605,8 @@
|
|||||||
CGDirectDisplayID displayID = [self.window.screen.deviceDescription[@"NSScreenNumber"] unsignedIntValue];
|
CGDirectDisplayID displayID = [self.window.screen.deviceDescription[@"NSScreenNumber"] unsignedIntValue];
|
||||||
CGImageRef capturedImage = CGDisplayCreateImage( displayID );
|
CGImageRef capturedImage = CGDisplayCreateImage( displayID );
|
||||||
if (!capturedImage || CGImageGetWidth( capturedImage ) <= 1) {
|
if (!capturedImage || CGImageGetWidth( capturedImage ) <= 1) {
|
||||||
CFRelease( capturedImage );
|
if (capturedImage)
|
||||||
|
CFRelease( capturedImage );
|
||||||
wrn( @"Failed to capture screen image for display: %d", displayID );
|
wrn( @"Failed to capture screen image for display: %d", displayID );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user