Skip to contentSkip to conversation

dump read-only anonymous memory in core files

PATCH In review Roland McGrath 3 participants 4 messages Oct 7, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dump read-only anonymous memory in core files

Currently core dumps omit all pages that are read-only at the time of the dump. I think that anonymous pages should be included even if read-only. This patch makes that change. Take for example this program:

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

	int main ()
	{
	  const size_t pagesz = sysconf (_SC_PAGE_SIZE);
	  void *page = mmap (0, pagesz, PROT_READ|PROT_WRITE,
			     MAP_ANON|MAP_PRIVATE, -1, 0);
	  memset (page, 0x17, pagesz);
	  mprotect (page, pagesz, PROT_READ);
	  printf ("pid %d address %p size %x\n", getpid (),
		  page, (unsigned int) pagesz);
	  abort ();
	}

Without this change, the core dump will not include the `page' page--when examining the dump, there will be no way to know it contains 0x17. This is obviously a contrived and useless example. But such cases do exist in the real world, e.g. garbage collectors that temporarily mprotect some pages to read-only--in a core dump of such a process, knowing the contents of these pages could be important. With my change, that page appears in the dump.

Thanks,
Roland

Index: linux-2.6/fs/binfmt_elf.c
===================================================================
RCS file: /home/cvs/linux-2.5/fs/binfmt_elf.c,v
retrieving revision 1.57

fs/binfmt_elf.c 7 Oct 2003 06:35:06 -0000 renamed from fs/binfmt_elf.c 3 Oct 2003 15:35:40 -0000 1.57 +4 -3
diff -u -b -p -r1.57 binfmt_elf.c
@@ -960,12 +960,13 @@ static int maydump(struct vm_area_struct
 	/* Do not dump I/O mapped devices! -DaveM */
 	if (vma->vm_flags & VM_IO)
 		return 0;
-#if 1
+	/* Dump anything writable.  */
 	if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
 		return 1;
-	if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
+	/* Don't dump unwritable segments mapped from files.  */
+	if (vma->vm_file != NULL)
 		return 0;
-#endif
+	/* Dump everything else.  */
 	return 1;
 }
 
#1 lore raw

What about shared mappings (that test should be VM_MAYSHARE)?

My inclination would be to not dump them if they are backed by a file, even if the mmap is writable.

Also, the VM_WRITE test should possibly be VM_MAYWRITE, since the thing could have been mprotect()'ed. Alternatively, we could really add a "this mapping has been writable" flag (a kind of "sticky VM_WRITE" bit). It's only mprotect that can do this, anyway.

Ehh?

Which would make the logic be something like

	/* Do not dump I/O mapped devices! -DaveM */
	if (vma->vm_flags & VM_IO)
		return 0;

	/* file-backed mapping? */
	if (vma->vm_file) {
		if (vma->vm_flags & VM_MAYSHARE)
			return 0;
		if (!(vma->vm_flags & VM_MAYWRITE))
			return 0;
	}

	return 1;

Hmm?

Linus

#2 lore raw

--On Tuesday, October 07, 2003 02:06:34 -0700 Linus Torvalds

Quoted 4 lines · <[email protected]> wrote:

What about shared mappings (that test should be VM_MAYSHARE)?

My inclination would be to not dump them if they are backed by a file, even if the mmap is writable.

All shared areas are 'file backed' but some of them are only backed by shmfs. I think we should at least dump those areas since they can't be retrieved later. I agree it'd be reasonable to not dump areas backed by real files, but it'd require a simple way to distinguish them from shmfs-backed areas.

Dave McCracken

======================================================================
Dave McCracken IBM Linux Base Kernel Team 1-512-838-3059
[email protected] T/L 678-3059

#3 lore raw
In reply to Linus Torvalds
Quoted 5 lines

What about shared mappings (that test should be VM_MAYSHARE)?

My inclination would be to not dump them if they are backed by a file, even if the mmap is writable.

That sounds reasonable to me (perhaps modulo the shmfs issue). I intended my patch not to omit any vmas that were dumped before, so I didn't consider omitting any writable areas. (I don't really understand the difference between VM_MAYSHARE and VM_SHARED, so I might be assuming something wrong when saying you are right.)

Quoted 4 lines

Also, the VM_WRITE test should possibly be VM_MAYWRITE, since the thing could have been mprotect()'ed. Alternatively, we could really add a "this mapping has been writable" flag (a kind of "sticky VM_WRITE" bit). It's only mprotect that can do this, anyway.

Actually the ideal would be "this mapping has been written to" (or might have been, i.e. it at least was writable in a pte, if not actually dirty). Then if you mmap some pages PROT_READ|PROT_WRITE, and then mprotect them PROT_READ before ever touching them, you don't dump those pages just as you wouldn't dump always-read-only pages. (If you had that, you could apply it to anonymous mappings as well and omit all zero-fill pages.) Ideally this would dump PROT_NONE pages that have been written in the past as well, which is ruled out by the VM_READ test at the top of maydump. Despite the comment, it looks to me like dump_write's use of get_user_pages will make it work, so it could be a VM_MAYREAD check.

Thanks,
Roland