From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14248 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "liucheng (G)" Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] The local variables "sym" and "bestsym" in dladdr function are assigned initial values to NULL Date: Wed, 19 Jun 2019 07:13:18 +0000 Message-ID: <869863DB5440B44FB22173F42FC3F3CE01D0843A@dggemm513-mbx.china.huawei.com> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="_000_869863DB5440B44FB22173F42FC3F3CE01D0843Adggemm513mbxchi_" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="80118"; mail-complaints-to="usenet@blaine.gmane.org" Cc: "liucheng (G)" To: "musl@lists.openwall.com" Original-X-From: musl-return-14264-gllmg-musl=m.gmane.org@lists.openwall.com Wed Jun 19 09:13:51 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1hdUn1-000Khw-95 for gllmg-musl@m.gmane.org; Wed, 19 Jun 2019 09:13:51 +0200 Original-Received: (qmail 11968 invoked by uid 550); 19 Jun 2019 07:13:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 11946 invoked from network); 19 Jun 2019 07:13:46 -0000 Thread-Topic: [musl] [PATCH] The local variables "sym" and "bestsym" in dladdr function are assigned initial values to NULL Thread-Index: AdUmbjX3JoZ4tHDfQiueWULR1Q7DdA== Accept-Language: zh-CN, en-US Content-Language: zh-CN x-originating-ip: [10.57.34.94] X-CFilter-Loop: Reflected Xref: news.gmane.org gmane.linux.lib.musl.general:14248 Archived-At: --_000_869863DB5440B44FB22173F42FC3F3CE01D0843Adggemm513mbxchi_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Dear all, The code bellow in the dladdr function has different behaviors at different= optimization levels. 2219 if (bestsym && besterr > bestsym->st_size-1) { 2220 best =3D 0; 2221 bestsym =3D 0; 2222 } Case of O1(arm32 little-endian): 154: e3580000 cmp r8, #0 158: 0a000003 beq 16c 15c: e5983008 ldr r3, [r8, #8] 160: e2433001 sub r3, r3, #1 164: e153000a cmp r3, sl 168: 3a000011 bcc 1bc Case of O2: 75e00: e5942044 ldr r2, [r4, #68] ; 0x44 75e04: e2433001 sub r3, r3, #1 75e08: e5941004 ldr r1, [r4, #4] 75e0c: e1530009 cmp r3, r9 75e10: 2a000007 bcs 75e34 75e14: e8870006 stm r7, {r1, r2} In case of O2, the first part "bestsym" has been optimized, which may cause= segment fault. [patch] Signed-off-by: l00383200 > --- ldso/dynlink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ldso/dynlink.c b/ldso/dynlink.c index 7cb66db..c5f5fb7 100644 --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -2175,7 +2175,8 @@ int dladdr(const void *addr_arg, Dl_info *info) { size_t addr =3D (size_t)addr_arg; struct dso *p; - Sym *sym, *bestsym; + Sym *sym =3D NULL; + Sym *bestsym =3D NULL; uint32_t nsym; char *strings; size_t best =3D 0; -- 1.8.5.6 [testcase] ------------ #define _GNU_SOURCE #include #include #include #include #include static int callback(struct dl_phdr_info *info, size_t size, void *data) { int j,ret; printf ("name=3D%s (%d segments)\n", info->dlpi_name, info->dlpi_ph= num); if(!strcmp(info->dlpi_name,"/lib/ld-musl-arm.so.1")) { printf("ld-musl-arm have no indo\n"); return 0; } for (j =3D 0; j < info->dlpi_phnum; j++) { void* addr =3D (void *) (info->dlpi_addr + info->dlpi_phdr[= j].p_vaddr); printf ("\t\t header %2d: address=3D%10p\n", j, addr); Dl_info dlinfo; ret =3D dladdr(addr, &dlinfo); printf("\t\t\t %s : %s.", dlinfo.dli_fname, dlinfo.dli_snam= e); if((addr =3D=3D NULL && ret =3D=3D 0) || (addr !=3D NULL &&= ret =3D=3D 1)) { printf(" dladdr pass return:%d\n",ret); } else { printf(" dladdr error return:%d\n",ret); } } return 0; } int main (int argc, char *argv[]) { dl_iterate_phdr(callback, NULL); exit(EXIT_SUCCESS); } ------------ --_000_869863DB5440B44FB22173F42FC3F3CE01D0843Adggemm513mbxchi_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Dear all,<= /p>

&= nbsp;

The co= de bellow in the dladdr function has different behaviors at different optim= ization levels.

