Skip to contentSkip to conversation

Non disruptive application core dump infrastructure

RFC PATCH In review Suzuki K. Poulose 8 participants 53 messages Dec 14, 2010 – Dec 16, 2010

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

cover Non disruptive application core dump infrastructure

Hi all,

This is series of patches implementing an infrastructure for capturing the core of an application without disrupting its process semantics.

The infrastructure makes use of the freezer subsystem in kernel to freeze the threads and then collect the information to generate the core.

The interface is provided by a /proc/pid/core file, reading which can give the ELF formatted core of the process with "pid". The interface supports "seek" operation on the fd, allowing the dumper to have control on the data that is being dumped. Also it allows the user to store the dump at any location.

The current implementation supports both native as well as the compat ELF tasks.

An open() call to the /proc/pid/core will try to freeze the threads in the process and the read() requests will dynamically generate the contents for the core file. The ELF header & Program Headers are stored in a kernel buffer to allow us to map the fpos to the required data section.

In case a thread is not frozen within a time interval, after issuing the freeze request, we fill the register state information with 0's to indicate we could not capture the data.

A close() would kick the threads out of the refrigerator().

The implementation reuses some of the existing ELF core generation code by exporting them. Some of the code common to both native and compat ELF class support has been moved to a common place, elfcore-common.c. Also some of the reusable functions, specific to the ELF class handling, has been made global, after renaming the compat version of the same.

We also added a new API -elf_core_copy_extra_phdrs() -for "reading" the arch specific program headers, versus the existing elf_core_write_extra_phdrs().

Patches 1 to 9 deals with re-arranging the ELF code to be reusable by the infrastructure.

Patches 10 to 21 implements the infrastructure.

TODO: Add support for collecting the arch specific notes, currently used only by Cell platform.

Please let me know your review comments / thoughts.

  arch/ia64/kernel/elfcore.c       |   34 ++
  arch/um/sys-i386/elfcore.c       |   32 ++
  fs/Makefile                      |    1
  fs/binfmt_elf.c                  |  162 +------------
  fs/compat_binfmt_elf.c           |    7
  fs/elfcore-common.c              |  138 +++++++++++
  fs/proc/Makefile                 |    2
  fs/proc/base.c                   |    1
  fs/proc/gencore-compat-elf.c     |   61 ++++
  fs/proc/gencore-elf.c            |  479+++++++++++++++++++++++++++++++++++++++ 
  fs/proc/gencore.c                |  297++++++++++++++++++++++++ 
  fs/proc/gencore.h                |   71 +++++
  fs/proc/internal.h               |    1
  include/linux/elfcore-internal.h |   73 +++++
  include/linux/elfcore.h          |    3
  include/linux/freezer.h          |   12
  kernel/elfcore.c                 |    6
  kernel/power/process.c           |    9
  18 files changed, 1241 insertions(+), 148 deletions(-)

Thanks

Suzuki

#1 lore raw

PATCH 1/21 Reuse freezable() predicate

Export the freezable() predicate.

---
 include/linux/freezer.h |   12 ++++++++++++
 kernel/power/process.c  |    9 ---------
 2 files changed, 12 insertions(+), 9 deletions(-)
include/linux/freezer.h +12 -0
Index: linux-2.6.36-rc7/include/linux/freezer.h
===================================================================
@@ -8,6 +8,18 @@
 
 #ifdef CONFIG_FREEZER
 /*
+ * Check if the task is freezeable ?
+ */
+static inline int freezeable(struct task_struct * p)
+{
+	if ((p == current) ||
+	    (p->flags & PF_NOFREEZE) ||
+	    (p->exit_state != 0))
+		return 0;
+	return 1;
+}
+
+/*
  * Check if a process has been frozen
  */
 static inline int frozen(struct task_struct *p)
kernel/power/process.c +0 -9
Index: linux-2.6.36-rc7/kernel/power/process.c
===================================================================
@@ -22,15 +22,6 @@
  */
 #define TIMEOUT	(20 * HZ)
 
