Bug 275393 - RTLD_DEEPBIND semantics not quite right
Summary: RTLD_DEEPBIND semantics not quite right
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: Unspecified
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-11-27 21:42 UTC by Kyle Evans
Modified: 2023-12-15 18:58 UTC (History)
1 user (show)

See Also:
kevans: mfc-stable14+
kevans: mfc-stable13+
kevans: mfc-stable12-


Attachments
Minimal reproducer (5.00 KB, application/x-tar)
2023-11-27 21:42 UTC, Kyle Evans
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kyle Evans freebsd_committer freebsd_triage 2023-11-27 21:42:56 UTC
Created attachment 246617 [details]
Minimal reproducer

Consider the attached example, with a build.sh to build it:

libutil provides get_value, set_value -- it's built into both libutil.so and libutil2.so, two separate libraries with purposefully conflicting symbols

libfoo provides print_value (which calls get_value) and links against libutil2

app links against libutil and dlopens $ORIGIN/libfoo.so with RTLD_DEEPBIND and calls set_value(5) to get a different value for get_value()

On Linux, this prints:

% ./app
application value 5
library value: 10

On FreeBSD:

$ ./app  
application value 5
library value: 5

So under glibc, symbol_lookup from the dlopened image prefers symbols from the same chain of objects that were loaded at that dlopen time, not just the same image.
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2023-11-29 20:59:37 UTC
https://reviews.freebsd.org/D42841

