Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp35780pxv; Wed, 14 Jul 2021 18:41:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDzLlYk5p2A8Vpmk7/Sl7FXvtSVRa088Ua6xgLbveJ091QvN4tjK1+1K1Dm0TzibLqSl4T X-Received: by 2002:a17:907:1ddb:: with SMTP id og27mr1494337ejc.540.1626313304678; Wed, 14 Jul 2021 18:41:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626313304; cv=none; d=google.com; s=arc-20160816; b=TXBmL/T3t56l5WrZCnjN16DjP5TzQLkdVPxU+f7Op2cU9CVqFCffY3S+ajqJsgbiwx aEvF/8fI1gc20+L2DW1Uco+vdbHE67HVfEseZL+EXKsihuNU+DpsH8K/jsqEynBnA4yA 9er+3ZXNzbKlKFDaKd8WRXBxFEv2BuAgj+1Jwnhrx5t68WsUtOowaEjPe3KjAeAB5S3w TKbO+/Y5Hm1oRYnmeiRrzYsGlIKvdQPl+nwwlImPUjd5YofZCVjQswS2dIWYggfdiQA6 djHnOGu5YfT26TgCCMVGp+fr1gQLsZtfZBALlxjI9IibyaHoheo8lxOlgZFDlqo5Hx6a yTeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=gvWXe+qS8mzkroqhMIe9tlAxSoJZ1mN+WmkWVFP4swk=; b=NzjXuivIx/jgms+HjYjaTTK0dR+EIaOej8Wmn/VV5mqUPBg+gE1yTYCJbI9U0l0zC3 EtKhpRWCYeELzfC7fa8IXrNg+F7ESWF243D+bc0PXbysqp+7WstgNBTb+H0UszICgA8/ O3ZyLL1IWFnPOeSMjZ2mwhO8qInaDZn8iIEd9bj92PUQxIVUumd+ZIAOyA9DBXrSo6Xh HzFZesw1vaXjRB0/8yzlXgAmz7/gc/3gPKsyyjQPjG7svGWYPIacQqcLn9PyRXvW4fs/ qtqPm1Kuas219oJlxim9fKvon5nALXXcGNpNou3+aiQIAsZgLaCAF6nCM7VaXpTx1Ojj O38Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w15si4514633edv.312.2021.07.14.18.41.22; Wed, 14 Jul 2021 18:41:44 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233625AbhGOBRg (ORCPT + 99 others); Wed, 14 Jul 2021 21:17:36 -0400 Received: from foss.arm.com ([217.140.110.172]:45128 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231165AbhGOBRf (ORCPT ); Wed, 14 Jul 2021 21:17:35 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9FFCC12FC; Wed, 14 Jul 2021 18:14:42 -0700 (PDT) Received: from entos-ampere-02.shanghai.arm.com (entos-ampere-02.shanghai.arm.com [10.169.214.103]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 770073F7D8; Wed, 14 Jul 2021 18:14:37 -0700 (PDT) From: Jia He To: Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , Jonathan Corbet , Alexander Viro , Linus Torvalds Cc: "Peter Zijlstra (Intel)" , Eric Biggers , "Ahmed S. Darwish" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Matthew Wilcox , Christoph Hellwig , nd@arm.com, Jia He Subject: [PATCH v7 4/5] lib/test_printf.c: split write-beyond-buffer check in two Date: Thu, 15 Jul 2021 09:14:06 +0800 Message-Id: <20210715011407.7449-5-justin.he@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210715011407.7449-1-justin.he@arm.com> References: <20210715011407.7449-1-justin.he@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rasmus Villemoes Before each invocation of vsnprintf(), do_test() memsets the entire allocated buffer to a sentinel value. That buffer includes leading and trailing padding which is never included in the buffer area handed to vsnprintf (spaces merely for clarity): pad test_buffer pad **** **************** **** Then vsnprintf() is invoked with a bufsize argument <= BUF_SIZE. Suppose bufsize=10, then we'd have e.g. |pad | test_buffer |pad | **** pizza0 **** ****** **** A B C D E where vsnprintf() was given the area from B to D. It is obviously a bug for vsnprintf to touch anything between A and B or between D and E. The former is checked for as one would expect. But for the latter, we are actually a little stricter in that we check the area between C and E. Split that check in two, providing a clearer error message in case it was a genuine buffer overrun and not merely a write within the provided buffer, but after the end of the generated string. So far, no part of the vsnprintf() implementation has had any use for using the whole buffer as scratch space, but it's not unreasonable to allow that, as long as the result is properly nul-terminated and the return value is the right one. However, it is somewhat unusual, and most % won't need this, so keep the [C,D] check, but make it easy for a later patch to make that part opt-out for certain tests. Signed-off-by: Rasmus Villemoes Tested-by: Jia He Signed-off-by: Jia He Reviewed-by: Petr Mladek Reviewed-by: Andy Shevchenko --- lib/test_printf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/test_printf.c b/lib/test_printf.c index 8ac71aee46af..cabdf9f5fd15 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -78,12 +78,17 @@ do_test(int bufsize, const char *expect, int elen, return 1; } - if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) { + if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", bufsize, fmt); return 1; } + if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) { + pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt); + return 1; + } + if (memcmp(test_buffer, expect, written)) { pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", bufsize, fmt, test_buffer, written, expect); -- 2.17.1