file: Reimplement a1bcf3c0 without use-after-free bug

This commit is contained in:
mintsuki 2022-10-14 00:59:16 +02:00
parent 502f77b30f
commit 01ee09373c
4 changed files with 5 additions and 4 deletions

View File

@ -553,7 +553,6 @@ static void ext2_close(struct file_handle *file) {
pmm_free(f->alloc_map, f->inode.i_blocks_count * sizeof(uint32_t)); pmm_free(f->alloc_map, f->inode.i_blocks_count * sizeof(uint32_t));
} }
pmm_free(f, sizeof(struct ext2_file_handle)); pmm_free(f, sizeof(struct ext2_file_handle));
pmm_free(file, sizeof(struct file_handle));
} }
static void ext2_read(struct file_handle *file, void *buf, uint64_t loc, uint64_t count) { static void ext2_read(struct file_handle *file, void *buf, uint64_t loc, uint64_t count) {

View File

@ -525,5 +525,4 @@ static void fat32_close(struct file_handle *file) {
struct fat32_file_handle *f = file->fd; struct fat32_file_handle *f = file->fd;
pmm_free(f->cluster_chain, f->chain_len * sizeof(uint32_t)); pmm_free(f->cluster_chain, f->chain_len * sizeof(uint32_t));
pmm_free(f, sizeof(struct fat32_file_handle)); pmm_free(f, sizeof(struct fat32_file_handle));
pmm_free(file, sizeof(struct file_handle));
} }

View File

@ -81,10 +81,10 @@ void fclose(struct file_handle *fd) {
if (fd->readall == false) { if (fd->readall == false) {
pmm_free(fd->fd, fd->size); pmm_free(fd->fd, fd->size);
} }
pmm_free(fd, sizeof(struct file_handle));
} else { } else {
fd->close(fd); fd->close(fd);
} }
pmm_free(fd, sizeof(struct file_handle));
} }
void fread(struct file_handle *fd, void *buf, uint64_t loc, uint64_t count) { void fread(struct file_handle *fd, void *buf, uint64_t loc, uint64_t count) {
@ -106,6 +106,10 @@ void *freadall(struct file_handle *fd, uint32_t type) {
} else { } else {
void *ret = ext_mem_alloc_type(fd->size, type); void *ret = ext_mem_alloc_type(fd->size, type);
fd->read(fd, ret, 0, fd->size); fd->read(fd, ret, 0, fd->size);
fd->close(fd);
fd->fd = ret;
fd->readall = true;
fd->is_memfile = true;
return ret; return ret;
} }
} }

View File

@ -294,5 +294,4 @@ static void iso9660_read(struct file_handle *file, void *buf, uint64_t loc, uint
static void iso9660_close(struct file_handle *file) { static void iso9660_close(struct file_handle *file) {
pmm_free(file->fd, sizeof(struct iso9660_file_handle)); pmm_free(file->fd, sizeof(struct iso9660_file_handle));
pmm_free(file, sizeof(struct file_handle));
} }