Bug 291446 - /libexec/ld-elf.so.1 not branded properly on riscv, breaks manual activation via ldd
Summary: /libexec/ld-elf.so.1 not branded properly on riscv, breaks manual activation ...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: riscv (show other bugs)
Version: 15.0-CURRENT
Hardware: riscv Any
: --- Affects Only Me
Assignee: freebsd-riscv (Team)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-12-07 01:42 UTC by Brandon Bergren
Modified: 2025-12-15 20:45 UTC (History)
3 users (show)

See Also:
jrtc27: mfc-stable15+
jrtc27: mfc-stable14+
jrtc27: mfc-stable13+
jrtc27: needs_errata?


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brandon Bergren 2025-12-07 01:42:59 UTC
After a fresh install of 15.0-RELEASE:

When manually activating rtld on risc-v (for example, when running ldd on something) the kernel is rejecting image activation, due to /libexec/ld-elf.so.1 being branded SYSV rather than FreeBSD.

Somewhat surprisingly, this doesn't break running normal dynamic executables, because normally, rtld is activated via being loaded as the interpreter, and as such is not actually checked against the brand list.

Normal programs are also being branded SYSV, but this is less of an issue for dynamically linked programs, as they will still match the branding via the interpreter path check.

Silly workaround:

cp /libexec/ld-elf.so.1 /libexec/ld-elf.so.1.patched && \
brandelf -t FreeBSD /libexec/ld-elf.so.1.patched && \
cp /libexec/ld-elf.so.1.patched /libexec/ld-elf.so.1.new &&\
chflags noschg /libexec/ld-elf.so.1 && \
/libexec/ld-elf.so.1.patched /bin/mv /libexec/ld-elf.so.1 /libexec/ld-elf.so.1.old && \
/libexec/ld-elf.so.1.patched /bin/mv /libexec/ld-elf.so.1.new /libexec/ld-elf.so.1 && \
chflags schg /libexec/ld-elf.so.1

I suppose an alternative might be to set the kern.elf64.fallback_brand sysctl, but I haven't verified that.

Ideally, the linker would be branding the ELF header automatically. This should theoretically already be happening, but maybe there's a bug in the triple-to-OSABI detection. It's been a long time since I've looked at that code.
Comment 1 Jessica Clarke freebsd_committer freebsd_triage 2025-12-07 01:59:35 UTC
This has always been the case for RISC-V, and IIRC is true of GCC+binutils too, not just Clang/LLVM+LLD. These days FreeBSD branding is supposed to come from ELF notes (the header is more about the extensions used by the ELF itself), specifically the NT_FREEBSD_ABI_TAG in crti.o, but rtld opts out of linking against all the CSU bits so doesn't get any ELF notes. I suppose the right thing to do is to add a subset of the missing ELF notes to rtld itself so direct exec works more sensibly?
Comment 2 Jessica Clarke freebsd_committer freebsd_triage 2025-12-07 02:10:15 UTC
Not sure what's going on for you. e9751a84dd2d ("Include FreeBSD ABI tag note in the ELF runtime loader.") from 2020 added branding to rtld. What specifically are you running that fails?
Comment 3 Brandon Bergren 2025-12-07 05:26:07 UTC
What's going wrong for me:

root@sifive:/usr/src/sys # ldd /usr/bin/grep
/usr/bin/grep:
ELF binary type "0" not known.
ldd: /usr/bin/grep: Exec format error
/usr/bin/grep: exit status 1
root@sifive:/usr/src/sys # cp /libexec/ld-elf.so.1 /libexec/ld-elf.so.1.patched && \
> brandelf -t FreeBSD /libexec/ld-elf.so.1.patched && \
> cp /libexec/ld-elf.so.1.patched /libexec/ld-elf.so.1.new &&\
> chflags noschg /libexec/ld-elf.so.1 && \
> /libexec/ld-elf.so.1.patched /bin/mv /libexec/ld-elf.so.1 /libexec/ld-elf.so.1.old && \
> /libexec/ld-elf.so.1.patched /bin/mv /libexec/ld-elf.so.1.new /libexec/ld-elf.so.1 && \
> chflags schg /libexec/ld-elf.so.1
root@sifive:/usr/src/sys # ldd /usr/bin/grep
/usr/bin/grep:
	libregex.so.1 => /usr/lib/libregex.so.1 (0x64eaba000)
	libc.so.7 => /lib/libc.so.7 (0x64ebe9000)
	libsys.so.7 => /lib/libsys.so.7 (0x64fb31000)
