Fix vulnerabilities

This commit is contained in:
lily 2026-01-13 13:03:16 +11:00
parent 2c9e0af72b
commit 89620e8751
No known key found for this signature in database
GPG key ID: 6A91E98FC1591F03
2 changed files with 61 additions and 8 deletions

View file

@ -2,6 +2,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h> #include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "config.h" #include "config.h"
void check_init() { void check_init() {
@ -20,9 +24,54 @@ void check_init() {
} }
void run(int runlevel) { void run(int runlevel) {
char command[1024]; char runlevel_str[12];
snprintf(command, sizeof(command), "cd %s && PATH=\"$PATH:%s\" %s %d", INIT_DIRECTORY, INIT_DIRECTORY, SHELL, runlevel); snprintf(runlevel_str, sizeof(runlevel_str), "%d", runlevel);
system(command);
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
}
if (pid == 0) {
// Child process
if (chdir(INIT_DIRECTORY) != 0) {
perror("chdir");
_exit(1);
}
const char *old_path = getenv("PATH");
char *new_path = NULL;
if (old_path) {
size_t len = strlen(old_path) + strlen(INIT_DIRECTORY) + 2;
new_path = (char *)malloc(len);
if (new_path) {
snprintf(new_path, len, "%s:%s", old_path, INIT_DIRECTORY);
}
} else {
size_t len = strlen(INIT_DIRECTORY) + 1;
new_path = (char *)malloc(len);
if (new_path) {
snprintf(new_path, len, "%s", INIT_DIRECTORY);
}
}
if (new_path) {
setenv("PATH", new_path, 1);
free(new_path);
}
execl(SHELL, SHELL, runlevel_str, (char *)NULL);
perror("execl");
_exit(1);
} else {
// Parent process
int status;
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid");
exit(1);
}
}
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {

View file

@ -45,15 +45,16 @@ static int has_char_before_dot(const char *filename, char c) {
static void get_generic_name(const char *filename, char *dest) { static void get_generic_name(const char *filename, char *dest) {
const char *dot = strchr(filename, '.'); const char *dot = strchr(filename, '.');
if (dot) { if (dot) {
strcpy(dest, dot + 1); strncpy(dest, dot + 1, MAX_NAME - 1);
} else { } else {
strcpy(dest, filename); strncpy(dest, filename, MAX_NAME - 1);
} }
dest[MAX_NAME - 1] = '\0';
} }
static int find_script_idx(const char *generic_name) { static int find_script_idx(const char *generic_name) {
for (int i = 0; i < num_scripts; i++) { for (int i = 0; i < num_scripts; i++) {
if (strcmp(scripts[i].generic_name, generic_name) == 0) { if (strncmp(scripts[i].generic_name, generic_name, MAX_NAME) == 0) {
return i; return i;
} }
} }
@ -82,7 +83,8 @@ void solve_dependencies(char runlevel) {
if (num_scripts >= MAX_SCRIPTS) break; if (num_scripts >= MAX_SCRIPTS) break;
Script *s = &scripts[num_scripts]; Script *s = &scripts[num_scripts];
strncpy(s->full_name, dir->d_name, MAX_NAME); strncpy(s->full_name, dir->d_name, MAX_NAME - 1);
s->full_name[MAX_NAME - 1] = '\0';
get_generic_name(dir->d_name, s->generic_name); get_generic_name(dir->d_name, s->generic_name);
s->num_dependencies = 0; s->num_dependencies = 0;
s->in_degree = 0; s->in_degree = 0;
@ -100,7 +102,9 @@ void solve_dependencies(char runlevel) {
} }
char *token = strtok(start, " \t\n\r"); char *token = strtok(start, " \t\n\r");
while (token && s->num_dependencies < MAX_DEPS) { while (token && s->num_dependencies < MAX_DEPS) {
strncpy(s->dependencies[s->num_dependencies++], token, MAX_NAME); strncpy(s->dependencies[s->num_dependencies], token, MAX_NAME - 1);
s->dependencies[s->num_dependencies][MAX_NAME - 1] = '\0';
s->num_dependencies++;
token = strtok(NULL, " \t\n\r"); token = strtok(NULL, " \t\n\r");
} }
} }