diff mbox

[5/7] proc/kcore: clean up ELF header generation

Message ID 196479b479237502d4d8b0e5a562b52205e5c374.1530904769.git.osandov@fb.com (mailing list archive)
State New, archived
Headers show

Commit Message

Omar Sandoval July 6, 2018, 7:32 p.m. UTC
From: Omar Sandoval <osandov@fb.com>

Currently, the ELF file header, program headers, and note segment are
allocated all at once, in some icky code dating back to 2.3. Programs
tend to read the file header, then the program headers, then the note
segment, all separately, so this is a waste of effort. It's cleaner and
more efficient to handle the three separately.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 fs/proc/kcore.c | 346 +++++++++++++++++++-----------------------------
 1 file changed, 139 insertions(+), 207 deletions(-)

Comments

kernel test robot July 7, 2018, 10:05 a.m. UTC | #1
Hi Omar,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v4.18-rc3 next-20180706]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Omar-Sandoval/proc-kcore-improvements/20180707-052548
reproduce:
        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__


sparse warnings: (new ones prefixed by >>)

   include/linux/nodemask.h:265:16: sparse: expression using sizeof(void)
   include/linux/nodemask.h:271:16: sparse: expression using sizeof(void)
>> fs//proc/kcore.c:328:23: sparse: expression using sizeof(void)
>> fs//proc/kcore.c:328:23: sparse: expression using sizeof(void)
   fs//proc/kcore.c:368:23: sparse: expression using sizeof(void)
   fs//proc/kcore.c:368:23: sparse: expression using sizeof(void)
>> fs//proc/kcore.c:384:49: sparse: missing braces around initializer
   fs//proc/kcore.c:408:23: sparse: expression using sizeof(void)
   fs//proc/kcore.c:408:23: sparse: expression using sizeof(void)

