Fixed unsafe/potentially buggy code

This commit is contained in:
notsomeidiot123 2026-03-14 11:29:44 -04:00
parent 60f6508519
commit cc61eec0cf
7 changed files with 43 additions and 294 deletions

View File

@ -77,6 +77,10 @@ void pci_make_file(uint32_t class, uint8_t bus, uint8_t slot, uint8_t func){
strcpy(str, cpy);
strcpy(num, cpy + strlen(cpy));
file = fcreate(cpy, VFILE_DEVICE, pci_write_file, pci_read_file);
if(tries >= 254){
mlog(MODULE_NAME, "Failed to make PCI file for device!\n", MLOG_PRINT);
return;
}
}
file->id = (uint32_t)slot | ((uint32_t)func << 8) | ((uint32_t)bus << 16);
}

View File

@ -23,12 +23,15 @@ void sysinit(){
pci_init();
// modules_init(boot_info, 0);
read_initrd(boot_info->initrd);
vfile_t *initrc = fget_file("/boot/initrc.conf");
modules_init();
vfile_t *initrc = fopen("/boot/initrc.conf");
if(initrc){
mlog("KERNEL", "Found initrc at: %x\n", MLOG_PRINT, initrc->access.data.ptr);
}
modules_init();
initrc_read(initrc);
}
else{
mlog("KERNEL", "ERROR: Initrc could not be located!\n", MLOG_PRINT);
}
// vfile_t *disk_dir = fget_file("/dev/disk");
// if(disk_dir == 0){
// mlog("KERNEL", "Error: /dev/disk does not exist\n", MLOG_PRINT);

View File

