From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12704 Path: news.gmane.org!.POSTED!not-for-mail From: "Siebenborn, Axel" Newsgroups: gmane.linux.lib.musl.general Subject: RE: [PATCH] dl_addr: compare addr with sym->st_size. Date: Wed, 11 Apr 2018 08:07:38 +0000 Message-ID: References: <6a42ca4b6c9b4ea08925e232d7b57667@sap.com> <20180410142312.GE3094@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1523433950 13514 195.159.176.226 (11 Apr 2018 08:05:50 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 11 Apr 2018 08:05:50 +0000 (UTC) To: "musl@lists.openwall.com" Original-X-From: musl-return-12720-gllmg-musl=m.gmane.org@lists.openwall.com Wed Apr 11 10:05:46 2018 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.84_2) (envelope-from ) id 1f6AlF-0003P1-GS for gllmg-musl@m.gmane.org; Wed, 11 Apr 2018 10:05:45 +0200 Original-Received: (qmail 7750 invoked by uid 550); 11 Apr 2018 08:07:51 -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 7729 invoked from network); 11 Apr 2018 08:07:50 -0000 Thread-Topic: [musl] [PATCH] dl_addr: compare addr with sym->st_size. Thread-Index: AdPLTEBvr5Sbu89kTKWUonO61LEwTwFenFuAACg772A= In-Reply-To: <20180410142312.GE3094@brightrain.aerifal.cx> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.21.23.236] Xref: news.gmane.org gmane.linux.lib.musl.general:12704 Archived-At: Hi, > -----Original Message----- > From: Rich Felker [mailto:dalias@aerifal.cx] On Behalf Of Rich Felker > Sent: Dienstag, 10. April 2018 16:23 > To: musl@lists.openwall.com > Subject: Re: [musl] [PATCH] dl_addr: compare addr with sym->st_size. >=20 > On Tue, Apr 03, 2018 at 01:06:09PM +0000, Siebenborn, Axel wrote: > > Hi, > > this patch fixes a problem with dl_addr. > > > > We found symbols, in cases we should not find a symbol, since the > > comparison with sym->st_size is missing. >=20 > This was intentional, as my understanding of the historical behavior > on other implementations was that it would do this. If that's > incorrect we should investigate and document (or find existing > documentation of) what they really do. I don't know how the historical behavior was. Maybe you could point me to=20 some resources. However, I found that st_size might be 0, if the symbol has no or an unknow= n size. How about comparing st_size to zero? - if (symaddr > addr || symaddr < best) + if (symaddr > addr || ((sym->st_size !=3D 0) && ((v= oid*) ((uint8_t*) symaddr + sym->st_size) < addr)) || symaddr < best) >=20 > > According to the spec, dl_addr should not return an error in this > > case. Instead dli_sname and dli_addr should be set to NULL. >=20 > OK, I found that part in the man page. >=20 > > diff --git a/ldso/dynlink.c b/ldso/dynlink.c > > index 9bf6924..cc87dc0 100644 > > --- a/ldso/dynlink.c > > +++ b/ldso/dynlink.c > > @@ -1958,7 +1958,7 @@ int dladdr(const void *addr, Dl_info *info) > > && (1<<(sym->st_info&0xf) & OK_TYPES) > > && (1<<(sym->st_info>>4) & OK_BINDS)) { > > void *symaddr =3D laddr(p, sym->st_value); > > - if (symaddr > addr || symaddr < best) > > + if (symaddr > addr || (void*) ((uint8_t*) symad= dr + sym- > >st_size) < addr || symaddr < best) >=20 > Not all symbols have st_size set. In that case the old "best match" > behavior should probably be kept unless there's a strong reason not > to. >=20 > > continue; > > best =3D symaddr; > > bestsym =3D sym; > > @@ -1967,13 +1967,16 @@ int dladdr(const void *addr, Dl_info *info) > > } > > } > > > > - if (!best) return 0; > > - > > - if (DL_FDPIC && (bestsym->st_info&0xf) =3D=3D STT_FUNC) > > - best =3D p->funcdescs + (bestsym - p->syms); > > - > > info->dli_fname =3D p->name; > > info->dli_fbase =3D p->map; > > + if (!best) { > > + info->dli_sname =3D 0; > > + info->dli_saddr =3D 0; > > + return 0 >=20 > This is missing a ; so it seems you tested a slightly different patch..? Sorry, that's embarrassing. I slightly refactored after testing. This line should be: + return 1; >=20 > > + } > > + > > + if ( DL_FDPIC && (bestsym->st_info&0xf) =3D=3D STT_FUNC) > > + best =3D p->funcdescs + (bestsym - p->syms); > > info->dli_sname =3D strings + bestsym->st_name; > > info->dli_saddr =3D best; >=20 > Otherwise this looks ok but I haven't tested it. >=20 > Rich Here is the complete patch: diff --git a/ldso/dynlink.c b/ldso/dynlink.c index 9bf6924..2801416 100644 --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -1958,7 +1958,7 @@ int dladdr(const void *addr, Dl_info *info) && (1<<(sym->st_info&0xf) & OK_TYPES) && (1<<(sym->st_info>>4) & OK_BINDS)) { void *symaddr =3D laddr(p, sym->st_value); - if (symaddr > addr || symaddr < best) + if (symaddr > addr || ((sym->st_size !=3D 0) && ((v= oid*) ((uint8_t*) symaddr + sym->st_size) < addr)) || symaddr < best) continue; best =3D symaddr; bestsym =3D sym; @@ -1967,13 +1967,16 @@ int dladdr(const void *addr, Dl_info *info) } } =20 - if (!best) return 0; - - if (DL_FDPIC && (bestsym->st_info&0xf) =3D=3D STT_FUNC) - best =3D p->funcdescs + (bestsym - p->syms); - info->dli_fname =3D p->name; info->dli_fbase =3D p->map; + if (!best) { + info->dli_sname =3D 0; + info->dli_saddr =3D 0; + return 1; + } + + if ( DL_FDPIC && (bestsym->st_info&0xf) =3D=3D STT_FUNC) + best =3D p->funcdescs + (bestsym - p->syms); info->dli_sname =3D strings + bestsym->st_name; info->dli_saddr =3D best; Regards, Axel