NetBSD Problem Report #11458

Received: (qmail 28913 invoked from network); 9 Nov 2000 15:15:00 -0000
Message-Id: <200011091514.QAA22148@polderland.nl>
Date: Thu, 9 Nov 2000 16:14:58 +0100 (CET)
From: Olaf Seibert <rhialto@polderland.nl>
Reply-To: rhialto@polderland.nl
To: gnats-bugs@gnats.netbsd.org, rhialto@polderland.nl
Subject: dlsym(3) returns the wrong symbols, ignoring handle
X-Send-Pr-Version: 3.95

>Number:         11458
>Category:       lib
>Synopsis:       dlsym(3) returns the wrong symbols, ignoring handle
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    martin
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 09 15:16:00 +0000 2000
>Closed-Date:    Wed Dec 06 05:32:37 +0000 2000
>Last-Modified:  Wed Dec 06 05:32:37 +0000 2000
>Originator:     Olaf Seibert
>Release:        NetBSD/alpha 1.4.1 (ELF)
>Organization:

>Environment:

System: NetBSD azenomei.falu.nl 1.4.1 NetBSD 1.4.1 (AZENOMEI) #27: Wed Jun 14 01:41:57 CEST 2000     rhialto@azenomei.falu.nl:/usr/src/sys/arch/alpha/compile/AZENOMEI alpha


>Description:

Note: NetBSD/alpha uses ELF.

.From the manpage:

     void *
     dlsym(void *handle, const char *symbol);

     dlsym() looks for a definition of symbol in the shared object designated
     by handle.  The symbols address is returned.  If the symbol cannot be re-
     solved, NULL is returned.

This turns out not to be true. The handle argument turns out to be
ignored. If the symbol name can be found in either the main program or an
earlier loaded object, that symbol will be returned. It may even return
symbols from objects which failed to link at load time.

