From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7850) id 291503858D28; Fri, 6 Jan 2023 17:45:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 291503858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673027129; bh=22J2bQmt5ktfD5UJNiaCFfdm6oQlSrIIIBtsKDihgxU=; h=From:To:Subject:Date:From; b=UVbvAH0hZdCD5SDyWQPj0vvM9RdX6D++Km0uApoO/sGuPqs2Ak1hru39j97/Rk6Xx gMrkOjUtAi+XyO1BaE64Wy1DBKvxr9nFq73ifFEjIS+3W2K6RPEiX+o7XQPkjogXem raE12LKoncLI701RlE0NsRJs/Bt9dKc9j0tHWeyc= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Indu Bhagat To: bfd-cvs@sourceware.org, gdb-cvs@sourceware.org Subject: [binutils-gdb] libsframe: adjust an incorrect check in flip_sframe X-Act-Checkin: binutils-gdb X-Git-Author: Indu Bhagat X-Git-Refname: refs/heads/master X-Git-Oldrev: 47bb5b33f742b4338bfa9608288830aecb085da4 X-Git-Newrev: cd9aea32cffd8089f6f63f4eb86d4dccfc0b3850 Message-Id: <20230106174529.291503858D28@sourceware.org> Date: Fri, 6 Jan 2023 17:45:29 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dcd9aea32cffd= 8089f6f63f4eb86d4dccfc0b3850 commit cd9aea32cffd8089f6f63f4eb86d4dccfc0b3850 Author: Indu Bhagat Date: Fri Jan 6 09:29:48 2023 -0800 libsframe: adjust an incorrect check in flip_sframe =20 When sframe_encoder_write needs to flip the buffer containing the SFrame section before writing, it is not necessary that the SFrame FDES are in the order of their sfde_func_start_fre_off. On the contrary, SFrame FDEs will be sorted in the order of their start address. So, remove this incorrect assumption which is basically assuming that the last sfde_func_start_fre_off seen will help determine the end of the flipped buffer. =20 The function now keeps track of the bytes_flipped and then compares it = with the expected value. Also, added two more checks at appropriate places: - check that the SFrame FDE read is within bounds - check that the SFrame FRE read is within bounds =20 libsframe/ =20 * sframe.c (flip_sframe): Adjust an incorrect check. Add other checks to ensure reads are within the buffer size. Diff: --- libsframe/sframe.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/libsframe/sframe.c b/libsframe/sframe.c index 4aada1a25e0..d206780289a 100644 --- a/libsframe/sframe.c +++ b/libsframe/sframe.c @@ -401,7 +401,10 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32_= t to_foreign) unsigned int fre_type =3D 0; uint32_t fre_offset =3D 0; size_t esz =3D 0; + size_t hdrsz =3D 0; int err =3D 0; + /* For error checking. */ + size_t bytes_flipped =3D 0; =20 /* Header must be in host endianness at this time. */ ihp =3D (sframe_header *)frame_buf; @@ -411,14 +414,18 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32= _t to_foreign) =20 /* The contents of the SFrame header are safe to read. Get the number of FDEs and the first FDE in the buffer. */ + hdrsz =3D sframe_get_hdr_size (ihp); num_fdes =3D ihp->sfh_num_fdes; - fdes =3D frame_buf + sframe_get_hdr_size (ihp) + ihp->sfh_fdeoff; + fdes =3D frame_buf + hdrsz + ihp->sfh_fdeoff; fdep =3D (sframe_func_desc_entry *)fdes; =20 j =3D 0; prev_frep_index =3D 0; for (i =3D 0; i < num_fdes; fdep++, i++) { + if ((char*)fdep >=3D (frame_buf + buf_size)) + goto bad; + if (to_foreign) { num_fres =3D fdep->sfde_func_num_fres; @@ -427,6 +434,7 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32_t= to_foreign) } =20 flip_fde (fdep); + bytes_flipped +=3D sizeof (sframe_func_desc_entry); =20 if (!to_foreign) { @@ -441,20 +449,16 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32= _t to_foreign) { if (flip_fre (fp, fre_type, &esz)) goto bad; + bytes_flipped +=3D esz; =20 - if (esz =3D=3D 0) + if (esz =3D=3D 0 || esz > buf_size) goto bad; fp +=3D esz; } prev_frep_index =3D j; } - /* All FREs must have been endian flipped by now. */ - if (j !=3D ihp->sfh_num_fres) - goto bad; - /* Contents, if any, must have been processed by now. - Recall that .sframe section with just a SFrame header may be generate= d by - GAS if no SFrame FDEs were found for the input file. */ - if (ihp->sfh_num_fres && ((frame_buf + buf_size) !=3D (void*)fp)) + /* All FDEs and FREs must have been endian flipped by now. */ + if ((j !=3D ihp->sfh_num_fres) || (bytes_flipped !=3D (buf_size - hdrsz)= )) goto bad; =20 /* Success. */