Richard Braun, le mar. 06 nov. 2018 11:52:45 +0100, a ecrit:
> On Mon, Nov 05, 2018 at 11:02:47PM +0100, Samuel Thibault wrote:
> > proxy-defpager is typically set on /servers/default-pager, but its
> > permissions are by default 644, which makes it unusable by normal users,
> > it'd need to be 755 (see the x check in the defpager source).
> > 
> > Apart from allowing users to eat memory, which they currently already
> > can do anyway, is there any downside to making this 755 so people can
> > mount their own tmpfs?
> 
> I personally wondered why it wasn't the case from the start.
> 
> That being said, I'll use this as an opportunity to restate a core
> problem of Mach memory management, as I couldn't find it on the wiki.
> This problem may or may not be even more triggered by using unprivileged
> tmpfs instances.

I actually encountered such an issue with php7.3's shm testing, which
triggers an ext2fs crash due to a tmpfs issue. I'll probably add the
attached patch to the debian package for now, but it seems that the
server side of RPCs needs to be more careful about receiving data when
it's passed out of line.

Samuel
Yes, the pointer provided by the caller, coming from the RPC buffer, may not
actually be safe to dereference. Try this with /run/shm as tmpfs with the crash server configured to dump cores:

#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define name "/run/shm/test.txt"
int main(void) {
	int fd = open(name, O_RDWR|O_CREAT, 0777);
	if (ftruncate(fd, 4096))
		perror("fruncate");
	char *c = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	if (c == MAP_FAILED)
		perror("mmap");
	if (close(fd))
		perror("close");
	if (unlink(name))
		perror("unlink");
	memset(c, 0, 4096);
	if (munmap(c, 4096))
		perror("munmap");

	return 0;
}

It will make *ext2fs* crash, because

- removing a file from tmpfs make its memory object go away, thus making *c
  unwritable (it's not the bug at stake, the program here is meant to crash)
- the crash server uses vm_read to read the process memory to write the
  core. GNU Mach achieves it by playing with virtual memory.
- the crash server uses vm_write to write this to the FS. GNU Mach passes the
  RPC data out of line by playing with virtual memory.
- ext2fs eventually tries to copy from the RPC data, assumed to be safe, to the
  memory object, here backed by the pager. But the data is actually not safe.


That probably needs to be fixed at the mig layer, to make sure incoming
out-of-line data is accessible before handing it to the routine?

Index: hurd-debian/libpager/pager-memcpy.c
===================================================================
--- hurd-debian.orig/libpager/pager-memcpy.c
+++ hurd-debian/libpager/pager-memcpy.c
@@ -124,11 +124,14 @@ pager_memcpy (struct pager *pager, memor
 	      __sync_synchronize();
 
 	      if (prot == VM_PROT_READ)
-		memcpy (other, (const void *) window + pageoff, copy_count);
+		err = hurd_safe_copyout (other, (const void *) window + pageoff, copy_count);
 	      else
-		memcpy ((void *) window + pageoff, other, copy_count);
+		err = hurd_safe_copyin ((void *) window + pageoff, other, copy_count);
 	      vm_deallocate (mach_task_self (), window, window_size);
 
+	      if (err)
+		return err;
+	      
 	      offset += copy_count;
 	      other += copy_count;
 	      to_copy -= copy_count;

Reply via email to