harden pseudo-YAML validation

This commit is contained in:
cathugger 2018-07-12 12:11:44 +00:00
parent ee0257dc88
commit 91f484cbc7
3 changed files with 7 additions and 4 deletions

View file

@ -419,4 +419,4 @@ test_ed25519.c.o: ed25519/ed25519-donna/ed25519-donna-64bit-tables.h
test_ed25519.c.o: ed25519/ed25519-donna/ed25519-donna-64bit-x86.h test_ed25519.c.o: ed25519/ed25519-donna/ed25519-donna-64bit-x86.h
test_ed25519.c.o: ed25519/ed25519-donna/ed25519-donna-impl-base.h test_ed25519.c.o: ed25519/ed25519-donna/ed25519-donna-impl-base.h
vec.c.o: vec.h vec.c.o: vec.h
yaml.c.o: types.h yaml.h ioutil.h base64.h common.h yaml.c.o: types.h yaml.h ioutil.h base32.h base64.h common.h

View file

@ -4,7 +4,7 @@ char *base32_to(char *dst,const u8 *src,size_t slen);
#define BASE32_TO_LEN(l) (((l) * 8 + 4) / 5) #define BASE32_TO_LEN(l) (((l) * 8 + 4) / 5)
// converts src string from base32 // converts src string from base32
size_t base32_from(u8 *dst,u8 *dmask,const char *src); size_t base32_from(u8 *dst,u8 *dmask,const char *src);
// calculates length needed to store data converted from base // calculates length needed to store data converted from base32
#define BASE32_FROM_LEN(l) (((l) * 5 + 7) / 8) #define BASE32_FROM_LEN(l) (((l) * 5 + 7) / 8)
// 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

7
yaml.c
View file

@ -10,6 +10,7 @@
#include "types.h" #include "types.h"
#include "yaml.h" #include "yaml.h"
#include "ioutil.h" #include "ioutil.h"
#include "base32.h"
#include "base64.h" #include "base64.h"
#include "common.h" #include "common.h"
@ -122,7 +123,7 @@ void yamlout_writekeys(const char *hostname,const u8 *formated_public,const u8 *
int yamlin_parseandcreate(FILE *fin,char *sname,const char *hostname) int yamlin_parseandcreate(FILE *fin,char *sname,const char *hostname)
{ {
char line[256]; char line[256];
size_t len; size_t len,cnt;
u8 pubbuf[FORMATTED_PUBLIC_LEN]; u8 pubbuf[FORMATTED_PUBLIC_LEN];
u8 secbuf[FORMATTED_SECRET_LEN]; u8 secbuf[FORMATTED_SECRET_LEN];
int hashost = 0,haspub = 0,hassec = 0,skipthis = 0; int hashost = 0,haspub = 0,hassec = 0,skipthis = 0;
@ -198,7 +199,9 @@ int yamlin_parseandcreate(FILE *fin,char *sname,const char *hostname)
len = strlen(p); len = strlen(p);
switch (keyt) { switch (keyt) {
case HOST: case HOST:
if (len != ONION_LEN) { if (len != ONION_LEN || base32_valid(p,&cnt) || cnt != BASE32_TO_LEN(PUBONION_LEN) ||
strcmp(&p[cnt],&hostname_example[cnt]) != 0)
{
fprintf(stderr,"ERROR: invalid hostname syntax\n"); fprintf(stderr,"ERROR: invalid hostname syntax\n");
return 1; return 1;
} }