vim +328 fs//proc/kcore.c

   285	
   286	static ssize_t
   287	read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
   288	{
   289		char *buf = file->private_data;
   290		size_t phdrs_offset, notes_offset, data_offset;
   291		size_t phdrs_len, notes_len;
   292		struct kcore_list *m;
   293		size_t tsz;
   294		int nphdr;
   295		unsigned long start;
   296		size_t orig_buflen = buflen;
   297		int ret = 0;
   298	
   299		down_read(&kclist_lock);
   300	
   301		get_kcore_size(&nphdr, &phdrs_len, &notes_len, &data_offset);
   302		phdrs_offset = sizeof(struct elfhdr);
   303		notes_offset = phdrs_offset + phdrs_len;
   304	
   305		/* ELF file header. */
   306		if (buflen && *fpos < sizeof(struct elfhdr)) {
   307			struct elfhdr ehdr = {
   308				.e_ident = {
   309					[EI_MAG0] = ELFMAG0,
   310					[EI_MAG1] = ELFMAG1,
   311					[EI_MAG2] = ELFMAG2,
   312					[EI_MAG3] = ELFMAG3,
   313					[EI_CLASS] = ELF_CLASS,
   314					[EI_DATA] = ELF_DATA,
   315					[EI_VERSION] = EV_CURRENT,
   316					[EI_OSABI] = ELF_OSABI,
   317				},
   318				.e_type = ET_CORE,
   319				.e_machine = ELF_ARCH,
   320				.e_version = EV_CURRENT,
   321				.e_phoff = sizeof(struct elfhdr),
   322				.e_flags = ELF_CORE_EFLAGS,
   323				.e_ehsize = sizeof(struct elfhdr),
   324				.e_phentsize = sizeof(struct elf_phdr),
   325				.e_phnum = nphdr,
   326			};
   327	
 > 328			tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos);
   329			if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) {
   330				ret = -EFAULT;
   331				goto out;
   332			}
   333	
   334			buffer += tsz;
   335			buflen -= tsz;
   336			*fpos += tsz;
   337		}
   338	
   339		/* ELF program headers. */
   340		if (buflen && *fpos < phdrs_offset + phdrs_len) {
   341			struct elf_phdr *phdrs, *phdr;
   342	
   343			phdrs = kzalloc(phdrs_len, GFP_KERNEL);
   344			if (!phdrs) {
   345				ret = -ENOMEM;
   346				goto out;
   347			}
   348	
   349			phdrs[0].p_type = PT_NOTE;
   350			phdrs[0].p_offset = notes_offset;
   351			phdrs[0].p_filesz = notes_len;
   352	
   353			phdr = &phdrs[1];
   354			list_for_each_entry(m, &kclist_head, list) {
   355				phdr->p_type = PT_LOAD;
   356				phdr->p_flags = PF_R | PF_W | PF_X;
   357				phdr->p_offset = kc_vaddr_to_offset(m->addr) + data_offset;
   358				phdr->p_vaddr = (size_t)m->addr;
   359				if (m->type == KCORE_RAM || m->type == KCORE_TEXT)
   360					phdr->p_paddr = __pa(m->addr);
   361				else
   362					phdr->p_paddr = (elf_addr_t)-1;
   363				phdr->p_filesz = phdr->p_memsz = m->size;
   364				phdr->p_align = PAGE_SIZE;
   365				phdr++;
   366			}
   367	
   368			tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos);
   369			if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset,
   370					 tsz)) {
   371				kfree(phdrs);
   372				ret = -EFAULT;
   373				goto out;
   374			}
   375			kfree(phdrs);
   376	
   377			buffer += tsz;
   378			buflen -= tsz;
   379			*fpos += tsz;
   380		}
   381	
   382		/* ELF note segment. */
   383		if (buflen && *fpos < notes_offset + notes_len) {
 > 384			struct elf_prstatus prstatus = {0};
   385			struct elf_prpsinfo prpsinfo = {
   386				.pr_sname = 'R',
   387				.pr_fname = "vmlinux",
   388			};
   389			char *notes;
   390			size_t i = 0;
   391	
   392			strlcpy(prpsinfo.pr_psargs, saved_command_line,
   393				sizeof(prpsinfo.pr_psargs));
   394	
   395			notes = kzalloc(notes_len, GFP_KERNEL);
   396			if (!notes) {
   397				ret = -ENOMEM;
   398				goto out;
   399			}
   400	
   401			append_kcore_note(notes, &i, CORE_STR, NT_PRSTATUS, &prstatus,
   402					  sizeof(prstatus));
   403			append_kcore_note(notes, &i, CORE_STR, NT_PRPSINFO, &prpsinfo,
   404					  sizeof(prpsinfo));
   405			append_kcore_note(notes, &i, CORE_STR, NT_TASKSTRUCT, current,
   406					  arch_task_struct_size);
   407	
   408			tsz = min_t(size_t, buflen, notes_offset + notes_len - *fpos);
   409			if (copy_to_user(buffer, notes + *fpos - notes_offset, tsz)) {
   410				kfree(notes);
   411				ret = -EFAULT;
   412				goto out;
   413			}
   414			kfree(notes);
   415	
   416			buffer += tsz;
   417			buflen -= tsz;
   418			*fpos += tsz;
   419		}
   420	
   421		/*
   422		 * Check to see if our file offset matches with any of
   423		 * the addresses in the elf_phdr on our list.
   424		 */
   425		start = kc_offset_to_vaddr(*fpos - data_offset);
   426		if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
   427			tsz = buflen;
   428	
   429		while (buflen) {
   430			list_for_each_entry(m, &kclist_head, list) {
   431				if (start >= m->addr && start < (m->addr+m->size))
   432					break;
   433			}
   434	
   435			if (&m->list == &kclist_head) {
   436				if (clear_user(buffer, tsz)) {
   437					ret = -EFAULT;
   438					goto out;
   439				}
   440			} else if (m->type == KCORE_VMALLOC) {
   441				vread(buf, (char *)start, tsz);
   442				/* we have to zero-fill user buffer even if no read */
   443				if (copy_to_user(buffer, buf, tsz)) {
   444					ret = -EFAULT;
   445					goto out;
   446				}
   447			} else if (m->type == KCORE_USER) {
   448				/* User page is handled prior to normal kernel page: */
   449				if (copy_to_user(buffer, (char *)start, tsz)) {
   450					ret = -EFAULT;
   451					goto out;
   452				}
   453			} else {
   454				if (kern_addr_valid(start)) {
   455					/*
   456					 * Using bounce buffer to bypass the
   457					 * hardened user copy kernel text checks.
   458					 */
   459					if (probe_kernel_read(buf, (void *) start, tsz)) {
   460						if (clear_user(buffer, tsz)) {
   461							ret = -EFAULT;
   462							goto out;
   463						}
   464					} else {
   465						if (copy_to_user(buffer, buf, tsz)) {
   466							ret = -EFAULT;
   467							goto out;
   468						}
   469					}
   470				} else {
   471					if (clear_user(buffer, tsz)) {
   472						ret = -EFAULT;
   473						goto out;
   474					}
   475				}
   476			}
   477			buflen -= tsz;
   478			*fpos += tsz;
   479			buffer += tsz;
   480			start += tsz;
   481			tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen);
   482		}
   483	
   484	out:
   485		up_write(&kclist_lock);
   486		if (ret)
   487			return ret;
   488		return orig_buflen - buflen;
   489	}
   490	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
