From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id D13E1385743A for ; Sat, 29 Oct 2022 04:53:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D13E1385743A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x635.google.com with SMTP id p3so6510564pld.10 for ; Fri, 28 Oct 2022 21:53:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=1SfJlr3YAPi7w9jvWp60qVA3eXqD1HFvtP/CGyeBEXQ=; b=OQpChvMfJRsKbIh+XzMciVCyAsF0pshvBh2a0u1SVZf0tErm4uAnwDP+f7x0fIHdFm 69b919StAU++ueZ3rvreZmv40PYuHE60mUvKqwFCts0m8nA0XxDJCfSxz5R1q0vYbeof fUujXmtMVRpBTAErGuNwk82FvA6WYjk9E2ki3g7OKG7DzlHd+wKopo+vP6l8poRPklzE 6mK2aHTBXVm24oRIXOfar6QjBEueajzZ0ekzvL3ckqCmvq5VJSkLEG/URpZcLL9yT0VP fAu76e6v/Db+Kt3JVGz9b+z0VDfrmsP63Xtz+P/gukaBddoCIID1e8MpcBkocCQWyuRs dL0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1SfJlr3YAPi7w9jvWp60qVA3eXqD1HFvtP/CGyeBEXQ=; b=ovVx5J3HHSx6xVj3yrTXGB55GOyEzAsKTqJrzAy2+L+qAf6Q7m7fqKl/kE8pv0YI30 /nY/sggKH+OVZOVsNU4aRIPprbNpTv2KaPK2hiDVLeRJfyIvBJPsv0iIK33KusNG6vH+ P1r9OeLm73G6+GLGNGYDkg9QCmOXxUSUXg89ZIGQEbUURaJ67SqZcJGdsfv2rHYOVpJZ dTyGw5d40oo8V8LfrEMmkvQTsUXm74LoNx98mroHcuk+/HSYGyu9GsBHpBFkjIdWlA06 yWHJmYDs1dBvHK0q7cK+KP2tUiLRsuncwSj2xYf3xUkSJQqQMy/6u2FjS6+6X2o0WsNO zxNQ== X-Gm-Message-State: ACrzQf24xlt/1M0tVqf/+sHI5xDUtNcZBL11iLBX/F09fT152m/9xm3V 5JBvKqTV8ABIY2Eanhxm8jTa7D851k0= X-Google-Smtp-Source: AMsMyM7HaTCznCM6/qiZzr9RqSRcHiWqhLXT6t4MyphH12t1HFJ7xDJ1FzADcuZaGHvXBrAlD9wV/Q== X-Received: by 2002:a17:90b:1d8a:b0:20f:95f9:ff34 with SMTP id pf10-20020a17090b1d8a00b0020f95f9ff34mr2915468pjb.227.1667019208384; Fri, 28 Oct 2022 21:53:28 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id t17-20020a170902e1d100b00186ae540083sm327036pla.91.2022.10.28.21.53.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 21:53:27 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 5D5FF11424AE; Sat, 29 Oct 2022 15:23:25 +1030 (ACDT) Date: Sat, 29 Oct 2022 15:23:25 +1030 From: Alan Modra To: binutils@sourceware.org Subject: pef: sanity check before malloc Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3036.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: And do the sanity check in a way that can't overflow. * pef.c (bfd_pef_parse_function_stubs): Sanity check header imported_library_count and total_imported_symbol_count before allocating memory. diff --git a/bfd/pef.c b/bfd/pef.c index d9936f750c1..334d802eb75 100644 --- a/bfd/pef.c +++ b/bfd/pef.c @@ -751,6 +751,13 @@ bfd_pef_parse_function_stubs (bfd *abfd, if (ret < 0) goto error; + if ((loaderlen - 56) / 24 < header.imported_library_count) + goto error; + + if ((loaderlen - 56 - header.imported_library_count * 24) / 4 + < header.total_imported_symbol_count) + goto error; + libraries = bfd_malloc (header.imported_library_count * sizeof (bfd_pef_imported_library)); imports = bfd_malloc @@ -758,8 +765,6 @@ bfd_pef_parse_function_stubs (bfd *abfd, if (libraries == NULL || imports == NULL) goto error; - if (loaderlen < (56 + (header.imported_library_count * 24))) - goto error; for (i = 0; i < header.imported_library_count; i++) { ret = bfd_pef_parse_imported_library @@ -768,9 +773,6 @@ bfd_pef_parse_function_stubs (bfd *abfd, goto error; } - if (loaderlen < (56 + (header.imported_library_count * 24) - + (header.total_imported_symbol_count * 4))) - goto error; for (i = 0; i < header.total_imported_symbol_count; i++) { ret = (bfd_pef_parse_imported_symbol -- Alan Modra Australia Development Lab, IBM