Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp876361pxk; Thu, 17 Sep 2020 20:03:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyOtKuMUfdu6+ZsHMO+9LAjQiTS+4GoqCkWyfQQ0dpBeWHgJcCtYbuKz0sK1VcUdPGp/nHR X-Received: by 2002:aa7:cd06:: with SMTP id b6mr35103029edw.196.1600398184758; Thu, 17 Sep 2020 20:03:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398184; cv=none; d=google.com; s=arc-20160816; b=Wih+YCSfud3rsY7qQHXiKsqmdFwlI3W/IrpwVcrih0TLY2A0nOXM8koWlDGDYC0en9 lI6SrKKrX0xQruPYAMcfn7DpqgT7ULxCiUGdqWPlbCLwo6T5wQM4ps7qQhajokzp+pSs Y+kLFr1TAbwws9PMo28FvUWqRdkZz1nAd8d3SgQN27ozAFIkQ+fnstNmR9UZqr45MJgj HcR63Huc0TYkSUc3rvir4uLXIqPhuvw7LUfkunkzJvt5xEB9HQKKgvTS4t0mq6i51OYI Br9Ee2+QPLeGzvscvGqpneOy2KGn6qGO7npV9DJ/nFhTxlcWJrWXJmdPUI4AU93A2rdi T8cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dg8dhpYF5g4AfGrsO3l33ywFmai8hj6cjWz695lwEu0=; b=ARnBTSUBD2NdE8lU7WTvUk348jWSClCr7dyGc+epnN9gGsQ7hOEdviON4CaIMyZM3b mFx59X35td80FJf0CQnYIpc1ftfMwz8iSV7vZc2OJaQ+SDqbuZ1DAt2y6mTmvv4CmFoi eUG++oOvGVUwGO18AE5fG3TDHLneAR7gLYMoUc8YPpuLQxuaTQYwEM8+oWvdzjKO1W/w Ilempvmdg+iaVpYE4BqpS4t5lngBeWm7PefhLOEziazYPb0LMKkxJsAfYDh4RL003j6J hLhJ0vyk5EcFqKTnemJPwzhcGRT6LBHInHHv7Zn6xXyAv12TM6CL2A5JmbepWjpvxM2J qMxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l3r67X5o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c15si1114665eds.227.2020.09.17.20.02.41; Thu, 17 Sep 2020 20:03:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l3r67X5o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729482AbgIRDA1 (ORCPT + 99 others); Thu, 17 Sep 2020 23:00:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:54108 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726835AbgIRCFj (ORCPT ); Thu, 17 Sep 2020 22:05:39 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 16779238E3; Fri, 18 Sep 2020 02:05:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394738; bh=TqL409WgYKpWzy5UO8kMIbz+JvY5JovFRKqmViX3KYE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l3r67X5o2YaclDueKE0aACl7zQrVK8UNVvvAJxlK7VDvX6TgqZ6sarRQuKXIOWkh6 kESD/InXEUc/8vGU6W2gqXo9FFT8eVlo94njY+GKYjNzXzVdfrT2Q5bfrt6C7mRC2T 5/1ZEuXM5jNfK9rGtE4OvBZQjoKKbV0t4q0LB+PY= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Liu Song , Richard Weinberger , Sasha Levin , linux-mtd@lists.infradead.org Subject: [PATCH AUTOSEL 5.4 219/330] ubifs: Fix out-of-bounds memory access caused by abnormal value of node_len Date: Thu, 17 Sep 2020 21:59:19 -0400 Message-Id: <20200918020110.2063155-219-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liu Song [ Upstream commit acc5af3efa303d5f36cc8c0f61716161f6ca1384 ] In “ubifs_check_node”, when the value of "node_len" is abnormal, the code will goto label of "out_len" for execution. Then, in the following "ubifs_dump_node", if inode type is "UBIFS_DATA_NODE", in "print_hex_dump", an out-of-bounds access may occur due to the wrong "ch->len". Therefore, when the value of "node_len" is abnormal, data length should to be adjusted to a reasonable safe range. At this time, structured data is not credible, so dump the corrupted data directly for analysis. Signed-off-by: Liu Song Signed-off-by: Richard Weinberger Signed-off-by: Sasha Levin --- fs/ubifs/io.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/fs/ubifs/io.c b/fs/ubifs/io.c index 8ceb51478800b..7e4bfaf2871fa 100644 --- a/fs/ubifs/io.c +++ b/fs/ubifs/io.c @@ -225,7 +225,7 @@ int ubifs_is_mapped(const struct ubifs_info *c, int lnum) int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, int offs, int quiet, int must_chk_crc) { - int err = -EINVAL, type, node_len; + int err = -EINVAL, type, node_len, dump_node = 1; uint32_t crc, node_crc, magic; const struct ubifs_ch *ch = buf; @@ -278,10 +278,22 @@ int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum, out_len: if (!quiet) ubifs_err(c, "bad node length %d", node_len); + if (type == UBIFS_DATA_NODE && node_len > UBIFS_DATA_NODE_SZ) + dump_node = 0; out: if (!quiet) { ubifs_err(c, "bad node at LEB %d:%d", lnum, offs); - ubifs_dump_node(c, buf); + if (dump_node) { + ubifs_dump_node(c, buf); + } else { + int safe_len = min3(node_len, c->leb_size - offs, + (int)UBIFS_MAX_DATA_NODE_SZ); + pr_err("\tprevent out-of-bounds memory access\n"); + pr_err("\ttruncated data node length %d\n", safe_len); + pr_err("\tcorrupted data node:\n"); + print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1, + buf, safe_len, 0); + } dump_stack(); } return err; -- 2.25.1