Omar Sandoval July 7, 2018, 9:28 p.m. UTC | #2
On Sat, Jul 07, 2018 at 06:05:17PM +0800, kbuild test robot wrote:
> Hi Omar,
> 
> Thank you for the patch! Perhaps something to improve:
> 
> [auto build test WARNING on linus/master]
> [also build test WARNING on v4.18-rc3 next-20180706]
> [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
> 
> url:    https://github.com/0day-ci/linux/commits/Omar-Sandoval/proc-kcore-improvements/20180707-052548
> reproduce:
>         # apt-get install sparse
>         make ARCH=x86_64 allmodconfig
>         make C=1 CF=-D__CHECK_ENDIAN__
> 
> 
> sparse warnings: (new ones prefixed by >>)
> 
>    include/linux/nodemask.h:265:16: sparse: expression using sizeof(void)
>    include/linux/nodemask.h:271:16: sparse: expression using sizeof(void)
> >> fs//proc/kcore.c:328:23: sparse: expression using sizeof(void)
> >> fs//proc/kcore.c:328:23: sparse: expression using sizeof(void)

Not sure why this confuses sparse. Maybe because it's #define elfhdr
elf64_hdr.

>    fs//proc/kcore.c:368:23: sparse: expression using sizeof(void)
>    fs//proc/kcore.c:368:23: sparse: expression using sizeof(void)
> >> fs//proc/kcore.c:384:49: sparse: missing braces around initializer

This is

>  > 384			struct elf_prstatus prstatus = {0};

GCC doesn't complain, but I guess I can change it to "= {};", which
isn't strict C89 but both GCC and sparse are happy with.
diff mbox

Patch

diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
index b7ff2e2ec350..de225b61f34f 100644
--- a/fs/proc/kcore.c
+++ b/fs/proc/kcore.c
@@ -49,15 +49,6 @@  static struct proc_dir_entry *proc_root_kcore;
 #define	kc_offset_to_vaddr(o) ((o) + PAGE_OFFSET)
 #endif
 
-/* An ELF note in memory */
-struct memelfnote
-{
-	const char *name;
-	int type;
-	unsigned int datasz;
-	void *data;
-};
-
 static LIST_HEAD(kclist_head);
 static DECLARE_RWSEM(kclist_lock);
 static atomic_t kcore_need_update = ATOMIC_INIT(1);
@@ -73,7 +64,8 @@  kclist_add(struct kcore_list *new, void *addr, size_t size, int type)
 	list_add_tail(&new->list, &kclist_head);
 }
 
-static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
+static size_t get_kcore_size(int *nphdr, size_t *phdrs_len, size_t *notes_len,
+			     size_t *data_offset)
 {
 	size_t try, size;
 	struct kcore_list *m;
@@ -87,15 +79,15 @@  static size_t get_kcore_size(int *nphdr, size_t *elf_buflen)
 			size = try;
 		*nphdr = *nphdr + 1;
 	}
