prevent possible buffer overflow

This commit is contained in:
cathugger 2018-07-12 12:58:42 +00:00
parent 8f1e08973d
commit cf5ac5f929
2 changed files with 13 additions and 9 deletions

View file

@ -9,3 +9,5 @@ size_t base64_from(u8 *dst,const char *src,size_t slen);
// validates base32 string and optionally stores length of valid data // validates base32 string and optionally stores length of valid data
// returns 1 if whole string is good, 0 if string contains invalid data // returns 1 if whole string is good, 0 if string contains invalid data
int base64_valid(const char *src,size_t *count); int base64_valid(const char *src,size_t *count);
// aligns data length to something base64 can represent without padding
#define BASE64_DATA_ALIGN(l) (((l + 2) / 3) * 3)

20
yaml.c
View file

@ -131,8 +131,8 @@ int yamlin_parseandcreate(FILE *fin,char *sname,const char *hostname)
{ {
char line[256]; char line[256];
size_t len,cnt; size_t len,cnt;
u8 pubbuf[FORMATTED_PUBLIC_LEN]; u8 pubbuf[BASE64_DATA_ALIGN(FORMATTED_PUBLIC_LEN)];
u8 secbuf[FORMATTED_SECRET_LEN]; u8 secbuf[BASE64_DATA_ALIGN(FORMATTED_SECRET_LEN)];
int hashost = 0,haspub = 0,hassec = 0,skipthis = 0; int hashost = 0,haspub = 0,hassec = 0,skipthis = 0;
enum keytype { HOST, PUB, SEC } keyt; enum keytype { HOST, PUB, SEC } keyt;
@ -219,19 +219,21 @@ int yamlin_parseandcreate(FILE *fin,char *sname,const char *hostname)
skipthis = 1; skipthis = 1;
break; break;
case PUB: case PUB:
if (len != PUBKEY_LEN || !base64_valid(p,0)) { if (len != PUBKEY_LEN || !base64_valid(p,0) ||
base64_from(pubbuf,p,len) != FORMATTED_PUBLIC_LEN)
{
fprintf(stderr,"ERROR: invalid pubkey syntax\n"); fprintf(stderr,"ERROR: invalid pubkey syntax\n");
return 1; return 1;
} }
base64_from(pubbuf,p,len);
haspub = 1; haspub = 1;
break; break;
case SEC: case SEC:
if (len != SECKEY_LEN || !base64_valid(p,0)) { if (len != SECKEY_LEN || !base64_valid(p,0) ||
base64_from(secbuf,p,len) != FORMATTED_SECRET_LEN)
{
fprintf(stderr,"ERROR: invalid seckey syntax\n"); fprintf(stderr,"ERROR: invalid seckey syntax\n");
return 1; return 1;
} }
base64_from(secbuf,p,len);
hassec = 1; hassec = 1;
break; break;
} }
@ -273,16 +275,16 @@ int yamlin_parseandcreate(FILE *fin,char *sname,const char *hostname)
skipthis = 1; skipthis = 1;
} }
} }
if (!feof(fin)) { if (!feof(fin)) {
fprintf(stderr,"error while reading input\n"); fprintf(stderr,"error while reading input\n");
return 1; return 1;
} }
if (hostname) { if (hostname) {
fprintf(stderr,"hostname wasn't found in input\n"); fprintf(stderr,"hostname wasn't found in input\n");
return 1; return 1;
} }
return 0; return 0;
} }