From patchwork Mon Mar 6 11:13:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 64608 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1780864wrd; Mon, 6 Mar 2023 03:23:39 -0800 (PST) X-Google-Smtp-Source: AK7set+jAKCEK7t4UQUs1loaUBhs1RmenrGAko/2inYAmV/FJmzA1KBczvKYtP86rfm+J7veTaa2 X-Received: by 2002:a17:907:a602:b0:8ae:e82a:3230 with SMTP id vt2-20020a170907a60200b008aee82a3230mr10489465ejc.70.1678101819196; Mon, 06 Mar 2023 03:23:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678101819; cv=none; d=google.com; s=arc-20160816; b=Iu7FITZgakJYWHy8UVb4yrbUnHRxZ/ZwwBodeYoYB7C+MqpYOXX8/hMuG4YaoXIg4w mZLL0WyOXut40gxeZvVIjBSEvPrQtI9UR+ZG+pN5hQOXVJWv4gG77F/NQLTooJX3GeUa BDfCyvWYMfI0GQ+C7TYtGpmihR+fSfHlZGrwfziToPc0c/p+HL1nF683AeaEsXFdf9fL 7utl/iVTAG6KpICH4mi/QCetwQUM5MLVlnyVtc+c4TGghy/uKPRbc6DifUBQZOi6KnBa 9PPQgSqdImb6d6FE8oJCboeDsEIY5+Rmx8plXvqqEcn+2wR549/H41CbXtQYXNP4yM/N 4fjg== 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:message-id:mime-version:date :dkim-signature; bh=1YDcalZhdE7jkjuSChjdb8BwAiaOVabLcoERtRrllCw=; b=g0VaSM3V0Ba+uYqkz05SGhQvHor5hvEhlTN7lHUAhpDcjG9YZmpiEWvGGoz8vewEDd ruAdD/s9zjEC/ogLpn3/jGIidfQsGb3Q0P/WfPze7k6AGkhSBYj6mAgFABBP8Q4zY39V kQLFONYToL9YqP/xgMlibAJlFUiqKVz/mlLeicsZB5L6SVMwE2HGieU1yHM7GIO91BsN EAYNVVsH5D/Yv4S4xOY0lvGKISKnHGXhJVHX1NvZwRUOEj7nChPouGTLlVmtApDwGvmd gu50PG829wtrLIgcCJ0J0AsTC3SuT1ssdmCbygEfxzRVT1DC1Bu0SENleFenC6J2cU4s BJrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=GQwsOOCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j11-20020a50ed0b000000b004c1de7910b0si10205728eds.130.2023.03.06.03.23.13; Mon, 06 Mar 2023 03:23:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=GQwsOOCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229844AbjCFLOK (ORCPT + 99 others); Mon, 6 Mar 2023 06:14:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230156AbjCFLNy (ORCPT ); Mon, 6 Mar 2023 06:13:54 -0500 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01CB81F5D5 for ; Mon, 6 Mar 2023 03:13:29 -0800 (PST) Received: by mail-ed1-x549.google.com with SMTP id b7-20020a056402350700b004d2a3d5cd3fso8112498edd.8 for ; Mon, 06 Mar 2023 03:13:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678101206; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=1YDcalZhdE7jkjuSChjdb8BwAiaOVabLcoERtRrllCw=; b=GQwsOOCwt1r07r7xKoYaCxR+u6XnS4orZ/5XRefQQxUFaCbyY2q5e5ZsvfxrpiDk9b SrwfsLoY5AOl6cH4+pIVBvvhMqLtXqfcy8LnJd9LtbA2puKgHz3XhwV1m7uKOrmoNcNZ hlyU8r2pyRGzu9SodZfYSbjLIs3eumpGUHxxGs8nswjvWh3ypHGXOhXmf3Z5y6t1yOHB PTUpGiRLo6SLlJ2eRtuMJJ71s3LG+L1Kf4lQq8GLOrUWsBFN7Z2GeK1vc6pd+PR5FSOz of5RXfO0kfqI0FEwSmJNakpzkIV4EY5XB1gC9vzImzS4WsgaqVPUR1M2jpo5c9uPmEJv OH/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678101206; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=1YDcalZhdE7jkjuSChjdb8BwAiaOVabLcoERtRrllCw=; b=lOiiwi1P7anCUDzimljItuCCtcGWj5N5TP/dsQ9dkXofVbp3MFru5Z8lkvuDdS1+ah fpwnJgv8B7rEjLuYyXwYRT3U4wuAUVL/TTvIyIiYc21ahjibQ2DcJ732Czf1dmYGo5Ly 9pviYCB9cRvN1GbZqinaOyZ2y85yDT6i8TzZ/pBv9M7OTQwrKgHvWOWu20ozOLclC5Vo kGpKF+OWB2QKPPRXWz8ve4fQMlH1DGc1IilzRVy67kR8iWMNzwKEv41MTHpiGZTJS9NO PZJAQR5EX2x5+2+FzHf7lEDD+8a7TEKOMm1Dn9fvtPtz5fp6YlKrWv6tD6faNNlv+e9W 6y2Q== X-Gm-Message-State: AO0yUKUc5Gp4zP2SZ8HZQxl4l0x/pvUKs5SSJjWj7kT5M31+ETCNVxCp /RCdWcHNTQaPk3Z18EIXnIWM0nJ1cq0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:b93a:5d85:6f2c:517d]) (user=glider job=sendgmr) by 2002:a17:906:ce38:b0:8b1:30da:b585 with SMTP id sd24-20020a170906ce3800b008b130dab585mr4991214ejb.6.1678101206445; Mon, 06 Mar 2023 03:13:26 -0800 (PST) Date: Mon, 6 Mar 2023 12:13:21 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230306111322.205724-1-glider@google.com> Subject: [PATCH 1/2] lib/stackdepot: kmsan: mark API outputs as initialized From: Alexander Potapenko To: glider@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, elver@google.com, dvyukov@google.com, kasan-dev@googlegroups.com, Andrey Konovalov X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759617293278554388?= X-GMAIL-MSGID: =?utf-8?q?1759617293278554388?= KMSAN does not instrument stackdepot and may treat memory allocated by it as uninitialized. This is not a problem for KMSAN itself, because its functions calling stackdepot API are also not instrumented. But other kernel features (e.g. netdev tracker) may access stack depot from instrumented code, which will lead to false positives, unless we explicitly mark stackdepot outputs as initialized. Cc: Andrey Konovalov Cc: Marco Elver Suggested-by: Dmitry Vyukov Signed-off-by: Alexander Potapenko Reported-by: syzbot Reviewed-by: Dmitry Vyukov Reviewed-by: Andrey Konovalov --- lib/stackdepot.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 036da8e295d19..2f5aa851834eb 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -306,6 +307,11 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->handle.extra = 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); pool_offset += required_size; + /* + * Let KMSAN know the stored stack record is initialized. This shall + * prevent false positive reports if instrumented code accesses it. + */ + kmsan_unpoison_memory(stack, required_size); return stack; } @@ -465,6 +471,12 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, struct stack_record *stack; *entries = NULL; + /* + * Let KMSAN know *entries is initialized. This shall prevent false + * positive reports if instrumented code accesses it. + */ + kmsan_unpoison_memory(entries, sizeof(*entries)); + if (!handle) return 0; From patchwork Mon Mar 6 11:13:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 64609 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1780981wrd; Mon, 6 Mar 2023 03:23:56 -0800 (PST) X-Google-Smtp-Source: AK7set9QyxtozTlxmgZJgTMpTt4fvn+2qECwQmCDug2vzvGw0ISlm8aYoRvgOObQyZ+3P2Iwoluc X-Received: by 2002:a17:907:a094:b0:8aa:c143:ffe1 with SMTP id hu20-20020a170907a09400b008aac143ffe1mr10929024ejc.48.1678101836453; Mon, 06 Mar 2023 03:23:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678101836; cv=none; d=google.com; s=arc-20160816; b=g46XIEAq8TRlEV8oVk9XmnpGvO4+83HSenLUw7cogOiMcDs4CitiXW2Eh38K+cYb7E Mgqw53XZo8kGqYm0WAakVWxVqnWKm911fFn/lsCZPUfD/RXCx01tKAOdRUbWWqDhZyMh A47Qdm5M/Owyaza6cdTdEy4nXSG3f7dwpwai5rmrfdL13wNRH9/k7KDj6/hfEA47/q1o Vr8rzVbIa+PY0q0KPS9SDXL7umCQKvVVxRD62xLtVrU59LBiNbZK5+bgMmPabKJJ6Dm2 K5pXaUAhKHnz4SvhWHHnhn0RQYFnsezMwdRBlLgX5IXZfopQmpeqVzU8+DPVJEXYpP3G 5iMg== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=i1L+pZCwrwV8WFeqR7MKglAsPEnoOPqo6PmA3RfPAYI=; b=N//HsO206M579IENvDfM5bo92VOZ4xCIAg7b7f0etm12ZyDZB+gZYPJVFFumbi/Ft2 eOAPb92OOatDpi7HOmJvpB4H9XYcNjyz4EGmWrUwJ+i8+98H+pCD/bUNHv0SQihwzrhA qO3kF1DM3PKete+wt8u8zb6ynFC091uQtBBS6Umb5FkEgQDbA+1p6w4N/mjRQFaVCJvX 5qVV0CV9l7KcKaaG3SRJehtV/n6ZXQmu18iJJjerISC16Nn+dpYquo4Vrriu6yFUmAsZ VW+wY6+1Lhl7GruVWnhmVIzlm2goDrULMrK5pEVGGEbbjaZ3bCETYy3Ya95HwAi+tvPS +oKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=P5DhRm6l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i21-20020a1709061cd500b008cd525eac7bsi9448020ejh.715.2023.03.06.03.23.33; Mon, 06 Mar 2023 03:23:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=P5DhRm6l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229952AbjCFLOM (ORCPT + 99 others); Mon, 6 Mar 2023 06:14:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229961AbjCFLN6 (ORCPT ); Mon, 6 Mar 2023 06:13:58 -0500 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D333233F5 for ; Mon, 6 Mar 2023 03:13:31 -0800 (PST) Received: by mail-ed1-x549.google.com with SMTP id h11-20020a0564020e8b00b004e59d4722a3so3197322eda.6 for ; Mon, 06 Mar 2023 03:13:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678101209; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i1L+pZCwrwV8WFeqR7MKglAsPEnoOPqo6PmA3RfPAYI=; b=P5DhRm6lyoKz3NoNdsdxwrRj/MfjpheohwuDiF7EpSFlNNXCKBaoHIBOiHLSs4Yzre 6zEPjyPu0V4FkWh1Jtnal2exA5k4WwW8AbTrCkoXx4RjCcJewqsOrcNgnaLuTBJEze/l VGuQ7KdXumG/1Oc8kkYYtbRiCbr1ZMr0hDisTKb/hBPLVRcl7r3dM9ZwyLhLY7bgcvph KiQP1/tCcC74t9C7yVQwSeKAFhacYwFs5vhzrqyMo5OOr6N+uRAZB0CYJdq9/1YDnZhg tYmjh5t8WjxwwB84qFIiW9FlQDDLzJjnFU3PfE7CAheT6nZrosN+8/etsfpD+UY+qiot Q12A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678101209; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i1L+pZCwrwV8WFeqR7MKglAsPEnoOPqo6PmA3RfPAYI=; b=fviWn4VTIR/9QFTgHiDBFDMFTQEZrHqax/iuSM8gNSQHLObnTsisK4brM8X8dfV87x T0wm2URyF/U+fFNLDHkAFfdALEX+6WyroqBPdAJE5mB/3puNIhsufzjenN0/waZ+xBRd SgIdAVp7kzRr1sc35pcYXT4GaEwYPJpNK0iYOr6HBAH6bNVkOJ24kLphChC5iF263bUN u7sZVzvIDJd0ICwPQNIdhXkSmDruXxQXhJ2hmj77vw/3K4TbOMP7HUX8iFoyawkKVfXw TCH8U2oRtjB8/rKRzPoQfkXqyFJNa11M7bZqydoynQFUv6BZBc4Y2EAflcHUHwkdMLyZ XOoA== X-Gm-Message-State: AO0yUKXrFahvJwvXF0JOtJotMZpthNEp2YFn283xb1Gm6jTZDzpnCNpZ NnpFHrgzSn+t0KYsHC9//2ewKhF2jV4= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:b93a:5d85:6f2c:517d]) (user=glider job=sendgmr) by 2002:a17:906:4f94:b0:8b1:7de9:b39b with SMTP id o20-20020a1709064f9400b008b17de9b39bmr5034537eju.1.1678101209226; Mon, 06 Mar 2023 03:13:29 -0800 (PST) Date: Mon, 6 Mar 2023 12:13:22 +0100 In-Reply-To: <20230306111322.205724-1-glider@google.com> Mime-Version: 1.0 References: <20230306111322.205724-1-glider@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230306111322.205724-2-glider@google.com> Subject: [PATCH 2/2] kmsan: add test_stackdepot_roundtrip From: Alexander Potapenko To: glider@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, elver@google.com, dvyukov@google.com, kasan-dev@googlegroups.com X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759617311588996352?= X-GMAIL-MSGID: =?utf-8?q?1759617311588996352?= Ensure that KMSAN does not report false positives in instrumented callers of stack_depot_save(), stack_depot_print(), and stack_depot_fetch(). Signed-off-by: Alexander Potapenko --- mm/kmsan/kmsan_test.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c index 7095d3fbb23ac..d9eb141c27aa4 100644 --- a/mm/kmsan/kmsan_test.c +++ b/mm/kmsan/kmsan_test.c @@ -551,6 +551,36 @@ static void test_long_origin_chain(struct kunit *test) KUNIT_EXPECT_TRUE(test, report_matches(&expect)); } +/* + * Test case: ensure that saving/restoring/printing stacks to/from stackdepot + * does not trigger errors. + * + * KMSAN uses stackdepot to store origin stack traces, that's why we do not + * instrument lib/stackdepot.c. Yet it must properly mark its outputs as + * initialized because other kernel features (e.g. netdev tracker) may also + * access stackdepot from instrumented code. + */ +static void test_stackdepot_roundtrip(struct kunit *test) +{ + unsigned long src_entries[16], *dst_entries; + unsigned int src_nentries, dst_nentries; + EXPECTATION_NO_REPORT(expect); + depot_stack_handle_t handle; + + kunit_info(test, "testing stackdepot roundtrip (no reports)\n"); + + src_nentries = + stack_trace_save(src_entries, ARRAY_SIZE(src_entries), 1); + handle = stack_depot_save(src_entries, src_nentries, GFP_KERNEL); + stack_depot_print(handle); + dst_nentries = stack_depot_fetch(handle, &dst_entries); + KUNIT_EXPECT_TRUE(test, src_nentries == dst_nentries); + + kmsan_check_memory((void *)dst_entries, + sizeof(*dst_entries) * dst_nentries); + KUNIT_EXPECT_TRUE(test, report_matches(&expect)); +} + static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_uninit_kmalloc), KUNIT_CASE(test_init_kmalloc), @@ -573,6 +603,7 @@ static struct kunit_case kmsan_test_cases[] = { KUNIT_CASE(test_memset32), KUNIT_CASE(test_memset64), KUNIT_CASE(test_long_origin_chain), + KUNIT_CASE(test_stackdepot_roundtrip), {}, };