Make a deep copy of envp before starting a new process.

This commit is contained in:
Andrius Štikonas 2024-01-12 22:06:03 +00:00
parent c10e10b1fb
commit dc1e5eaf0e
Signed by: andrius
GPG Key ID: 0C0331D5228A3B62
2 changed files with 25 additions and 5 deletions

View File

@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
ESP_SIZE_MIB = 50
ESP_SIZE_MIB = 350
QEMU = qemu-system-x86_64
QEMU_KVM = -enable-kvm

View File

@ -38,6 +38,7 @@ struct process {
int forked;
int fd_map[__FILEDES_MAX];
int argc;
int envc;
char **argv;
char **envp;
};
@ -94,8 +95,9 @@ void* entry_point(char* raw_elf)
void jump(void* start_address, int argc, char** argv, char** envp)
{
/* Make a copy of argv */
current_process->argc = argc;
current_process->argv = calloc(argc + 1, sizeof(char*));
current_process->argv = calloc(argc + 1, sizeof(char *));
int i;
size_t length;
for (i = 0; i < argc; i += 1) {
@ -105,10 +107,24 @@ void jump(void* start_address, int argc, char** argv, char** envp)
}
current_process->argv[argc] = 0;
/* Make a copy of envp */
current_process->envc = 0;
char** e = envp;
for (; *e != 0; e += sizeof(char *)) {
current_process->envc += 1;
}
current_process->envp = calloc(current_process->envc + 1, sizeof(char *));
for (i = 0; i < current_process->envc; i += 1) {
length = strlen(envp[i]) + 1;
current_process->envp[i] = malloc(length);
memcpy(current_process->envp[i], envp[i], length);
}
current_process->envp[current_process->envc] = 0;
/* Prepare stack of the new executable */
current_process->stack = get_stack();
asm("push !0");
for (; *envp != 0; envp += sizeof(char *)) {
*envp;
for (i = current_process->envc; i >= 0; i -= 1) {
current_process->envp[i];
asm("push_rax");
}
for (i = argc; i >= 0; i -= 1) {
@ -259,6 +275,10 @@ void sys_exit(unsigned value, void, void, void, void, void)
free(current_process->argv[i]);
}
free(current_process->argv);
for (i = 0; i < current_process->envc; i += 1) {
free(current_process->envp[i]);
}
free(current_process->envp);
if (current_process->parent == NULL) {
exit(value);