[02/10] libsframe: guard against malformed rep_block_size

Message ID 20260827224146.3391610-3-ibhagatgnu@gmail.com
State New
Headers
Series fix two PRs and guard against bad data |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Indu Bhagat Aug. 27, 2026, 10:41 p.m. UTC
  From: Indu Bhagat <indu.bhagat@oracle.com>

When checking for an applicable FRE, guard against invalid data in
rep_block_size, lest there be an arithmetic exception caused by division
by zero.  Similar code was flagged by Sashiko in the stacktracer patches
for Linux kernel earlier.
---
 libsframe/sframe.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
  

Comments

Jens Remus Sept. 7, 2026, 12:02 p.m. UTC | #1
On 8/28/2026 12:41 AM, Indu Bhagat wrote:
> From: Indu Bhagat <indu.bhagat@oracle.com>
> 
> When checking for an applicable FRE, guard against invalid data in
> rep_block_size, lest there be an arithmetic exception caused by division
> by zero.  Similar code was flagged by Sashiko in the stacktracer patches
> for Linux kernel earlier.
> ---
>  libsframe/sframe.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)

Reviewed-by: Jens Remus <jremus@linux.ibm.com>

> diff --git a/libsframe/sframe.c b/libsframe/sframe.c

> @@ -600,7 +600,12 @@ sframe_fre_check_range_p (const sframe_decoder_ctx *dctx, uint32_t func_idx,
>    /* For SFrame FDEs encoding information for repetitive pattern of insns,
>       masking with the rep_block_size is necessary to find the matching FRE.  */
>    if (mask_p)
> -    pc_offset = pc_offset % rep_block_size;
> +    {
> +      /* Treat a zero-sized repeat block as malformed input.  */
> +      if (rep_block_size == 0)
> +	return false;

This works, as sframe_find_fre() will error with SFRAME_ERR_FDE_INVAL if
no matching FRE is found for the IP within the already located FDE.

> +      pc_offset = pc_offset % rep_block_size;
> +    }
>  
>    return (start_ip_offset <= pc_offset) && (end_ip_offset >= pc_offset);
>  }

Regards,
Jens
  

Patch

diff --git a/libsframe/sframe.c b/libsframe/sframe.c
index 011884c0768..712dd0fdb68 100644
--- a/libsframe/sframe.c
+++ b/libsframe/sframe.c
@@ -600,7 +600,12 @@  sframe_fre_check_range_p (const sframe_decoder_ctx *dctx, uint32_t func_idx,
   /* For SFrame FDEs encoding information for repetitive pattern of insns,
      masking with the rep_block_size is necessary to find the matching FRE.  */
   if (mask_p)
-    pc_offset = pc_offset % rep_block_size;
+    {
+      /* Treat a zero-sized repeat block as malformed input.  */
+      if (rep_block_size == 0)
+	return false;
+      pc_offset = pc_offset % rep_block_size;
+    }
 
   return (start_ip_offset <= pc_offset) && (end_ip_offset >= pc_offset);
 }