Could you, please, convert the reproducer into a test case?
Comment 2 Kyle Evans freebsd_committer freebsd_triage 2023-11-29 21:24:13 UTC
(In reply to Konstantin Belousov from comment #1)

Thanks! Yes, I will go ahead and turn this into a proper test-case.
Comment 3 Kyle Evans freebsd_committer freebsd_triage 2023-11-29 22:16:50 UTC
I created https://reviews.freebsd.org/D42843 and threw it on the same stack for discoverability.
Comment 4 commit-hook freebsd_committer freebsd_triage 2023-11-30 19:36:10 UTC
A commit in branch main references this bug:

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

commit 9daf6cd0f46416d9c6eb0411ea6042cd42b8a9bc
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-11-29 18:30:59 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-11-30 19:34:36 +0000

    RTLD_DEEPBIND: make lookup not just symbolic, but walk all refobj' DAGs

    before starting the walk over the global list.  Effectively we visit
    needed objects first as well, instead of just the object itself.
    This seems to better match the semantic offered by the glibc flag.

    Reported by:    kevans
    PR:     275393
    Reviewed by:    kevans
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D42841

 libexec/rtld-elf/rtld.c | 8 ++++++--
 libexec/rtld-elf/rtld.h | 1 +
 2 files changed, 7 insertions(+), 2 deletions(-)
Comment 5 commit-hook freebsd_committer freebsd_triage 2023-12-01 01:27:13 UTC
A commit in branch main references this bug:

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

commit d9c543b6b0cabea6e6679d70b4e701018e7eab80
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-12-01 01:26:09 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-12-01 01:26:09 +0000

    rtld: add a test for RTLD_DEEPBIND

    This tests that with RTLD_DEEPBIND, symbols are looked up in all of the
    object's needed objects before the global object.

    PR:             275393
    Reviewed by:    kib
    Sponsored by:   NetApp, Inc.
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D42843

 etc/mtree/BSD.tests.dist                           |  2 +
 libexec/rtld-elf/tests/Makefile                    |  5 +-
 libexec/rtld-elf/tests/Makefile.inc (new)          |  3 +
 libexec/rtld-elf/tests/libdeep/Makefile (new)      | 15 +++++
 libexec/rtld-elf/tests/libdeep/libdeep.c (new)     | 28 ++++++++++
 libexec/rtld-elf/tests/libval/Makefile (new)       | 10 ++++
 libexec/rtld-elf/tests/libval/libval.c (new)       | 26 +++++++++
 libexec/rtld-elf/tests/libval2/Makefile (new)      |  8 +++
 .../rtld-elf/tests/rtld_deepbind/Makefile (new)    | 10 ++++
 .../tests/rtld_deepbind/rtld_deepbind.c (new)      | 65 ++++++++++++++++++++++
 libexec/rtld-elf/tests/target/Makefile             |  2 +-
 11 files changed, 172 insertions(+), 2 deletions(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2023-12-07 00:28:21 UTC
A commit in branch stable/14 references this bug:

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

commit 8d0740e8b9dd05513ef23e2dee7194f32551df13
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-11-29 18:30:59 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-12-07 00:27:19 +0000

    RTLD_DEEPBIND: make lookup not just symbolic, but walk all refobj' DAGs

    PR:     275393

    (cherry picked from commit 9daf6cd0f46416d9c6eb0411ea6042cd42b8a9bc)

 libexec/rtld-elf/rtld.c | 8 ++++++--
 libexec/rtld-elf/rtld.h | 1 +
 2 files changed, 7 insertions(+), 2 deletions(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2023-12-07 00:28:22 UTC
A commit in branch stable/13 references this bug:

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

commit 07fa8d431c0ea291a0e4b79a0bb81707d0b119e6
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-11-29 18:30:59 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-12-07 00:27:34 +0000

    RTLD_DEEPBIND: make lookup not just symbolic, but walk all refobj' DAGs

    PR:     275393

    (cherry picked from commit 9daf6cd0f46416d9c6eb0411ea6042cd42b8a9bc)

 libexec/rtld-elf/rtld.c | 8 ++++++--
 libexec/rtld-elf/rtld.h | 1 +
 2 files changed, 7 insertions(+), 2 deletions(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2023-12-15 16:51:26 UTC
A commit in branch stable/14 references this bug:

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

commit 2ef9079ece5a97aba7a288040fea805d8599a6f4
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-12-01 01:26:09 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-12-15 00:58:36 +0000

    rtld: add a test for RTLD_DEEPBIND

    This tests that with RTLD_DEEPBIND, symbols are looked up in all of the
    object's needed objects before the global object.

    PR:             275393
    Reviewed by:    kib
    Sponsored by:   NetApp, Inc.
    Sponsored by:   Klara, Inc.

    (cherry picked from commit d9c543b6b0cabea6e6679d70b4e701018e7eab80)

 etc/mtree/BSD.tests.dist                           |  2 +
 libexec/rtld-elf/tests/Makefile                    |  5 +-
 libexec/rtld-elf/tests/Makefile.inc (new)          |  3 +
 libexec/rtld-elf/tests/libdeep/Makefile (new)      | 15 +++++
 libexec/rtld-elf/tests/libdeep/libdeep.c (new)     | 28 ++++++++++
 libexec/rtld-elf/tests/libval/Makefile (new)       | 10 ++++
 libexec/rtld-elf/tests/libval/libval.c (new)       | 26 +++++++++
 libexec/rtld-elf/tests/libval2/Makefile (new)      |  8 +++
 .../rtld-elf/tests/rtld_deepbind/Makefile (new)    | 10 ++++
 .../tests/rtld_deepbind/rtld_deepbind.c (new)      | 65 ++++++++++++++++++++++
 libexec/rtld-elf/tests/target/Makefile             |  2 +-
 11 files changed, 172 insertions(+), 2 deletions(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2023-12-15 18:57:51 UTC
A commit in branch stable/13 references this bug:

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

commit 9986fd59d855234cbb999b2a4099ad3cd094aa49
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-12-01 01:26:09 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-12-15 16:56:59 +0000

    rtld: add a test for RTLD_DEEPBIND

    This tests that with RTLD_DEEPBIND, symbols are looked up in all of the
    object's needed objects before the global object.

    ATF_REQUIRE_INTEQ was added after stable/13 branched, so we shim it out
    here.

    PR:             275393
    Reviewed by:    kib
    Sponsored by:   NetApp, Inc.
    Sponsored by:   Klara, Inc.

    (cherry picked from commit d9c543b6b0cabea6e6679d70b4e701018e7eab80)

 etc/mtree/BSD.tests.dist                           |  2 +
 libexec/rtld-elf/tests/Makefile                    |  5 +-
 libexec/rtld-elf/tests/Makefile.inc (new)          |  3 +
 libexec/rtld-elf/tests/libdeep/Makefile (new)      | 15 +++++
 libexec/rtld-elf/tests/libdeep/libdeep.c (new)     | 28 +++++++++
 libexec/rtld-elf/tests/libval/Makefile (new)       | 10 +++
 libexec/rtld-elf/tests/libval/libval.c (new)       | 26 ++++++++
 libexec/rtld-elf/tests/libval2/Makefile (new)      |  8 +++
 .../rtld-elf/tests/rtld_deepbind/Makefile (new)    | 10 +++
 .../tests/rtld_deepbind/rtld_deepbind.c (new)      | 72 ++++++++++++++++++++++
 libexec/rtld-elf/tests/target/Makefile             |  2 +-
 11 files changed, 179 insertions(+), 2 deletions(-)
Comment 10 Kyle Evans freebsd_committer freebsd_triage 2023-12-15 18:58:13 UTC
Thanks!