From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 6FB663858D31 for ; Mon, 20 Apr 2020 01:38:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6FB663858D31 Received: by mail-pl1-x636.google.com with SMTP id t16so3343020plo.7 for ; Sun, 19 Apr 2020 18:38:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=7H6Uy/+wvosayNOLZqrx12nR9p30EjNUH7X+yhbN6aU=; b=pky7+GY0vo8ek9ryxM6O5yOqo5XcVRfrSwcjqX7OZCjwV9GGOl5Ca/KXV+9M5mTUUe QhV4esC2Fqmg++dP0cCJrAOBkAX+bKnPqWtK7qzCnPM8ozTHCDPIRGNU8cZC87gqsNo4 AaSfcr9r3EXx4bQa+EJVV9cOaA1QG52kkx7iI4Ilfxl0q2hIN+0zJdsylaBc0uV3tijO 5F0OmljXzun7FdHLAonq3+wgxDPP375l2dRN3jSX/nhW0Fln/k4ZyoEn62CJK1hnQIFt WFtc1co13GfUj/fN+2u0LW/OW8qRtysEz/wZTJWxIIJJbe1vqksbRf/t2dacQS8xNpe0 JqjQ== X-Gm-Message-State: AGi0PubxCdOwgcE0dCtEWGLoXKravtnWUXxAr+A9teG6PUaI8+Zaq84A BqzUjIorHqd5+F7MybyfSLwKd5BxZEQ= X-Google-Smtp-Source: APiQypI375n7VSXxu2Ht+WWs4MLCtCaWx10Ge1b5/d5MGAVDXxHZptx5cPi8rN7iFyM1MsAlmBM++g== X-Received: by 2002:a17:902:123:: with SMTP id 32mr14715368plb.38.1587346738279; Sun, 19 Apr 2020 18:38:58 -0700 (PDT) Received: from bubble.grove.modra.org ([2406:3400:51d:8cc0:6cfe:f5f7:f1a0:c9aa]) by smtp.gmail.com with ESMTPSA id h66sm24966282pgc.42.2020.04.19.18.38.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2020 18:38:57 -0700 (PDT) Received: by bubble.grove.modra.org (Postfix, from userid 1000) id E3D9680FBB; Mon, 20 Apr 2020 11:08:53 +0930 (ACST) Date: Mon, 20 Apr 2020 11:08:53 +0930 From: Alan Modra To: binutils@sourceware.org Subject: readelf: segfaults fuzzing multiple object files Message-ID: <20200420013853.GK29913@bubble.grove.modra.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Spam-Status: No, score=-23.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Apr 2020 01:39:00 -0000 This patch is aimed at fixing a number of oss-fuzz segfaults that don't reproduce reliably with their current infrastructure, the problem being that one invocation of readelf is effectively being run on multiple object files. I believe that these segfaults could be reliably reproduced with just two fuzzed objects being presented to readelf, but those inputs are currently not identified by oss-fuzz. So there is some guesswork involved in this patch. The idea here is to clear stashed data such as symtab_shndx_list that is processed using section header info, at the same time that header info is cleared. * readelf.c (process_section_headers): Free dynamic symbols etc. earlier. diff --git a/binutils/readelf.c b/binutils/readelf.c index 601e329fdc..d9c9b7ea0c 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -6132,6 +6132,20 @@ process_section_headers (Filedata * filedata) free (filedata->section_headers); filedata->section_headers = NULL; + free (dynamic_symbols); + dynamic_symbols = NULL; + num_dynamic_syms = 0; + free (dynamic_strings); + dynamic_strings = NULL; + dynamic_strings_length = 0; + free (dynamic_syminfo); + dynamic_syminfo = NULL; + while (symtab_shndx_list != NULL) + { + elf_section_list *next = symtab_shndx_list->next; + free (symtab_shndx_list); + symtab_shndx_list = next; + } if (filedata->file_header.e_shnum == 0) { @@ -6186,21 +6200,6 @@ process_section_headers (Filedata * filedata) /* Scan the sections for the dynamic symbol table and dynamic string table and debug sections. */ - free (dynamic_symbols); - dynamic_symbols = NULL; - num_dynamic_syms = 0; - free (dynamic_strings); - dynamic_strings = NULL; - dynamic_strings_length = 0; - free (dynamic_syminfo); - dynamic_syminfo = NULL; - while (symtab_shndx_list != NULL) - { - elf_section_list *next = symtab_shndx_list->next; - free (symtab_shndx_list); - symtab_shndx_list = next; - } - eh_addr_size = is_32bit_elf ? 4 : 8; switch (filedata->file_header.e_machine) { -- Alan Modra Australia Development Lab, IBM