|
| 1 | +/************************************************************************ |
| 2 | + * Copyright (C) 2017-2018 IAIK TU Graz and Fraunhofer AISEC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + ***********************************************************************/ |
| 17 | + |
| 18 | +/** |
| 19 | + * @file dmt-access.c |
| 20 | + * @brief Dynamic memory tracking simple access selftest. |
| 21 | + * @license This project is released under the GNU GPLv3+ License. |
| 22 | + * @author See AUTHORS file. |
| 23 | + * @version 0.3 |
| 24 | + */ |
| 25 | + |
| 26 | +/***********************************************************************/ |
| 27 | + |
| 28 | +#include <stdlib.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <unistd.h> |
| 31 | +#include <time.h> |
| 32 | +#include "dmt-utils.h" |
| 33 | + |
| 34 | +/***********************************************************************/ |
| 35 | + |
| 36 | +#define LARGE_INIT 524288 |
| 37 | + |
| 38 | +/***********************************************************************/ |
| 39 | + |
| 40 | +void help(); |
| 41 | + |
| 42 | +/***********************************************************************/ |
| 43 | + |
| 44 | +uint16_t ALLOC_TYPE = 0; |
| 45 | +uint16_t USE_ALTERNATIVE = 0; |
| 46 | + |
| 47 | +/***********************************************************************/ |
| 48 | + |
| 49 | +/*** |
| 50 | + * Main function. |
| 51 | + * |
| 52 | + * @param argc number of command line arguments |
| 53 | + * @param argv command line arguments |
| 54 | + * @return 0=success, else=error |
| 55 | + */ |
| 56 | +int main(int argc, char **argv) |
| 57 | +{ |
| 58 | + /* init */ |
| 59 | + int err = 1; |
| 60 | + uint8_t key = 0; |
| 61 | + FILE *kfile = NULL; |
| 62 | + unsigned char str[2]; |
| 63 | + void *obj = NULL; |
| 64 | + size_t objsize = 0; |
| 65 | + char lookup = 0; |
| 66 | + |
| 67 | + /* check args */ |
| 68 | + if (argc != 4) { |
| 69 | + help(); |
| 70 | + return (1); |
| 71 | + } |
| 72 | + |
| 73 | + /* get cmdline args */ |
| 74 | + sscanf(argv[1], "%hu", &ALLOC_TYPE); |
| 75 | + sscanf(argv[2], "%hu", &USE_ALTERNATIVE); |
| 76 | + |
| 77 | + /* read key */ |
| 78 | + kfile = fopen(argv[3], "r"); |
| 79 | + if (!kfile) { |
| 80 | + printf("[Error] Unable to open key file!\n"); |
| 81 | + return (1); |
| 82 | + } |
| 83 | + if (fread(&str, 1, 2, kfile) != 2) { |
| 84 | + printf("[Error] Unable to read key file!\n"); |
| 85 | + return (1); |
| 86 | + } |
| 87 | + key = (str[0] % 32 + 9) % 25; |
| 88 | + fclose(kfile); |
| 89 | + |
| 90 | + /* ini rnd */ |
| 91 | + srand(getpid() + time(NULL)); |
| 92 | + |
| 93 | + /* allocate object */ |
| 94 | + objsize = LARGE_INIT; |
| 95 | + obj = dmt_allocate(objsize, ALLOC_TYPE, USE_ALTERNATIVE); |
| 96 | + if (INVALIDPTR(obj)) { |
| 97 | + printf("[Error] Unable to allocate object!\n"); |
| 98 | + return (1); |
| 99 | + } |
| 100 | + |
| 101 | + /* debug info */ |
| 102 | +#ifdef DEBUG |
| 103 | + printf("Max rand() value: %u\n", RAND_MAX); |
| 104 | + printf("Allocate type: %s\n", (ALLOC_TYPE ? "mmap" : "malloc")); |
| 105 | + printf("Use alternative: %s\n", (USE_ALTERNATIVE ? "yes" : "no")); |
| 106 | + printf("Key nibble: %x\n", key); |
| 107 | + printf("Object size: %zu\n", objsize); |
| 108 | + printf("Object address: %0*"PRIxPTR"\n", (int)(sizeof(void*) * 2), (uintptr_t)obj); |
| 109 | +#endif |
| 110 | + |
| 111 | + /* key-dependent data access */ |
| 112 | + /* triggers H_pos(b), H_addr */ |
| 113 | + /* expected leak stats: */ |
| 114 | + /* difference: 1 */ |
| 115 | + /* data leak generic: 1 */ |
| 116 | + /* data leak specific: 1 (key byte nibble high) */ |
| 117 | + lookup = ((uint8_t*)obj)[key]; |
| 118 | + fprintf(stdin, "%d\n", lookup); |
| 119 | + |
| 120 | + /* key-independent data access */ |
| 121 | + /* expected leak stats: */ |
| 122 | + /* difference = 1 */ |
| 123 | + /* data leak generic = 0 */ |
| 124 | + /* data leak specific = 0 */ |
| 125 | + key = rand() % 16; |
| 126 | + lookup = ((uint8_t*)obj)[key]; |
| 127 | + fprintf(stdin, "%d\n", lookup); |
| 128 | + |
| 129 | + /* deallocate */ |
| 130 | + err = dmt_deallocate(obj, objsize, ALLOC_TYPE); |
| 131 | + |
| 132 | + /* done */ |
| 133 | + return (err); |
| 134 | +} |
| 135 | + |
| 136 | +/***********************************************************************/ |
| 137 | + |
| 138 | +/*** |
| 139 | + * Print help text. |
| 140 | + */ |
| 141 | +void help() |
| 142 | +{ |
| 143 | + printf("Usage:\n"); |
| 144 | + printf(" dmt-access <type> <alt> <key>\n\n"); |
| 145 | + printf(" <type> ....... 0: malloc & co, else: mmap & co\n"); |
| 146 | + printf(" <alt> ........ 0: standard (malloc,mmap private), else: alternative (calloc,mmap shared)\n"); |
| 147 | + printf(" <key file> ... file containing secret key byte as hex string\n"); |
| 148 | +} |
| 149 | + |
0 commit comments