Path: blob/main/crypto/heimdal/lib/asn1/check-common.c
34907 views
/*1* Copyright (c) 1999 - 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#ifdef HAVE_CONFIG_H36#include <config.h>37#endif38#ifdef HAVE_SYS_MMAN_H39#include <sys/mman.h>40#endif41#include <stdio.h>42#include <string.h>43#include <err.h>44#include <roken.h>4546#include "asn1-common.h"47#include "check-common.h"4849RCSID("$Id$");5051struct map_page {52void *start;53size_t size;54void *data_start;55size_t data_size;56enum map_type type;57};5859/* #undef HAVE_MMAP */6061void *62map_alloc(enum map_type type, const void *buf,63size_t size, struct map_page **map)64{65#ifndef HAVE_MMAP66unsigned char *p;67size_t len = size + sizeof(long) * 2;68int i;6970*map = ecalloc(1, sizeof(**map));7172p = emalloc(len);73(*map)->type = type;74(*map)->start = p;75(*map)->size = len;76(*map)->data_start = p + sizeof(long);77for (i = sizeof(long); i > 0; i--)78p[sizeof(long) - i] = 0xff - i;79for (i = sizeof(long); i > 0; i--)80p[len - i] = 0xff - i;81#else82unsigned char *p;83int flags, ret, fd;84size_t pagesize = getpagesize();8586*map = ecalloc(1, sizeof(**map));8788(*map)->type = type;8990#ifdef MAP_ANON91flags = MAP_ANON;92fd = -1;93#else94flags = 0;95fd = open ("/dev/zero", O_RDONLY);96if(fd < 0)97err (1, "open /dev/zero");98#endif99flags |= MAP_PRIVATE;100101(*map)->size = size + pagesize - (size % pagesize) + pagesize * 2;102103p = (unsigned char *)mmap(0, (*map)->size, PROT_READ | PROT_WRITE,104flags, fd, 0);105if (p == (unsigned char *)MAP_FAILED)106err (1, "mmap");107108(*map)->start = p;109110ret = mprotect (p, pagesize, 0);111if (ret < 0)112err (1, "mprotect");113114ret = mprotect (p + (*map)->size - pagesize, pagesize, 0);115if (ret < 0)116err (1, "mprotect");117118switch (type) {119case OVERRUN:120(*map)->data_start = p + (*map)->size - pagesize - size;121break;122case UNDERRUN:123(*map)->data_start = p + pagesize;124break;125default:126abort();127}128#endif129(*map)->data_size = size;130if (buf)131memcpy((*map)->data_start, buf, size);132return (*map)->data_start;133}134135void136map_free(struct map_page *map, const char *test_name, const char *map_name)137{138#ifndef HAVE_MMAP139unsigned char *p = map->start;140int i;141142for (i = sizeof(long); i > 0; i--)143if (p[sizeof(long) - i] != 0xff - i)144errx(1, "%s: %s underrun %d\n", test_name, map_name, i);145for (i = sizeof(long); i > 0; i--)146if (p[map->size - i] != 0xff - i)147errx(1, "%s: %s overrun %lu\n", test_name, map_name,148(unsigned long)map->size - i);149free(map->start);150#else151int ret;152153ret = munmap (map->start, map->size);154if (ret < 0)155err (1, "munmap");156#endif157free(map);158}159160static void161print_bytes (unsigned const char *buf, size_t len)162{163int i;164165for (i = 0; i < len; ++i)166printf ("%02x ", buf[i]);167}168169#ifndef MAP_FAILED170#define MAP_FAILED (-1)171#endif172173static char *current_test = "<uninit>";174static char *current_state = "<uninit>";175176static RETSIGTYPE177segv_handler(int sig)178{179int fd;180char msg[] = "SIGSEGV i current test: ";181182fd = open("/dev/stdout", O_WRONLY, 0600);183if (fd >= 0) {184write(fd, msg, sizeof(msg));185write(fd, current_test, strlen(current_test));186write(fd, " ", 1);187write(fd, current_state, strlen(current_state));188write(fd, "\n", 1);189close(fd);190}191_exit(1);192}193194int195generic_test (const struct test_case *tests,196unsigned ntests,197size_t data_size,198int (ASN1CALL *encode)(unsigned char *, size_t, void *, size_t *),199int (ASN1CALL *length)(void *),200int (ASN1CALL *decode)(unsigned char *, size_t, void *, size_t *),201int (ASN1CALL *free_data)(void *),202int (*cmp)(void *a, void *b),203int (ASN1CALL *copy)(const void *from, void *to))204{205unsigned char *buf, *buf2;206int i;207int failures = 0;208void *data;209struct map_page *data_map, *buf_map, *buf2_map;210211#ifdef HAVE_SIGACTION212struct sigaction sa, osa;213#endif214215for (i = 0; i < ntests; ++i) {216int ret;217size_t sz, consumed_sz, length_sz, buf_sz;218void *to = NULL;219220current_test = tests[i].name;221222current_state = "init";223224#ifdef HAVE_SIGACTION225sigemptyset (&sa.sa_mask);226sa.sa_flags = 0;227#ifdef SA_RESETHAND228sa.sa_flags |= SA_RESETHAND;229#endif230sa.sa_handler = segv_handler;231sigaction (SIGSEGV, &sa, &osa);232#endif233234data = map_alloc(OVERRUN, NULL, data_size, &data_map);235236buf_sz = tests[i].byte_len;237buf = map_alloc(UNDERRUN, NULL, buf_sz, &buf_map);238239current_state = "encode";240ret = (*encode) (buf + buf_sz - 1, buf_sz,241tests[i].val, &sz);242if (ret != 0) {243printf ("encoding of %s failed %d\n", tests[i].name, ret);244++failures;245continue;246}247if (sz != tests[i].byte_len) {248printf ("encoding of %s has wrong len (%lu != %lu)\n",249tests[i].name,250(unsigned long)sz, (unsigned long)tests[i].byte_len);251++failures;252continue;253}254255current_state = "length";256length_sz = (*length) (tests[i].val);257if (sz != length_sz) {258printf ("length for %s is bad (%lu != %lu)\n",259tests[i].name, (unsigned long)length_sz, (unsigned long)sz);260++failures;261continue;262}263264current_state = "memcmp";265if (memcmp (buf, tests[i].bytes, tests[i].byte_len) != 0) {266printf ("encoding of %s has bad bytes:\n"267"correct: ", tests[i].name);268print_bytes ((unsigned char *)tests[i].bytes, tests[i].byte_len);269printf ("\nactual: ");270print_bytes (buf, sz);271printf ("\n");272#if 0273rk_dumpdata("correct", tests[i].bytes, tests[i].byte_len);274rk_dumpdata("actual", buf, sz);275exit (1);276#endif277++failures;278continue;279}280281buf2 = map_alloc(OVERRUN, buf, sz, &buf2_map);282283current_state = "decode";284ret = (*decode) (buf2, sz, data, &consumed_sz);285if (ret != 0) {286printf ("decoding of %s failed %d\n", tests[i].name, ret);287++failures;288continue;289}290if (sz != consumed_sz) {291printf ("different length decoding %s (%ld != %ld)\n",292tests[i].name,293(unsigned long)sz, (unsigned long)consumed_sz);294++failures;295continue;296}297current_state = "cmp";298if ((*cmp)(data, tests[i].val) != 0) {299printf ("%s: comparison failed\n", tests[i].name);300++failures;301continue;302}303304current_state = "copy";305if (copy) {306to = emalloc(data_size);307ret = (*copy)(data, to);308if (ret != 0) {309printf ("copy of %s failed %d\n", tests[i].name, ret);310++failures;311continue;312}313314current_state = "cmp-copy";315if ((*cmp)(data, to) != 0) {316printf ("%s: copy comparison failed\n", tests[i].name);317++failures;318continue;319}320}321322current_state = "free";323if (free_data) {324(*free_data)(data);325if (to) {326(*free_data)(to);327free(to);328}329}330331current_state = "free";332map_free(buf_map, tests[i].name, "encode");333map_free(buf2_map, tests[i].name, "decode");334map_free(data_map, tests[i].name, "data");335336#ifdef HAVE_SIGACTION337sigaction (SIGSEGV, &osa, NULL);338#endif339}340current_state = "done";341return failures;342}343344/*345* check for failures346*347* a test size (byte_len) of -1 means that the test tries to trigger a348* integer overflow (and later a malloc of to little memory), just349* allocate some memory and hope that is enough for that test.350*/351352int353generic_decode_fail (const struct test_case *tests,354unsigned ntests,355size_t data_size,356int (ASN1CALL *decode)(unsigned char *, size_t, void *, size_t *))357{358unsigned char *buf;359int i;360int failures = 0;361void *data;362struct map_page *data_map, *buf_map;363364#ifdef HAVE_SIGACTION365struct sigaction sa, osa;366#endif367368for (i = 0; i < ntests; ++i) {369int ret;370size_t sz;371const void *bytes;372373current_test = tests[i].name;374375current_state = "init";376377#ifdef HAVE_SIGACTION378sigemptyset (&sa.sa_mask);379sa.sa_flags = 0;380#ifdef SA_RESETHAND381sa.sa_flags |= SA_RESETHAND;382#endif383sa.sa_handler = segv_handler;384sigaction (SIGSEGV, &sa, &osa);385#endif386387data = map_alloc(OVERRUN, NULL, data_size, &data_map);388389if (tests[i].byte_len < 0xffffff && tests[i].byte_len >= 0) {390sz = tests[i].byte_len;391bytes = tests[i].bytes;392} else {393sz = 4096;394bytes = NULL;395}396397buf = map_alloc(OVERRUN, bytes, sz, &buf_map);398399if (tests[i].byte_len == -1)400memset(buf, 0, sz);401402current_state = "decode";403ret = (*decode) (buf, tests[i].byte_len, data, &sz);404if (ret == 0) {405printf ("sucessfully decoded %s\n", tests[i].name);406++failures;407continue;408}409410current_state = "free";411if (buf)412map_free(buf_map, tests[i].name, "encode");413map_free(data_map, tests[i].name, "data");414415#ifdef HAVE_SIGACTION416sigaction (SIGSEGV, &osa, NULL);417#endif418}419current_state = "done";420return failures;421}422423424