-	*elf_buflen =	sizeof(struct elfhdr) + 
-			(*nphdr + 2)*sizeof(struct elf_phdr) + 
-			3 * ((sizeof(struct elf_note)) +
-			     roundup(sizeof(CORE_STR), 4)) +
-			roundup(sizeof(struct elf_prstatus), 4) +
-			roundup(sizeof(struct elf_prpsinfo), 4) +
-			roundup(arch_task_struct_size, 4);
-	*elf_buflen = PAGE_ALIGN(*elf_buflen);
-	return size + *elf_buflen;
+
+	*phdrs_len = *nphdr * sizeof(struct elf_phdr);
+	*notes_len = (3 * (sizeof(struct elf_note) + ALIGN(sizeof(CORE_STR), 4)) +
+		      ALIGN(sizeof(struct elf_prstatus), 4) +
+		      ALIGN(sizeof(struct elf_prpsinfo), 4) +
+		      ALIGN(arch_task_struct_size, 4));
+	*data_offset = PAGE_ALIGN(sizeof(struct elfhdr) + *phdrs_len +
+				  *notes_len);
+	return *data_offset + size;
 }
 
 #ifdef CONFIG_HIGHMEM
@@ -241,7 +233,7 @@  static int kcore_update_ram(void)
 	LIST_HEAD(list);
 	LIST_HEAD(garbage);
 	int nphdr;
-	size_t size;
+	size_t phdrs_len, notes_len, data_offset;
 	struct kcore_list *tmp, *pos;
 	int ret = 0;
 
@@ -263,7 +255,8 @@  static int kcore_update_ram(void)
 	}
 	list_splice_tail(&list, &kclist_head);
 
-	proc_root_kcore->size = get_kcore_size(&nphdr, &size);
+	proc_root_kcore->size = get_kcore_size(&nphdr, &phdrs_len, &notes_len,
+					       &data_offset);
 
 out:
 	up_write(&kclist_lock);
@@ -274,226 +267,166 @@  static int kcore_update_ram(void)
 	return ret;
 }
 