root@sifive:/usr/src/sys # 


ldd works by running the runtime linker as a program rather than an interpreter, with some extra environment set.

Anyway, I'll stare at the __elfN(get_brandinfo) code and the riscv md bits and see if I can spot why it isn't happy with things as-is.
Comment 4 Jessica Clarke freebsd_committer freebsd_triage 2025-12-07 15:24:38 UTC
Does /libexec/ld-elf.so.1 -h work? If not, what does readelf -Wn /libexec/ld-elf.so.1 say?
Comment 5 Brandon Bergren 2025-12-08 01:55:40 UTC
(In reply to Jessica Clarke from comment #4)

/libexec/ld-elf.so.1 -h does not work.

root@sifive:/usr/src # /libexec/ld-elf.so.1 -h
ELF binary type "0" not known.
-sh: /libexec/ld-elf.so.1: Exec format error

As for the readelf output:

Displaying notes found in: .note.tag
  Owner                Data size        Description
  FreeBSD              0x00000004       NT_FREEBSD_ABI_TAG (ABI version tag)
    ABI tag: 1500068

I'm also pretty sure that the note lives in the first page, which is another requirement for the tag detection IIRC.

Elf file type is DYN (Shared object file)
Entry point 0x71e0
There are 11 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000000040 0x0000000000000040 0x000268 0x000268 R   0x8
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x0061d4 0x0061d4 R   0x1000
  LOAD           0x0061e0 0x00000000000071e0 0x00000000000071e0 0x010548 0x010548 R E 0x1000
  LOAD           0x016728 0x0000000000018728 0x0000000000018728 0x000a20 0x0018d8 RW  0x1000
  LOAD           0x017148 0x000000000001a148 0x000000000001a148 0x000310 0x001108 RW  0x1000
  DYNAMIC        0x016f50 0x0000000000018f50 0x0000000000018f50 0x0000f0 0x0000f0 RW  0x8
  GNU_RELRO      0x016728 0x0000000000018728 0x0000000000018728 0x000a20 0x0018d8 R   0x1
  GNU_EH_FRAME   0x005d24 0x0000000000005d24 0x0000000000005d24 0x000154 0x000154 R   0x4
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x0
  NOTE           0x0002a8 0x00000000000002a8 0x00000000000002a8 0x000018 0x000018 R   0x4
  ATTRIBUTES     0x017458 0x0000000000000000 0x0000000000000000 0x000047 0x000047 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .note.tag .dynsym .gnu.version .gnu.version_d .gnu.hash .hash .dynstr .rela.dyn .rodata .eh_frame_hdr .eh_frame 
   02     .text 
   03     .data.rel.ro .dynamic .got .relro_padding 
   04     .data .bss 
   05     .dynamic 
   06     .data.rel.ro .dynamic .got .relro_padding 
   07     .eh_frame_hdr 
   08     
   09     .note.tag 
   10     .riscv.attributes 
   None   .comment .gnu.warning.readdir_r .gnu_debuglink .shstrtab 


And for comparison, grep:

root@sifive:/usr/src # readelf -Wn /usr/bin/grep
Displaying notes found in: .note.tag
  Owner                Data size        Description
  FreeBSD              0x00000004       NT_FREEBSD_ABI_TAG (ABI version tag)
    ABI tag: 1500068
  FreeBSD              0x00000004       NT_FREEBSD_FEATURE_CTL (FreeBSD feature control)
    Feature flags: 0x0
  FreeBSD              0x00000004       NT_FREEBSD_NOINIT_TAG (no .init tag)
   description data: 00 00 00 00


and the headers for grep:

Elf file type is DYN (Shared object file)
Entry point 0x3da0
There are 12 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000000040 0x0000000000000040 0x0002a0 0x0002a0 R   0x8
  INTERP         0x0002e0 0x00000000000002e0 0x0000000000000000 0x000015 0x000015 R   0x1
      [Requesting program interpreter: /libexec/ld-elf.so.1]
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x002d78 0x002d78 R   0x1000
  LOAD           0x002d80 0x0000000000003d80 0x0000000000003d80 0x002f40 0x002f40 R E 0x1000
  LOAD           0x005cc0 0x0000000000007cc0 0x0000000000007cc0 0x000920 0x001340 RW  0x1000
  LOAD           0x0065e0 0x00000000000095e0 0x00000000000095e0 0x000250 0x000561 RW  0x1000
  DYNAMIC        0x006290 0x0000000000008290 0x0000000000008290 0x000180 0x000180 RW  0x8
  GNU_RELRO      0x005cc0 0x0000000000007cc0 0x0000000000007cc0 0x000920 0x001340 R   0x1
  GNU_EH_FRAME   0x002920 0x0000000000002920 0x0000000000002920 0x0000d4 0x0000d4 R   0x4
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x0
  NOTE           0x0002f8 0x00000000000002f8 0x00000000000002f8 0x000048 0x000048 R   0x4
  ATTRIBUTES     0x00a014 0x0000000000000000 0x0000000000000000 0x000047 0x000047 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.tag .dynsym .gnu.version .gnu.version_r .gnu.hash .hash .dynstr .rela.dyn .rela.plt .rodata .srodata.cst8 .eh_frame_hdr .eh_frame 
   03     .text .init .fini .plt 
   04     .ctors .dtors .data.rel.ro .dynamic .got .relro_padding 
   05     .data .got.plt .sdata .sbss .bss 
   06     .dynamic 
   07     .ctors .dtors .data.rel.ro .dynamic .got .relro_padding 
   08     .eh_frame_hdr 
   09     
   10     .note.tag 
   11     .riscv.attributes 
   None   .comment .gnu_debuglink .shstrtab
Comment 6 Brandon Bergren 2025-12-08 02:20:37 UTC
Hmm, I wonder if it's the lack of a feature control note that is messing things up.
Comment 7 Jessica Clarke freebsd_committer freebsd_triage 2025-12-08 02:26:13 UTC
I remembered c86af2cc4cd1 ("imgact_elf: Check note body sizes"), which looks like it has an off-by-one to me. Well, it sort of preserves an off-by-one that was there if the data was zero length and the name size was already rounded. Effectively as of that commit it ignores the final note in a well-formed object. Can you try a kernel with the following:

diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index 779158b41221..f1d848e95f18 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -2840,7 +2840,7 @@ __elfN(parse_notes)(const struct image_params *imgp, const Elf_Note *checknote,
 			goto nextnote;
 		note_name = (const char *)(note + 1);
 		if (note_name + roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
-		    note->n_descsz >= (const char *)note_end ||
+		    note->n_descsz > (const char *)note_end ||
 		    strncmp(note_vendor, note_name, checknote->n_namesz) != 0)
 			goto nextnote;
Comment 8 Jessica Clarke freebsd_committer freebsd_triage 2025-12-08 02:28:43 UTC
Well, b1faa50c18e8f65a43c7fb4c4170a9ff47706805 for the releng/15.0 cherry-pick, as that post-dates both the stable/15 and releng/15.0 branch points.
Comment 9 Brandon Bergren 2025-12-08 02:35:20 UTC
Will check, thanks for the pointer!
Comment 10 Brandon Bergren 2025-12-08 05:27:52 UTC
That change does indeed fix the problem!
Comment 11 commit-hook freebsd_committer freebsd_triage 2025-12-08 13:32:08 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5d58198ccc2b562098ee5fc4898013622b32b065

commit 5d58198ccc2b562098ee5fc4898013622b32b065
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-12-08 13:01:57 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-12-08 13:01:57 +0000

    imgact_elf: Fix off-by-one in note size check

    Prior to c86af2cc4cd1 ("imgact_elf: Check note body sizes"), this was
    note_name + n_namesz >= note_end, which checks that there is at least
    one byte after the unpadded name (which could be either padding or
    data), and given our notes always have data with them this was fine.
    However, once we started checking the padded name (note that "FreeBSD\0"
    is already a multiple of 4 bytes, so has no padding) and data, this
    turned into checking that there is at least one byte after the unpadded
    data, and since our ELF notes already have a multiple of 4 bytes for
    their data and therefore have no padding, this means that we are now
    checking that there is at least one byte after the ELF note, which is
    not going to be the case for the last ELF note. Instead, switch this to
    a strict greater than, as should be used when comparing one-past-the-end
    pointers, which both sides of the inequality are.

    For executables, this was generally not a problem in reality, since the
    last of our ELF notes is NT_FREEBSD_NOINIT_TAG, which isn't read by the
    kernel. However, ld-elf.so.1 (and libcompat variants), like shared
    libraries, only has NT_FREEBSD_ABI_TAG, which meant the kernel did not
    see this ELF note when directly executing it (e.g. as done by ldd), and
    on RISC-V this is the only branding present, so doing so would fail with
    ENOEXEC. This does also mean on non-RISC-V direct exec ld-elf.so.1 runs
    with the wrong p_osrel, but given it sets kern.proc.osrel.PID to the
    executable's NT_FREEBSD_ABI_TAG that it loads, this probably doesn't
    matter in practice.

    PR:             291446
    Reported by:    bdragon
    Tested by:      bdragon
    Fixes:          c86af2cc4cd1 ("imgact_elf: Check note body sizes")
    MFC after:      3 days

 sys/kern/imgact_elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 12 commit-hook freebsd_committer freebsd_triage 2025-12-15 14:55:19 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=e0f70ea729c58720f565c85690de5b39a83e640d

commit e0f70ea729c58720f565c85690de5b39a83e640d
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-12-08 13:01:57 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-12-15 14:54:23 +0000

    imgact_elf: Fix off-by-one in note size check

    Prior to c86af2cc4cd1 ("imgact_elf: Check note body sizes"), this was
    note_name + n_namesz >= note_end, which checks that there is at least
    one byte after the unpadded name (which could be either padding or
    data), and given our notes always have data with them this was fine.
    However, once we started checking the padded name (note that "FreeBSD\0"
    is already a multiple of 4 bytes, so has no padding) and data, this
    turned into checking that there is at least one byte after the unpadded
    data, and since our ELF notes already have a multiple of 4 bytes for
    their data and therefore have no padding, this means that we are now
    checking that there is at least one byte after the ELF note, which is
    not going to be the case for the last ELF note. Instead, switch this to
    a strict greater than, as should be used when comparing one-past-the-end
    pointers, which both sides of the inequality are.

    For executables, this was generally not a problem in reality, since the
    last of our ELF notes is NT_FREEBSD_NOINIT_TAG, which isn't read by the
    kernel. However, ld-elf.so.1 (and libcompat variants), like shared
    libraries, only has NT_FREEBSD_ABI_TAG, which meant the kernel did not
    see this ELF note when directly executing it (e.g. as done by ldd), and
    on RISC-V this is the only branding present, so doing so would fail with
    ENOEXEC. This does also mean on non-RISC-V direct exec ld-elf.so.1 runs
    with the wrong p_osrel, but given it sets kern.proc.osrel.PID to the
    executable's NT_FREEBSD_ABI_TAG that it loads, this probably doesn't
    matter in practice.

    PR:             291446
    Reported by:    bdragon
    Tested by:      bdragon
    Fixes:          c86af2cc4cd1 ("imgact_elf: Check note body sizes")
    MFC after:      3 days

    (cherry picked from commit 5d58198ccc2b562098ee5fc4898013622b32b065)

 sys/kern/imgact_elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 13 commit-hook freebsd_committer freebsd_triage 2025-12-15 17:00:49 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=d37b3562e701ab1583ea7ae47b9bd3214ef39d37

commit d37b3562e701ab1583ea7ae47b9bd3214ef39d37
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-12-08 13:01:57 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-12-15 16:58:45 +0000

    imgact_elf: Fix off-by-one in note size check

    Prior to c86af2cc4cd1 ("imgact_elf: Check note body sizes"), this was
    note_name + n_namesz >= note_end, which checks that there is at least
    one byte after the unpadded name (which could be either padding or
    data), and given our notes always have data with them this was fine.
    However, once we started checking the padded name (note that "FreeBSD\0"
    is already a multiple of 4 bytes, so has no padding) and data, this
    turned into checking that there is at least one byte after the unpadded
    data, and since our ELF notes already have a multiple of 4 bytes for
    their data and therefore have no padding, this means that we are now
    checking that there is at least one byte after the ELF note, which is
    not going to be the case for the last ELF note. Instead, switch this to
    a strict greater than, as should be used when comparing one-past-the-end
    pointers, which both sides of the inequality are.

    For executables, this was generally not a problem in reality, since the
    last of our ELF notes is NT_FREEBSD_NOINIT_TAG, which isn't read by the
    kernel. However, ld-elf.so.1 (and libcompat variants), like shared
    libraries, only has NT_FREEBSD_ABI_TAG, which meant the kernel did not
    see this ELF note when directly executing it (e.g. as done by ldd), and
    on RISC-V this is the only branding present, so doing so would fail with
    ENOEXEC. This does also mean on non-RISC-V direct exec ld-elf.so.1 runs
    with the wrong p_osrel, but given it sets kern.proc.osrel.PID to the
    executable's NT_FREEBSD_ABI_TAG that it loads, this probably doesn't
    matter in practice.

    PR:             291446
    Reported by:    bdragon
    Tested by:      bdragon
    Fixes:          c86af2cc4cd1 ("imgact_elf: Check note body sizes")
    MFC after:      3 days

    (cherry picked from commit 5d58198ccc2b562098ee5fc4898013622b32b065)

 sys/kern/imgact_elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 14 commit-hook freebsd_committer freebsd_triage 2025-12-15 18:20:08 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=3e264f4d35863f9888f3779c64bf027e52fc9bc0

commit 3e264f4d35863f9888f3779c64bf027e52fc9bc0
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-12-08 13:01:57 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-12-15 17:56:37 +0000

    imgact_elf: Fix off-by-one in note size check

    Prior to c86af2cc4cd1 ("imgact_elf: Check note body sizes"), this was
    note_name + n_namesz >= note_end, which checks that there is at least
    one byte after the unpadded name (which could be either padding or
    data), and given our notes always have data with them this was fine.
    However, once we started checking the padded name (note that "FreeBSD\0"
    is already a multiple of 4 bytes, so has no padding) and data, this
    turned into checking that there is at least one byte after the unpadded
    data, and since our ELF notes already have a multiple of 4 bytes for
    their data and therefore have no padding, this means that we are now
    checking that there is at least one byte after the ELF note, which is
    not going to be the case for the last ELF note. Instead, switch this to
    a strict greater than, as should be used when comparing one-past-the-end
    pointers, which both sides of the inequality are.

    For executables, this was generally not a problem in reality, since the
    last of our ELF notes is NT_FREEBSD_NOINIT_TAG, which isn't read by the
    kernel. However, ld-elf.so.1 (and libcompat variants), like shared
    libraries, only has NT_FREEBSD_ABI_TAG, which meant the kernel did not
    see this ELF note when directly executing it (e.g. as done by ldd), and
    on RISC-V this is the only branding present, so doing so would fail with
    ENOEXEC. This does also mean on non-RISC-V direct exec ld-elf.so.1 runs
    with the wrong p_osrel, but given it sets kern.proc.osrel.PID to the
    executable's NT_FREEBSD_ABI_TAG that it loads, this probably doesn't
    matter in practice.

    PR:             291446
    Reported by:    bdragon
    Tested by:      bdragon
    Fixes:          c86af2cc4cd1 ("imgact_elf: Check note body sizes")
    MFC after:      3 days

    (cherry picked from commit 5d58198ccc2b562098ee5fc4898013622b32b065)

 sys/kern/imgact_elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 15 Mark Johnston freebsd_committer freebsd_triage 2025-12-15 18:27:05 UTC
Jessica, do you think this warrants an erratum notice?  That won't help 15.0 riscv users since we don't ship binary updates (I don't see any riscv pkgbase packages on pkg.freebsd.org...?), but is there some chance amd64/arm64 users will be affected?
Comment 16 Jessica Clarke freebsd_committer freebsd_triage 2025-12-15 18:50:01 UTC
For amd64 and arm64 it *shouldn't* matter, because they set EI_OSABI to ELFOSABI_FREEBSD, not ELFOSABI_NONE, so that's sufficient branding for the kernel to deem it a FreeBSD binary. Plus p_osrel being wrong probably doesn't matter (see the commit message), unless rtld-elf is doing something very early on where the differing semantics matter, which I'm not aware of. But that doesn't mean there isn't something subtly wrong going on... My bigger concern would be for other executables, whether old (I don't know whether we've ever produced binaries where the ABI tag note is last, perhaps because it was the only one in ancient versions) or produced by third-party toolchains (e.g. Go does its own thing). I could see there being a risk there that we're picking up the wrong p_osrel when running those binaries (not using rtld direct-exec mode).