@ -18,6 +18,10 @@ int is_num(char c){
void initrc_read(vfile_t *file){
mlog("KERNEL", "Reading initrc:\n", MLOG_PRINT);
char *ptr = file->access.data.ptr;
if(!file || !ptr){
mlog("KERNEL", "Failed to read initrc!\n", MLOG_PRINT);
return;
}
uint32_t size = file->access.data.size_pgs * 4096;
char statement[512];
uint32_t i = 0;
@ -52,13 +56,13 @@ void initrc_read(vfile_t *file){
i++;
char module_name[512];
int j = 0;
for(; j < 512 && ptr[i + j] && ptr[i+j] != '\n' && ptr[i + j] != ' '; j++){
for(; j < 512 && ptr[i + j] && ptr[i+j] != '\n' && ptr[i + j] != ' ' && i+j < size; j++){
module_name[j] = ptr[i + j];
}
i+=j;
module_name[j] = 0;
// printf("%s\n", module_name);
vfile_t *module = fget_file(module_name);
vfile_t *module = fopen(module_name);
if(!module){
printf("Error: Could not find module in Initrc.conf: %s\n", module_name);
continue;
@ -71,12 +75,12 @@ void initrc_read(vfile_t *file){
uint32_t offset = 0;
i++;
int j = 0;
for(; j < 512 && ptr[i + j] && ptr[i+j] != '\n' && ptr[i+j] != ' '; j++){
for(; j < 512 && ptr[i + j] && ptr[i+j] != '\n' && ptr[i+j] != ' ' && i+j < size; j++){
mount_src_name[j] = ptr[i + j];
}
i+=j;
mount_src_name[j] = 0;
for(j = 1; ptr[i + j] && ptr[i + j] != '\n' && ptr[i + j] != ' '; j++){
for(j = 1; ptr[i + j] && ptr[i + j] != '\n' && ptr[i + j] != ' ' && i+j < size; j++){
mount_dest[j-1] = ptr[i + j];
}
@ -115,7 +119,11 @@ void initrc_read(vfile_t *file){
// printf("TEST: %s, %s, %d\n", mount_src_name, mount_dest, ptr[i]);
vfile_t *to_mount = fget_file(mount_src_name);
vfile_t *to_mount = fopen(mount_src_name);
if(!to_mount){
mlog("KERNEL", "Could not locate file: %s. Aborting mount.\n", MLOG_PRINT, mount_src_name);
continue;
}
if(!dispatch_message(MESSAGE_MOUNT_FS, to_mount, mount_dest, offset)){
mlog("INITRC", "Error: Could not mount device %s at %s", MLOG_PRINT, mount_src_name, mount_dest);
asm("int $13");

View File

@ -88,7 +88,7 @@ uint32_t module_api(uint32_t func, ...){
break;
case MODULE_API_OPEN:
name = va_arg(vars, char *);
return_value = (uint32_t)fget_file(name);
return_value = (uint32_t)fopen(name);
break;
case MODULE_API_MAP:
return_value = 0;

View File

@ -7,90 +7,6 @@ vfile_t root_dir = {"/", VFILE_DIRECTORY};
// !TODO: Modify code to become thread-safe
struct mount_handler{
int (*callback)(vfile_t *device, MOUNT_OPERATION op, ...);
uint32_t key;
}mount_handlers[32];
int vfs_link_exists(vfile_t *file){
if(get_paddr(file) == 0 || file->type >= VFILE_MOUNT){
return 0;
}
return 1;
}
void vfs_write_part(vfile_t *file, void *data, uint32_t offset, uint32_t count){
if(!vfs_link_exists(file->parent) || count > file->size){
return;
}
fread(file->parent, data, offset + file->id, count);
}
void vfs_read_part(vfile_t *file, void *data, uint32_t offset, uint32_t count){
if(!vfs_link_exists(file->parent) || count > file->size){
return;
}
fwrite(file->parent, data, offset + file->id, count);
}
void vfs_detect_partitions(vfile_t *file){
char *buffer = kmalloc(1);
fread(file, buffer, 0, 512);
mbr_t *mbr = buffer;
if(mbr->magic != MBR_MAGIC){
mlog(MODULE_NAME, "No MBR!\n", MLOG_PRINT);
mlog(MODULE_NAME, "Magic: %x\n", mbr->magic, MLOG_PRINT);
return;
}
char cat[2] = {'a', 0};
char nfname[512] = {0};
strcpy(file->name, nfname);
strcat(cat, nfname);
char finalfname[512] = {0};
strcpy("/dev/", finalfname);
strcat(nfname, finalfname);
for(int i = 0; i < 4; i++){
partition_t part = mbr->partitions[i];
if(part.type == 0xee){
mlog(MODULE_NAME, "Found gpt\n", MLOG_PRINT);
return;
}
if((part.attributes != 0 && part.attributes != 0x80) ){
//check disk for filesystem
mlog(MODULE_NAME, "No partitions in %s\n", MLOG_PRINT, file->name);
// fcreate() new file representing partition
vfile_t *nfile = fcreate(finalfname, VFILE_DEVICE, vfs_write_part, vfs_read_part);
nfile->id = 0;
nfile->size = file->size;
nfile->parent = file;
return;
}
finalfname[strlen(finalfname)-1]++;
}
mlog(MODULE_NAME, "Magic: %x\n", MLOG_PRINT, mbr->magic);
return;
}
// void vfs_add_mount_handler(int (*mount_handler)(vfile_t *device, MOUNT_OPERATION op, ...), uint32_t key){
// if(mount_handler == 0){
// return;
// }
// for(uint32_t i = 0; i < 32; i++){
// if(mount_handlers[i].callback == 0){
// mount_handlers[i].callback = mount_handler;
// mount_handlers[i].key = key;
// }
// }
// }
// void vfs_del_mount_handler(uint32_t key){
// for(uint32_t i = 0; i < 32; i++){
// if(mount_handlers[i].key == key){
// mount_handlers[i].callback = 0;
// mount_handlers[i].key = 0;
// }
// }
// }
void vfs_init(){
mlog(MODULE_NAME, "Initializing VFS\n", MLOG_PRINT);
root_dir.access.data.ptr = kmalloc(1);
@ -98,211 +14,31 @@ void vfs_init(){
}
void add_file(vfile_t *vfile, vfile_t *current_dir){
vfile_t **dir_data = current_dir->access.data.ptr;
int i = 0;
while(dir_data[i] && (i * sizeof(vfile_t *))/4096 < current_dir->access.data.size_pgs){
i++;
}
dir_data[i] = vfile;
dir_data[i + 1] = 0;
return;
}
/*
Long comment cause this one's a complicated one.
Create a file of the specified path in name
if the path leads to a mounted filesystem then
use the mounted filesystem's specified create() function to create the file.
else, create a pointer in the virtual path that points to a file entry for the new file.
If this new file is a Directory or Pointer, specify a pointer to be used as a buffer to store data for this file, as well as the size of the buffer, in pages.
If this new file is a Mounted filesystem, this pointer must be an array of functions as defined below:
int fwrite(vfile_t *file, void *data, uint32_t offset, uint32_t count);
//must write count bytes from data to file offset by offset bytes.
int fread(vfile_t *file, void *data, uint32_t offset, uint32_t count);
//must read count bytes from data to file offset by offset bytes.
int fopen(char *filename, vfile_t *file);
//must return a vfile_t struct written to the address specified in file, to represent a file named name (from the mount's filename)
(eg. /mount/root/test is passed as /root/test)
fcreate(char *filename, FS_FILE_FLAGS flags);
FS_FILE_FLAGS is in the same format as FAT's file attribute byte of dirents.
filenames are truncated, as with the previous example.
fdelete(vfile_t *file);
//the file specified in the file pointer must be deleted.
else, if the file is a virtual representation of a device, of of a file (any miscellaneous file object that does not fit the other types) then
specify a pointer to be used as the read function, as defined below:
void read(struct virtual_file *file, void *data, uint32_t offset, uint32_t count);
specify a pointer to be used as the write function, with the same function prototype as the function above
void write(struct virtual_file *file, void *data, uint32_t offset, uint32_t count);
*/
vfile_t *fcreate(char *name, VFILE_TYPE type, ...){
name += name[0]== '/';
char *last = strtok(name, '/');
char *tmp = last;
uint32_t filename_offset = 0;
vfile_t *current_dir = &root_dir;
vfile_t *tmpfile;
va_list args;
va_start(args, type);
while(tmp != 0){
// dir = tmp;
tmpfile = search_dir(tmp, *current_dir);
// printf("%s\n", name + filename_offset);
if(!tmpfile){
//create file in folder;
vfile_t *vfile = kmalloc(1);
vfile->type = type;
vfile->parent = current_dir;
uint32_t tmpnsize = strlen(tmp);
if(tmp[tmpnsize - 1] == '/'){
tmp[tmpnsize - 1] = 0;
}
strcpy(tmp, vfile->name);
switch(type){
case VFILE_POINTER:
case VFILE_DIRECTORY:
case VFILE_MOUNT:
case VFILE_SYMLINK:
case VFILE_FILE:
void *ptr = va_arg(args, void*);
vfile->access.data.ptr = ptr;
vfile->access.data.size_pgs = va_arg(args, uint32_t);
// *(uint32_t *)(ptr + (vfile->access.data.size_pgs * 4096)) = 0;
// what was the above line even for
add_file(vfile, current_dir);
break;
case VFILE_DEVICE:
void *write = va_arg(args, void *);
void *read = va_arg(args, void *);
vfile->access.funcs.write = write;
vfile->access.funcs.read = read;
// strcpy
add_file(vfile, current_dir);
break;
}
return vfile; //allow drivers to make last minute changes before it's sent to the user
}
if(tmpfile->type == VFILE_SYMLINK){
tmpfile = (vfile_t *)tmpfile->access.data.ptr;
}
switch(tmpfile->type){
case VFILE_DIRECTORY:
current_dir = tmpfile;
break;
case VFILE_MOUNT:
((mount_t*)(tmpfile->access.data.ptr))->create(name + filename_offset, type == VFILE_DIRECTORY ? FS_FILE_IS_DIR : 0);
return (void *)-1;
default:
// mlog(MODULE_NAME, "Error: Cannot create file with same name as another file! %d\n", MLOG_ERR, tmpfile->type);
return 0;
break;
}
filename_offset += strlen(tmp) + 1;
tmp = strtok(0, '/');
}
return 0;
}
//who needs a way to delete things?
//TODO: Write a function to delete files
void fdelete();
int fdelete(vfile_t *file_entry){
return 0;
}
// navya was here https://github.com/novabansal
int fwrite(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count){
//sorry navya, I deleted the comment and it's not coming back :(
switch(file_entry->type){
case VFILE_DIRECTORY:
case VFILE_POINTER:
memcpy(byte_array, file_entry->access.data.ptr + offset, count);
return 0; //always successful. If it's not, there's been a page fault. Yk, quantum sort style.
break;
case VFILE_MOUNT:
case VFILE_FILE:
mount_t *funcs = file_entry->access.data.ptr;
return funcs->write(file_entry, byte_array, offset, count);
case VFILE_PDIR:
case VFILE_DEVICE:
return file_entry->access.funcs.write(file_entry, byte_array, offset, count);
break;
case VFILE_SYMLINK:
return fwrite(file_entry->access.data.ptr, byte_array, offset, count);
}
return INT32_MIN;//how did we get here?
return 0;
}
int fread(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count){
switch(file_entry->type){
case VFILE_DIRECTORY:
case VFILE_POINTER:
memcpy(file_entry->access.data.ptr + offset, byte_array, count);
return 0; //always successful. If it's not, there's been a page fault. Yk, quantum sort style.
break;
case VFILE_MOUNT:
case VFILE_FILE:
mount_t *funcs = file_entry->access.data.ptr;
return funcs->read(file_entry, byte_array, offset, count);
case VFILE_PDIR:
case VFILE_DEVICE:
return file_entry->access.funcs.read(file_entry, byte_array, offset, count);
break;
case VFILE_SYMLINK:
return fread(file_entry->access.data.ptr, byte_array, offset, count);
}
return INT32_MIN; //Okay, alright, funny joke guys but we really shouldn't be able to get here
}
vfile_t *search_dir(char *name, vfile_t dir){
vfile_t **dir_data = (vfile_t **)dir.access.data.ptr;
uint32_t i = 0;
while(dir_data[i]){
if(!strcmp(dir_data[i]->name, name)){
return dir_data[i];
}
i++;
}
return 0;
}
vfile_t *fget_file(char *name){
if(name[0] == '/'){
if(strlen(name) == 1){
return &root_dir;
}
name++;
}
if(name[strlen(name) - 1] == '/'){
name[strlen(name) - 1] = 0;
}
// printf("%s\n", name);
char *dir = strtok(name, '/');
// printf("%s", name);
char *tmp = dir;
uint32_t filename_offset = 0;
vfile_t *current_dir = &root_dir;
vfile_t *tmpfile = 0;
while(tmp != 0){
dir = tmp;
tmpfile = search_dir(tmp, *current_dir);
if(!tmpfile){
// printf("not found\n");
vfile_t *lookup(char *name, vfile_t dir){
return 0;
}
vfile_t *fopen(char *name){
return 0;
}
if(tmpfile->type == VFILE_SYMLINK){
tmpfile = (vfile_t *)tmpfile->access.data.ptr;
}
switch(tmpfile->type){
case VFILE_DIRECTORY:
// printf("found directory in %s: %s\n", current_dir->name, tmpfile->name);
current_dir = tmpfile;
break;
case VFILE_MOUNT:
vfile_t *file = kmalloc(1);
((mount_t*)(tmpfile->access.data.ptr))->open(name + filename_offset, file);
return file;
default:
return tmpfile;
}
filename_offset += strlen(tmp) + 1;
tmp = strtok(0, '/');
}
return tmpfile;
}

View File

@ -77,11 +77,11 @@ typedef struct mbr{
void vfs_init();
vfile_t *fcreate(char *name, VFILE_TYPE type, ...);
void fdelete();
int fdelete(vfile_t* file_entry);
int fwrite(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count);
int fread(vfile_t *file_entry, void *byte_array, uint32_t offset, uint32_t count);
vfile_t *search_dir(char *name, vfile_t dir);
vfile_t *fget_file(char *name);
vfile_t *lookup(char *name, vfile_t dir);
vfile_t *fopen(char *name);
// void vfs_del_mount_handler(uint32_t key);
// void vfs_add_mount_handler(int (*mount_handler)(vfile_t *device, MOUNT_OPERATION op, ...), uint32_t key);
void vfs_detect_partitions(vfile_t *file);

View File

@ -34,8 +34,6 @@ uint32_t fat32_mount(vfile_t *dev_file, char *destination, uint32_t offset){
}
puts(api, MODULE_NAME, "Valid BPB found!\n");
fat32_make_mount();
return 1;
}