-/*****************************************************************************/
-/*
- * determine size of ELF note
- */
-static int notesize(struct memelfnote *en)
+static void append_kcore_note(char *notes, size_t *i, const char *name,
+			      unsigned int type, const void *desc,
+			      size_t descsz)
 {
-	int sz;
-
-	sz = sizeof(struct elf_note);
-	sz += roundup((strlen(en->name) + 1), 4);
-	sz += roundup(en->datasz, 4);
-
-	return sz;
-} /* end notesize() */
-
-/*****************************************************************************/
-/*
- * store a note in the header buffer
- */
-static char *storenote(struct memelfnote *men, char *bufp)
-{
-	struct elf_note en;
-
-#define DUMP_WRITE(addr,nr) do { memcpy(bufp,addr,nr); bufp += nr; } while(0)
-
-	en.n_namesz = strlen(men->name) + 1;
-	en.n_descsz = men->datasz;
-	en.n_type = men->type;
-
-	DUMP_WRITE(&en, sizeof(en));
-	DUMP_WRITE(men->name, en.n_namesz);
-
-	/* XXX - cast from long long to long to avoid need for libgcc.a */
-	bufp = (char*) roundup((unsigned long)bufp,4);
-	DUMP_WRITE(men->data, men->datasz);
-	bufp = (char*) roundup((unsigned long)bufp,4);
-
-#undef DUMP_WRITE
-
-	return bufp;
-} /* end storenote() */
-
-/*
- * store an ELF coredump header in the supplied buffer
- * nphdr is the number of elf_phdr to insert
- */
-static void elf_kcore_store_hdr(char *bufp, int nphdr, int dataoff)
-{
-	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
-	struct elf_prpsinfo prpsinfo;	/* NT_PRPSINFO */
-	struct elf_phdr *nhdr, *phdr;
-	struct elfhdr *elf;
-	struct memelfnote notes[3];
-	off_t offset = 0;
-	struct kcore_list *m;
-
-	/* setup ELF header */
-	elf = (struct elfhdr *) bufp;
-	bufp += sizeof(struct elfhdr);
-	offset += sizeof(struct elfhdr);
-	memcpy(elf->e_ident, ELFMAG, SELFMAG);
-	elf->e_ident[EI_CLASS]	= ELF_CLASS;
-	elf->e_ident[EI_DATA]	= ELF_DATA;
-	elf->e_ident[EI_VERSION]= EV_CURRENT;
-	elf->e_ident[EI_OSABI] = ELF_OSABI;
-	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
-	elf->e_type	= ET_CORE;
-	elf->e_machine	= ELF_ARCH;
-	elf->e_version	= EV_CURRENT;
-	elf->e_entry	= 0;
-	elf->e_phoff	= sizeof(struct elfhdr);
-	elf->e_shoff	= 0;
-	elf->e_flags	= ELF_CORE_EFLAGS;
-	elf->e_ehsize	= sizeof(struct elfhdr);
-	elf->e_phentsize= sizeof(struct elf_phdr);
-	elf->e_phnum	= nphdr;
-	elf->e_shentsize= 0;
-	elf->e_shnum	= 0;
-	elf->e_shstrndx	= 0;
-
-	/* setup ELF PT_NOTE program header */
-	nhdr = (struct elf_phdr *) bufp;
-	bufp += sizeof(struct elf_phdr);
-	offset += sizeof(struct elf_phdr);
-	nhdr->p_type	= PT_NOTE;
-	nhdr->p_offset	= 0;
-	nhdr->p_vaddr	= 0;
-	nhdr->p_paddr	= 0;
-	nhdr->p_filesz	= 0;
-	nhdr->p_memsz	= 0;
-	nhdr->p_flags	= 0;
-	nhdr->p_align	= 0;
-
-	/* setup ELF PT_LOAD program header for every area */
-	list_for_each_entry(m, &kclist_head, list) {
-		phdr = (struct elf_phdr *) bufp;
-		bufp += sizeof(struct elf_phdr);
-		offset += sizeof(struct elf_phdr);
-
-		phdr->p_type	= PT_LOAD;
-		phdr->p_flags	= PF_R|PF_W|PF_X;
-		phdr->p_offset	= kc_vaddr_to_offset(m->addr) + dataoff;
-		phdr->p_vaddr	= (size_t)m->addr;
-		if (m->type == KCORE_RAM || m->type == KCORE_TEXT)
-			phdr->p_paddr	= __pa(m->addr);
-		else
-			phdr->p_paddr	= (elf_addr_t)-1;
-		phdr->p_filesz	= phdr->p_memsz	= m->size;
-		phdr->p_align	= PAGE_SIZE;
-	}
-
-	/*
-	 * Set up the notes in similar form to SVR4 core dumps made
-	 * with info from their /proc.
-	 */
-	nhdr->p_offset	= offset;
-
-	/* set up the process status */
-	notes[0].name = CORE_STR;
-	notes[0].type = NT_PRSTATUS;
-	notes[0].datasz = sizeof(struct elf_prstatus);
-	notes[0].data = &prstatus;
-
-	memset(&prstatus, 0, sizeof(struct elf_prstatus));
-
-	nhdr->p_filesz	= notesize(&notes[0]);
-	bufp = storenote(&notes[0], bufp);
-
-	/* set up the process info */
-	notes[1].name	= CORE_STR;
-	notes[1].type	= NT_PRPSINFO;
-	notes[1].datasz	= sizeof(struct elf_prpsinfo);
-	notes[1].data	= &prpsinfo;
-
-	memset(&prpsinfo, 0, sizeof(struct elf_prpsinfo));
-	prpsinfo.pr_state	= 0;
-	prpsinfo.pr_sname	= 'R';
-	prpsinfo.pr_zomb	= 0;
-
-	strcpy(prpsinfo.pr_fname, "vmlinux");
-	strlcpy(prpsinfo.pr_psargs, saved_command_line, sizeof(prpsinfo.pr_psargs));
-
-	nhdr->p_filesz	+= notesize(&notes[1]);
-	bufp = storenote(&notes[1], bufp);
-
-	/* set up the task structure */
-	notes[2].name	= CORE_STR;
-	notes[2].type	= NT_TASKSTRUCT;
-	notes[2].datasz	= arch_task_struct_size;
-	notes[2].data	= current;
-
-	nhdr->p_filesz	+= notesize(&notes[2]);
-	bufp = storenote(&notes[2], bufp);
-
-} /* end elf_kcore_store_hdr() */
+	struct elf_note *note = (struct elf_note *)&notes[*i];
+
+	note->n_namesz = strlen(name) + 1;
+	note->n_descsz = descsz;
+	note->n_type = type;
+	*i += sizeof(*note);
+	memcpy(&notes[*i], name, note->n_namesz);
+	*i = ALIGN(*i + note->n_namesz, 4);
+	memcpy(&notes[*i], desc, descsz);
+	*i = ALIGN(*i + descsz, 4);
+}
 
