Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jYU5o-0002e4-6R for mharc-grub-devel@gnu.org; Tue, 12 May 2020 08:33:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41158) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jYU5c-0002dG-Md for grub-devel@gnu.org; Tue, 12 May 2020 08:32:56 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:29834 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jYU5Z-0007oU-Tn for grub-devel@gnu.org; Tue, 12 May 2020 08:32:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589286767; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AHw+ag4w0nXBncK+4oGG/IM7k5TZFgCxfQPT6OsNYBo=; b=J4I4lBO4mU+gHlDHfguB7fqv6ok1qVCyelU0xe3+/lzTI8leqAiH9dyqia3RKoZrG2nBgh snvFhLkShFt6t0RVB/3wLEQ7eeHxKcMyAhQ03wb8yxDoj6GSEmh9IgA7AefX97ixV3YAQb 1EVxUh7xjg1PKQvjnmbtTk6p4EUYZdc= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-115-cgr64_BvPGqLFNq2-zYW4A-1; Tue, 12 May 2020 08:32:45 -0400 X-MC-Unique: cgr64_BvPGqLFNq2-zYW4A-1 Received: by mail-wr1-f71.google.com with SMTP id f2so6882246wrm.9 for ; Tue, 12 May 2020 05:32:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AHw+ag4w0nXBncK+4oGG/IM7k5TZFgCxfQPT6OsNYBo=; b=i6KZCHSs5LSSATtPoob0d39ZQqlGrmFf/auM/isbCbeIfWv7wR8SZ/pv8h4OEQrjJC Dkrp1v5gv/5ZEEszhZIjlNNwRWRmD49EADpmf5hLNPtJ12CaP/WMeUlFYmzIQ1AL2UBK kD9IeRmdi7w6ixgl3vuS9IH/I8ZIsASuYvssuuz3DiiVYlenzu8m2bze6mCFxviN4x1J az/7P/R0vAjShOtZiQUWDCne693eEooye9BDsZOf2Vu5QyZgwPuUZFjcdcMDtJyhZDYf XYIegJhp8gmYv0Byg8DrL0ey6WswZxX4ScAkmYEsw+3B63LOHEW+7I6gGRzMg/B72aPL cI2w== X-Gm-Message-State: AGi0PuZGfXEGYIrfZICITNcWhAhvzMYQBOVqNy05i11xgUk8TBFGdVoC OEsVZZJXino93TYEnp1/ZofvCN8sP3pUdUK8DW/jlbpyhIBSzV2r84/tsXTOC2rKkmJnmqyfQsL Wh8Xs9kLKydI= X-Received: by 2002:adf:dc86:: with SMTP id r6mr15493394wrj.45.1589286764265; Tue, 12 May 2020 05:32:44 -0700 (PDT) X-Google-Smtp-Source: APiQypIDgbywrv2r0OePh0GvW35Spd+Pjvn+emCLzxYpy17iUWOsRiRNErLm6YaAlzcii7k+ak/4Qg== X-Received: by 2002:adf:dc86:: with SMTP id r6mr15493374wrj.45.1589286763961; Tue, 12 May 2020 05:32:43 -0700 (PDT) Received: from minerva.redhat.com (208.red-88-2-40.staticip.rima-tde.net. [88.2.40.208]) by smtp.gmail.com with ESMTPSA id n25sm32045257wmk.9.2020.05.12.05.32.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 May 2020 05:32:43 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Javier Martinez Canillas , =?UTF-8?q?Renaud=20M=C3=A9trich?= , Daniel Kiper Subject: [PATCH] envblk: Fix buffer overrun when attempting to shrink a variable value Date: Tue, 12 May 2020 14:32:25 +0200 Message-Id: <20200512123225.2910561-1-javierm@redhat.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=207.211.31.120; envelope-from=javierm@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/12 02:20:15 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 May 2020 12:32:57 -0000 If an existing variable is set with a value whose length is smaller than the current value, a memory corruption can happen due copying padding '#' characters outside of the environment block buffer. This is caused by a wrong calculation of the previous free space position after moving backward the characters that followed the old variable value. That position is calculated to fill the remaining of the buffer with the padding '#' characters. But since isn't calculated correctly, it can lead to copies outside of the buffer. The issue can be reproduced by creating a variable with a large value and then try to set a new value that is much smaller: $ grub2-editenv --version grub2-editenv (GRUB) 2.04 $ grub2-editenv env create $ grub2-editenv env set a="$(for i in {1..500}; do var="b$var"; done; echo $var)" $ wc -c env 1024 grubenv $ grub2-editenv env set a="$(for i in {1..50}; do var="b$var"; done; echo $var)" malloc(): corrupted top size Aborted (core dumped) $ wc -c env 0 grubenv Reported-by: Renaud Métrich Signed-off-by: Javier Martinez Canillas --- grub-core/lib/envblk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grub-core/lib/envblk.c b/grub-core/lib/envblk.c index 230e0e9d9ab..2e4e78b132d 100644 --- a/grub-core/lib/envblk.c +++ b/grub-core/lib/envblk.c @@ -143,7 +143,7 @@ grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value) /* Move the following characters backward, and fill the new space with harmless characters. */ grub_memmove (p + vl, p + len, pend - (p + len)); - grub_memset (space + len - vl, '#', len - vl); + grub_memset (space - (len - vl), '#', len - vl); } else /* Move the following characters forward. */ -- 2.25.4