Message ID | 20250107-elf-v3-2-99cb505b1ab2@daynix.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | elf: Define note name macros | expand |
On Tue, Jan 07, 2025 at 09:45:53PM +0900, Akihiko Odaki wrote: > Use note name macros to match with the userspace's expectation. Also (and more importantly) get rid of duplicated knowledge about the mapping of note types to note names, so that elf.h is the authoritative source of this information? > > Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com> > Acked-by: Baoquan He <bhe@redhat.com> > --- > fs/binfmt_elf.c | 21 ++++++++++----------- > fs/binfmt_elf_fdpic.c | 8 ++++---- > 2 files changed, 14 insertions(+), 15 deletions(-) > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > index 106f0e8af177..5b4a92e5e508 100644 > --- a/fs/binfmt_elf.c > +++ b/fs/binfmt_elf.c [...] > @@ -1538,7 +1538,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) > do > i += 2; > while (auxv[i - 2] != AT_NULL); > - fill_note(&auxv_note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv); > + fill_note(&auxv_note, NN_AUXV, NT_AUXV, i * sizeof(elf_addr_t), auxv); > thread_status_size += notesize(&auxv_note); > > offset = sizeof(*elf); /* ELF header */ Looking at this code, it appears that the right name is explicitly taken from elf.h for a few specific notes, but for those that are specified by the arch code (e.g., in struct user_regset entries) the name is still guessed locally: static int fill_thread_core_info(...) { ... fill_note(&t->notes[note_iter], is_fpreg ? "CORE" : "LINUX", note_type, ret, data); It would be preferable to clean this up if we want elf.h to be the authoritative source for the names. It would be possible to add a .core_note_name entry in struct user_regset, and define a helper macro to populate the note type and name, something like the following: struct user_regset { ... unsigned int core_note_type; + unsigned int core_note_name; }; #define USER_REGSET_NOTE_TYPE(type) \ .core_note_type = NT_ ## type, \ .core_note_name = NN_ ## name, ...and then replace every .core_note_type assignment with an invocation of this macro. A quick git grep should easily find all the affected cases. Alternatively, as discussed in the last review round, a helper could be defined to get the name for a note type: const char *elf_note_name(int Elf32_Word n_type) { switch (n_type) { case NT_PRSTATUS: return NN_PRSTATUS; case NT_PRFPREG: return NN_PRFPREG; /* ...and all the rest..., then: */ default: WARN(); return "LINUX"; } } This avoids the caller having to specify the name explicitly, but only works if all the n_type values are unique for the note types that Linux knows about (currently true). Experimenting with this shows that GCC 11.4.0 (for example) doesn't do a very good job with this switch, though, and it requires building knowledge about irrelevant arch-specific note types into every kernel. I think that extending struct user_regset is probably the better approach -- though other people may disagree. Cheers ---Dave
On 2025/01/08 1:18, Dave Martin wrote: > On Tue, Jan 07, 2025 at 09:45:53PM +0900, Akihiko Odaki wrote: >> Use note name macros to match with the userspace's expectation. > > Also (and more importantly) get rid of duplicated knowledge about the > mapping of note types to note names, so that elf.h is the authoritative > source of this information? > >> >> Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com> >> Acked-by: Baoquan He <bhe@redhat.com> >> --- >> fs/binfmt_elf.c | 21 ++++++++++----------- >> fs/binfmt_elf_fdpic.c | 8 ++++---- >> 2 files changed, 14 insertions(+), 15 deletions(-) >> >> diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c >> index 106f0e8af177..5b4a92e5e508 100644 >> --- a/fs/binfmt_elf.c >> +++ b/fs/binfmt_elf.c > > [...] > >> @@ -1538,7 +1538,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) >> do >> i += 2; >> while (auxv[i - 2] != AT_NULL); >> - fill_note(&auxv_note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv); >> + fill_note(&auxv_note, NN_AUXV, NT_AUXV, i * sizeof(elf_addr_t), auxv); >> thread_status_size += notesize(&auxv_note); >> >> offset = sizeof(*elf); /* ELF header */ > > Looking at this code, it appears that the right name is explicitly > taken from elf.h for a few specific notes, but for those that are > specified by the arch code (e.g., in struct user_regset entries) the > name is still guessed locally: > > static int fill_thread_core_info(...) { > > ... > > fill_note(&t->notes[note_iter], is_fpreg ? "CORE" : "LINUX", > note_type, ret, data); > > > It would be preferable to clean this up if we want elf.h to be the > authoritative source for the names. If we want elf.h to be the authoritative source, yes, but I like the current form as it ensures nobody adds a note with a name different from "LINUX" and it is also simpler. There is a trade-off so I'd like to keep the current form unless anyone has a strong preference for one option. Regards, Akihiko Odaki > > It would be possible to add a .core_note_name entry in struct > user_regset, and define a helper macro to populate the note type and > name, something like the following: > > struct user_regset { > ... > unsigned int core_note_type; > + unsigned int core_note_name; > }; > > #define USER_REGSET_NOTE_TYPE(type) \ > .core_note_type = NT_ ## type, \ > .core_note_name = NN_ ## name, > > ...and then replace every .core_note_type assignment with an invocation > of this macro. A quick git grep should easily find all the affected > cases. > > > Alternatively, as discussed in the last review round, a helper could > be defined to get the name for a note type: > > const char *elf_note_name(int Elf32_Word n_type) > { > switch (n_type) { > case NT_PRSTATUS: return NN_PRSTATUS; > case NT_PRFPREG: return NN_PRFPREG; > /* ...and all the rest..., then: */ > > default: > WARN(); > return "LINUX"; > } > } > > This avoids the caller having to specify the name explicitly, but only > works if all the n_type values are unique for the note types that Linux > knows about (currently true). > > Experimenting with this shows that GCC 11.4.0 (for example) doesn't do > a very good job with this switch, though, and it requires building > knowledge about irrelevant arch-specific note types into every kernel. > I think that extending struct user_regset is probably the better > approach -- though other people may disagree. > > Cheers > ---Dave
Hi, On Wed, Jan 08, 2025 at 01:34:24PM +0900, Akihiko Odaki wrote: > On 2025/01/08 1:18, Dave Martin wrote: > > On Tue, Jan 07, 2025 at 09:45:53PM +0900, Akihiko Odaki wrote: > > > Use note name macros to match with the userspace's expectation. > > > > Also (and more importantly) get rid of duplicated knowledge about the > > mapping of note types to note names, so that elf.h is the authoritative > > source of this information? > > > > > > > > Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com> > > > Acked-by: Baoquan He <bhe@redhat.com> > > > --- > > > fs/binfmt_elf.c | 21 ++++++++++----------- > > > fs/binfmt_elf_fdpic.c | 8 ++++---- > > > 2 files changed, 14 insertions(+), 15 deletions(-) > > > > > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > > > index 106f0e8af177..5b4a92e5e508 100644 > > > --- a/fs/binfmt_elf.c > > > +++ b/fs/binfmt_elf.c > > > > [...] > > > > > @@ -1538,7 +1538,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) > > > do > > > i += 2; > > > while (auxv[i - 2] != AT_NULL); > > > - fill_note(&auxv_note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv); > > > + fill_note(&auxv_note, NN_AUXV, NT_AUXV, i * sizeof(elf_addr_t), auxv); > > > thread_status_size += notesize(&auxv_note); > > > offset = sizeof(*elf); /* ELF header */ > > > > Looking at this code, it appears that the right name is explicitly > > taken from elf.h for a few specific notes, but for those that are > > specified by the arch code (e.g., in struct user_regset entries) the > > name is still guessed locally: > > > > static int fill_thread_core_info(...) { > > > > ... > > > > fill_note(&t->notes[note_iter], is_fpreg ? "CORE" : "LINUX", > > note_type, ret, data); > > > > > > It would be preferable to clean this up if we want elf.h to be the > > authoritative source for the names. > > If we want elf.h to be the authoritative source, yes, but I like the current > form as it ensures nobody adds a note with a name different from "LINUX" and > it is also simpler. There is a trade-off so I'd like to keep the current > form unless anyone has a strong preference for one option. > > Regards, > Akihiko Odaki I can see where you're coming from here. It would be nice to at least be able to check that elf.h is consistent with the behaviour here, but you're right -- there is a tradeoff. Maybe add a comment in elf.h at the end of the block of #defines saying that new Linux-specific entries should use the name "LINUX"? Either way, I don't think it's a huge deal. If people are happy with this code as-is, then I don't have an issue with it. I might follow up with a separate patch if this series is merged, and people can consider it on its own merits (or lack thereof). Cheers ---Dave
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 106f0e8af177..5b4a92e5e508 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -762,8 +762,7 @@ static int parse_elf_property(const char *data, size_t *off, size_t datasz, } #define NOTE_DATA_SZ SZ_1K -#define GNU_PROPERTY_TYPE_0_NAME "GNU" -#define NOTE_NAME_SZ (sizeof(GNU_PROPERTY_TYPE_0_NAME)) +#define NOTE_NAME_SZ (sizeof(NN_GNU_PROPERTY_TYPE_0)) static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr, struct arch_elf_state *arch) @@ -800,7 +799,7 @@ static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr, if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 || note.nhdr.n_namesz != NOTE_NAME_SZ || strncmp(note.data + sizeof(note.nhdr), - GNU_PROPERTY_TYPE_0_NAME, n - sizeof(note.nhdr))) + NN_GNU_PROPERTY_TYPE_0, n - sizeof(note.nhdr))) return -ENOEXEC; off = round_up(sizeof(note.nhdr) + NOTE_NAME_SZ, @@ -1603,14 +1602,14 @@ static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm) do i += 2; while (auxv[i - 2] != AT_NULL); - fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv); + fill_note(note, NN_AUXV, NT_AUXV, i * sizeof(elf_addr_t), auxv); } static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata, const kernel_siginfo_t *siginfo) { copy_siginfo_to_external(csigdata, siginfo); - fill_note(note, "CORE", NT_SIGINFO, sizeof(*csigdata), csigdata); + fill_note(note, NN_SIGINFO, NT_SIGINFO, sizeof(*csigdata), csigdata); } /* @@ -1706,7 +1705,7 @@ static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm } size = name_curpos - (char *)data; - fill_note(note, "CORE", NT_FILE, size, data); + fill_note(note, NN_FILE, NT_FILE, size, data); return 0; } @@ -1767,7 +1766,7 @@ static int fill_thread_core_info(struct elf_thread_core_info *t, regset_get(t->task, &view->regsets[0], sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg); - fill_note(&t->notes[0], "CORE", NT_PRSTATUS, + fill_note(&t->notes[0], NN_PRSTATUS, NT_PRSTATUS, PRSTATUS_SIZE, &t->prstatus); info->size += notesize(&t->notes[0]); @@ -1801,7 +1800,7 @@ static int fill_thread_core_info(struct elf_thread_core_info *t, if (is_fpreg) SET_PR_FPVALID(&t->prstatus); - fill_note(&t->notes[note_iter], is_fpreg ? "CORE" : "LINUX", + fill_note(&t->notes[note_iter], is_fpreg ? NN_PRFPREG : "LINUX", note_type, ret, data); info->size += notesize(&t->notes[note_iter]); @@ -1821,7 +1820,7 @@ static int fill_thread_core_info(struct elf_thread_core_info *t, fill_prstatus(&t->prstatus.common, p, signr); elf_core_copy_task_regs(p, &t->prstatus.pr_reg); - fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), + fill_note(&t->notes[0], NN_PRSTATUS, NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus)); info->size += notesize(&t->notes[0]); @@ -1832,7 +1831,7 @@ static int fill_thread_core_info(struct elf_thread_core_info *t, } t->prstatus.pr_fpvalid = 1; - fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(*fpu), fpu); + fill_note(&t->notes[1], NN_PRFPREG, NT_PRFPREG, sizeof(*fpu), fpu); info->size += notesize(&t->notes[1]); return 1; @@ -1852,7 +1851,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs, psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL); if (!psinfo) return 0; - fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo); + fill_note(&info->psinfo, NN_PRPSINFO, NT_PRPSINFO, sizeof(*psinfo), psinfo); #ifdef CORE_DUMP_USE_REGSET view = task_user_regset_view(dump_task); diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c index f1a7c4875c4a..96bd9d2f23d7 100644 --- a/fs/binfmt_elf_fdpic.c +++ b/fs/binfmt_elf_fdpic.c @@ -1397,7 +1397,7 @@ static struct elf_thread_status *elf_dump_thread_status(long signr, struct task_ regset_get(p, &view->regsets[0], sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg); - fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), + fill_note(&t->notes[0], NN_PRSTATUS, NT_PRSTATUS, sizeof(t->prstatus), &t->prstatus); t->num_notes++; *sz += notesize(&t->notes[0]); @@ -1415,7 +1415,7 @@ static struct elf_thread_status *elf_dump_thread_status(long signr, struct task_ } if (t->prstatus.pr_fpvalid) { - fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), + fill_note(&t->notes[1], NN_PRFPREG, NT_PRFPREG, sizeof(t->fpu), &t->fpu); t->num_notes++; *sz += notesize(&t->notes[1]); @@ -1530,7 +1530,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) */ fill_psinfo(psinfo, current->group_leader, current->mm); - fill_note(&psinfo_note, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo); + fill_note(&psinfo_note, NN_PRPSINFO, NT_PRPSINFO, sizeof(*psinfo), psinfo); thread_status_size += notesize(&psinfo_note); auxv = (elf_addr_t *) current->mm->saved_auxv; @@ -1538,7 +1538,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) do i += 2; while (auxv[i - 2] != AT_NULL); - fill_note(&auxv_note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv); + fill_note(&auxv_note, NN_AUXV, NT_AUXV, i * sizeof(elf_addr_t), auxv); thread_status_size += notesize(&auxv_note); offset = sizeof(*elf); /* ELF header */