Skip to content

Commit cd9aea3

Browse files
committed
libsframe: adjust an incorrect check in flip_sframe
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. 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 libsframe/ * sframe.c (flip_sframe): Adjust an incorrect check. Add other checks to ensure reads are within the buffer size.
1 parent 47bb5b3 commit cd9aea3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

libsframe/sframe.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,10 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32_t to_foreign)
401401
unsigned int fre_type = 0;
402402
uint32_t fre_offset = 0;
403403
size_t esz = 0;
404+
size_t hdrsz = 0;
404405
int err = 0;
406+
/* For error checking. */
407+
size_t bytes_flipped = 0;
405408

406409
/* Header must be in host endianness at this time. */
407410
ihp = (sframe_header *)frame_buf;
@@ -411,14 +414,18 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32_t to_foreign)
411414

412415
/* The contents of the SFrame header are safe to read. Get the number of
413416
FDEs and the first FDE in the buffer. */
417+
hdrsz = sframe_get_hdr_size (ihp);
414418
num_fdes = ihp->sfh_num_fdes;
415-
fdes = frame_buf + sframe_get_hdr_size (ihp) + ihp->sfh_fdeoff;
419+
fdes = frame_buf + hdrsz + ihp->sfh_fdeoff;
416420
fdep = (sframe_func_desc_entry *)fdes;
417421

418422
j = 0;
419423
prev_frep_index = 0;
420424
for (i = 0; i < num_fdes; fdep++, i++)
421425
{
426+
if ((char*)fdep >= (frame_buf + buf_size))
427+
goto bad;
428+
422429
if (to_foreign)
423430
{
424431
num_fres = fdep->sfde_func_num_fres;
@@ -427,6 +434,7 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32_t to_foreign)
427434
}
428435

429436
flip_fde (fdep);
437+
bytes_flipped += sizeof (sframe_func_desc_entry);
430438

431439
if (!to_foreign)
432440
{
@@ -441,20 +449,16 @@ flip_sframe (char *frame_buf, size_t buf_size, uint32_t to_foreign)
441449
{
442450
if (flip_fre (fp, fre_type, &esz))
443451
goto bad;
452+
bytes_flipped += esz;
444453

445-
if (esz == 0)
454+
if (esz == 0 || esz > buf_size)
446455
goto bad;
447456
fp += esz;
448457
}
449458
prev_frep_index = j;
450459
}
451-
/* All FREs must have been endian flipped by now. */
452-
if (j != ihp->sfh_num_fres)
453-
goto bad;
454-
/* Contents, if any, must have been processed by now.
455-
Recall that .sframe section with just a SFrame header may be generated by
456-
GAS if no SFrame FDEs were found for the input file. */
457-
if (ihp->sfh_num_fres && ((frame_buf + buf_size) != (void*)fp))
460+
/* All FDEs and FREs must have been endian flipped by now. */
461+
if ((j != ihp->sfh_num_fres) || (bytes_flipped != (buf_size - hdrsz)))
458462
goto bad;
459463

460464
/* Success. */

0 commit comments

Comments
 (0)