vestigia

Detect where execution depends on secrets.
git clone git://git.ppad.tech/vestigia.git
Log | Files | Refs | README | LICENSE

probe.c (1108B)


      1 /* A secret that reaches a branch, a load address, and a store
      2  * address, and a public value that reaches none: memcheck should
      3  * report the first three with a client-request origin. */
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include <valgrind/memcheck.h>
      7 
      8 static unsigned char table[256];
      9 
     10 __attribute__((noinline)) int branchy(unsigned char s) {
     11   if (s & 1) return 7;
     12   return 3;
     13 }
     14 
     15 __attribute__((noinline)) int lookup(unsigned char s) {
     16   return table[s];
     17 }
     18 
     19 __attribute__((noinline)) unsigned char ct(unsigned char s,
     20                                            unsigned char p) {
     21   unsigned char m = (unsigned char)(-(p & 1));
     22   return (unsigned char)((s & m) | (s ^ m));
     23 }
     24 
     25 int main(void) {
     26   unsigned char secret[4] = { 1, 2, 3, 4 };
     27   unsigned char out[4];
     28   memset(table, 5, sizeof table);
     29   VALGRIND_MAKE_MEM_UNDEFINED(secret, sizeof secret);
     30   out[0] = (unsigned char)branchy(secret[0]);
     31   out[1] = (unsigned char)lookup(secret[1]);
     32   out[2] = ct(secret[2], 1);
     33   table[secret[3]] = 9;
     34   VALGRIND_MAKE_MEM_DEFINED(out, sizeof out);
     35   printf("%d %d %d\n", out[0], out[1], out[2]);
     36   return 0;
     37 }