-static inline int freezeable(struct task_struct * p)
-{
-	if ((p == current) ||
-	    (p->flags & PF_NOFREEZE) ||
-	    (p->exit_state != 0))
-		return 0;
-	return 1;
-}
-
 static int try_to_freeze_tasks(bool sig_only)
 {
 	struct task_struct *g, *p;
#2 lore raw

PATCH 2/21 Create elfcore-common.c for ELF class independent core generation helpers

In reply to Suzuki K. Poulose

Move the common code, shared by both native and compat ELF core generation code to a single instance.These functions could be re-used later for application core dump infrastructure.

---
 fs/Makefile                      |    1 
 fs/binfmt_elf.c                  |  129 ---------------------------------------
 fs/elfcore-common.c              |  126 ++++++++++++++++++++++++++++++++++++++
 include/linux/elfcore-internal.h |   35 ++++++++++
 4 files changed, 163 insertions(+), 128 deletions(-)
fs/binfmt_elf.c +1 -128
Index: linux-2.6.36-rc7/fs/binfmt_elf.c
===================================================================
@@ -46,6 +46,7 @@ static unsigned long elf_map(struct file
  * don't even try.
  */
 #ifdef CONFIG_ELF_CORE
+#include <linux/elfcore-internal.h>
 static int elf_core_dump(struct coredump_params *cprm);
 #else
 #define elf_core_dump	NULL
@@ -1087,98 +1088,6 @@ out:
  * Jeremy Fitzhardinge <[email protected]>
  */
 
-/*
- * Decide what to dump of a segment, part, all or none.
- */
-static unsigned long vma_dump_size(struct vm_area_struct *vma,
-				   unsigned long mm_flags)
-{
-#define FILTER(type)	(mm_flags & (1UL << MMF_DUMP_##type))
-
-	/* The vma can be set up to tell us the answer directly.  */
-	if (vma->vm_flags & VM_ALWAYSDUMP)
-		goto whole;
-
-	/* Hugetlb memory check */
-	if (vma->vm_flags & VM_HUGETLB) {
-		if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
-			goto whole;
-		if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
-			goto whole;
-	}
-
-	/* Do not dump I/O mapped devices or special mappings */
-	if (vma->vm_flags & (VM_IO | VM_RESERVED))
-		return 0;
-
-	/* By default, dump shared memory if mapped from an anonymous file. */
-	if (vma->vm_flags & VM_SHARED) {
-		if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
-		    FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
-			goto whole;
-		return 0;
-	}
-
-	/* Dump segments that have been written to.  */
-	if (vma->anon_vma && FILTER(ANON_PRIVATE))
-		goto whole;
-	if (vma->vm_file == NULL)
-		return 0;
-
-	if (FILTER(MAPPED_PRIVATE))
-		goto whole;
-
-	/*
-	 * If this looks like the beginning of a DSO or executable mapping,
-	 * check for an ELF header.  If we find one, dump the first page to
-	 * aid in determining what was mapped here.
-	 */
-	if (FILTER(ELF_HEADERS) &&
-	    vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
-		u32 __user *header = (u32 __user *) vma->vm_start;
-		u32 word;
-		mm_segment_t fs = get_fs();
-		/*
-		 * Doing it this way gets the constant folded by GCC.
-		 */
-		union {
-			u32 cmp;
-			char elfmag[SELFMAG];
-		} magic;
-		BUILD_BUG_ON(SELFMAG != sizeof word);
-		magic.elfmag[EI_MAG0] = ELFMAG0;
-		magic.elfmag[EI_MAG1] = ELFMAG1;
-		magic.elfmag[EI_MAG2] = ELFMAG2;
-		magic.elfmag[EI_MAG3] = ELFMAG3;
-		/*
-		 * Switch to the user "segment" for get_user(),
-		 * then put back what elf_core_dump() had in place.
-		 */
-		set_fs(USER_DS);
-		if (unlikely(get_user(word, header)))
-			word = 0;
-		set_fs(fs);
-		if (word == magic.cmp)
-			return PAGE_SIZE;
-	}
-
-#undef	FILTER
-
-	return 0;
-
-whole:
-	return vma->vm_end - vma->vm_start;
-}
-
-/* An ELF note in memory */
-struct memelfnote
-{
-	const char *name;
-	int type;
-	unsigned int datasz;
-	void *data;
-};
-
 static int notesize(struct memelfnote *en)
 {
 	int sz;
@@ -1256,16 +1165,6 @@ static void fill_elf_note_phdr(struct el
 	return;
 }
 
-static void fill_note(struct memelfnote *note, const char *name, int type, 
-		unsigned int sz, void *data)
-{
-	note->name = name;
-	note->type = type;
-	note->datasz = sz;
-	note->data = data;
-	return;
-}
-
 /*
  * fill up all the fields in prstatus from the given task struct, except
  * registers which need to be filled up separately.
@@ -1812,32 +1711,6 @@ static void free_note_info(struct elf_no
 
 #endif
 
-static struct vm_area_struct *first_vma(struct task_struct *tsk,
-					struct vm_area_struct *gate_vma)
-{
-	struct vm_area_struct *ret = tsk->mm->mmap;
-
-	if (ret)
-		return ret;
-	return gate_vma;
-}
-/*
- * Helper function for iterating across a vma list.  It ensures that the caller
- * will visit `gate_vma' prior to terminating the search.
- */
-static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
-					struct vm_area_struct *gate_vma)
-{
-	struct vm_area_struct *ret;
-
-	ret = this_vma->vm_next;
-	if (ret)
-		return ret;
-	if (this_vma == gate_vma)
-		return NULL;
-	return gate_vma;
-}
-
 static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
 			     elf_addr_t e_shoff, int segs)
 {
fs/elfcore-common.c new file +131 -0

131 lines not on this page · raw message

include/linux/elfcore-internal.h new file +44 -0

44 lines not on this page · raw message

fs/Makefile +1 -0
Index: linux-2.6.36-rc7/fs/Makefile
===================================================================
@@ -42,6 +42,7 @@ obj-y				+= binfmt_script.o
 
 obj-$(CONFIG_BINFMT_ELF)	+= binfmt_elf.o
 obj-$(CONFIG_COMPAT_BINFMT_ELF)	+= compat_binfmt_elf.o
+obj-$(CONFIG_ELF_CORE)		+= elfcore-common.o
 obj-$(CONFIG_BINFMT_ELF_FDPIC)	+= binfmt_elf_fdpic.o
 obj-$(CONFIG_BINFMT_SOM)	+= binfmt_som.o
 obj-$(CONFIG_BINFMT_FLAT)	+= binfmt_flat.o
#3 lore raw

PATCH 3/21 Make vma_dump_size() generic

In reply to Suzuki K. Poulose

vma_dump_size calculates the file size of a vma area in the core file. It assumes the vma belongs to the "current". Make it generic to work for any task. This will be reused by application core dump infrastructure.

---
 fs/binfmt_elf.c                  |    6 +++---
 fs/elfcore-common.c              |   21 ++++++++++++++-------
 include/linux/elfcore-internal.h |    4 ++--
 3 files changed, 19 insertions(+), 12 deletions(-)
fs/binfmt_elf.c +3 -3
Index: linux-2.6.36-rc7/fs/binfmt_elf.c
===================================================================
@@ -1735,7 +1735,7 @@ static size_t elf_core_vma_data_size(str
 
 	for (vma = first_vma(current, gate_vma); vma != NULL;
 	     vma = next_vma(vma, gate_vma))
-		size += vma_dump_size(vma, mm_flags);
+		size += vma_dump_size(current, vma, mm_flags);
 	return size;
 }
 
@@ -1860,7 +1860,7 @@ static int elf_core_dump(struct coredump
 		phdr.p_offset = offset;
 		phdr.p_vaddr = vma->vm_start;
 		phdr.p_paddr = 0;
-		phdr.p_filesz = vma_dump_size(vma, cprm->mm_flags);
+		phdr.p_filesz = vma_dump_size(current, vma, cprm->mm_flags);
 		phdr.p_memsz = vma->vm_end - vma->vm_start;
 		offset += phdr.p_filesz;
 		phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
@@ -1895,7 +1895,7 @@ static int elf_core_dump(struct coredump
 		unsigned long addr;
 		unsigned long end;
 
-		end = vma->vm_start + vma_dump_size(vma, cprm->mm_flags);
+		end = vma->vm_start + vma_dump_size(current, vma, cprm->mm_flags);
 
 		for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
 			struct page *page;
fs/elfcore-common.c +14 -7
Index: linux-2.6.36-rc7/fs/elfcore-common.c
===================================================================
@@ -50,8 +50,8 @@ struct vm_area_struct *next_vma(struct v
 /*
  * Decide what to dump of a segment, part, all or none.
  */
-unsigned long vma_dump_size(struct vm_area_struct *vma,
-					 unsigned long mm_flags)
+unsigned long vma_dump_size(struct task_struct *p, struct vm_area_struct *vma,
+				unsigned long mm_flags)
 {
 #define FILTER(type)	(mm_flags & (1UL << MMF_DUMP_##type))
 
@@ -97,7 +97,6 @@ unsigned long vma_dump_size(struct vm_ar
 	    vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
 		u32 __user *header = (u32 __user *) vma->vm_start;
 		u32 word;
-		mm_segment_t fs = get_fs();
 		/*
 		 * Doing it this way gets the constant folded by GCC.
 		 */
@@ -114,10 +113,18 @@ unsigned long vma_dump_size(struct vm_ar
 		 * Switch to the user "segment" for get_user(),
 		 * then put back what elf_core_dump() had in place.
 		 */
-		set_fs(USER_DS);
-		if (unlikely(get_user(word, header)))
-			word = 0;
-		set_fs(fs);
+		if (p->group_leader == current->group_leader) {
+			mm_segment_t fs = get_fs();
+			set_fs(USER_DS);
+			if (unlikely(get_user(word, header)))
+				word = 0;
+			set_fs(fs);
+		} else {
+			int bytes = access_process_vm(p, (unsigned long)header,
+						&word, sizeof (word), 0);
+			if (bytes != sizeof(word))
+				word = 0;
+		}
 		if (word == magic.cmp)
 			return PAGE_SIZE;
 	}
include/linux/elfcore-internal.h +2 -2
Index: linux-2.6.36-rc7/include/linux/elfcore-internal.h
===================================================================
@@ -37,8 +37,8 @@ extern struct vm_area_struct *first_vma(
 					struct vm_area_struct *gate_vma);
 extern struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
 					struct vm_area_struct *gate_vma);
-extern unsigned long vma_dump_size(struct vm_area_struct *vma,
-					 unsigned long mm_flags);
+extern unsigned long vma_dump_size(struct task_struct *task,
+			struct vm_area_struct *vma, unsigned long mm_flags);
 
 #endif
 #endif
#4 lore raw

Re: [Patch 3/21] Make vma_dump_size() generic

Suzuki, I didn't read this series. Still I have some concerns after the very brief look...

fs/elfcore-common.c unsigned long vma_dump_size(struct vm_ar · line 116

Quoted 10 lines · Suzuki K. Poulose
fs/elfcore-common.c +1 -4
@@ -114,10 +113,18 @@ unsigned long vma_dump_size(struct vm_ar
 		 * Switch to the user "segment" for get_user(),
 		 * then put back what elf_core_dump() had in place.
 		 */
-		set_fs(USER_DS);
-		if (unlikely(get_user(word, header)))
-			word = 0;
-		set_fs(fs);
+		if (p->group_leader == current->group_leader) {

same_thread_group() ?

fs/elfcore-common.c unsigned long vma_dump_size(struct vm_ar · line 124

Quoted 8 lines
fs/elfcore-common.c +8 -0
+			mm_segment_t fs = get_fs();
+			set_fs(USER_DS);
+			if (unlikely(get_user(word, header)))
+				word = 0;
+			set_fs(fs);
+		} else {
+			int bytes = access_process_vm(p, (unsigned long)header,
+						&word, sizeof (word), 0);

Well. This adds a little pessimization. It is possible that the "normal" coredump dumps the CLONE_VM process which doesn't below to current's thread group.

May be you can compare ->mm's, but I am not sure.

Oleg.

#5 lore raw

PATCH 4/21 Make fill_psinfo generic

In reply to Suzuki K. Poulose

fill_psinfo() fills the NT_PRPSINFO note for the core. NT_PRPSINFO stores the command line of the process, which is stored at from mm->arg_start. Make fill_psinfo reusable by supporting other tasks. Use access_process_vm() to read the command line args for non-current task.

---
 fs/binfmt_elf.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
fs/binfmt_elf.c +14 -3
Index: linux-2.6.36-rc7/fs/binfmt_elf.c
===================================================================
@@ -1211,9 +1211,20 @@ static int fill_psinfo(struct elf_prpsin
 	len = mm->arg_end - mm->arg_start;
 	if (len >= ELF_PRARGSZ)
 		len = ELF_PRARGSZ-1;
-	if (copy_from_user(&psinfo->pr_psargs,
-		           (const char __user *)mm->arg_start, len))
-		return -EFAULT;
+	/*
+	 * If we are dumping core for "another task"
+	 * we can't use copy_from_user().
+	 */
+	if (p->group_leader == current->group_leader) {
+		if (copy_from_user(&psinfo->pr_psargs,
+			           (const char __user *)mm->arg_start, len))
+			return -EFAULT;
+	} else {
+		int bytes = access_process_vm(p, mm->arg_start,
+						&psinfo->pr_psargs, len, 0);
+		if (bytes != len)
+			return -EFAULT;
+	}
 	for(i = 0; i < len; i++)
 		if (psinfo->pr_psargs[i] == 0)
 			psinfo->pr_psargs[i] = ' ';
#6 lore raw

Re: [Patch 4/21] Make fill_psinfo generic

Quoted 5 lines · Suzuki K. Poulose

+       /*
+        * If we are dumping core for "another task"
+        * we can't use copy_from_user().
+        */
+       if (p->group_leader == current->group_leader) {

This is totally bogus. "group_leader" has absolutely nothing to do with whether the process shares the address space. It's about signal sharing etc.

The way to check whether they share the same address space is to check whether "mm" is the same.

Linus

#7 lore raw

Re: [Patch 4/21] Make fill_psinfo generic

Quoted 7 lines · On Tue, 14 Dec 2010 07:55:00 -0800 Linus Torvalds…
Quoted 5 lines · Suzuki K. Poulose

+       /*
+        * If we are dumping core for "another task"
+        * we can't use copy_from_user().
+        */
+       if (p->group_leader == current->group_leader) {

Quoted 7 lines

This is totally bogus. "group_leader" has absolutely nothing to do with whether the process shares the address space. It's about signal sharing etc.

The way to check whether they share the same address space is to check whether "mm" is the same.

OK. I will make use of the mm here.

Thanks
Suzuki

#8 lore raw

Re: [Patch 4/21] Make fill_psinfo generic

In reply to Suzuki K. Poulose

fs/binfmt_elf.c static int fill_psinfo(struct elf_prpsin · line 1224

Quoted 8 lines · Suzuki K. Poulose
fs/binfmt_elf.c +7 -0
+	if (p->group_leader == current->group_leader) {
+		if (copy_from_user(&psinfo->pr_psargs,
+			           (const char __user *)mm->arg_start, len))
+			return -EFAULT;
+	} else {
+		int bytes = access_process_vm(p, mm->arg_start,
+						&psinfo->pr_psargs, len, 0);

The same.

Oleg.

#9 lore raw

PATCH 5/21 Rename compat versions of the reusable core generation routines

In reply to Suzuki K. Poulose

Rename the ELF class specific functions reusable for the application core dump infrastructure. The compat ELF class routines are prepended with compat_ .

---
 fs/compat_binfmt_elf.c |    7 +++++++
 1 file changed, 7 insertions(+)
fs/compat_binfmt_elf.c +7 -0
Index: linux-2.6.36-rc7/fs/compat_binfmt_elf.c
===================================================================
@@ -127,6 +127,13 @@ static void cputime_to_compat_timeval(co
 #define init_elf_binfmt		init_compat_elf_binfmt
 #define exit_elf_binfmt		exit_compat_elf_binfmt
 
+/* Rename the functions that may be reused */
+#define fill_elf_header 	compat_fill_elf_header
+#define fill_psinfo		compat_fill_psinfo
+#define fill_prstatus		compat_fill_prstatus
+#define fill_extnum_info	compat_fill_extnum_info
+#define fill_auxv_note		compat_fill_auxv_note
+
 /*
  * We share all the actual code with the native (64-bit) version.
  */
#10 lore raw

Re: [Patch 5/21] Rename compat versions of the reusable core generation routines

Quoted 14 lines · Suzuki K. Poulose

Rename the ELF class specific functions reusable for the application core dump infrastructure. The compat ELF class routines are prepended with compat_ .

---
 fs/compat_binfmt_elf.c |    7 +++++++
 1 file changed, 7 insertions(+)

Index: linux-2.6.36-rc7/fs/compat_binfmt_elf.c
===================================================================

fs/compat_binfmt_elf.c static void cputime_to_compat_timeval(co · line 135

Quoted 9 lines
fs/compat_binfmt_elf.c +6 -0
@@ -127,6 +127,13 @@ static void cputime_to_compat_timeval(co
 #define init_elf_binfmt		init_compat_elf_binfmt
 #define exit_elf_binfmt		exit_compat_elf_binfmt
 
+/* Rename the functions that may be reused */
+#define fill_elf_header 	compat_fill_elf_header
+#define fill_psinfo		compat_fill_psinfo
+#define fill_prstatus		compat_fill_prstatus
+#define fill_extnum_info	compat_fill_extnum_info
+#define fill_auxv_note		compat_fill_auxv_note

Can't undestand.... Afaics, the kernel can't be compiled with this change until the next patches actually implement compat_xxx ?

This is not bisect friendly.

Oleg.

#11 lore raw

Re: [Patch 5/21] Rename compat versions of the reusable core generation routines

Quoted 18 lines · On Tue, 14 Dec 2010 16:57:08 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 17 lines · Suzuki K. Poulose

Rename the ELF class specific functions reusable for the application core dump infrastructure. The compat ELF class routines are prepended with compat_ .

---
 fs/compat_binfmt_elf.c |    7 +++++++
 1 file changed, 7 insertions(+)

Index: linux-2.6.36-rc7/fs/compat_binfmt_elf.c
===================================================================

fs/compat_binfmt_elf.c +0 -0
@@ -127,6 +127,13 @@ static void cputime_to_compat_timeval(co
 #define init_elf_binfmt		init_compat_elf_binfmt
 #define exit_elf_binfmt		exit_compat_elf_binfmt
 
Quoted 10 lines

fs/compat_binfmt_elf.c static void cputime_to_compat_timeval(co · line 135

Quoted 6 lines
fs/compat_binfmt_elf.c +6 -0
+/* Rename the functions that may be reused */
+#define fill_elf_header 	compat_fill_elf_header
+#define fill_psinfo		compat_fill_psinfo
+#define fill_prstatus		compat_fill_prstatus
+#define fill_extnum_info	compat_fill_extnum_info
+#define fill_auxv_note		compat_fill_auxv_note

Can't undestand.... Afaics, the kernel can't be compiled with this change until the next patches actually implement compat_xxx ?

The compat_binfmt_elf.c shares the actual ELF handling code from binfmt_elf.c (by #includ ing it). This patch renames the functions so that we can later export it. The "#include binfmt_elf.c" comes later in the compat_binfmt_elf.c. So we are fine.

Quoted 5 lines

This is not bisect friendly.

Oleg.

#12 lore raw

PATCH 6/21 Export the reusable ELF core generation routines

In reply to Suzuki K. Poulose

Export the ELF class specific core generation helper functions via elfcore-internal.h

---
 fs/binfmt_elf.c                  |   10 +++++-----
 include/linux/elfcore-internal.h |   29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 5 deletions(-)
fs/binfmt_elf.c +5 -5
Index: linux-2.6.36-rc7/fs/binfmt_elf.c
===================================================================
@@ -1129,7 +1129,7 @@ static int writenote(struct memelfnote *
 }
 #undef DUMP_WRITE
 
-static void fill_elf_header(struct elfhdr *elf, int segs,
+void fill_elf_header(struct elfhdr *elf, int segs,
 			    u16 machine, u32 flags, u8 osabi)
 {
 	memset(elf, 0, sizeof(*elf));
@@ -1169,7 +1169,7 @@ static void fill_elf_note_phdr(struct el
  * fill up all the fields in prstatus from the given task struct, except
  * registers which need to be filled up separately.
  */
-static void fill_prstatus(struct elf_prstatus *prstatus,
+void fill_prstatus(struct elf_prstatus *prstatus,
 		struct task_struct *p, long signr)
 {
 	prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
@@ -1199,7 +1199,7 @@ static void fill_prstatus(struct elf_prs
 	cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
 }
 
-static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
+int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
 		       struct mm_struct *mm)
 {
 	const struct cred *cred;
@@ -1253,7 +1253,7 @@ static int fill_psinfo(struct elf_prpsin
 	return 0;
 }
 
-static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
+void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
 {
 	elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
 	int i = 0;
@@ -1722,7 +1722,7 @@ static void free_note_info(struct elf_no
 
 #endif
 
-static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
+void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
 			     elf_addr_t e_shoff, int segs)
 {
 	elf->e_shoff = e_shoff;
include/linux/elfcore-internal.h +29 -0
Index: linux-2.6.36-rc7/include/linux/elfcore-internal.h
===================================================================
@@ -15,6 +15,8 @@
 
 #ifdef __KERNEL__
 
+#include <linux/elfcore.h>
+
 /* An ELF note in memory */
 struct memelfnote
 {
@@ -40,5 +42,32 @@ extern struct vm_area_struct *next_vma(s
 extern unsigned long vma_dump_size(struct task_struct *task,
 			struct vm_area_struct *vma, unsigned long mm_flags);
 
+/* Core dump generation routines for native ELF class */
+extern void fill_elf_header(struct elfhdr *elf, int segs,
+				u16 machine, u32 flags, u8 osabi);
+extern void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr,
+				elf_addr_t e_shoff, int segs);
+extern void fill_prstatus(struct elf_prstatus *prstatus,
+				struct task_struct *p, long signr);
+extern int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
+				struct mm_struct *mm);
+extern void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm);
+
+/* compat ELF class variant of the above helpers*/
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+#include <linux/elfcore-compat.h>
+
+extern void compat_fill_extnum_info(struct elf32_hdr *elf, struct elf32_shdr *shdr,
+				Elf32_Addr e_shoff, int segs);
+extern void compat_fill_prstatus(struct compat_elf_prstatus *prstatus,
+					struct task_struct *p, long signr);
+extern int compat_fill_psinfo(struct compat_elf_prpsinfo *psinfo, struct task_struct *p,
+				struct mm_struct *mm);
+extern void compat_fill_auxv_note(struct memelfnote *note, struct mm_struct *mm);
+
+extern void compat_fill_elf_header(struct elf32_hdr *elf, int segs,
+				u16 machine, u32 flags, u8 osabi);
+#endif /* CONFIG_COMPAT_BINFMT_ELF */
+
 #endif
 #endif
#13 lore raw

PATCH 7/21 Define API for reading arch specif Program Headers for Core

In reply to Suzuki K. Poulose

The binfmt ELF defines APIs for the "arch" specific Program headers to be written to the ELF core. Define the "read" (or copy) variants of the APIs, to collect it for the application core dump(which is a read based approach).

---
 include/linux/elfcore.h |    3 +++
 kernel/elfcore.c        |    6 ++++++
 2 files changed, 9 insertions(+)
include/linux/elfcore.h +3 -0
Index: linux-2.6.36-rc7/include/linux/elfcore.h
===================================================================
@@ -163,6 +163,9 @@ extern int
 elf_core_write_extra_phdrs(struct file *file, loff_t offset, size_t *size,
 			   unsigned long limit);
 extern int
+elf_core_copy_extra_phdrs(char *buf, loff_t offset, size_t *size,
+			   unsigned long limit);
+extern int
 elf_core_write_extra_data(struct file *file, size_t *size, unsigned long limit);
 extern size_t elf_core_extra_data_size(void);
 
kernel/elfcore.c +6 -0
Index: linux-2.6.36-rc7/kernel/elfcore.c
===================================================================
@@ -16,6 +16,12 @@ int __weak elf_core_write_extra_phdrs(st
 	return 1;
 }
 
+int __weak elf_core_copy_extra_phdrs(char *buf, loff_t offset, size_t *size,
+					unsigned long limit)
+{
+	return 1;
+}
+
 int __weak elf_core_write_extra_data(struct file *file, size_t *size,
 				     unsigned long limit)
 {
#14 lore raw

PATCH 8/21 ia64 Implementation of elf_core_copy_extra_phdrs()

In reply to Suzuki K. Poulose

ia64 impelementation for elf_core_copy_extra_phdrs(). Adapted from elf_core_write_extra_phdrs().

---
 arch/ia64/kernel/elfcore.c |   34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
arch/ia64/kernel/elfcore.c +34 -0
Index: linux-2.6.36-rc7/arch/ia64/kernel/elfcore.c
===================================================================
@@ -42,6 +42,40 @@ int elf_core_write_extra_phdrs(struct fi
 	return 1;
 }
 
+int elf_core_copy_extra_phdrs(char *bufp, loff_t offset, size_t *size,
+				unsigned long limit)
+{
+	const struct elf_phdr *const gate_phdrs =
+		(const struct elf_phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
+	int i;
+	Elf64_Off ofs = 0;
+	struct elf_phdr *phdr = (struct elf_phdr *) bufp;
+
+	for (i = 0; i < GATE_EHDR->e_phnum; ++i) {
+		*size += sizeof(*phdr);
+		if (*size > limit)
+			return 0;
+
+		*phdr = gate_phdrs[i];
+
+		if (phdr->p_type == PT_LOAD) {
+			phdr->p_memsz = PAGE_ALIGN(phdr->p_memsz);
+			phdr->p_filesz = phdr->p_memsz;
+			if (ofs == 0) {
+				ofs = phdr->p_offset = *offset;
+				offset += phdr->p_filesz;
+			} else {
+				phdr->p_offset = ofs;
+			}
+		} else {
+			phdr->p_offset += ofs;
+		}
+		phdr->p_paddr = 0; /* match other core phdrs */
+		phdr++;
+	}
+	return 1;
+}
+
 int elf_core_write_extra_data(struct file *file, size_t *size,
 			      unsigned long limit)
 {
#15 lore raw

PATCH 9/21 UML (i386) Implementation of elf_core_copy_extra_phdrs()

In reply to Suzuki K. Poulose

elf_core_copy_extra_phdrs() for UML. Adapted from elf_core_write_extra_phdrs()

---
 arch/um/sys-i386/elfcore.c |   32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/um/sys-i386/elfcore.c +32 -0
Index: linux-2.6.36-rc7/arch/um/sys-i386/elfcore.c
===================================================================
@@ -41,6 +41,38 @@ int elf_core_write_extra_phdrs(struct fi
 	return 1;
 }
 
+int elf_core_copy_extra_phdrs(char *bufp, loff_t offset, size_t *size,
+				unsigned long limit)
+{
+	if (vsyscall_ehdr) {
+		const struct elfhdr *const ehdrp =
+			(struct elfhdr *) vsyscall_ehdr;
+		const struct elf_phdr *const phdrp =
+			(const struct elf_phdr *) (vsyscall_ehdr + ehdrp->e_phoff);
+		struct elf_phdr *phdr = (struct elf_phdr *)bufp;
+		int i;
+		Elf32_Off ofs = 0;
+
+		for (i = 0; i < ehdrp->e_phnum; ++i) {
+			*size += sizeof(*phdr);
+			if (*size > limit)
+				return 0;
+
+			*phdr = phdrp[i];
+
+			if (phdr.p_type == PT_LOAD) {
+				ofs = phdr.p_offset = offset;
+				offset += phdr.p_filesz;
+			} else {
+				phdr.p_offset += ofs;
+			}
+			phdr.p_paddr = 0; /* match other core phdrs */
+			phdr++;
+		}
+	}
+	return 1;
+}
+
 int elf_core_write_extra_data(struct file *file, size_t *size,
 			      unsigned long limit)
 {
#16 lore raw

PATCH 10/21 Create /proc/pid/core entry

In reply to Suzuki K. Poulose

Create the /proc/PID/core entry.

---
 fs/proc/Makefile   |    1 +
 fs/proc/base.c     |    1 +
 fs/proc/gencore.c  |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/proc/internal.h |    1 +
 4 files changed, 51 insertions(+)
fs/proc/Makefile +1 -0
Index: linux-2.6.36-rc7/fs/proc/Makefile
===================================================================
@@ -19,6 +19,7 @@ proc-y	+= stat.o
 proc-y	+= uptime.o
 proc-y	+= version.o
 proc-y	+= softirqs.o
+proc-$(CONFIG_ELF_CORE)	+= gencore.o
 proc-$(CONFIG_PROC_SYSCTL)	+= proc_sysctl.o
 proc-$(CONFIG_NET)		+= proc_net.o
 proc-$(CONFIG_PROC_KCORE)	+= kcore.o
fs/proc/base.c +1 -0
Index: linux-2.6.36-rc7/fs/proc/base.c
===================================================================
@@ -2735,6 +2735,7 @@ static const struct pid_entry tgid_base_
 #endif
 #ifdef CONFIG_ELF_CORE
 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
+	REG("core", S_IRUSR, proc_gen_core_operations),
 #endif
 #ifdef CONFIG_TASK_IO_ACCOUNTING
 	INF("io",	S_IRUGO, proc_tgid_io_accounting),
fs/proc/gencore.c new file +48 -0

48 lines not on this page · raw message

fs/proc/internal.h +1 -0
Index: linux-2.6.36-rc7/fs/proc/internal.h
===================================================================
@@ -60,6 +60,7 @@ extern const struct file_operations proc
 extern const struct file_operations proc_pagemap_operations;
 extern const struct file_operations proc_net_operations;
 extern const struct inode_operations proc_net_inode_operations;
+extern const struct file_operations proc_gen_core_operations;
 
 void proc_init_inodecache(void);
 
#18 lore raw

Re: [Patch 10/21] Create /proc/pid/core entry

Quoted 8 lines · On Tue, 14 Dec 2010 18:36:36 +0800 Cong Wang <[email protected]> wrote:
Quoted 4 lines · Suzuki K. Poulose

Create the /proc/PID/core entry.

Can we have a new Kconfig for this? Instead of reusing CONFIG_ELF_CORE. You have to make it depend on CONFIG_FREEZER, I think.

Completely agree with your suggestion. I will rework this in the next iteration.

Thanks
Suzuki

#19 lore raw

PATCH 11/21 Track the core generation requests

In reply to Suzuki K. Poulose

Keep track of the core generation requests. Concurrent core generation requests for the same target process are not allowed.

---
 fs/proc/gencore.c |   85 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/proc/gencore.h |   12 +++++++
 2 files changed, 95 insertions(+), 2 deletions(-)
fs/proc/gencore.c +83 -2
Index: linux-2.6.36-rc7/fs/proc/gencore.c
===================================================================
@@ -24,21 +24,102 @@
 
 #include <linux/seq_file.h>
 #include "internal.h"
+#include "gencore.h"
+
+static LIST_HEAD(core_list);
+static DEFINE_MUTEX(core_mutex);
+
+/* Called with core_mutex held */
+static struct core_proc* get_core_proc(struct task_struct *t)
+{
+	struct core_proc *cp;
+
+	list_for_each_entry(cp, &core_list, list) {
+		if (cp->task == t->group_leader)
+			return cp;
+	}
+	return NULL;
+}
 
 static ssize_t read_gencore(struct file *file, char __user *buffer,
 		size_t buflen, loff_t *fpos)
 {
-	return 0;
+	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
+	struct core_proc *cp;
+	ssize_t ret = 0;
+
+	if (!task)
+		return -EIO;
+
+	mutex_lock(&core_mutex);
+	cp = get_core_proc(task);
+	if (!cp) {
+		mutex_unlock(&core_mutex);
+		ret = -EIO;
+		goto out;
+	}
+	mutex_unlock(&core_mutex);
+
+out:
+	put_task_struct(task);
+	return ret;
 }
 
 static int release_gencore(struct inode *inode, struct file *file)
 {
+	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
+	struct core_proc *cp;
+
+	if (!task)
+		return -EIO;
+
+	mutex_lock(&core_mutex);
+	cp = get_core_proc(task);
+	if (cp) {
+		list_del(&cp->list);
+		kfree(cp);
+	}
+	mutex_unlock(&core_mutex);
+	put_task_struct(task);
 	return 0;
 }
 
+/*
+ * Validate if the call is valid. We also need to prevent >1 open
+ * of the same file.
+ */
 static int open_gencore(struct inode *inode, struct file *filp)
 {
-	return 0;
+	struct task_struct *task = get_proc_task(inode);
+	struct core_proc *cp;
+	int ret = 0;
+
+	if (!task)
+		return -ENOENT;
+
+	mutex_lock(&core_mutex);
+	cp = get_core_proc(task);
+	if (cp) {
+		ret = -EALREADY;
+		mutex_unlock(&core_mutex);
+		goto out;
+	}
+
+	cp = kzalloc(sizeof (*cp), GFP_KERNEL);
+	if (!cp) {
+		ret = -ENOMEM;
+		mutex_unlock(&core_mutex);
+		goto out;
+	}
+
+	cp->task = task->group_leader;
+	INIT_LIST_HEAD(&cp->list);
+	list_add(&cp->list, &core_list);
+	mutex_unlock(&core_mutex);
+
+out:
+	put_task_struct(task);
+	return ret;
 }
 
 const struct file_operations proc_gen_core_operations = {
fs/proc/gencore.h new file +12 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -0,0 +1,12 @@
+#ifndef __GEN_CORE_H
+#define __GEN_CORE_H
+
+#include <linux/list.h>
+#include <linux/sched.h>
+
+struct core_proc {
+	struct list_head list;
+	struct task_struct *task;
+};
+
+#endif
#20 lore raw

Re: [Patch 11/21] Track the core generation requests

fs/proc/gencore.h

Quoted 4 lines · Suzuki K. Poulose
fs/proc/gencore.h new file +1 -0
 
+#include <linux/sched.h>

struct task_struct; is enough.

Quoted 4 lines
fs/proc/gencore.h new file +4 -0
+struct core_proc {
+	struct list_head list;
+	struct task_struct *task;
+};
#21 lore raw

Re: [Patch 11/21] Track the core generation requests

In reply to Suzuki K. Poulose
Quoted 3 lines · Suzuki K. Poulose

Concurrent core generation requests
for the same target process are not allowed.

OK, but

Quoted 15 lines
fs/proc/gencore.c +11 -1
+static struct core_proc* get_core_proc(struct task_struct *t)
+{
+	struct core_proc *cp;
+
+	list_for_each_entry(cp, &core_list, list) {
+		if (cp->task == t->group_leader)
+			return cp;
+	}
+	return NULL;
+}
...
 static int open_gencore(struct inode *inode, struct file *filp)
 {
-	return 0;
+	struct task_struct *task = get_proc_task(inode);

fs/proc/gencore.c line 103

Quoted 10 lines
fs/proc/gencore.c +10 -0
+	struct core_proc *cp;
+	int ret = 0;
+
+	if (!task)
+		return -ENOENT;
+
+	mutex_lock(&core_mutex);
+	cp = get_core_proc(task);
+	if (cp) {
+		ret = -EALREADY;

I don't think this can work.

The task can change ->group_leader. This means 2 or more open_gencore() can succeed if this task execs in between.

Oleg.

#22 lore raw

PATCH 12/21 Check if the process is an ELF executable

In reply to Suzuki K. Poulose

Validate if the process is an ELF exec. This will be later extended to identify if the task is a native ELF or a compat ELF.

---
 fs/proc/gencore.c |   31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
fs/proc/gencore.c +31 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.c
===================================================================
@@ -23,6 +23,8 @@
  */
 
 #include <linux/seq_file.h>
+#include <linux/elf.h>
+
 #include "internal.h"
 #include "gencore.h"
 
@@ -85,6 +87,30 @@ static int release_gencore(struct inode 
 }
 
 /*
+ * Determine whether the task is an ELF executable.
+ * Returns
+ * 	< 0	- Non-ELF
+ * 	0 	- Native ELF Executable
+ */
+static int get_elf_class(struct task_struct *task)
+{
+	struct elfhdr hdr;
+	int ret = 0;
+
+	ret = kernel_read(task->mm->exe_file, 0, (char*)&hdr, sizeof(hdr));
+	if (ret < 0)
+		return ret;
+
+	/* Verify the ELF magic on the exe_file */
+	if (memcmp(hdr.e_ident, ELFMAG, SELFMAG))
+		return -EINVAL;
+	if (elf_check_arch(&hdr))
+		return 0;
+
+	return -EINVAL;
+}
+
+/*
  * Validate if the call is valid. We also need to prevent >1 open
  * of the same file.
  */
@@ -93,10 +119,15 @@ static int open_gencore(struct inode *in
 	struct task_struct *task = get_proc_task(inode);
 	struct core_proc *cp;
 	int ret = 0;
+	int elf_class;
 
 	if (!task)
 		return -ENOENT;
 
+	elf_class = get_elf_class(task);
+	if (elf_class < 0)
+		return elf_class;
+
 	mutex_lock(&core_mutex);
 	cp = get_core_proc(task);
 	if (cp) {
#23 lore raw

Re: [Patch 12/21] Check if the process is an ELF executable

fs/proc/gencore.c static int release_gencore(struct inode · line 100

Quoted 8 lines · Suzuki K. Poulose

+ */

fs/proc/gencore.c +6 -0
+static int get_elf_class(struct task_struct *task)
+{
+	struct elfhdr hdr;
+	int ret = 0;
+
+	ret = kernel_read(task->mm->exe_file, 0, (char*)&hdr, sizeof(hdr));

->exe_file can be NULL.

Oleg.

#24 lore raw

PATCH 13/21 Freeze / Thaw threads

In reply to Suzuki K. Poulose

All the threads in the target process are issued a freeze request on an "open()". They are woken up when we release the fd.

---
 fs/proc/gencore.c |   26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
fs/proc/gencore.c +26 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.c
===================================================================
@@ -24,6 +24,7 @@
 
 #include <linux/seq_file.h>
 #include <linux/elf.h>
+#include <linux/freezer.h>
 
 #include "internal.h"
 #include "gencore.h"
@@ -70,11 +71,20 @@ out:
 static int release_gencore(struct inode *inode, struct file *file)
 {
 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
+	struct task_struct *t;
 	struct core_proc *cp;
 
 	if (!task)
 		return -EIO;
 
+	/* Thaw all frozen threads */
+	t = task;
+	read_lock(&tasklist_lock);
+	do {
+		thaw_process(t);
+	} while ((t = next_thread(t)) != task);
+	read_unlock(&tasklist_lock);
+
 	mutex_lock(&core_mutex);
 	cp = get_core_proc(task);
 	if (cp) {
@@ -117,6 +127,7 @@ static int get_elf_class(struct task_str
 static int open_gencore(struct inode *inode, struct file *filp)
 {
 	struct task_struct *task = get_proc_task(inode);
+	struct task_struct *t;
 	struct core_proc *cp;
 	int ret = 0;
 	int elf_class;
@@ -148,6 +159,21 @@ static int open_gencore(struct inode *in
 	list_add(&cp->list, &core_list);
 	mutex_unlock(&core_mutex);
 
+	/* freeze the processes */
+	t = task;
+	read_lock(&tasklist_lock);
+	do {
+		if (frozen(t) || !freezeable(t) || freezing(t))
+			continue;
+
+		if (freeze_task(t, true))
+			continue;
+
+		if (task_is_stopped_or_traced(t))
+			continue;
+	} while ((t = next_thread(t)) != task);
+	read_unlock(&tasklist_lock);
+
 out:
 	put_task_struct(task);
 	return ret;
#25 lore raw

Re: [Patch 13/21] Freeze / Thaw threads

Quoted 9 lines · Suzuki K. Poulose
fs/proc/gencore.c +4 -0
@@ -148,6 +159,21 @@ static int open_gencore(struct inode *in
 	list_add(&cp->list, &core_list);
 	mutex_unlock(&core_mutex);
 
+	/* freeze the processes */
+	t = task;
+	read_lock(&tasklist_lock);
+	do {

fs/proc/gencore.c static int open_gencore(struct inode *in · line 175

Quoted 10 lines
fs/proc/gencore.c +10 -0
+		if (frozen(t) || !freezeable(t) || freezing(t))
+			continue;
+
+		if (freeze_task(t, true))
+			continue;
+
+		if (task_is_stopped_or_traced(t))
+			continue;
+	} while ((t = next_thread(t)) != task);
+	read_unlock(&tasklist_lock);

Ooooh. Sorry, I dislike this approach very much.

Firstly, I can't understand why this can't race with the kernel doing freeze/thaw.

But the main problem is: these series adds the new and nice way to create the unkillable processes. This can't be good.

Say, until you close this file, even oom-killer can't kill it. It is easy to DoS the system. Just fork the new processes which use the memory and freeze them.

And, given that we have to handle task_is_stopped_or_traced() tasks correctly anyway, I think gencore should rely on STOPPED/ TRACED and doesn't use freezer at all.

Review static int open_gencore(struct inode *in · line 176

Quoted 1 line
(quoted diff) +1 -0
+     } while ((t = next_thread(t)) != task);

while_each_thread()

Oleg.

#26 lore raw

PATCH 14/21 Create ELF header

In reply to Suzuki K. Poulose

Build the ELF header on the fly for the very first read request. The ELF Header,
Program Headers are stored in a buffer for processing future read() requests.

gencore-elf.c contains the ELF class specific functions.

---
 fs/proc/Makefile      |    2 
 fs/proc/gencore-elf.c |  177 ++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/proc/gencore.c     |    5 +
 fs/proc/gencore.h     |    6 +
 4 files changed, 189 insertions(+), 1 deletion(-)
fs/proc/gencore.h +6 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -7,6 +7,12 @@
 struct core_proc {
 	struct list_head list;
 	struct task_struct *task;
+	void *shdr;		/* elf_shdr, in case nphdrs > PN_XNUM */
+	char *elf_buf;		/* buffer for elf_hdr + phdrs + notes */
+	size_t elf_buflen;	/* size of elf_buf */
+	size_t nphdrs;		/* number of phdrs */
 };
 
+extern ssize_t elf_read_gencore(struct core_proc *cp, char __user *buffer,
+					size_t buflen, loff_t *foffset);
 #endif
fs/proc/gencore-elf.c new file +177 -0

177 lines not on this page · raw message

fs/proc/Makefile +1 -1
Index: linux-2.6.36-rc7/fs/proc/Makefile
===================================================================
@@ -19,7 +19,7 @@ proc-y	+= stat.o
 proc-y	+= uptime.o
 proc-y	+= version.o
 proc-y	+= softirqs.o
-proc-$(CONFIG_ELF_CORE)	+= gencore.o
+proc-$(CONFIG_ELF_CORE)	+= gencore.o gencore-elf.o
 proc-$(CONFIG_PROC_SYSCTL)	+= proc_sysctl.o
 proc-$(CONFIG_NET)		+= proc_net.o
 proc-$(CONFIG_PROC_KCORE)	+= kcore.o
fs/proc/gencore.c +5 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.c
===================================================================
@@ -63,6 +63,8 @@ static ssize_t read_gencore(struct file 
 	}
 	mutex_unlock(&core_mutex);
 
+	ret = elf_read_gencore(cp, buffer, buflen, fpos);
+
 out:
 	put_task_struct(task);
 	return ret;
@@ -89,6 +91,9 @@ static int release_gencore(struct inode 
 	cp = get_core_proc(task);
 	if (cp) {
 		list_del(&cp->list);
+		if (cp->shdr)
+			kfree(cp->shdr);
+		kfree(cp->elf_buf);
 		kfree(cp);
 	}
 	mutex_unlock(&core_mutex);
#27 lore raw

Re: [Patch 14/21] Create ELF header

fs/proc/gencore-elf.c line 38

Quoted 7 lines · Suzuki K. Poulose
fs/proc/gencore-elf.c new file +6 -0
+static void get_elfhdr_size(struct core_proc *cp)
+{
+	struct vm_area_struct *gate_vma;
+	int segs;
+
+	segs = cp->task->mm->map_count;

But this is unsafe. What if cp->task was stopped when open_gencore() was called? In this case it can be killed/dead.

cp->task->mm can be NULL. In fact, cp->task can point to nothing.

Any usage of cp->task is not safe, afaics.

Oleg.

#28 lore raw

PATCH 15/21 Collect ELF Core notes data

In reply to Suzuki K. Poulose

Collect the PT_NOTE information for the core.

There are two "process wide" notes. NT_PRPSINFO and NT_AUXV. These are captured in the core_proc structure.

Each thread gets a NT_PRSTATUS note, which will contain the GPR contents. A thread may have additional notes depending on the other register sets used by it. Uses struct elf_thread_core_info to capture the thread specific information.

fill_thread_core_info() fills in the notes for a thread.

---
 fs/proc/gencore-elf.c |  183 +++++++++++++++++++++++++++++++++++++++++++++++++-
 fs/proc/gencore.c     |   67 ++++++++++++++++--
 fs/proc/gencore.h     |   28 +++++++
 3 files changed, 268 insertions(+), 10 deletions(-)
fs/proc/gencore.h +28 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -3,16 +3,44 @@
 
 #include <linux/list.h>
 #include <linux/sched.h>
+#include <linux/elfcore.h>
+#include <linux/elfcore-internal.h>
+
+struct elf_thread_core_info {
+	unsigned short num_notes;	/* Number of notes for this thread */
+	struct elf_thread_core_info *next;
+	struct task_struct *task;
+	struct elf_prstatus prstatus;
+	struct memelfnote notes[0];
+};
 
 struct core_proc {
 	struct list_head list;
 	struct task_struct *task;
 	void *shdr;		/* elf_shdr, in case nphdrs > PN_XNUM */
 	char *elf_buf;		/* buffer for elf_hdr + phdrs + notes */
+	struct elf_thread_core_info *tinfo;
+	struct memelfnote psinfo;
+	struct memelfnote auxv;
+	struct elf_prpsinfo prpsinfo;
 	size_t elf_buflen;	/* size of elf_buf */
 	size_t nphdrs;		/* number of phdrs */
+	size_t notes_size;
 };
 
+#ifdef CORE_DUMP_USE_REGSET
+#include <linux/regset.h>
+
+static inline int  get_max_regsets(struct task_struct *task)
+{
+	const struct user_regset_view *view = task_user_regset_view(task);
+	return view->n;
+}
+
+#else
+#define get_max_regsets(task)	3 /* GPR, FP, XFP? */
+#endif
+
 extern ssize_t elf_read_gencore(struct core_proc *cp, char __user *buffer,
 					size_t buflen, loff_t *foffset);
 #endif
fs/proc/gencore.c +59 -8

104 lines not on this page · raw message

fs/proc/gencore-elf.c +181 -2

224 lines not on this page · raw message

#29 lore raw

Re: [Patch 15/21] Collect ELF Core notes data

Quoted 8 lines · Suzuki K. Poulose
fs/proc/gencore.c +3 -0
@@ -164,10 +197,28 @@ static int open_gencore(struct inode *in
 	list_add(&cp->list, &core_list);
 	mutex_unlock(&core_mutex);
 
+	max_regset = get_max_regsets(task);
+
+	for (i = 0; i < get_nr_threads(task); i++) {

fs/proc/gencore.c static int open_gencore(struct inode *in · line 212

Quoted 10 lines
fs/proc/gencore.c +10 -0
+		tinfo = kzalloc(offsetof(struct elf_thread_core_info,
+					notes[max_regset]), GFP_KERNEL);
+		if (unlikely(!tinfo)) {
+			cleanup_cp(cp);
+			ret = -ENOMEM;
+			goto out;
+		}
+		tinfo->next = cp->tinfo;
+		cp->tinfo = tinfo;
+	}

This looks strange... Obviously we can't trust get_nr_threads() before this process is stopped/frozen.

Just noticed... release_gencore() does thaw_process() for each thread. But, again, if it was stopped we can race with SIGCONT and exit. IOW, -EIO is possible. Who will thaw other threads? Also, we can probably race with exec, but I am not sure.

Oleg.

#30 lore raw

PATCH 16/21 Wait for threads to freeze

In reply to Suzuki K. Poulose

The threads may not have entered the "refrigerator()" by the time we start generating the NOTE sections, which can result in invalid register states. Wait for a short interval of time for the threads to freeze. The register information of the threads which are not frozen at the end of the interval, is filled with 0's, to indicate we couldn't capture the information.

---
 fs/proc/gencore-elf.c |   20 ++++++++++++++++++++
 fs/proc/gencore.c     |   42 ++++++++++++++++++++++++++++++++++++++++++
 fs/proc/gencore.h     |    3 +++
 3 files changed, 65 insertions(+)
fs/proc/gencore-elf.c +20 -0
Index: linux-2.6.36-rc7/fs/proc/gencore-elf.c
===================================================================
@@ -159,6 +159,21 @@ static int fill_thread_core_info(struct 
 }
 #endif
 
+/*
+ * zero_thread_state :
+ * For threads, which are not frozen, we cannot capture the register state.
+ * We zero them out.
+ */
+static void zero_thread_state(struct elf_thread_core_info *tinfo)
+{
+	int i;
+
+	memset(&tinfo->prstatus.pr_reg, 0, sizeof(tinfo->prstatus.pr_reg));
+	for (i = 1; i < tinfo->num_notes; i++)
+		if (tinfo->notes[i].data)
+			memset(tinfo->notes[i].data, 0, tinfo->notes[i].datasz);
+}
+
 /* Returns 0 on error, 1 on success */
 static int collect_notes(struct core_proc *cp)
 {
@@ -173,10 +188,15 @@ static int collect_notes(struct core_pro
 	fill_auxv_note(&cp->auxv, cp->task->mm);
 	cp->notes_size += notesize(&cp->auxv);
 
+	/* Make sure threads are frozen, before we start collecting notes */
+	try_to_freeze_core_threads(cp);
+
 	tinfo = cp->tinfo;
 	while (tinfo != NULL) {
 		if (!fill_thread_core_info(tinfo, cp))
 			return 0;
+		if (!tinfo->frozen)
+			zero_thread_state(tinfo);
 		tinfo = tinfo->next;
 	}
 	return 1;
fs/proc/gencore.h +3 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -7,6 +7,7 @@
 #include <linux/elfcore-internal.h>
 
 struct elf_thread_core_info {
+	unsigned short frozen;
 	unsigned short num_notes;	/* Number of notes for this thread */
 	struct elf_thread_core_info *next;
 	struct task_struct *task;
@@ -28,6 +29,8 @@ struct core_proc {
 	size_t notes_size;
 };
 
+extern void try_to_freeze_core_threads(struct core_proc *cp);
+
 #ifdef CORE_DUMP_USE_REGSET
 #include <linux/regset.h>
 
fs/proc/gencore.c +42 -0

54 lines not on this page · raw message

#31 lore raw

Re: [Patch 16/21] Wait for threads to freeze

Quoted 3 lines · Suzuki K. Poulose

+ * zero_thread_state :
+ * For threads, which are not frozen, we cannot capture the register state.

Indeed, but

fs/proc/gencore-elf.c static int collect_notes(struct core_pro · line 199

Quoted 2 lines
fs/proc/gencore-elf.c +2 -0
+		if (!tinfo->frozen)
+			zero_thread_state(tinfo);

Again, we can race with TASK_STOPPED/SIGCONT. And is_task_frozen_enough() thins task_is_stopped_or_traced() is fine.

Oleg.

#32 lore raw

PATCH 17/21 Calculate the size of the core file

In reply to Suzuki K. Poulose

Calculate the size of the core file

---
 fs/proc/gencore-elf.c |    6 ++++++
 fs/proc/gencore.h     |    1 +
 2 files changed, 7 insertions(+)
fs/proc/gencore.h +1 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -27,6 +27,7 @@ struct core_proc {
 	size_t elf_buflen;	/* size of elf_buf */
 	size_t nphdrs;		/* number of phdrs */
 	size_t notes_size;
+	size_t size;
 };
 
 extern void try_to_freeze_core_threads(struct core_proc *cp);
fs/proc/gencore-elf.c +6 -0
Index: linux-2.6.36-rc7/fs/proc/gencore-elf.c
===================================================================
@@ -310,6 +310,9 @@ static int create_elf_header(struct core
 						dataoff, cp->nphdrs);
 		dataoff += sizeof(struct elf_shdr);
 	}
+
+	cp->size = dataoff;
+
 	/* Store the notes */
 	tinfo = cp->tinfo;
 	do {
@@ -371,6 +374,9 @@ ssize_t elf_read_gencore(struct core_pro
 		}
 	}
 
+	if (*fpos > cp->size)
+		goto out;
+
 out:
 	return ret;
 }
#33 lore raw

PATCH 18/21 Generate the data sections for ELF Core

In reply to Suzuki K. Poulose

Generate the "data" for the memory regions. Also write down the section header if we have, number of phdrs > PN_XNUM.

The vma areas are read, page by page using access_process_vm() without an mmap_sem. If there are active threads, then we may miss a vma if it is removed while we are doing the read.

---
 fs/proc/gencore-elf.c |   90 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 87 insertions(+), 3 deletions(-)
fs/proc/gencore-elf.c +87 -3
Index: linux-2.6.36-rc7/fs/proc/gencore-elf.c
===================================================================
@@ -333,10 +333,29 @@ static int create_elf_header(struct core
 	return 0;
 }
 
+/*
+ * Verify if the fpos asked for in read is valid.
+ * Returns the phdr corresponding to offset, else NULL.
+ */
+static struct elf_phdr *get_pos_elfphdr(struct core_proc *cp, loff_t pos)
+{
+	struct elfhdr *elf_hdr = (struct elfhdr *)cp->elf_buf;
+	struct elf_phdr *phdr = (struct elf_phdr*)(cp->elf_buf + elf_hdr->e_phoff);
+	int i;
+
+	for (i = 0; i < cp->nphdrs; i++, phdr++) {
+		unsigned long end = phdr->p_offset + phdr->p_filesz;
+		if ((pos >= phdr->p_offset) && (pos < end))
+			return phdr;
+	}
+	return NULL;
+}
+
 ssize_t elf_read_gencore(struct core_proc *cp, char __user *buffer,
 				size_t buflen, loff_t *fpos)
 {
-	ssize_t ret = 0;
+	ssize_t ret = 0, acc = 0;
+	struct elfhdr *elf_hdr = (struct elfhdr *)cp->elf_buf;
 
 	if (!cp->notes_size) {
 		if (!collect_notes(cp)) {
@@ -367,16 +386,81 @@ ssize_t elf_read_gencore(struct core_pro
 			ret = -EFAULT;
 			goto out;
 		} else {
-			ret = bcp;
+			acc = bcp;
 			*fpos += bcp;
 			buflen -= bcp;
 			buffer += bcp;
 		}
 	}
 
+
 	if (*fpos > cp->size)
-		goto out;
+		goto done;
+
+	/*
+	 * Read from the vma segments
+	 * a. verify if the *fpos is within a phdr
+	 * b. Use access_process_vm() to get data page by page
+	 * c. copy_to_user into user buffer
+	 */
+
+	while (buflen) {
+		size_t bufsz, offset, bytes;
+		char *readbuf;
+		struct elf_phdr *phdr = get_pos_elfphdr(cp, *fpos);
+
+		if (!phdr)
+			break;
+
+		bufsz = (buflen > PAGE_SIZE) ? PAGE_SIZE : buflen;
+		readbuf = kmalloc(bufsz, GFP_KERNEL);
+		if (!readbuf) {
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		offset = *fpos - phdr->p_offset;
+		bytes = access_process_vm(cp->task, (phdr->p_vaddr + offset),
+						readbuf, bufsz, 0);
+		if (!bytes) {
+			ret = -EIO;
+			goto out;
+		}
+		if (copy_to_user(buffer, readbuf, bytes)) {
+			ret = -EFAULT;
+			kfree(readbuf);
+			goto out;
+		} else
+			acc += bytes;
+
+		kfree(readbuf);
+		buflen -= bytes;
+		buffer += bytes;
+		*fpos += bytes;
+	}
+
+	/* Fill extnum section header if present */
+	if (buflen &&
+		elf_hdr->e_shoff &&
+		(*fpos >= elf_hdr->e_shoff) &&
+		(*fpos < (elf_hdr->e_shoff + sizeof(struct elf_shdr)))) {
+
+		off_t offset = *fpos - elf_hdr->e_shoff;
+		size_t shdrsz = sizeof(struct elf_shdr) - offset;
+
+		shdrsz = (buflen < shdrsz) ? buflen : shdrsz;
+		if (copy_to_user(buffer, ((char *)cp->shdr) + offset, shdrsz)) {
+			ret = -EFAULT;
+			goto out;
+		} else {
+			acc += shdrsz;
+			buflen -= shdrsz;
+			buffer += shdrsz;
+		}
+	}
 
+done:
+	ret = acc;
 out:
 	return ret;
 }
#34 lore raw

Re: [Patch 18/21] Generate the data sections for ELF Core

于 2010年12月14日 18:22, Suzuki K. Poulose 写道:
...

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 405

Quoted 6 lines
fs/proc/gencore-elf.c +6 -0
+	/*
+	 * Read from the vma segments
+	 * a. verify if the *fpos is within a phdr
+	 * b. Use access_process_vm() to get data page by page
+	 * c. copy_to_user into user buffer
+	 */

This kind of comments is useless, "code tells you how, comments tell you why."

Quoted 12 lines
fs/proc/gencore-elf.c +12 -0
+
+	while (buflen) {
+		size_t bufsz, offset, bytes;
+		char *readbuf;
+		struct elf_phdr *phdr = get_pos_elfphdr(cp, *fpos);
+
+		if (!phdr)
+			break;
+
+		bufsz = (buflen>  PAGE_SIZE) ? PAGE_SIZE : buflen;
+		readbuf = kmalloc(bufsz, GFP_KERNEL);
+		if (!readbuf) {

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 427

Quoted 10 lines
fs/proc/gencore-elf.c +10 -0
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		offset = *fpos - phdr->p_offset;
+		bytes = access_process_vm(cp->task, (phdr->p_vaddr + offset),
+						readbuf, bufsz, 0);
+		if (!bytes) {
+			ret = -EIO;
+			goto out;

Why you don't kfree(readbuf) here?

Quoted 9 lines
fs/proc/gencore-elf.c +9 -0
+		}
+		if (copy_to_user(buffer, readbuf, bytes)) {
+			ret = -EFAULT;
+			kfree(readbuf);
+			goto out;
+		} else
+			acc += bytes;
+
+		kfree(readbuf);

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro

Quoted 10 lines
fs/proc/gencore-elf.c +10 -0
+		buflen -= bytes;
+		buffer += bytes;
+		*fpos += bytes;
+	}
+
+	/* Fill extnum section header if present */
+	if (buflen&&
+		elf_hdr->e_shoff&&
+		(*fpos>= elf_hdr->e_shoff)&&
+		(*fpos<  (elf_hdr->e_shoff + sizeof(struct elf_shdr)))) {

This indention seems ugly.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+		off_t offset = *fpos - elf_hdr->e_shoff;

Are you sure it is 'off_t' not 'loff_t'?

Quoted 8 lines
fs/proc/gencore-elf.c +8 -0
+		size_t shdrsz = sizeof(struct elf_shdr) - offset;
+
+		shdrsz = (buflen<  shdrsz) ? buflen : shdrsz;
+		if (copy_to_user(buffer, ((char *)cp->shdr) + offset, shdrsz)) {
+			ret = -EFAULT;
+			goto out;
+		} else {
+			acc += shdrsz;

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 466

Quoted 10 lines
fs/proc/gencore-elf.c +6 -0
+			buflen -= shdrsz;
+			buffer += shdrsz;
+		}
+	}
 
+done:
+	ret = acc;
  out:
  	return ret;
  }

Thanks.

#35 lore raw

Re: [Patch 18/21] Generate the data sections for ELF Core

Quoted 30 lines · On Wed, 15 Dec 2010 15:19:54 +0800 Cong Wang <[email protected]> wrote:

于 2010年12月14日 18:22, Suzuki K. Poulose 写道:
...

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 405

Quoted 6 lines
fs/proc/gencore-elf.c +6 -0
+	/*
+	 * Read from the vma segments
+	 * a. verify if the *fpos is within a phdr
+	 * b. Use access_process_vm() to get data page by page
+	 * c. copy_to_user into user buffer
+	 */

This kind of comments is useless, "code tells you how, comments tell you why."

Quoted 17 lines
fs/proc/gencore-elf.c +17 -0
+
+	while (buflen) {
+		size_t bufsz, offset, bytes;
+		char *readbuf;
+		struct elf_phdr *phdr = get_pos_elfphdr(cp, *fpos);
+
+		if (!phdr)
+			break;
+
+		bufsz = (buflen>  PAGE_SIZE) ? PAGE_SIZE : buflen;
+		readbuf = kmalloc(bufsz, GFP_KERNEL);
+		if (!readbuf) {
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		offset = *fpos - phdr->p_offset;
Quoted 9 lines

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 427

Quoted 5 lines
fs/proc/gencore-elf.c +5 -0
+		bytes = access_process_vm(cp->task, (phdr->p_vaddr + offset),
+						readbuf, bufsz, 0);
+		if (!bytes) {
+			ret = -EIO;
+			goto out;

Why you don't kfree(readbuf) here?

I will add that.

Quoted 14 lines
Quoted 14 lines
fs/proc/gencore-elf.c +14 -0
+		}
+		if (copy_to_user(buffer, readbuf, bytes)) {
+			ret = -EFAULT;
+			kfree(readbuf);
+			goto out;
+		} else
+			acc += bytes;
+
+		kfree(readbuf);
+		buflen -= bytes;
+		buffer += bytes;
+		*fpos += bytes;
+	}
+
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	/* Fill extnum section header if present */
+	if (buflen&&
+		elf_hdr->e_shoff&&
+		(*fpos>= elf_hdr->e_shoff)&&
+		(*fpos<  (elf_hdr->e_shoff + sizeof(struct elf_shdr)))) {

This indention seems ugly.

I will fix this.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+		off_t offset = *fpos - elf_hdr->e_shoff;

Are you sure it is 'off_t' not 'loff_t'?

You are right. I will change it

Quoted 13 lines
Quoted 13 lines
fs/proc/gencore-elf.c +12 -0
+		size_t shdrsz = sizeof(struct elf_shdr) - offset;
+
+		shdrsz = (buflen<  shdrsz) ? buflen : shdrsz;
+		if (copy_to_user(buffer, ((char *)cp->shdr) + offset, shdrsz)) {
+			ret = -EFAULT;
+			goto out;
+		} else {
+			acc += shdrsz;
+			buflen -= shdrsz;
+			buffer += shdrsz;
+		}
+	}
Quoted 6 lines

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 466

Quoted 5 lines
fs/proc/gencore-elf.c +2 -0
+done:
+	ret = acc;
  out:
  	return ret;
  }

Thank You !

Suzuki

Quoted 1 line

Thanks.

#36 lore raw

Re: [Patch 18/21] Generate the data sections for ELF Core

Quoted 4 lines · Suzuki K. Poulose
Quoted 1 line · On Wed, 15 Dec 2010 15:19:54 +0800 Cong Wang<[email protected]> wrote:

于 2010年12月14日 18:22, Suzuki K. Poulose 写道:

...

Quoted 9 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+		off_t offset = *fpos - elf_hdr->e_shoff;

Are you sure it is 'off_t' not 'loff_t'?

You are right. I will change it

Quoted 2 lines

fs/proc/gencore-elf.c ssize_t elf_read_gencore(struct core_pro · line 450

Quoted 2 lines
fs/proc/gencore-elf.c +2 -0
+		size_t shdrsz = sizeof(struct elf_shdr) - offset;
+

If you use loff_t, then this line needs to be changed too. size_t could be shorter than loff_t.

Thanks.

#37 lore raw

PATCH 19/21 Identify the ELF class of the process

In reply to Suzuki K. Poulose

Identify the ELF class of the process to native or compat.

---
 fs/proc/gencore.c |   17 +++++++++++++----
 fs/proc/gencore.h |    3 +++
 2 files changed, 16 insertions(+), 4 deletions(-)
fs/proc/gencore.c +13 -4
Index: linux-2.6.36-rc7/fs/proc/gencore.c
===================================================================
@@ -106,7 +106,8 @@ static ssize_t read_gencore(struct file 
 	}
 	mutex_unlock(&core_mutex);
 
-	ret = elf_read_gencore(cp, buffer, buflen, fpos);
+	if (cp->elf_class == ELF_CLASS_NATIVE)
+		ret = elf_read_gencore(cp, buffer, buflen, fpos);
 
 out:
 	put_task_struct(task);
@@ -177,8 +178,8 @@ static int release_gencore(struct inode 
 /*
  * Determine whether the task is an ELF executable.
  * Returns
- * 	< 0	- Non-ELF
- * 	0 	- Native ELF Executable
+ * 	-EINVAL - On non ELF execs
+ * 	Returns the ELF_CLASS_NATIVE or ELF_CLASS_COMPAT
  */
 static int get_elf_class(struct task_struct *task)
 {
@@ -193,8 +194,15 @@ static int get_elf_class(struct task_str
 	if (memcmp(hdr.e_ident, ELFMAG, SELFMAG))
 		return -EINVAL;
 	if (elf_check_arch(&hdr))
-		return 0;
+		return ELF_CLASS_NATIVE;
 
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+	{
+		struct elf32_hdr *chdr = (struct elf32_hdr *)&hdr;
+		if (compat_elf_check_arch(chdr))
+			return ELF_CLASS_COMPAT;
+	}
+#endif
 	return -EINVAL;
 }
 
@@ -238,6 +246,7 @@ static int open_gencore(struct inode *in
 	INIT_LIST_HEAD(&cp->list);
 	list_add(&cp->list, &core_list);
 	mutex_unlock(&core_mutex);
+	cp->elf_class = elf_class;
 
 	max_regset = get_max_regsets(task);
 
fs/proc/gencore.h +3 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -6,6 +6,8 @@
 #include <linux/elfcore.h>
 #include <linux/elfcore-internal.h>
 
+enum { ELF_CLASS_NATIVE, ELF_CLASS_COMPAT };
+
 struct elf_thread_core_info {
 	unsigned short frozen;
 	unsigned short num_notes;	/* Number of notes for this thread */
@@ -28,6 +30,7 @@ struct core_proc {
 	size_t nphdrs;		/* number of phdrs */
 	size_t notes_size;
 	size_t size;
+	unsigned char elf_class;
 };
 
 extern void try_to_freeze_core_threads(struct core_proc *cp);
#38 lore raw

PATCH 20/21 Add supporting for compat ELF class data structures

In reply to Suzuki K. Poulose

Redefine the structures to include the compat class variant of the data structures.

gencore-elf.c will be reused for the compat ELF class support. Hence, define macros to get the right member for each class.

---
 fs/proc/gencore-elf.c |   37 +++++++++++++++++++++++++------------
 fs/proc/gencore.h     |   18 ++++++++++++++++--
 2 files changed, 41 insertions(+), 14 deletions(-)
fs/proc/gencore-elf.c +25 -12
Index: linux-2.6.36-rc7/fs/proc/gencore-elf.c
===================================================================
@@ -16,6 +16,8 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
  * Copyright (C) IBM Corporation, 2010
+ *
+ * This code is reused in gencore-compat-elf.c for compat ELF class support.
  */
 #include <linux/mm.h>
 #include <linux/slab.h>
@@ -30,6 +32,17 @@
 
 #include "gencore.h"
 
+#ifndef et_prstatus
+/* prstatus field for native elf,  in elf_thread_core_info*/
+#define et_prstatus u.prstatus
+#endif
+
+#ifndef cp_prpsinfo
+/* prpsinfo field for native elf, in core_proc */
+#define cp_prpsinfo u.prpsinfo
+#endif
+
+
 static int notesize(struct memelfnote *men)
 {
 	int size = sizeof(struct elf_note);
@@ -73,14 +86,14 @@ static int fill_thread_core_info(struct 
 	unsigned int i;
 	const struct user_regset_view *view = task_user_regset_view(tinfo->task);
 
-	fill_prstatus(&tinfo->prstatus, tinfo->task, 0);
+	fill_prstatus(&tinfo->et_prstatus, tinfo->task, 0);
 
 	do_thread_regset_writeback(tinfo->task, &view->regsets[0]);
 	(void) view->regsets[0].get(tinfo->task, &view->regsets[0],
-				0, sizeof(tinfo->prstatus.pr_reg),
-				&tinfo->prstatus.pr_reg, NULL);
+				0, sizeof(tinfo->et_prstatus.pr_reg),
+				&tinfo->et_prstatus.pr_reg, NULL);
 	fill_note(&tinfo->notes[0], "CORE", NT_PRSTATUS,
-			sizeof(tinfo->prstatus), &tinfo->prstatus);
+			sizeof(tinfo->et_prstatus), &tinfo->et_prstatus);
 	cp->notes_size += notesize(&tinfo->notes[0]);
 	tinfo->num_notes = view->n;
 
@@ -105,7 +118,7 @@ static int fill_thread_core_info(struct 
 						regset->core_note_type,
 						size, data);
 				else {
-					tinfo->prstatus.pr_fpvalid = 1;
+					tinfo->et_prstatus.pr_fpvalid = 1;
 					fill_note(&tinfo->notes[i], "CORE",
 						NT_PRFPREG, size, data);
 				}
@@ -124,15 +137,15 @@ static int fill_thread_core_info(struct 
 	elf_fpxregset_t xfpu, *pxfpu;
 #endif
 
-	fill_prstatus(&tinfo->prstatus, t->task, 0);
-	elf_core_copy_task_regs(t->task, &tinfo->prstatus.pr_reg);
+	fill_prstatus(&tinfo->et_prstatus, t->task, 0);
+	elf_core_copy_task_regs(t->task, &tinfo->et_prstatus.pr_reg);
 	fill_note(&tinfo->notes[0], "CORE", NT_PRSTATUS,
-			sizeof(t->prstatus), &t->prstatus);
+			sizeof(t->et_prstatus), &t->et_prstatus);
 
 	cp->notes_size += notesize(&tinfo->notes[0]);
 	tinfo->num_notes = 1;
 
-	if (tinfo->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(tinfo->task,
+	if (tinfo->et_prstatus.pr_fpvalid = elf_core_copy_task_fpregs(tinfo->task,
 							NULL, &fpu)) {
 		pfpu = kzalloc(sizeof(*pfpu), GFP_KERNEL);
 		if (pfpu == NULL)
@@ -168,7 +181,7 @@ static void zero_thread_state(struct elf
 {
 	int i;
 
-	memset(&tinfo->prstatus.pr_reg, 0, sizeof(tinfo->prstatus.pr_reg));
+	memset(&tinfo->et_prstatus.pr_reg, 0, sizeof(tinfo->et_prstatus.pr_reg));
 	for (i = 1; i < tinfo->num_notes; i++)
 		if (tinfo->notes[i].data)
 			memset(tinfo->notes[i].data, 0, tinfo->notes[i].datasz);
@@ -180,9 +193,9 @@ static int collect_notes(struct core_pro
 	struct elf_thread_core_info *tinfo;
 
 	/* Fill the 2 process wide notes */
-	fill_psinfo(&cp->prpsinfo, cp->task, cp->task->mm);
+	fill_psinfo(&cp->cp_prpsinfo, cp->task, cp->task->mm);
 	fill_note(&cp->psinfo, "CORE", NT_PRPSINFO,
-			sizeof(struct elf_prpsinfo), &cp->prpsinfo);
+			sizeof(struct elf_prpsinfo), &cp->cp_prpsinfo);
 	cp->notes_size += notesize(&cp->psinfo);
 
 	fill_auxv_note(&cp->auxv, cp->task->mm);
fs/proc/gencore.h +16 -2
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -6,6 +6,10 @@
 #include <linux/elfcore.h>
 #include <linux/elfcore-internal.h>
 
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+#include <linux/elfcore-compat.h>
+#endif
+
 enum { ELF_CLASS_NATIVE, ELF_CLASS_COMPAT };
 
 struct elf_thread_core_info {
@@ -13,7 +17,12 @@ struct elf_thread_core_info {
 	unsigned short num_notes;	/* Number of notes for this thread */
 	struct elf_thread_core_info *next;
 	struct task_struct *task;
-	struct elf_prstatus prstatus;
+	union {
+		struct elf_prstatus prstatus;
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+		struct compat_elf_prstatus compat_prstatus;
+#endif
+	} u;
 	struct memelfnote notes[0];
 };
 
@@ -25,7 +34,12 @@ struct core_proc {
 	struct elf_thread_core_info *tinfo;
 	struct memelfnote psinfo;
 	struct memelfnote auxv;
-	struct elf_prpsinfo prpsinfo;
+	union {
+		struct elf_prpsinfo prpsinfo;
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+		struct compat_elf_prpsinfo compat_prpsinfo;
+#endif
+	} u;
 	size_t elf_buflen;	/* size of elf_buf */
 	size_t nphdrs;		/* number of phdrs */
 	size_t notes_size;
#39 lore raw

PATCH 21/21 Compat ELF class Core generation support

In reply to Suzuki K. Poulose

Add full support for the compat ELF class objects. The gencore-compat-elf.c reuses the gencore-elf.c, by renaming the ABI structures and functions. (Inspired by compat_binfmt_elf.c)

---
 fs/proc/Makefile             |    1 
 fs/proc/gencore-compat-elf.c |   61 +++++++++++++++++++++++++++++++++++++++++++
 fs/proc/gencore.c            |    4 ++
 fs/proc/gencore.h            |    4 ++
 4 files changed, 70 insertions(+)
fs/proc/gencore.h +4 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.h
===================================================================
@@ -64,4 +64,8 @@ static inline int  get_max_regsets(struc
 
 extern ssize_t elf_read_gencore(struct core_proc *cp, char __user *buffer,
 					size_t buflen, loff_t *foffset);
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+extern ssize_t compat_elf_read_gencore(struct core_proc *cp, char __user *buffer,
+					size_t buflen, loff_t *foffset);
+#endif
 #endif
fs/proc/gencore.c +4 -0
Index: linux-2.6.36-rc7/fs/proc/gencore.c
===================================================================
@@ -108,6 +108,10 @@ static ssize_t read_gencore(struct file 
 
 	if (cp->elf_class == ELF_CLASS_NATIVE)
 		ret = elf_read_gencore(cp, buffer, buflen, fpos);
+#ifdef CONFIG_COMPAT_BINFMT_ELF
+	else if (cp->elf_class == ELF_CLASS_COMPAT)
+		ret = compat_elf_read_gencore(cp, buffer, buflen, fpos);
+#endif
 
 out:
 	put_task_struct(task);
fs/proc/gencore-compat-elf.c new file +61 -0

61 lines not on this page · raw message

fs/proc/Makefile +1 -0
Index: linux-2.6.36-rc7/fs/proc/Makefile
===================================================================
@@ -20,6 +20,7 @@ proc-y	+= uptime.o
 proc-y	+= version.o
 proc-y	+= softirqs.o
 proc-$(CONFIG_ELF_CORE)	+= gencore.o gencore-elf.o
+proc-$(CONFIG_COMPAT_BINFMT_ELF) += gencore-compat-elf.o
 proc-$(CONFIG_PROC_SYSCTL)	+= proc_sysctl.o
 proc-$(CONFIG_NET)		+= proc_net.o
 proc-$(CONFIG_PROC_KCORE)	+= kcore.o
#40 lore raw
In reply to Suzuki K. Poulose
Quoted 1 line · Suzuki K. Poulose

The interface is provided by a /proc/pid/core file, reading which can give the

What happens when process opens /proc/self/core ?
What happens when process opens /proc/self/core and coredumps before close(2).

#41 lore raw
Quoted 4 lines · On Tue, 14 Dec 2010 12:54:05 +0200 Alexey Dobriyan <[email protected]> wrote:
Quoted 1 line · Suzuki K. Poulose

The interface is provided by a /proc/pid/core file, reading which can give the

What happens when process opens /proc/self/core ?

The current thread will be skipped for freeze(), and all the others will be frozen.We fill in the register states with 0's for the active threads.

Quoted 1 line

What happens when process opens /proc/self/core and coredumps before close(2).

Thanks for pointing this out. With the current code, this causes a hang for the
current process, waiting for the other threads to clear. I will think about
a solution to this making use of the mm->core_state information.

Thanks a lot for the review.

Suzuki

#42 lore raw
In reply to Suzuki K. Poulose
Quoted 6 lines · Suzuki K. Poulose

This is series of patches implementing an infrastructure for capturing the core of an application without disrupting its process semantics.

The infrastructure makes use of the freezer subsystem in kernel to freeze the threads and then collect the information to generate  the core.

This seems to be a fundamentally flawed approach.

Quoted 1 line

From a security standpoint, it looks like a total disaster. A frozen

process is really hard to get rid of, so it looks like an obvious DoS attack to just create lots of processes, then sneakily freeze them all, and then laugh at the poor system admin who has no idea what's going on. While frozen, the things are basically unkillable but look entirely normal, no?

Linus

#43 lore raw

Hello,

Quoted 8 lines · Linus Torvalds
Quoted 6 lines · Suzuki K. Poulose

This is series of patches implementing an infrastructure for capturing the core of an application without disrupting its process semantics.

The infrastructure makes use of the freezer subsystem in kernel to freeze the threads and then collect the information to generate the core.

Quoted 8 lines

This seems to be a fundamentally flawed approach.

Quoted 1 line

From a security standpoint, it looks like a total disaster. A frozen

process is really hard to get rid of, so it looks like an obvious DoS attack to just create lots of processes, then sneakily freeze them all, and then laugh at the poor system admin who has no idea what's going on. While frozen, the things are basically unkillable but look entirely normal, no?

I think a better way would be adding a ptrace attach which is nestable and doesn't have the nasty side effect caused by the interactions between the implicit SIGSTOP and group stop. As a preparation step, I posted a patchset to cleanup the interactions between ptrace and group stop which is being reviewed. Once we have a nestable ptrace attach, we should be able to simply adapt gcore(1) to use it and write out core dump from userland.

Thanks.

Signature · 1 line
tejun
#44 lore raw
Quoted 4 lines · Tejun Heo

I think a better way would be adding a ptrace attach which is nestable and doesn't have the nasty side effect caused by the interactions between the implicit SIGSTOP and group stop.

Don't we already nest at least to some degree? At least you can strace a gdb session or another strace, no?

Linus

#45 lore raw

Hello, Linus.

Quoted 8 lines · Linus Torvalds
Quoted 4 lines · Tejun Heo

I think a better way would be adding a ptrace attach which is nestable and doesn't have the nasty side effect caused by the interactions between the implicit SIGSTOP and group stop.

Don't we already nest at least to some degree? At least you can strace a gdb session or another strace, no?

Yes, it depends on what the 'nesting' means. I was thinking about gcore'ing or attching gdb to an already strace'd process. This might not be strictly necessary but for ptrace attach to be transparent, we need another PTRACE_* call anyway as PTRACE_ATTACH implies SIGSTOP and having nestable attach would be beneficial in several use cases including using debugging tools nested and allowing userland checkpointing of debugging sessions.

Thanks.

Signature · 1 line
tejun
#46 lore raw
In reply to Linus Torvalds
Quoted 8 lines · On Tue, 14 Dec 2010 07:49:37 -0800 Linus Torvalds…
Quoted 6 lines · Suzuki K. Poulose

This is series of patches implementing an infrastructure for capturing the core of an application without disrupting its process semantics.

The infrastructure makes use of the freezer subsystem in kernel to freeze the threads and then collect the information to generate  the core.

Quoted 8 lines

This seems to be a fundamentally flawed approach.

From a security standpoint, it looks like a total disaster. A frozen process is really hard to get rid of, so it looks like an obvious DoS attack to just create lots of processes, then sneakily freeze them all, and then laugh at the poor system admin who has no idea what's going on. While frozen, the things are basically unkillable but look entirely normal, no?

You are right. We need a simple mechanism to hold the threads, so that we could collect the register information of the process without affecting its process semantics (eg, signals etc.). The suggestion by Kamezawa-san -a freeze state variant which allows SIGKILL - is one possibility.

I'd be very glad not using the freezer if there is a neat way to accomplish this without the undesired side effects. Tejun's ptrace enhancement would still require a userland program to control it(gcore); something contained in the kernel would be ideal.

Thanks

Suzuki

Quoted 2 lines

Linus

#47 lore raw

Hello, Suzuki.

Quoted 4 lines · Suzuki K. Poulose

I'd be very glad not using the freezer if there is a neat way to accomplish this without the undesired side effects. Tejun's ptrace enhancement would still require a userland program to control it(gcore); something contained in the kernel would be ideal.

Why is using gcore a bad thing? If we make ptrace avoid the implicit SIGSTOP, the side effects of ptrace would be the same as using freezer but with the benefit that it's properly integrated to the process model and job control.

Thanks.

Signature · 1 line
tejun
#48 lore raw
Quoted 8 lines · On Wed, 15 Dec 2010 10:37:48 +0100 Tejun Heo <[email protected]> wrote:

Hello, Suzuki.

Quoted 4 lines · Suzuki K. Poulose

I'd be very glad not using the freezer if there is a neat way to accomplish this without the undesired side effects. Tejun's ptrace enhancement would still require a userland program to control it(gcore); something contained in the kernel would be ideal.

Quoted 4 lines

Why is using gcore a bad thing? If we make ptrace avoid the implicit SIGSTOP, the side effects of ptrace would be the same as using freezer but with the benefit that it's properly integrated to the process model and job control.

The advantages of the new approach are :

1) A process can trigger a core synchronously, upon an event, say a signal handler and continue from there. gcore would require a fork(), which is not safe to use from a signal handler.

2) We can seek to only the data we need

Thanks
Suzuki

#49 lore raw
Quoted 3 lines

1) A process can trigger a core synchronously, upon an event, say a signal handler and continue from there. gcore would require a fork(), which is not safe to use from a signal handler.

Why is it not safe for a signal?

Quoted 1 line

From the kernel side it should be. glibc may do some funky things

on fork, but you could always call the syscall directly.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only.
#50 lore raw
Quoted 8 lines · On Wed, 15 Dec 2010 12:51:54 +0100 Andi Kleen <[email protected]> wrote:
Quoted 3 lines

1) A process can trigger a core synchronously, upon an event, say a signal handler and continue from there. gcore would require a fork(), which is not safe to use from a signal handler.

Why is it not safe for a signal?

From the kernel side it should be. glibc may do some funky things on fork, but you could always call the syscall directly.

I came across the following links,

http://sourceware.org/bugzilla/show_bug.cgi?id=4737
https://www.opengroup.org/austin/docs/austin_445.txt

which seem to suggest that fork() is not async-safe due to malloc and the fork handlers. But a direct fork syscall by-passing glibc should be safe.

Thanks
Suzuki

Quoted 2 lines

-Andi

#51 lore raw
In reply to Suzuki K. Poulose
Quoted 40 lines · On Tue, 14 Dec 2010 15:22:59 +0530 "Suzuki K. Poulose" <[email protected]>…

Hi all,

This is series of patches implementing an infrastructure for capturing the core of an application without disrupting its process semantics.

The infrastructure makes use of the freezer subsystem in kernel to freeze the threads and then collect the information to generate the core.

The interface is provided by a /proc/pid/core file, reading which can give the ELF formatted core of the process with "pid". The interface supports "seek" operation on the fd, allowing the dumper to have control on the data that is being dumped. Also it allows the user to store the dump at any location.

The current implementation supports both native as well as the compat ELF tasks.

An open() call to the /proc/pid/core will try to freeze the threads in the process and the read() requests will dynamically generate the contents for the core file. The ELF header & Program Headers are stored in a kernel buffer to allow us to map the fpos to the required data section.

In case a thread is not frozen within a time interval, after issuing the freeze request, we fill the register state information with 0's to indicate we could not capture the data.

A close() would kick the threads out of the refrigerator().

The implementation reuses some of the existing ELF core generation code by exporting them. Some of the code common to both native and compat ELF class support has been moved to a common place, elfcore-common.c. Also some of the reusable functions, specific to the ELF class handling, has been made global, after renaming the compat version of the same.

We also added a new API -elf_core_copy_extra_phdrs() -for "reading" the arch specific program headers, versus the existing elf_core_write_extra_phdrs().

Patches 1 to 9 deals with re-arranging the ELF code to be reusable by the infrastructure.

Quoted 7 lines

Patches 10 to 21 implements the infrastructure.

TODO: Add support for collecting the arch specific notes, currently used only by Cell platform.

Please let me know your review comments / thoughts.

Your purpose of this patch is to debug an application without attaching to gdb or take coredump by gcore ?

IIUC, "freeze" is a bit dangerous because no one can ends the application while it's freezed and there is no information "it's frozen" via usaual user commands as 'ps' or 'top'.

Can you add a new freeze state where the application can get SIGKILL, at least ? and show task's state as "frozen" in some way ? as task_state_array[] shows it in /proc/<pid>/status

Thanks,
-Kame

#52 lore raw
Quoted 45 lines · On Wed, 15 Dec 2010 10:04:37 +0900 KAMEZAWA Hiroyuki…
Quoted 42 lines · On Tue, 14 Dec 2010 15:22:59 +0530 "Suzuki K. Poulose" <[email protected]>…

Hi all,

This is series of patches implementing an infrastructure for capturing the core of an application without disrupting its process semantics.

The infrastructure makes use of the freezer subsystem in kernel to freeze the threads and then collect the information to generate the core.

The interface is provided by a /proc/pid/core file, reading which can give the ELF formatted core of the process with "pid". The interface supports "seek" operation on the fd, allowing the dumper to have control on the data that is being dumped. Also it allows the user to store the dump at any location.

The current implementation supports both native as well as the compat ELF tasks.

An open() call to the /proc/pid/core will try to freeze the threads in the process and the read() requests will dynamically generate the contents for the core file. The ELF header & Program Headers are stored in a kernel buffer to allow us to map the fpos to the required data section.

In case a thread is not frozen within a time interval, after issuing the freeze request, we fill the register state information with 0's to indicate we could not capture the data.

A close() would kick the threads out of the refrigerator().

The implementation reuses some of the existing ELF core generation code by exporting them. Some of the code common to both native and compat ELF class support has been moved to a common place, elfcore-common.c. Also some of the reusable functions, specific to the ELF class handling, has been made global, after renaming the compat version of the same.

We also added a new API -elf_core_copy_extra_phdrs() -for "reading" the arch specific program headers, versus the existing elf_core_write_extra_phdrs().

Patches 1 to 9 deals with re-arranging the ELF code to be reusable by the infrastructure.

Patches 10 to 21 implements the infrastructure.

Quoted 8 lines
Quoted 5 lines

TODO: Add support for collecting the arch specific notes, currently used only by Cell platform.

Please let me know your review comments / thoughts.

Your purpose of this patch is to debug an application without attaching to gdb or take coredump by gcore ?

The purpose is to take the coredump in a more reliable way without affecting the process semantics.

Quoted 8 lines

IIUC, "freeze" is a bit dangerous because no one can ends the application while it's freezed and there is no information "it's frozen" via usaual user commands as 'ps' or 'top'.

Can you add a new freeze state where the application can get SIGKILL, at least ? and show task's state as "frozen" in some way ? as task_state_array[] shows it in /proc/<pid>/status

I will investigate this approach.

Thanks
Suzuki