2219&n= bsp;        if (bestsym && beste= rr > bestsym->st_size-1) {

2220&n= bsp;            = ;    best =3D 0;

2221&n= bsp;            = ;    bestsym =3D 0;

2222&n= bsp;        }

&= nbsp;

Case o= f O1(arm32 little-endian):

154: &= nbsp;       e3580000   &n= bsp;    cmp   r8, #0

158:&n= bsp;        0a000003   &n= bsp;    beq   16c <dladdr+0x16c>

15c:&n= bsp;        e5983008   &n= bsp;    ldr    r3, [r8, #8]=

160:&n= bsp;        e2433001   &n= bsp;    sub   r3, r3, #1

164:&n= bsp;        e153000a   &n= bsp;    cmp   r3, sl

168:&n= bsp;        3a000011   &n= bsp;    bcc   1bc <dladdr+0x1b4>

&= nbsp;

Case o= f O2:

75e00:=        e5942044     =    ldr     r2, [r4, #68]   ; 0x44

75e04:=        e2433001     =    sub     r3, r3, #1

75e08:=        e5941004     =    ldr     r1, [r4, #4]

75e0c:=        e1530009     =    cmp     r3, r9

75e10:=        2a000007     =    bcs     75e34 <dladdr+0xfc>=

75e14:=        e8870006     =    stm     r7, {r1, r2}

&= nbsp;

In cas= e of O2, the first part "bestsym" has been optimized, which may c= ause segment fault.

&= nbsp;

[patch]

Signed-off-by: l00383200 <= ;liucheng32@huawei.com>=

---

ldso/dynlink.c | 3 ++= ;-

1 file changed, 2 insertions= (+), 1 deletion(-)

 

diff --git a/ldso/dynlink.c = b/ldso/dynlink.c

index 7cb66db..c5f5fb7 10064= 4

--- a/ldso/dynlink.c

+++ b/ldso/dynli= nk.c

@@ -2175,7 +2175,8 @@ in= t dladdr(const void *addr_arg, Dl_info *info)

{

    &nbs= p;  size_t addr =3D (size_t)addr_arg;

    &nbs= p;  struct dso *p;

-    &nb= sp;   Sym *sym, *bestsym;

+    = ;   Sym *sym =3D NULL;

+    = ;   Sym *bestsym =3D NULL;

    &nbs= p;  uint32_t nsym;

    &nbs= p;  char *strings;

    &nbs= p;  size_t best =3D 0;

--

1.8.5.6

 

&= nbsp;

[testcase]

------= ------

#defin= e _GNU_SOURCE

#inclu= de <link.h>

#inclu= de <stdlib.h>

#inclu= de <stdio.h>

#inclu= de <string.h>

#inclu= de <dlfcn.h>

&= nbsp;

static= int callback(struct dl_phdr_info *info, size_t size, void *data)

{=

 =        int j,ret;

 =        printf ("name=3D%s (%d segments)\= n", info->dlpi_name, info->dlpi_phnum);

&= nbsp;

 =        if(!strcmp(info->dlpi_name,"/l= ib/ld-musl-arm.so.1")) {

 =             &nb= sp;  printf("ld-musl-arm have no indo\n");=

 =             &nb= sp;  return 0;

 =        }

&= nbsp;

 =        for (j =3D 0; j < info->dlpi_phn= um; j++) {

 =             &nb= sp;  void* addr =3D (void *) (info->dlpi_addr + info->dlpi_p= hdr[j].p_vaddr);

 =             &nb= sp;  printf ("\t\t header %2d: address=3D%10p\n", j, addr);<= o:p>

 =             &nb= sp;  Dl_info dlinfo;

 =             &nb= sp;  ret =3D dladdr(addr, &dlinfo);

&= nbsp;

 =             &nb= sp;  printf("\t\t\t %s : %s.", dlinfo.dli_fname, dlinfo.dli_= sname);

&= nbsp;

 =             &nb= sp;  if((addr =3D=3D NULL && ret =3D=3D 0) || (addr !=3D NULL = && ret =3D=3D 1)) {

 =             &nb= sp;          printf(" dla= ddr pass return:%d\n",ret);

 =             &nb= sp;  } else {

 =              &n= bsp;         printf(" dla= ddr error return:%d\n",ret);

 =             &nb= sp;  }

 =        }

&= nbsp;

 =        return 0;

&= nbsp;

}=

&= nbsp;

int ma= in (int argc, char *argv[])

{=

 =        dl_iterate_phdr(callback, NULL);<= /o:p>

 =        exit(EXIT_SUCCESS);<= /p>

}=

------= ------

 

--_000_869863DB5440B44FB22173F42FC3F3CE01D0843Adggemm513mbxchi_--