2020-06-30 04:37:13

by Al Viro

[permalink] [raw]
Subject: [RFC][PATCHES] converting FDPIC coredumps to regsets

Conversion of ELF coredumps to regsets has not touched
ELF_FDPIC. Right now all architectures that support FDPIC have
regsets sufficient for switching it to regset-based coredumps. A bit
of backstory: original ELF (and ELF_FDPIC) coredumps reused the old
helpers used by a.out coredumps. These days a.out coredumps are gone;
we could remove the dead code, if not for several obstacles. And one
of those obstacles is ELF_FDPIC.

This series more or less reproduces the conversion done
by Roland for ELF coredumps. The branch is in
git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git #work.fdpic
and it's based on top of #regset.base there (just the introduction of
regset_get() wrapper for ->get(); nothing else from the regset series
is needed). Killing the old aout helpers is _not_ in this branch;
followup cleanups live separately.

First we need to sort out the mess with struct elf_prstatus,
though. It's used both for ELF and ELF_FDPIC coredumps, and it
contains a couple of fields under ifdef on CONFIG_BINFMT_ELF_FDPIC.
ELF is MMU-dependent and most, but not all configs that allow ELF_FDPIC
are non-MMU. ARM is an exception - there ELF_FDPIC is allowed both for
MMU and non-MMU configs. That's a problem - struct elf_prstatus is a
part of coredump layout, so ELF coredumps produced by arm kernels that
have ELF_FDPIC enabled are incompatible with those that have it disabled.

The obvious solution is to introduce struct elf_prstatus_fdpic
and use that in binfmt_elf_fdpic.c, taking these fields out of the
normal struct elf_prstatus. Unfortunately, the damn thing is defined in
include/uapi/linux/elfcore.h, so nominally it's a part of userland ABI.
However, not a single userland program actually includes linux/elfcore.h.
The reason is that the definition in there uses elf_gregset_t as a member,
and _that_ is not defined anywhere in the exported headers. It is defined
in (libc) sys/procfs.h, but the same file defines struct elf_prstatus
as well. So if you try to include linux/elfcore.h without having already
pulled sys/procfs.h, it'll break on incomplete type of a member. And if
you have pulled sys/procfs.h, it'll break on redefining a structure.
IOW, it's not usable and it never had been; as the matter of fact,
that's the reason sys/procfs.h had been introduced back in 1996.

1/7) unexport linux/elfcore.h
Takes it out of include/uapi/linux and moves the stuff that used
to live there into include/linux/elfcore.h

2/7) take fdpic-related parts of elf_prstatus out
Now we can take that ifdef out of the definition of elf_prstatus
(as well as compat_elf_prstatus) and put the variant with those extra
fields into binfmt_elf_fdpic.c, calling it elf_prstatus_fdpic there.

3/7) kill elf_fpxregs_t
All code dealing with it (both in elf_fdpic and non-regset side
of elf) is conditional upon ELF_CORE_COPY_XFPREGS. And no architectures
define that anymore. Take the dead code out.

4/7) [elf-fdpic] coredump: don't bother with cyclic list for per-thread
objects
5/7) [elf-fdpic] move allocation of elf_thread_status into
elf_dump_thread_status()
6/7) [elf-fdpic] use elf_dump_thread_status() for the dumper thread as well
Massaging fdpic coredump logics towards the regset side of
elf coredump.

7/7) [elf-fdpic] switch coredump to regsets
... and now we can switch from elf_core_copy_task_{,fp}regs()
to regset_get().


Diffstat:
arch/ia64/include/asm/elf.h | 2 -
arch/powerpc/include/asm/elf.h | 2 -
arch/x86/include/asm/elf.h | 2 -
fs/binfmt_elf.c | 30 ------
fs/binfmt_elf_fdpic.c | 205 ++++++++++++++++++-----------------------
include/linux/elfcore-compat.h | 4 -
include/linux/elfcore.h | 66 +++++++++++--
include/uapi/linux/elfcore.h | 101 --------------------
scripts/headers_install.sh | 1 -
usr/include/Makefile | 1 -
10 files changed, 146 insertions(+), 268 deletions(-)


2020-06-30 04:42:44

by Al Viro

[permalink] [raw]
Subject: [PATCH 1/7] unexport linux/elfcore.h

From: Al Viro <[email protected]>

It's unusable from userland - it uses elf_gregset_t, which is not
provided by exported headers. glibc has it in sys/procfs.h, but
the same file defines struct elf_prstatus, so linux/elfcore.h can't
be included once sys/procfs.h has been pulled. Same goes for uclibc
and dietlibc simply doesn't have elf_gregset_t defined anywhere.

