-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_matrix.c
More file actions
56 lines (44 loc) · 1.74 KB
/
Copy pathmain_matrix.c
File metadata and controls
56 lines (44 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include "security_layer.h"
// Low-level function: Calculates the sum of a single row
int calculate_row_sum(int *row, int cols) {
SHADOW_PROLOGUE(); // Backup the address on entry
int row_total = 0;
for (int j = 0; j < cols; j++) {
row_total += row[j];
}
SHADOW_EPILOGUE(); // Verify the address on exit
return row_total;
}
// High-level function: Processes the entire matrix
void sum_matrix(int rows, int cols, int matrix[rows][cols], int trigger_attack) {
SHADOW_PROLOGUE(); // Pushing high-level function address to shadow stack (index 0)
printf("Matrix summation process started....\n");
for (int i = 0; i < rows; i++) {
// Invoking the sub-function (Address will be added to index 1)
int r_sum = calculate_row_sum(matrix[i], cols);
printf("Row %d Sum: %d\n", i, r_sum);
// Attack Simulation: Corrupting the stack during the 2nd row
if (trigger_attack && i == 1) {
printf("\n[Attack] Intercepted during matrix processing! Corrupting the stack...\n");
unsigned long *ret_addr_ptr = (unsigned long *)__builtin_frame_address(0) + 1;
*ret_addr_ptr = 0xBADBEEF;
}
}
SHADOW_EPILOGUE(); // At this point, the shadow stack pointer should return to index 0
}
int main() {
int my_matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("=== Nested Shadow Stack Test ===\n");
// Scenario 1: Normal Execution
printf("\n--- Test 1: Standard Operation ---\n");
sum_matrix(3, 3, my_matrix, 0);
// Scenario 2: Attack during nested loop execution
printf("\n--- Test 2: Attack During Loop Execution ---\n");
sum_matrix(3, 3, my_matrix, 1);
return 0;
}