This horribly breaks gnupg module loading... :-(

To illustrate the problem with gpg:

gpg can load modules. Each of those modules exports two symbols, which
identify the modules and their entry points. Thesy are called
gnupgext_version and gnupgext_enum_func.

gpg uses el->handle = dlopen(el->name, RTLD_NOW), then dlsym(el->handle,
"gnupgext_version") to get the symbol gnupgext_version from the module.
(cipher/dynload.c)

Howerver, due to an oversight the symbol gnupgext_version also exists in
the main program, so I never got that symbol from the module. I got the
version from the main program. This results in gpg thinking I wanted to
add the twofish cipher, which it already has:

    $ gpg -v -v -v /tmp/xx.gpg
    :pubkey enc packet: version 3, algo 1, keyid 260A8308DAFEAFB5
	    data: [1536 bits]
    gpg: public key is DAFEAFB5

    You need a passphrase to unlock the secret key for
    user: "Olaf 'Rhialto' Seibert <rhialto@mbfys.kun.nl> 1536"
    gpg: loaded digest 2
    gpg: /usr/pkg/lib/gnupg/idea: TWOFISH ($Revision: 1.8.2.3 $)
    gpg: /usr/pkg/lib/gnupg/idea: provides cipher algorithm 10
    gpg: /usr/pkg/lib/gnupg/idea: provides cipher algorithm 102
    gpg: /usr/pkg/lib/gnupg/tiger: TWOFISH ($Revision: 1.8.2.3 $)
    gpg: /usr/pkg/lib/gnupg/tiger: provides cipher algorithm 10
    gpg: /usr/pkg/lib/gnupg/tiger: provides cipher algorithm 102
    1536-bit RSA key, ID DAFEAFB5, created 1997-07-08

Even when I fixed that (add static in front of gnupgext_version and
gnupgext_enum_func in cipher/twofish.c), for the second module that was
loaded I got the gnupgext_version from the first module.

gpg was already linked with -Wl,-export-dynamic. Removing it made
loading the first module fail:

gpg: /usr/pkg/lib/gnupg/idea: error loading extension: /usr/pkg/lib/gnupg/idea:
+Undefined PLT symbol "g10_log_fatal" (reloc type = 26, symnum = 38)

then the second module STILL got the wrong symbol:

gpg: /usr/pkg/lib/gnupg/tiger: IDEA ($Revision: 1.11 $)
gpg: /usr/pkg/lib/gnupg/tiger: provides cipher algorithm 1
gpg: loaded cipher 1 (IDEA)


>How-To-Repeat:

Compile gnupg package. Fetch idea.c from
ftp://ftp.gnupg.org/pub/gcrypt/contrib/idea.c .  Compile, install, and
wonder why it gives messages about cipher #10 (TWOFISH) instead of #1
(IDEA).

Install both idea and tiger (which comes with gnupg) for the second
problem. This can be done by adding the following to
$HOME/.gnupg/options:

    load-extension tiger
    load-extension idea

gpg -v -v -v testfile.gpg, where testfile is a file made by encrypting
with a key from PGP 2.6.x. Or you could try signing with the key. Gnupg
is smart enough that it only tries to load extensions when it needs any,
and the IDEA cipher is only needed with such old keys, to access the
private key. The modules are loaded in reverse order.

>Fix:
	I don't know :-(

-Olaf.
-- 
___ Olaf 'Rhialto' Seibert - rhialto@polder    -- Ah only did well at school
\X/ land.nl       -- tae git intae an O level class tae git away fae Begbie.
Hi! I am a .signature virus. Copy me into your .signature to help me spread.
>Release-Note:
>Audit-Trail:

From: Martin Husemann <martin@duskware.de>
To: rhialto@polderland.nl
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: lib/11458: dlsym(3) returns the wrong symbols, ignoring handle
Date: Sat, 25 Nov 2000 11:00:15 +0100 (MET)

 > This turns out not to be true. The handle argument turns out to be
 > ignored. If the symbol name can be found in either the main program or an
 > earlier loaded object, that symbol will be returned. It may even return
 > symbols from objects which failed to link at load time.

 I think this analysis is not correct.

 Below you'll find a test program that will exercise the above described
 behaviour and from the output you can see that ld.elf_so does indeed work
 as described:

 martin > ./chkdl 
 dlsym test
 This is libt1.so:testfunc, caller expets us to be "libt1:testfunc"
 function returned 1
 This is libt2.so:testfunc, caller expets us to be "libt2:testfunc"
 function returned 2
 This is main:testfunc, caller expets us to be "main:testfunc"
 function returned 0

 So the effect you are seeing must be caused by something else.

 Martin

 # This is a shell archive.  Save it in a file, remove anything before
 # this line, and then unpack it by entering "sh file".  Note, it may
 # create directories; files and directories will be owned by you and
 # have default permissions.
 #
 # This archive contains:
 #
 #	dlsym
 #	dlsym/libt1
 #	dlsym/libt1/main.c
 #	dlsym/libt1/Makefile
 #	dlsym/libt2
 #	dlsym/libt2/main.c
 #	dlsym/libt2/Makefile
 #	dlsym/Makefile
 #	dlsym/main.c
 #
 echo c - dlsym
 mkdir -p dlsym > /dev/null 2>&1
 echo c - dlsym/libt1
 mkdir -p dlsym/libt1 > /dev/null 2>&1
 echo x - dlsym/libt1/main.c
 sed 's/^X//' >dlsym/libt1/main.c << 'END-of-dlsym/libt1/main.c'
 X#include <stdio.h>
 X
 Xint testfunc(const char *name)
 X{
 X	printf("This is libt1.so:testfunc, caller expets us to be \"%s\"\n",
 X		name);
 X	return 1;	/* we are from libt1 */
 X}
 END-of-dlsym/libt1/main.c
 echo x - dlsym/libt1/Makefile
 sed 's/^X//' >dlsym/libt1/Makefile << 'END-of-dlsym/libt1/Makefile'
 XNOLINT=		1
 XNOPROFILE=	1
 X
 XSRCS=		main.c
 XLIB=		t1
 XSHLIB_MAJOR=	0
 XSHLIB_MINOR=	1
 X
 X.include <bsd.lib.mk>
 END-of-dlsym/libt1/Makefile
 echo c - dlsym/libt2
 mkdir -p dlsym/libt2 > /dev/null 2>&1
 echo x - dlsym/libt2/main.c
 sed 's/^X//' >dlsym/libt2/main.c << 'END-of-dlsym/libt2/main.c'
 X#include <stdio.h>
 X
 Xint testfunc(const char *name)
 X{
 X	printf("This is libt2.so:testfunc, caller expets us to be \"%s\"\n",
 X		name);
 X	return 2;	/* we are from libt2 */
 X}
 END-of-dlsym/libt2/main.c
 echo x - dlsym/libt2/Makefile
 sed 's/^X//' >dlsym/libt2/Makefile << 'END-of-dlsym/libt2/Makefile'
 XNOLINT=		1
 XNOPROFILE=	1
 X
 XSRCS=		main.c
 XLIB=		t2
 XSHLIB_MAJOR=	0
 XSHLIB_MINOR=	1
 X
 X.include <bsd.lib.mk>
 END-of-dlsym/libt2/Makefile
 echo x - dlsym/Makefile
 sed 's/^X//' >dlsym/Makefile << 'END-of-dlsym/Makefile'
 XSUBDIR=		libt1 libt2
 XPROG=		chkdl
 XSRCS=		main.c
 XNOMAN=		1
 XLDFLAGS+=	-Wl,-E
 X
 X.include <bsd.prog.mk>
 X.include <bsd.subdir.mk>
 END-of-dlsym/Makefile
 echo x - dlsym/main.c
 sed 's/^X//' >dlsym/main.c << 'END-of-dlsym/main.c'
 X#include <stdio.h>
 X#include <dlfcn.h>
 X
 Xtypedef int (*testfunc_call)(const char *name);
 X
 Xint testfunc(const char *name)
 X{
 X	printf("This is main:testfunc, caller expets us to be \"%s\"\n",
 X		name);
 X	return 0;	/* we are from main */
 X}
 X
 Xint main(int argc, char **argv)
 X{
 X	int res;
 X	void * lib;
 X	testfunc_call testf;
 X
 X	printf("dlsym test\n");
 X
 X	lib = dlopen("libt1/libt1.so", RTLD_NOW);
 X	if (!lib) {
 X		perror("libt1/libt1.so could not be loaded");
 X	} else {
 X		testf = (testfunc_call)dlsym(lib, "testfunc");
 X		if (testf) {
 X			res = testf("libt1:testfunc");
 X			printf("function returned %d\n", res);
 X		}
 X		dlclose(lib);
 X	}
 X
 X	lib = dlopen("libt2/libt2.so", RTLD_NOW);
 X	if (!lib) {
 X		perror("libt2/libt2.so could not be loaded");
 X	} else {
 X		testf = (testfunc_call)dlsym(lib, "testfunc");
 X		if (testf) {
 X			res = testf("libt2:testfunc");
 X			printf("function returned %d\n", res);
 X		}
 X		dlclose(lib);
 X	}
 X
 X	/* check if we can resolve symbols from ourself */
 X	lib = dlopen(NULL, RTLD_NOW);
 X	if (lib) {
 X		testf = (testfunc_call)dlsym(lib, "testfunc");
 X		if (testf) {
 X			res = testf("main:testfunc");
 X			printf("function returned %d\n", res);
 X		}
 X		dlclose(lib);
 X	}
 X	return 0;
 X}
 END-of-dlsym/main.c
 exit


State-Changed-From-To: open->feedback 
State-Changed-By: martin 
State-Changed-When: Sat Nov 25 02:07:26 PST 2000 
State-Changed-Why:  
As demonstrated, the analysis leding to this PR is incomplete; the bug 
described in this PR just is not there. 

I would like to close the PR and ask you to refile a new one after 
more detailed examination of the original problem. 


Responsible-Changed-From-To: lib-bug-people->martin 
Responsible-Changed-By: martin 
Responsible-Changed-When: Sat Nov 25 02:07:26 PST 2000 
Responsible-Changed-Why:  
I'll handle this. 
State-Changed-From-To: feedback->closed 
State-Changed-By: martin 
State-Changed-When: Tue Dec 5 21:31:43 PST 2000 
State-Changed-Why:  
Submitter will file a new PR after closer analyzis of his original problem. 
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.