IOW, no userland source is including that thing.

Signed-off-by: Al Viro <[email protected]>
---
include/linux/elfcore.h | 69 +++++++++++++++++++++++++++--
include/uapi/linux/elfcore.h | 101 -------------------------------------------
scripts/headers_install.sh | 1 -
usr/include/Makefile | 1 -
4 files changed, 66 insertions(+), 106 deletions(-)
delete mode 100644 include/uapi/linux/elfcore.h

diff --git a/include/linux/elfcore.h b/include/linux/elfcore.h
index 4cad0e784b28..96ab215dad2d 100644
--- a/include/linux/elfcore.h
+++ b/include/linux/elfcore.h
@@ -5,12 +5,75 @@
#include <linux/user.h>
#include <linux/bug.h>
#include <linux/sched/task_stack.h>
-
-#include <asm/elf.h>
-#include <uapi/linux/elfcore.h>
+#include <linux/types.h>
+#include <linux/signal.h>
+#include <linux/time.h>
+#include <linux/ptrace.h>
+#include <linux/fs.h>
+#include <linux/elf.h>

struct coredump_params;

+struct elf_siginfo
+{
+ int si_signo; /* signal number */
+ int si_code; /* extra code */
+ int si_errno; /* errno */
+};
+
+/*
+ * Definitions to generate Intel SVR4-like core files.
+ * These mostly have the same names as the SVR4 types with "elf_"
+ * tacked on the front to prevent clashes with linux definitions,
+ * and the typedef forms have been avoided. This is mostly like
+ * the SVR4 structure, but more Linuxy, with things that Linux does
+ * not support and which gdb doesn't really use excluded.
+ */
+struct elf_prstatus
+{
+ struct elf_siginfo pr_info; /* Info associated with signal */
+ short pr_cursig; /* Current signal */
+ unsigned long pr_sigpend; /* Set of pending signals */
+ unsigned long pr_sighold; /* Set of held signals */
+ pid_t pr_pid;
+ pid_t pr_ppid;
+ pid_t pr_pgrp;
+ pid_t pr_sid;
+ struct __kernel_old_timeval pr_utime; /* User time */
+ struct __kernel_old_timeval pr_stime; /* System time */
+ struct __kernel_old_timeval pr_cutime; /* Cumulative user time */
+ struct __kernel_old_timeval pr_cstime; /* Cumulative system time */
+ elf_gregset_t pr_reg; /* GP registers */
+#ifdef CONFIG_BINFMT_ELF_FDPIC
+ /* When using FDPIC, the loadmap addresses need to be communicated
+ * to GDB in order for GDB to do the necessary relocations. The
+ * fields (below) used to communicate this information are placed
+ * immediately after ``pr_reg'', so that the loadmap addresses may
+ * be viewed as part of the register set if so desired.
+ */
+ unsigned long pr_exec_fdpic_loadmap;
+ unsigned long pr_interp_fdpic_loadmap;
+#endif
+ int pr_fpvalid; /* True if math co-processor being used. */
+};
+
+#define ELF_PRARGSZ (80) /* Number of chars for args */
+
+struct elf_prpsinfo
+{
+ char pr_state; /* numeric process state */
+ char pr_sname; /* char for pr_state */
+ char pr_zomb; /* zombie */
+ char pr_nice; /* nice val */
+ unsigned long pr_flag; /* flags */
+ __kernel_uid_t pr_uid;
+ __kernel_gid_t pr_gid;
+ pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
+ /* Lots missing */
+ char pr_fname[16]; /* filename of executable */
+ char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
+};
+
static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
{
#ifdef ELF_CORE_COPY_REGS
diff --git a/include/uapi/linux/elfcore.h b/include/uapi/linux/elfcore.h
deleted file mode 100644
index baf03562306d..000000000000
--- a/include/uapi/linux/elfcore.h
+++ /dev/null
@@ -1,101 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
-#ifndef _UAPI_LINUX_ELFCORE_H
-#define _UAPI_LINUX_ELFCORE_H
-
-#include <linux/types.h>
-#include <linux/signal.h>
-#include <linux/time.h>
-#include <linux/ptrace.h>
-#include <linux/elf.h>
-#include <linux/fs.h>
-
-struct elf_siginfo
-{
- int si_signo; /* signal number */
- int si_code; /* extra code */
- int si_errno; /* errno */
-};
-
-
-#ifndef __KERNEL__
-typedef elf_greg_t greg_t;
-typedef elf_gregset_t gregset_t;
-typedef elf_fpregset_t fpregset_t;
-typedef elf_fpxregset_t fpxregset_t;
-#define NGREG ELF_NGREG
-#endif
-
-/*
- * Definitions to generate Intel SVR4-like core files.
- * These mostly have the same names as the SVR4 types with "elf_"
- * tacked on the front to prevent clashes with linux definitions,
- * and the typedef forms have been avoided. This is mostly like
- * the SVR4 structure, but more Linuxy, with things that Linux does
- * not support and which gdb doesn't really use excluded.
- * Fields present but not used are marked with "XXX".
- */
-struct elf_prstatus
-{
-#if 0
- long pr_flags; /* XXX Process flags */
- short pr_why; /* XXX Reason for process halt */
- short pr_what; /* XXX More detailed reason */
-#endif
- struct elf_siginfo pr_info; /* Info associated with signal */
- short pr_cursig; /* Current signal */
- unsigned long pr_sigpend; /* Set of pending signals */
- unsigned long pr_sighold; /* Set of held signals */
-#if 0
- struct sigaltstack pr_altstack; /* Alternate stack info */
- struct sigaction pr_action; /* Signal action for current sig */
-#endif
- pid_t pr_pid;
- pid_t pr_ppid;
- pid_t pr_pgrp;
- pid_t pr_sid;
- struct __kernel_old_timeval pr_utime; /* User time */
- struct __kernel_old_timeval pr_stime; /* System time */
- struct __kernel_old_timeval pr_cutime; /* Cumulative user time */
- struct __kernel_old_timeval pr_cstime; /* Cumulative system time */
-#if 0
- long pr_instr; /* Current instruction */
-#endif
- elf_gregset_t pr_reg; /* GP registers */
-#ifdef CONFIG_BINFMT_ELF_FDPIC
- /* When using FDPIC, the loadmap addresses need to be communicated
- * to GDB in order for GDB to do the necessary relocations. The
- * fields (below) used to communicate this information are placed
- * immediately after ``pr_reg'', so that the loadmap addresses may
- * be viewed as part of the register set if so desired.
- */
- unsigned long pr_exec_fdpic_loadmap;
- unsigned long pr_interp_fdpic_loadmap;
-#endif
- int pr_fpvalid; /* True if math co-processor being used. */
-};
-
-#define ELF_PRARGSZ (80) /* Number of chars for args */
-
-struct elf_prpsinfo
-{
- char pr_state; /* numeric process state */
- char pr_sname; /* char for pr_state */
- char pr_zomb; /* zombie */
- char pr_nice; /* nice val */
- unsigned long pr_flag; /* flags */
- __kernel_uid_t pr_uid;
- __kernel_gid_t pr_gid;
- pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
- /* Lots missing */
- char pr_fname[16]; /* filename of executable */
- char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
-};
-
-#ifndef __KERNEL__
-typedef struct elf_prstatus prstatus_t;
-typedef struct elf_prpsinfo prpsinfo_t;
-#define PRARGSZ ELF_PRARGSZ
-#endif
-
-
-#endif /* _UAPI_LINUX_ELFCORE_H */
diff --git a/scripts/headers_install.sh b/scripts/headers_install.sh
index 955cf3aedf21..9314247bb222 100755
--- a/scripts/headers_install.sh
+++ b/scripts/headers_install.sh
@@ -86,7 +86,6 @@ arch/x86/include/uapi/asm/auxvec.h:CONFIG_X86_64
arch/x86/include/uapi/asm/mman.h:CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
include/uapi/asm-generic/fcntl.h:CONFIG_64BIT
include/uapi/linux/atmdev.h:CONFIG_COMPAT
-include/uapi/linux/elfcore.h:CONFIG_BINFMT_ELF_FDPIC
include/uapi/linux/eventpoll.h:CONFIG_PM_SLEEP
include/uapi/linux/hw_breakpoint.h:CONFIG_HAVE_MIXED_BREAKPOINTS_REGS
include/uapi/linux/pktcdvd.h:CONFIG_CDROM_PKTCDVD_WCACHE
diff --git a/usr/include/Makefile b/usr/include/Makefile
index 55362f3ab393..f6b3c85d900e 100644
--- a/usr/include/Makefile
+++ b/usr/include/Makefile
@@ -28,7 +28,6 @@ no-header-test += linux/am437x-vpfe.h
no-header-test += linux/android/binder.h
no-header-test += linux/android/binderfs.h
no-header-test += linux/coda.h
-no-header-test += linux/elfcore.h
no-header-test += linux/errqueue.h
no-header-test += linux/fsmap.h
no-header-test += linux/hdlc/ioctl.h
--
2.11.0

2020-06-30 04:43:37

by Al Viro

[permalink] [raw]
Subject: [PATCH 4/7] [elf-fdpic] coredump: don't bother with cyclic list for per-thread objects

From: Al Viro <[email protected]>

plain single-linked list is just fine here...

Signed-off-by: Al Viro <[email protected]>
---
fs/binfmt_elf_fdpic.c | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index a6ee92137529..bcbf756fba39 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -1453,7 +1453,7 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
/* Here is the structure in which status of each thread is captured. */
struct elf_thread_status
{
- struct list_head list;
+ struct elf_thread_status *next;
struct elf_prstatus_fdpic prstatus; /* NT_PRSTATUS */
elf_fpregset_t fpu; /* NT_PRFPREG */
struct task_struct *thread;
@@ -1578,8 +1578,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
struct memelfnote *notes = NULL;
struct elf_prstatus_fdpic *prstatus = NULL; /* NT_PRSTATUS */
struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
- LIST_HEAD(thread_list);
- struct list_head *t;
+ struct elf_thread_status *thread_list = NULL;
elf_fpregset_t *fpu = NULL;
int thread_status_size = 0;
elf_addr_t *auxv;
@@ -1627,15 +1626,12 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
goto end_coredump;

tmp->thread = ct->task;
- list_add(&tmp->list, &thread_list);
+ tmp->next = thread_list;
+ thread_list = tmp;
}

- list_for_each(t, &thread_list) {
- struct elf_thread_status *tmp;
- int sz;
-
- tmp = list_entry(t, struct elf_thread_status, list);
- sz = elf_dump_thread_status(cprm->siginfo->si_signo, tmp);
+ for (tmp = thread_list; tmp; tmp = tmp->next) {
+ int sz = elf_dump_thread_status(cprm->siginfo->si_signo, tmp);
thread_status_size += sz;
}

@@ -1760,10 +1756,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
goto end_coredump;

/* write out the thread status notes section */
- list_for_each(t, &thread_list) {
- struct elf_thread_status *tmp =
- list_entry(t, struct elf_thread_status, list);
-
+ for (tmp = thread_list; tmp; tmp = tmp->next) {
for (i = 0; i < tmp->num_notes; i++)
if (!writenote(&tmp->notes[i], cprm))
goto end_coredump;
@@ -1791,10 +1784,10 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)
}

end_coredump:
- while (!list_empty(&thread_list)) {
- struct list_head *tmp = thread_list.next;
- list_del(tmp);
- kfree(list_entry(tmp, struct elf_thread_status, list));
+ while (thread_list) {
+ tmp = thread_list;
+ thread_list = thread_list->next;
+ kfree(tmp);
}
kfree(phdr4note);
kfree(elf);
--
2.11.0

2020-06-30 15:47:55

by Nicolas Pitre

[permalink] [raw]
Subject: Re: [RFC][PATCHES] converting FDPIC coredumps to regsets

On Tue, 30 Jun 2020, Al Viro wrote:

> The obvious solution is to introduce struct elf_prstatus_fdpic
> and use that in binfmt_elf_fdpic.c, taking these fields out of the
> normal struct elf_prstatus. Unfortunately, the damn thing is defined in
> include/uapi/linux/elfcore.h, so nominally it's a part of userland ABI.
> However, not a single userland program actually includes linux/elfcore.h.
> The reason is that the definition in there uses elf_gregset_t as a member,
> and _that_ is not defined anywhere in the exported headers. It is defined
> in (libc) sys/procfs.h, but the same file defines struct elf_prstatus
> as well. So if you try to include linux/elfcore.h without having already
> pulled sys/procfs.h, it'll break on incomplete type of a member. And if
> you have pulled sys/procfs.h, it'll break on redefining a structure.
> IOW, it's not usable and it never had been; as the matter of fact,
> that's the reason sys/procfs.h had been introduced back in 1996.

Huh! That's convenient alright.

Acked-by: Nicolas Pitre <[email protected]>


Nicolas

2020-07-14 17:18:36

by Eric W. Biederman

[permalink] [raw]
Subject: Re: [RFC][PATCHES] converting FDPIC coredumps to regsets

Al Viro <[email protected]> writes:

> Conversion of ELF coredumps to regsets has not touched
> ELF_FDPIC. Right now all architectures that support FDPIC have
> regsets sufficient for switching it to regset-based coredumps. A bit
> of backstory: original ELF (and ELF_FDPIC) coredumps reused the old
> helpers used by a.out coredumps. These days a.out coredumps are gone;
> we could remove the dead code, if not for several obstacles. And one
> of those obstacles is ELF_FDPIC.
>
> This series more or less reproduces the conversion done
> by Roland for ELF coredumps. The branch is in
> git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs.git #work.fdpic
> and it's based on top of #regset.base there (just the introduction of
> regset_get() wrapper for ->get(); nothing else from the regset series
> is needed). Killing the old aout helpers is _not_ in this branch;
> followup cleanups live separately.
>
> First we need to sort out the mess with struct elf_prstatus,
> though. It's used both for ELF and ELF_FDPIC coredumps, and it
> contains a couple of fields under ifdef on CONFIG_BINFMT_ELF_FDPIC.
> ELF is MMU-dependent and most, but not all configs that allow ELF_FDPIC
> are non-MMU. ARM is an exception - there ELF_FDPIC is allowed both for
> MMU and non-MMU configs. That's a problem - struct elf_prstatus is a
> part of coredump layout, so ELF coredumps produced by arm kernels that
> have ELF_FDPIC enabled are incompatible with those that have it disabled.
>
> The obvious solution is to introduce struct elf_prstatus_fdpic
> and use that in binfmt_elf_fdpic.c, taking these fields out of the
> normal struct elf_prstatus. Unfortunately, the damn thing is defined in
> include/uapi/linux/elfcore.h, so nominally it's a part of userland ABI.
> However, not a single userland program actually includes linux/elfcore.h.
> The reason is that the definition in there uses elf_gregset_t as a member,
> and _that_ is not defined anywhere in the exported headers. It is defined
> in (libc) sys/procfs.h, but the same file defines struct elf_prstatus
> as well. So if you try to include linux/elfcore.h without having already
> pulled sys/procfs.h, it'll break on incomplete type of a member. And if
> you have pulled sys/procfs.h, it'll break on redefining a structure.
> IOW, it's not usable and it never had been; as the matter of fact,
> that's the reason sys/procfs.h had been introduced back in 1996.
>
> 1/7) unexport linux/elfcore.h
> Takes it out of include/uapi/linux and moves the stuff that used
> to live there into include/linux/elfcore.h
>
> 2/7) take fdpic-related parts of elf_prstatus out
> Now we can take that ifdef out of the definition of elf_prstatus
> (as well as compat_elf_prstatus) and put the variant with those extra
> fields into binfmt_elf_fdpic.c, calling it elf_prstatus_fdpic there.
>
> 3/7) kill elf_fpxregs_t
> All code dealing with it (both in elf_fdpic and non-regset side
> of elf) is conditional upon ELF_CORE_COPY_XFPREGS. And no architectures
> define that anymore. Take the dead code out.
>
> 4/7) [elf-fdpic] coredump: don't bother with cyclic list for per-thread
> objects
> 5/7) [elf-fdpic] move allocation of elf_thread_status into
> elf_dump_thread_status()
> 6/7) [elf-fdpic] use elf_dump_thread_status() for the dumper thread as well
> Massaging fdpic coredump logics towards the regset side of
> elf coredump.
>
> 7/7) [elf-fdpic] switch coredump to regsets
> ... and now we can switch from elf_core_copy_task_{,fp}regs()
> to regset_get().

I just did a quick read through.

The KABI bits look sane, or rather pulling definitions out of the KABI
headers because they are not usable seems like a reasonable response to
a messed up situation. In the long run it would be good if we could get
some proper KABI headers for the format of coredumps.

I am a bit confused about what is happening in the cleanups, and frankly
the fault really lies with the binfmt_elf.c. As binfmt_elf.c in Linus's
tree still has a regset and a non-regset version of core dumping.

What I see happening is that you are transforming what started off
as a copy of the non-regset version of elf coredumping and transforming
it into something close to the regset version of coredumping. Which is
sensible. The fact that the elf_fdpic code continues to use the
non-regset names for the functions it calls, and does not synchronize
it's structure with the ordinary elf core dumping code may be sensible
but it is extremely confusing to follow.

As a follow up it would probably good to sort out synchronize the
elf and elf_fdpic coredumping code as much as possible, just to simplify
future maintenance.

So for as much as I could understand and verify.

Acked-by: "Eric W. Biederman" <[email protected]>

Eric