Skip to content

Commit

Permalink
Use the actual protection for file mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodore Dubois committed Jun 14, 2018
1 parent dc440ac commit 70d019e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
11 changes: 9 additions & 2 deletions emu/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,15 @@ int pt_set_flags(struct mem *mem, page_t start, pages_t pages, int flags) {
for (page_t page = start; page < start + pages; page++)
if (mem->pt[page].data == NULL)
return _ENOMEM;
for (page_t page = start; page < start + pages; page++)
mem->pt[page].flags = flags;
for (page_t page = start; page < start + pages; page++) {
struct pt_entry *entry = &mem->pt[page];
entry->flags = flags;
void *data = (char *) entry->data->data + entry->offset;
int prot = PROT_READ;
if (flags & P_WRITE) prot |= PROT_WRITE;
if (mprotect(data, PAGE_SIZE, prot) < 0)
return errno_map();
}
mem_changed(mem);
return 0;
}
Expand Down
6 changes: 5 additions & 1 deletion fs/real.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,17 @@ off_t realfs_lseek(struct fd *fd, off_t offset, int whence) {
int realfs_mmap(struct fd *fd, struct mem *mem, page_t start, pages_t pages, off_t offset, int prot, int flags) {
if (pages == 0)
return 0;

int mmap_flags = 0;
if (flags & MMAP_PRIVATE) mmap_flags |= MAP_PRIVATE;
if (flags & MMAP_SHARED) mmap_flags |= MAP_SHARED;
int mmap_prot = PROT_READ;
if (prot & P_WRITE) mmap_prot |= PROT_WRITE;

off_t real_offset = (offset / real_page_size) * real_page_size;
off_t correction = offset - real_offset;
char *memory = mmap(NULL, (pages * PAGE_SIZE) + correction,
PROT_READ | PROT_WRITE, mmap_flags, fd->real_fd, real_offset);
mmap_prot, mmap_flags, fd->real_fd, real_offset);
if (memory != MAP_FAILED)
memory += correction;
return pt_map(mem, start, pages, memory, prot);
Expand Down
2 changes: 1 addition & 1 deletion kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static int load_entry(struct prg_header ph, addr_t bias, struct fd *fd) {
addr_t memsize = ph.memsize;
addr_t filesize = ph.filesize;

int flags = 0;
int flags = P_READ;
if (ph.flags & PH_W) flags |= P_WRITE;

if ((err = fd->ops->mmap(fd, curmem, PAGE(addr),
Expand Down

0 comments on commit 70d019e

Please sign in to comment.