From patchwork Tue Oct 10 11:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 150689 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp132929vqb; Tue, 10 Oct 2023 04:45:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHofztZ1BohOTRG2mhFhT5VJpZx2B//Dx5zjcace6/qGySkSl/57GmLsISl1/zotYpAa7uD X-Received: by 2002:a17:906:1097:b0:9b2:b786:5e9c with SMTP id u23-20020a170906109700b009b2b7865e9cmr15739090eju.28.1696938347476; Tue, 10 Oct 2023 04:45:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696938347; cv=none; d=google.com; s=arc-20160816; b=CoKEXFYv0ecaymAFCLsLuT6Q+RyIwYmVaEpoiZJVYifI87zM1dxYOWs4zNAxj45tCX CNMo6VVQ0soBoQQKdtEnTIDNv9ZeW4w2aF0x4S6V2hE8PLQ99pHaBJJWzTgnKi56hsI9 w8vdY0ObyiXq3ZvtVDWZk8m64cqd7bVFwlQ0iy+FQujRtkT6rVxduE7tg2nZRKuN6Ec2 UqAGFntKfk1tUou0p3APV01xA974xGmNQECIRet18a9RJ1bgCo/yifDMJ7gFN1IqQrK+ 7lbwel9I17KVZ5/G+P/h+4X6+gdUK+T/CMTmAkUIHukAln2oaTlMUNetBwO3DHNvnu/X /b7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:to:from:date:dkim-signature :dmarc-filter:delivered-to; bh=kDGvHlU5BSvandKxXhGpvwoZkbTINcvHi2wx3/cjWcg=; fh=NLxAvL/bDfPg4AGOtxqvQlND8vazkZrNzKLY8+LAbBY=; b=tRxeoY9B02XegdJG7nYoFlvHeKFcTGfk4rTEVhZjs1r5MX/b0DcLJHA3AsPDgV+N/n wCIaN1c7F5XeWFMfVkJ5HrllSXQdPPKKucMfCMM57mtnkCV+uROF1y3QwqsU5m4b7xw9 hI3lleAXeFLTG+84KqejvGty12athHRbzbPFEPwLPoY/BdE717E0Oao8EiunVwVVl8+j M7QorU+oKUs51m+gHqh6IxGwcfA+JNDa0TNO4sKFC2QL64qpM1nipwrxE7p2enjveeh1 gWGBy1uaMHLmUh+ub6oct3lhOqSlwvkRu5LYgwpINRiUVNfw5NfJFc7Abl3msWwq2CpC cwpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ZeNMQYNP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c23-20020a17090603d700b009b2b9bfc986si5391940eja.351.2023.10.10.04.45.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 04:45:47 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ZeNMQYNP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA50C385C6EE for ; Tue, 10 Oct 2023 11:45:31 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id D4B0B3861822 for ; Tue, 10 Oct 2023 11:45:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D4B0B3861822 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-x62c.google.com with SMTP id d9443c01a7336-1c62d61dc96so33663455ad.0 for ; Tue, 10 Oct 2023 04:45:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696938324; x=1697543124; darn=sourceware.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=kDGvHlU5BSvandKxXhGpvwoZkbTINcvHi2wx3/cjWcg=; b=ZeNMQYNP3sWdYsXy6t5fLfJQNH/0kc/7y18qe+0br9CI7TZTAX4td485NWvpk/gcHv IrNslIOcMYoYneDsqc0KDPBEjpOVLNNozrqtF43VYAxMmByugiZzBnFC9FxrnF0KdFGq nL96Xs0AhFZDqBMC+kZnoycll7qmkmLTWBtyFy+NHPPA6+Ztczq78teAcCU8KtYClDLN khV/LZg5299UQFBUNArPPUNE+xvu12QzKv6q47tCf8943jv0peinemNaTNyQpltCf8M8 innhXumRrVR+bN5Q+BTfk2Dw6qeWGz49xRq0Du3K898P+T4GnBJ8YwjTj4ukm5etNqFD NLhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696938324; x=1697543124; 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=kDGvHlU5BSvandKxXhGpvwoZkbTINcvHi2wx3/cjWcg=; b=iEP03h6HQQEvRpqxyeoJCaSgdDRCmO+kKKxPBpVcGGLL3zhnIk3PbX7PIwuEQ4MGzn /Toja0DfoAPsYoXKUh94wZ2R9MsnKY2CczdRb4igWcZfvW3Qu2a9JkBON/m79tYqcrN4 CF4X1F+3K3c/CBcSu8Ari8e44xsT5yMDUpqPJDPuX7cnPTC8TzSQ5KWjNfg5RWiTDYUO +/x9iHBGj2E90dnqpLawchgr4mk3BfavJl3R1ySNT/oS0zZvD6yK4fY946lDxbEiHdaP L0Lw8hCfyQNbnN1vPDS+cHQ5y3c88yEctvSIlMGJZNWf5w5xD3cgLpmNWb+t0P4JVbTx Xdlw== X-Gm-Message-State: AOJu0Yy/egdYxdFjN9z9ENF7yEXYAWqHLsWWJqvLEYNSINI/8ZUTcSfC IWmZP7yRU+RzRs/Z0F8UASYR6wzHIg8= X-Received: by 2002:a17:902:dacd:b0:1c0:c174:3695 with SMTP id q13-20020a170902dacd00b001c0c1743695mr17498653plx.13.1696938324133; Tue, 10 Oct 2023 04:45:24 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:27d:3c0c:670b:6125]) by smtp.gmail.com with ESMTPSA id c8-20020a170903234800b001c32fd9e412sm11500355plh.58.2023.10.10.04.45.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 04:45:23 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 822791142280; Tue, 10 Oct 2023 22:15:20 +1030 (ACDT) Date: Tue, 10 Oct 2023 22:15:20 +1030 From: Alan Modra To: binutils@sourceware.org Subject: asan: invalid free in bfd_init_section_compress_status Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3033.6 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779368824557410055 X-GMAIL-MSGID: 1779368824557410055 With specially crafted compressed sections, it's possible to tickle a problem when decompressing: If the compression headers says the uncompressed size is zero, this will be seen as an error return from bfd_compress_section_contents. On errors the caller should free any malloc'd input buffers, but this isn't really an error and the section contents have been updated to a bfd_alloc'd buffer which can't be freed. * compress.c (bfd_compress_section_contents): Return -1 as error rather than 0. (bfd_init_section_compress_status, bfd_compress_section): Adjust. diff --git a/bfd/compress.c b/bfd/compress.c index 83564fea15f..7ec7b0edf38 100644 --- a/bfd/compress.c +++ b/bfd/compress.c @@ -558,7 +558,7 @@ decompress_contents (bool is_zstd, bfd_byte *compressed_buffer, field was allocated using bfd_malloc() or equivalent. Return the uncompressed size if the full section contents is - compressed successfully. Otherwise return 0. */ + compressed successfully. Otherwise return (bfd_size_type) -1. */ static bfd_size_type bfd_compress_section_contents (bfd *abfd, sec_ptr sec) @@ -610,7 +610,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec) buffer_size = uncompressed_size; buffer = bfd_malloc (buffer_size); if (buffer == NULL) - return 0; + return (bfd_size_type) -1; if (!decompress_contents (ch_type == ch_compress_zstd, input_buffer + orig_header_size, @@ -618,7 +618,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec) { bfd_set_error (bfd_error_bad_value); free (buffer); - return 0; + return (bfd_size_type) -1; } free (input_buffer); bfd_set_section_alignment (sec, uncompressed_alignment_pow); @@ -636,7 +636,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec) buffer_size = compressed_size; buffer = bfd_alloc (abfd, buffer_size); if (buffer == NULL) - return 0; + return (bfd_size_type) -1; if (update) { @@ -659,7 +659,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec) { bfd_release (abfd, buffer); bfd_set_error (bfd_error_bad_value); - return 0; + return (bfd_size_type) -1; } #endif } @@ -669,7 +669,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec) { bfd_release (abfd, buffer); bfd_set_error (bfd_error_bad_value); - return 0; + return (bfd_size_type) -1; } compressed_size += new_header_size; @@ -1087,7 +1087,7 @@ bfd_init_section_compress_status (bfd *abfd, sec_ptr sec) } sec->contents = uncompressed_buffer; - if (bfd_compress_section_contents (abfd, sec) == 0) + if (bfd_compress_section_contents (abfd, sec) == (bfd_size_type) -1) { free (sec->contents); sec->contents = NULL; @@ -1130,7 +1130,7 @@ bfd_compress_section (bfd *abfd, sec_ptr sec, bfd_byte *uncompressed_buffer) } sec->contents = uncompressed_buffer; - if (bfd_compress_section_contents (abfd, sec) == 0) + if (bfd_compress_section_contents (abfd, sec) == (bfd_size_type) -1) { free (sec->contents); sec->contents = NULL;