-/*****************************************************************************/
-/*
- * read from the ELF header and then kernel memory
- */
 static ssize_t
 read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
 {
 	char *buf = file->private_data;
-	size_t size, tsz;
-	size_t elf_buflen;
+	size_t phdrs_offset, notes_offset, data_offset;
+	size_t phdrs_len, notes_len;
+	struct kcore_list *m;
+	size_t tsz;
 	int nphdr;
 	unsigned long start;
 	size_t orig_buflen = buflen;
 	int ret = 0;
 
 	down_read(&kclist_lock);
-	size = get_kcore_size(&nphdr, &elf_buflen);
 
-	if (buflen == 0 || *fpos >= size)
-		goto out;
+	get_kcore_size(&nphdr, &phdrs_len, &notes_len, &data_offset);
+	phdrs_offset = sizeof(struct elfhdr);
+	notes_offset = phdrs_offset + phdrs_len;
+
+	/* ELF file header. */
+	if (buflen && *fpos < sizeof(struct elfhdr)) {
+		struct elfhdr ehdr = {
+			.e_ident = {
+				[EI_MAG0] = ELFMAG0,
+				[EI_MAG1] = ELFMAG1,
+				[EI_MAG2] = ELFMAG2,
+				[EI_MAG3] = ELFMAG3,
+				[EI_CLASS] = ELF_CLASS,
+				[EI_DATA] = ELF_DATA,
+				[EI_VERSION] = EV_CURRENT,
+				[EI_OSABI] = ELF_OSABI,
+			},
+			.e_type = ET_CORE,
+			.e_machine = ELF_ARCH,
+			.e_version = EV_CURRENT,
+			.e_phoff = sizeof(struct elfhdr),
+			.e_flags = ELF_CORE_EFLAGS,
+			.e_ehsize = sizeof(struct elfhdr),
+			.e_phentsize = sizeof(struct elf_phdr),
+			.e_phnum = nphdr,
+		};
+
+		tsz = min_t(size_t, buflen, sizeof(struct elfhdr) - *fpos);
+		if (copy_to_user(buffer, (char *)&ehdr + *fpos, tsz)) {
+			ret = -EFAULT;
+			goto out;
+		}
 
-	/* trim buflen to not go beyond EOF */
-	if (buflen > size - *fpos)
-		buflen = size - *fpos;
+		buffer += tsz;
+		buflen -= tsz;
+		*fpos += tsz;
+	}
 
-	/* construct an ELF core header if we'll need some of it */
-	if (*fpos < elf_buflen) {
-		char * elf_buf;
+	/* ELF program headers. */
+	if (buflen && *fpos < phdrs_offset + phdrs_len) {
+		struct elf_phdr *phdrs, *phdr;
 
-		tsz = elf_buflen - *fpos;
-		if (buflen < tsz)
-			tsz = buflen;
-		elf_buf = kzalloc(elf_buflen, GFP_KERNEL);
-		if (!elf_buf) {
+		phdrs = kzalloc(phdrs_len, GFP_KERNEL);
+		if (!phdrs) {
 			ret = -ENOMEM;
 			goto out;
 		}
-		elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen);
-		if (copy_to_user(buffer, elf_buf + *fpos, tsz)) {
-			kfree(elf_buf);
+
+		phdrs[0].p_type = PT_NOTE;
+		phdrs[0].p_offset = notes_offset;
+		phdrs[0].p_filesz = notes_len;
+
+		phdr = &phdrs[1];
+		list_for_each_entry(m, &kclist_head, list) {
+			phdr->p_type = PT_LOAD;
+			phdr->p_flags = PF_R | PF_W | PF_X;
+			phdr->p_offset = kc_vaddr_to_offset(m->addr) + data_offset;
+			phdr->p_vaddr = (size_t)m->addr;
+			if (m->type == KCORE_RAM || m->type == KCORE_TEXT)
+				phdr->p_paddr = __pa(m->addr);
+			else
+				phdr->p_paddr = (elf_addr_t)-1;
+			phdr->p_filesz = phdr->p_memsz = m->size;
+			phdr->p_align = PAGE_SIZE;
+			phdr++;
+		}
+
+		tsz = min_t(size_t, buflen, phdrs_offset + phdrs_len - *fpos);
+		if (copy_to_user(buffer, (char *)phdrs + *fpos - phdrs_offset,
+				 tsz)) {
+			kfree(phdrs);
 			ret = -EFAULT;
 			goto out;
 		}
-		kfree(elf_buf);
+		kfree(phdrs);
+
+		buffer += tsz;
 		buflen -= tsz;
 		*fpos += tsz;
-		buffer += tsz;
+	}
+
+	/* ELF note segment. */
+	if (buflen && *fpos < notes_offset + notes_len) {
+		struct elf_prstatus prstatus = {0};
+		struct elf_prpsinfo prpsinfo = {
+			.pr_sname = 'R',
+			.pr_fname = "vmlinux",
+		};
+		char *notes;
+		size_t i = 0;
+
+		strlcpy(prpsinfo.pr_psargs, saved_command_line,
+			sizeof(prpsinfo.pr_psargs));
+
+		notes = kzalloc(notes_len, GFP_KERNEL);
+		if (!notes) {
+			ret = -ENOMEM;
+			goto out;
+		}
+
+		append_kcore_note(notes, &i, CORE_STR, NT_PRSTATUS, &prstatus,
+				  sizeof(prstatus));
+		append_kcore_note(notes, &i, CORE_STR, NT_PRPSINFO, &prpsinfo,
+				  sizeof(prpsinfo));
+		append_kcore_note(notes, &i, CORE_STR, NT_TASKSTRUCT, current,
+				  arch_task_struct_size);
 
-		/* leave now if filled buffer already */
-		if (buflen == 0)
+		tsz = min_t(size_t, buflen, notes_offset + notes_len - *fpos);
+		if (copy_to_user(buffer, notes + *fpos - notes_offset, tsz)) {
+			kfree(notes);
+			ret = -EFAULT;
 			goto out;
+		}
+		kfree(notes);
+
+		buffer += tsz;
+		buflen -= tsz;
+		*fpos += tsz;
 	}
 
 	/*
 	 * Check to see if our file offset matches with any of
 	 * the addresses in the elf_phdr on our list.
 	 */
-	start = kc_offset_to_vaddr(*fpos - elf_buflen);
+	start = kc_offset_to_vaddr(*fpos - data_offset);
 	if ((tsz = (PAGE_SIZE - (start & ~PAGE_MASK))) > buflen)
 		tsz = buflen;
-		
-	while (buflen) {
-		struct kcore_list *m;
 
+	while (buflen) {
 		list_for_each_entry(m, &kclist_head, list) {
 			if (start >= m->addr && start < (m->addr+m->size))
 				break;
@@ -555,7 +488,6 @@  read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
 	return orig_buflen - buflen;
 }
 
-
 static int open_kcore(struct inode *inode, struct file *filp)
 {
 	if (!capable(CAP_SYS_RAWIO))