Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp731536pxu; Sun, 25 Oct 2020 15:21:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWy6Yb+H1yRiqWmArTMoY74yTMEfaHG2H0+CnitEN0Os1qeVOEL7eJiKpqroGI/P8EIalR X-Received: by 2002:a17:907:a9c:: with SMTP id by28mr12295781ejc.62.1603664485288; Sun, 25 Oct 2020 15:21:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603664485; cv=none; d=google.com; s=arc-20160816; b=L36pB5mffgGivnwjZGhZd35rsupQM11+/5PeJ7LMnSowMvHjNN/PK04Su+T3Qc2RyC lPp3DvfoZKzgOxsxLGxr6X6D8HoLbbQOw5vjZsNAFSaF5jZuVY0gozqs2ouF93MSAICG ilG5mMG+X63viOQ4Peo0+bgmkobtywLfDs4dPCZR/pDMtrz304KqhlpnGUM+jKo5eB5v A86c14QmKjJ2szapKDTdT9ozLwsqOMfhghgLB+cjCEoRh8Pk6RjUH0fYlMyE+QqZ3SI4 VFJJbpJNtRkK3vsKgnaCnUYa+1JIs0nu03g9EfwCO296xKOvhSGfBZzEfJxUHVFbEcSb 8L0g== 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=FOzxSXm+DGRoQV0sCFeUY614U62H2fJhtP9E6Tri+CI=; b=vAic+vyhoqCS/4eYlGEQixQ4XwL9TP69S2VY2LIpiRgzko6rzhkoiIu8evGTNnWZKZ m0fpUqA7drGfPAHIMGd0l1DGk+Iob0ZTcqxHavMSKsMJpa/IVwU1VgmlNqSCGpTK2HLy 6rfCP7UgLd4cntE22vzSbmel2XZb0g/fUQK2pzJFEcO9/DP0Y4wwluSSLM/ftrzKZhOQ sXNR4NlOmzk3/6GtE1/9GFvVXmeo7svRng2gPEkeNjqP+xncjkA7rrx5Un0MPgCmj+IW 7b87k5e87ZV3uru4oteBd7S/kFDEdCx6sURr2YneA4YwtlGh0d0y2UngnbD+Az0S7qoF mxSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=DO8DQqwb; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mb25si7333424ejb.677.2020.10.25.15.21.03; Sun, 25 Oct 2020 15:21:25 -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=@rasmusvillemoes.dk header.s=google header.b=DO8DQqwb; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1419009AbgJYVtD (ORCPT + 99 others); Sun, 25 Oct 2020 17:49:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1418996AbgJYVsy (ORCPT ); Sun, 25 Oct 2020 17:48:54 -0400 Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 907F2C061755 for ; Sun, 25 Oct 2020 14:48:52 -0700 (PDT) Received: by mail-ed1-x542.google.com with SMTP id l16so7413757eds.3 for ; Sun, 25 Oct 2020 14:48:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FOzxSXm+DGRoQV0sCFeUY614U62H2fJhtP9E6Tri+CI=; b=DO8DQqwbYycUzHRUdNhG9dr1ti5lEGR2HO916Oncn+gYlMV1VTWMH2xiOwy0qP+bs4 PAu/zKktPCtkEtJJufdHiqxkftnY+eEyCsGMpqaXmZvr8LrLyoeupnX+ds1aevZKLlY6 SNj6kz6lI7sd9YTv88UQ1k+yHDlMIcrzo/yag= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=FOzxSXm+DGRoQV0sCFeUY614U62H2fJhtP9E6Tri+CI=; b=F4ytp/8iu3tYUDRtKkHF/DmE/NhLHVybxT6BVVnRZuvk1XrKVa+ouVfUzdqx6J9kj5 78XtZSlIWGgplcGnQK7ZSq4kr5W5J1io2nTecoVgvhv6jil3zOHtTPGG8ald/3oa2LXn uDZDofkkOb6zD1nVsO3EQVAiCtl0MycYC9wVePHgKsp4cupiV638y3yxBhdUvmEd7Uis m3sgpUghhqDSwbQQOR2l+VRX9y8YWv4bHLaI0aFM1ClskCIyrkmeqxuUVx5GucWbSH1t fqhLaSsJviDHVL6aK6yaCEZ2i0OpgUH6qOqsDvz/oEAz01iaJU+0+nVOT32AsTIN75uU g+fA== X-Gm-Message-State: AOAM5339BA1mGsbM4RTGSR10xR+6LmTW9iWYHexozaihicx/ZKzIvAcj OKtgPqzHcFZnj8bWSAwQiNHB7w== X-Received: by 2002:aa7:d28a:: with SMTP id w10mr13156098edq.192.1603662531235; Sun, 25 Oct 2020 14:48:51 -0700 (PDT) Received: from prevas-ravi.prevas.se (5.186.115.188.cgn.fibianet.dk. [5.186.115.188]) by smtp.gmail.com with ESMTPSA id k18sm4115867eds.93.2020.10.25.14.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 14:48:50 -0700 (PDT) From: Rasmus Villemoes To: Shuah Khan , Kees Cook Cc: Petr Mladek , Willy Tarreau , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Arpitha Raghunandan <98.arpi@gmail.com>, Andy Shevchenko , Brendan Higgins , Rasmus Villemoes Subject: [PATCH 4/4] lib/test_printf.c: use deterministic sequence of random numbers Date: Sun, 25 Oct 2020 22:48:42 +0100 Message-Id: <20201025214842.5924-5-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201025214842.5924-1-linux@rasmusvillemoes.dk> References: <20201025214842.5924-1-linux@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The printf test suite does each test with a few different buffer sizes to ensure vsnprintf() behaves correctly with respect to truncation and size reporting. It calls vsnprintf() with a buffer size that is guaranteed to be big enough, a buffer size of 0 to ensure that nothing gets written to the buffer, but it also calls vsnprintf() with a buffer size chosen to guarantee the output gets truncated somewhere in the middle. That buffer size is chosen randomly to increase the chance of finding some corner case bug (for example, there used to be some %p extension that would fail to produce any output if there wasn't room enough for it all, despite the requirement of producing as much as there's room for). I'm not aware of that having found anything yet, but should it happen, it's annoying not to be able to repeat the test with the same sequence of truncated lengths. For demonstration purposes, if we break one of the test cases deliberately, we still get different buffer sizes if we don't pass the seed parameter: root@(none):/# modprobe test_printf [ 15.317783] test_printf: vsnprintf(buf, 18, "%piS|%pIS", ...) wrote '127.000.000.001|1', expected '127-000.000.001|1' [ 15.323182] test_printf: failed 3 out of 388 tests [ 15.324034] test_printf: random seed used was 0x278bb9311979cc91 modprobe: ERROR: could not insert 'test_printf': Invalid argument root@(none):/# modprobe test_printf [ 13.940909] test_printf: vsnprintf(buf, 22, "%piS|%pIS", ...) wrote '127.000.000.001|127.0', expected '127-000.000.001|127.0' [ 13.944744] test_printf: failed 3 out of 388 tests [ 13.945607] test_printf: random seed used was 0x9f72eee1c9dc02e5 modprobe: ERROR: could not insert 'test_printf': Invalid argument but to repeat a specific sequence of tests, we can do root@(none):/# modprobe test_printf seed=0x9f72eee1c9dc02e5 [ 448.328685] test_printf: vsnprintf(buf, 22, "%piS|%pIS", ...) wrote '127.000.000.001|127.0', expected '127-000.000.001|127.0' [ 448.331650] test_printf: failed 3 out of 388 tests [ 448.332295] test_printf: random seed used was 0x9f72eee1c9dc02e5 modprobe: ERROR: could not insert 'test_printf': Invalid argument Signed-off-by: Rasmus Villemoes --- lib/test_printf.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/test_printf.c b/lib/test_printf.c index 1ed4a27390cb621715ab..bbea8b807d1eafe67e01 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -24,6 +24,7 @@ #include +#define KSTM_RANDOM 1 #include "../tools/testing/selftests/kselftest_module.h" #define BUF_SIZE 256 @@ -111,8 +112,14 @@ __test(const char *expect, int elen, const char *fmt, ...) * be able to print it as expected. */ failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap); - rand = 1 + prandom_u32_max(elen+1); - /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ + rand = prandom_u32_range_state(&rnd_state, 1, elen + 1); + /* + * Except for elen == 0, we have 1 <= rand <= elen < BUF_SIZE, + * i.e., the output is guaranteed to be truncated somewhere in + * the middle, and we're not pretending the buffer to be + * larger than it really is. For the boring case of elen == 0, + * rand is 1, which is of course also <= BUF_SIZE. + */ failed_tests += do_test(rand, expect, elen, fmt, ap); failed_tests += do_test(0, expect, elen, fmt, ap); -- 2.23.0