From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2103) id 4E644385438E; Thu, 12 Jan 2023 15:39:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E644385438E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673537984; bh=FqCS//T1oRYQHbBbNjXg+HGuKfguSJJhQUqGAwX5xAw=; h=From:To:Subject:Date:From; b=TEYKJuIexwk8ZFpt70xbB28UxJQTmXVs52Io2G41GotFggDQDVhM3YJ+O2f92ufsi YBlwVXVZ30jfElPOXhBmy3Uo/L+WHrxpdD8J4pS/AwJgf4NljArUNDb63MmWjLLITE HN6ObSllHJx6HElAVcK7LJNwOPMVHWKIdVehQQns= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nick Alcock To: bfd-cvs@sourceware.org, gdb-cvs@sourceware.org Subject: [binutils-gdb] libctf: ctf-link outdated input check faulty X-Act-Checkin: binutils-gdb X-Git-Author: Nick Alcock X-Git-Refname: refs/heads/master X-Git-Oldrev: e2dc08c6f0642ca2c94e91997a8d4e971bea9971 X-Git-Newrev: c777aa9765b6892c1ef7d7584385b9377071248e Message-Id: <20230112153944.4E644385438E@sourceware.org> Date: Thu, 12 Jan 2023 15:39:44 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc777aa9765b6= 892c1ef7d7584385b9377071248e commit c777aa9765b6892c1ef7d7584385b9377071248e Author: Nick Alcock Date: Mon Jan 9 13:43:09 2023 +0000 libctf: ctf-link outdated input check faulty =20 This check has a pair of faults which, combined, can lead to memory corruption. Firstly, it assumes that the values of the ctf_link_inputs hash are ctf_dict_t's: they are not, they are ctf_link_input_t's, a much shorter structure. So the flags check which is the core of this is faulty (but happens, by chance, to give the right output on most architectures, since usually we happen to get a 0 here, so the test that checks this usually passes). Worse, the warning that is emitted when the test fails is added to the wrong dict -- it's added to the input dict, whose warning list is never consumed, rendering the whole check useless. But the dict it adds to is still the wrong type, so we end up overwriting something deep in memory (or, much more likely, dereferencing a garbage pointer and crashing). =20 Fixing both reveals another problem: the link input is an *archive* consisting of multiple members, so we have to consider whether to check all of them for the outdated-func-info thing we are checking here. However, no compiler exists that emits a mixture of members with this flag on and members with it off, and the linker always reserializes (and upgrades) such things when it sees them: so all members in a given archive must have the same value of the flag, so we only need to check one member per input archive. =20 libctf/ PR libctf/29983 * ctf-link.c (ctf_link_warn_outdated_inputs): Get the types of members of ctf_link_inputs right, fixing a possible spurious tesst failure / wild pointer deref / overwrite. Emit the warning message into the right dict. Diff: --- libctf/ctf-link.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/libctf/ctf-link.c b/libctf/ctf-link.c index 2837168b2a6..016bce5f6d6 100644 --- a/libctf/ctf-link.c +++ b/libctf/ctf-link.c @@ -1848,19 +1848,42 @@ ctf_link_warn_outdated_inputs (ctf_dict_t *fp) { ctf_next_t *i =3D NULL; void *name_; - void *ifp_; + void *input_; int err; =20 - while ((err =3D ctf_dynhash_next (fp->ctf_link_inputs, &i, &name_, &ifp_= )) =3D=3D 0) + while ((err =3D ctf_dynhash_next (fp->ctf_link_inputs, &i, &name_, &inpu= t_)) =3D=3D 0) { const char *name =3D (const char *) name_; - ctf_dict_t *ifp =3D (ctf_dict_t *) ifp_; + ctf_link_input_t *input =3D (ctf_link_input_t *) input_; + ctf_next_t *j =3D NULL; + ctf_dict_t *ifp; + int err; + + /* We only care about CTF archives by this point: lazy-opened archiv= es + have always been opened by this point, and short-circuited entries have + a matching corresponding archive member. Entries with NULL clin_arc can + exist, and constitute old entries renamed via a name changer: the + renamed entries exist elsewhere in the list, so we can just skip + those. */ + + if (!input->clin_arc) + continue; + + /* All entries in the archive will necessarily contain the same + CTF_F_NEWFUNCINFO flag, so we only need to check the first. We don't + even need to do that if we can't open it for any reason at all: the + link will fail later on regardless, since an input can't be opened. */ + + ifp =3D ctf_archive_next (input->clin_arc, &j, NULL, 0, &err); + if (!ifp) + continue; + ctf_next_destroy (j); =20 if (!(ifp->ctf_header->cth_flags & CTF_F_NEWFUNCINFO) && (ifp->ctf_header->cth_varoff - ifp->ctf_header->cth_funcoff) > 0) - ctf_err_warn (ifp, 1, 0, _("linker input %s has CTF func info but uses " - "an old, unreleased func info format: " - "this func info section will be dropped."), + ctf_err_warn (fp, 1, 0, _("linker input %s has CTF func info but uses " + "an old, unreleased func info format: " + "this func info section will be dropped."), name); } if (err !=3D ECTF_NEXT_END)