Skip to content

Commit

Permalink
Add a first implementation for malloc check
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigmanificient committed Jan 21, 2025
0 parents commit d44dcbb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ecsls.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[reports]
ignore = [ "C-G1" ]
severity_levels = true
merge = "multiplier"
62 changes: 62 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <dlfcn.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static const int MAGIC_BAD_STATUS = 77;

void resolve_symbol(void **funcp, const char *sym_name)
{
if (*funcp == NULL)
*funcp = dlsym(RTLD_NEXT, sym_name);
if (*funcp == NULL)
fprintf(stderr, "couldn't find symbol: [%s].", sym_name);
}

static
void *dispath_fork(pid_t pid, bool *in_fork, size_t request_size)
{
static void *(*real)(size_t size) = NULL;
int status;

if (real == NULL)
resolve_symbol((void **)&real, "malloc");
if (pid == 0) {
fprintf(stderr, "returning NULL in child...\n");
*in_fork = true;
return NULL;
}
wait(&status);
status %= 255;
fprintf(stderr, "child existed with status: %d\n", status);
if (status == MAGIC_BAD_STATUS) {
fprintf(stderr, "WAS NOT PROPERLY CHEKCED\n");
exit(EXIT_FAILURE);
}
fprintf(stderr, "returning real malloc...\n");
return real(request_size);
}

void *malloc(size_t size)
{
static bool in_fork = false;
void *p;
pid_t pid;

if (in_fork) {
fprintf(stderr, "\033[48;5;1m\033[38;5;232m"
"CALL AFTER A BAD PREVIOUS MALLOC\033[0m\n");
exit(MAGIC_BAD_STATUS);
}
fprintf(stderr, "\033[48;5;23m\033[38;5;232m"
"malloc(size=%zu)\033[0m\n", size);
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
return dispath_fork(pid, &in_fork, size);
}

0 comments on commit d44dcbb

Please sign in to comment.