Permalink
Browse files

cat-like using open() and mmap() works!

That means that the mapped file is correctly projected in memory.
  • Loading branch information...
1 parent c128b11 commit 69150981fd788cb33b8c42a10b9901536c3153cf @nbareil committed Mar 2, 2010
Showing with 61 additions and 14 deletions.
  1. +13 −1 helper.c
  2. +1 −1 mm.c
  3. +23 −0 t/open-mmap.c
  4. +7 −7 trusted.py
  5. +13 −1 vfs.py
  6. +4 −4 vm.py
View
@@ -23,7 +23,19 @@ ssize_t peek_asciiz_request(const int fd, const char *start) {
}
ssize_t poke_memory_request(const int fd, const struct memory_op_msg * req) {
- return fxread(fd, req->addr, req->len);
+ ssize_t ret;
+ size_t bytesread;
+ char *ptr = req->addr;
+
+ while (bytesread < req->len) {
+ ret = read(fd, ptr, req->len - bytesread);
+ if (ret < 0) {
+ PERROR("poke_memory/read failed:");
+ }
+ ptr += ret;
+ bytesread += ret;
+ }
+ return bytesread;
}
ssize_t peek_memory_request(const int fd, const struct memory_op_msg * req) {
View
2 mm.c
@@ -13,7 +13,7 @@ void init_memory(size_t mem)
mm = create_mspace(mem, 0);
big_memory_pool = mmap(NULL,
- 0xf000,
+ 0x10000000,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
-1, 0);
View
@@ -0,0 +1,23 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ ssize_t ret;
+ size_t j;
+ struct stat st;
+ char *addr;
+ int fd = open(argc > 1 ? argv[1] : "/etc/motd", 0);
+
+ fstat(fd, &st);
+ addr = mmap(0, st.st_size, 0x1, 0x2, fd, 0);
+
+ j=0;
+ while (j < st.st_size) {
+ j += write(1, addr+j, st.st_size -j);
+ }
+ close(fd);
+}
+
View
@@ -98,14 +98,16 @@ def poke_memory(self, addr, buf):
length = len(buf)
tubelog.debug('<<< poke_memory(%#x, "...") len=%d' % (addr, length))
self.write(struct.pack('III', POKE_MEMORY, addr, length))
- self.write(buf)
+ written=0
+ while written < length:
+ written += self.write(buf[written:])
def exit(self, status):
tubelog.debug('<<< native_exit(%x)' % status)
self.write(struct.pack('II', NATIVE_EXIT, status))
def write(self, buf, forceflush=True):
- ret = self.fd.write(buf)
+ ret = os.write(self.fd.fileno(), buf)
if forceflush:
self.fd.flush()
return ret
@@ -166,7 +168,7 @@ def fstat64(self, fd, addr):
self.op_retval(ret, errno)
def mmap(self, addr, length, prot, flags, fd, offset):
- fd = ~((fd + 1) & 0xffffffff)
+ #fd = ~((fd + 1) & 0xffffffff)
sandboxlog.info('+++ mmap(%#x, %#x, %#x, %#x, %#d, %d)' %
(addr, length, prot, flags, fd, offset))
@@ -179,10 +181,8 @@ def mmap(self, addr, length, prot, flags, fd, offset):
if not self.vm.mm:
self.vm.set_pool_addr(self.get_memory_pool())
addr = self.vm.new_mapping(addr, length, prot, flags)
- if fd >= 0:
- os.lseek(fd, offset, os.SEEK_SET)
- self.poke_memory(addr, os.read(fd, length))
- self.op_retval(int(addr & 0xffffffff), 0)
+ ret,errno = self.vfs.mmap(addr, length, prot, flags, fd, offset, self)
+ self.op_retval(ret, errno)
def munmap(self, addr, length):
sandboxlog.info('+++ munmap(%#x, %d)' % (addr, length))
View
14 vfs.py
@@ -69,8 +69,20 @@ def fstat(self, remote):
errno = e.errno
return (ret, st, errno)
+ def lseek(self, remote, offset, base):
+ local = self.bridge.get(remote)
+ os.lseek(local, offset, base)
+
+ def mmap(self, addr, length, prot, flags, fd, offset, process):
+ local = self.bridge.get(fd)
+ if local > 0:
+ self.lseek(fd, offset, os.SEEK_SET) # Pay attention! It's fd, not remote
+ buf = os.read(local, length)
+ process.poke_memory(addr, buf) # XXX I know it sucks!
+ return (addr, 0)
+
def close(self, fd):
- ret = (-1, 0)
+ ret = (0, 0)
if self.security.unregister_descriptor(fd):
pass
return ret
View
8 vm.py
@@ -60,13 +60,13 @@ def __repr__(self):
class MemoryManager(object):
def __init__(self, addr, size):
- if size % 4096 != 0:
- raise MemoryException('Size not page-aligned')
+ # if size % 4096 != 0:
+ # raise MemoryException('Size not page-aligned')
self.pool = Chunk(addr, size, 'F')
def allocate(self, chunksize):
- if chunksize % 4096 != 0:
- raise MemoryException('Size not page-aligned')
+ # if chunksize % 4096 != 0:
+ # raise MemoryException('Size not page-aligned')
chunk = self.pool.find_freechunk(chunksize)
return chunk

0 comments on commit 6915098

Please sign in to comment.