Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1737313pxb; Fri, 1 Oct 2021 18:39:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBLBB2ytEP1TK/vSSLwp+AcvGMZ+lfhOUBn+cSB//nmBHo6cl6nGdYeQ20c9/B0JnTfgAT X-Received: by 2002:a62:5a41:0:b0:448:152d:78bf with SMTP id o62-20020a625a41000000b00448152d78bfmr13148899pfb.77.1633138787229; Fri, 01 Oct 2021 18:39:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633138787; cv=none; d=google.com; s=arc-20160816; b=ffsQhU/sMaWudhOdu7kkQ6VUWp/YCkM8E5l5uJPg87gUOC9bUhasDXGR3daBFRlbJz zJb/3mxkUMD4l4S4yFN028REWMIAWVz1ukhDgUPy+eJcwM649bC1DO+KErlhVm+Gujqm MbHQgYpnjX3zbWQ18gAlFe6nCvEySnVPzN9f+CYFVe6D4hjrLnHrct6myOn0X78mIK5o WW6o339j1uvDjGEedRsX+Zxr+uOl2oiS0aOtwhO80fv7hoxzVab6BTroWf98f/pYeGk7 /eufcb0eD2QSOk6kRgJ5/NotEKMGmDdRxoT92HLEJMN1uzdAnblAO/YKR0GWH2vas2Qf WmQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=3skiXeSFx93MB2hEdNJh2A8SAF5NjVQ9efw10AOuPtg=; b=0cz8YJkk4EFsCTWAJowjQ2bT1Je08vYNnyzwzdP+7QXakqc4h9DfsKTVmQ5osOrze1 kVEl3g2wL5zfHuCW46kD1ofhGQSviOocH6pjcQBUrWqV/uNgywrXFqkF0RIyFnkzyGV7 N/HvFRDpk/qEyPNsfP70/fgIyjNr422Pv6VBzsu+5zJPx3j0BfZEB9eluAWrBn3CnUS8 c6ZbNNDVEgAht2xiT/cHo2ah5cQy8q2reS1VKxFMByxPHqOaT1Xhk6EQH+K1WcAN/Mcv TJxxC++AywDg5IQAr/7BM4XA+1hBQSUk8GcvPrcISDHG8oatTWKbCE0hmVfhEUDJj39u Z0hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BB8RSZu+; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l10si9600198pgq.267.2021.10.01.18.39.22; Fri, 01 Oct 2021 18:39:47 -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=@google.com header.s=20210112 header.b=BB8RSZu+; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232154AbhJBBie (ORCPT + 99 others); Fri, 1 Oct 2021 21:38:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230255AbhJBBid (ORCPT ); Fri, 1 Oct 2021 21:38:33 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA099C0613E4 for ; Fri, 1 Oct 2021 18:36:48 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id x3-20020a05620a448300b0045e3e24de82so18357136qkp.3 for ; Fri, 01 Oct 2021 18:36:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=3skiXeSFx93MB2hEdNJh2A8SAF5NjVQ9efw10AOuPtg=; b=BB8RSZu+hVVoIAHC03lA+KeGFyxaW6TysPpj05M/nHJglND+GBmXBz2mSwDJlxlM7b H6ZXYazCPfTMzW5LV2MaaKivMQHjvMb76bpNbEGVwrd47NI/UYn4jdFT+t8TJE07h4mo imlgbXAUIhit7X9b0CY8NKgTJ1FcStGiPAaf0YE1uaBYLx+uKhqJoXZ+Imgb+X2UQf64 qaFE8sskISeUiE1vt6gRwmFBFlzqAczVAJWVXW8Km5SXTOe9IDJxTW9ej9rWhPEkD6xd PlsHp9sN5Y0WSMRgGTxrQ6fDzR0b1P7HD+EcWFbR+UJBD1Rhj+6SbY8xVo2lYPXortAf IUiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=3skiXeSFx93MB2hEdNJh2A8SAF5NjVQ9efw10AOuPtg=; b=giXKCPfEHtT7W8tnr7iVNbPfloRqbYtgEL3lAXOhGvjQNphMkdsIdZxQZy4jFNJrOW xc6qXTLho0RhYv45w5nhKkxNW2AKfZK8dPwloFi6XH6I3//Fesp5w71f0iC0rDGQzsL3 YtIf4YOHB0uFJ6v1CRadnYdbmbC+lNd1aP6ySUkY+RUXOHaXoH8RG4EuHfi3hCEYpY3x Ve0CFEvBCsya3tcM0cAb+wnaZtQJsj4qH9eUg9jZALrqT+uBrT6zdZC0Y4j2ulB0VuEI LP+yPCgTaeXGWuQffuR3I4T85hZMjrDlhwdJsD2xJF0JJEwbvkPvlUQk3Gkc7OXrRt5u /SkA== X-Gm-Message-State: AOAM530CxbnM2TU/W5SNbZNljuR5GZ+7ejtpapJUcJqi5Rkzro58YWF9 9VfABcWBLdmp8Ev6O3hkPjlwPvr3B3LABg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:1e46:b2fa:2be8:301e]) (user=dlatypov job=sendgmr) by 2002:a0c:c2c4:: with SMTP id c4mr13460268qvi.30.1633138607708; Fri, 01 Oct 2021 18:36:47 -0700 (PDT) Date: Fri, 1 Oct 2021 18:36:35 -0700 Message-Id: <20211002013635.2076371-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.800.g4c38ced690-goog Subject: [PATCH] kunit: fix too small allocation when using suite-only kunit.filter_glob From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov , kernel test robot Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a user filters by a suite and not a test, e.g. $ ./tools/testing/kunit/kunit.py run 'suite_name' it hits this code const int len = strlen(filter_glob); ... parsed->suite_glob = kmalloc(len, GFP_KERNEL); which fails to allocate space for the terminating NULL. Somehow, it seems like we can't easily reproduce this under UML, so the existing `parse_filter_test()` didn't catch this. Fix this by allocating `len + 1` and switch to kzalloc() just to be a bit more defensive. We're only going to run this code once per kernel boot, and it should never be very long. Also update the unit tests to be a bit more cautious. This bug showed up as a NULL pointer dereference here: > KUNIT_EXPECT_STREQ(test, (const char *)filtered.start[0][0]->name, "suite0"); `filtered.start[0][0]` was NULL, and `name` is at offset 0 in the struct, so `...->name` was also NULL. Fixes: 3b29021ddd10 ("kunit: tool: allow filtering test cases via glob") Reported-by: kernel test robot Signed-off-by: Daniel Latypov --- lib/kunit/executor.c | 2 +- lib/kunit/executor_test.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c index bab3ab940acc..1d7fecd33261 100644 --- a/lib/kunit/executor.c +++ b/lib/kunit/executor.c @@ -33,7 +33,7 @@ static void kunit_parse_filter_glob(struct kunit_test_filter *parsed, const char *period = strchr(filter_glob, '.'); if (!period) { - parsed->suite_glob = kmalloc(len, GFP_KERNEL); + parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL); parsed->test_glob = NULL; strcpy(parsed->suite_glob, filter_glob); return; diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c index e6323f398dfa..7d2b8dc668b1 100644 --- a/lib/kunit/executor_test.c +++ b/lib/kunit/executor_test.c @@ -149,6 +149,7 @@ static void filter_suites_test(struct kunit *test) KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start[0]); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start[0][0]); KUNIT_EXPECT_STREQ(test, (const char *)filtered.start[0][0]->name, "suite0"); } base-commit: 3b29021ddd10cfb6b2565c623595bd3b02036f33 -- 2.33.0.800.g4c38ced690-goog