Message ID | 20231206150729.54604-1-marpagan@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4167246vqy; Wed, 6 Dec 2023 07:08:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnCYsJ5qpBAz++eJ19DxjlXmRl0VGj+HUH4v5Yjf66fmNBZf1s151m4i0ANGwHtP0lg1nL X-Received: by 2002:a05:6a00:939f:b0:6b6:5ed4:dd42 with SMTP id ka31-20020a056a00939f00b006b65ed4dd42mr976069pfb.31.1701875283347; Wed, 06 Dec 2023 07:08:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701875283; cv=none; d=google.com; s=arc-20160816; b=NuE/MUJyNdS7x6pHthcbbVxvnVB8TRfGV3/cYs8EVR3E3eIiNbcRfG2GeJZtsP1D33 sBrkhfmPql2thZoYUT98HQqnR64tNcyYRY9pOhOm1zfzWu9fb2AzIh2EcRI7EXEesuW3 Dps67hUMgucdyonpsr5EFlbZJBPHBD4nAiYiRoLj7RkbsmWxVhZxNiJ/p7sf7lNREslL AcFHLhoAC8CXyxmNt+WKdvHtdzhd8LkgktjuiEblPlx0AY64DP8/q868Q8LhpbfpMMdJ 1c63aNM6jGF11CVYqmZbx2E3gRkKMQbBb3XMU/0Nf8efQSWfSN31uIVtVkIhWa47SRqR vX1Q== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=/JBJQVO5XBoc9lUdPYm1Dkdtmlp9iMHA1ds5P3k5wtk=; fh=LkR0Ve3fsrJ9yqENNfOAEu74XyeF9ECftA2ty52GHrw=; b=CRu/ykHiz4Hn7kjU8WIMYG/lpNKndoSM+VqskqPXMtloljDM2yYvBnD0+cROjP080k 60VeHovMdesWo1JLYzJpnzOOJ3fLsd29nJ8AekDcgwjLWH1oac5LS0+wRUQx4TCfNbfB QzjfynTBenrKlLisA0vX2oUYRFsC6KrOIypPMzjDiPwhfyQJgxi19zEb4Z+i016LGYh+ GlLLgCsJg4boKQ9xmg2f0Q3oYPwa4kNb7In02hJFk8ENOaIAwzNhHBW+0UEexGg/N0v3 h8Jklqq4eeFAeWQp7pS2agzxLqfcHi177XcMj1Uj5KROn4f+GWRdMoB/HMyhWLqIhxGx n7Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JNeHjo0Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id bw14-20020a056a02048e00b005c69765acddsi48487pgb.87.2023.12.06.07.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 07:08:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JNeHjo0Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id C8B8D83348BA; Wed, 6 Dec 2023 07:07:53 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442388AbjLFPHp (ORCPT <rfc822;pusanteemu@gmail.com> + 99 others); Wed, 6 Dec 2023 10:07:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379115AbjLFPHm (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 6 Dec 2023 10:07:42 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62302D46 for <linux-kernel@vger.kernel.org>; Wed, 6 Dec 2023 07:07:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701875267; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=/JBJQVO5XBoc9lUdPYm1Dkdtmlp9iMHA1ds5P3k5wtk=; b=JNeHjo0Zqd9VpCbcepdiHQ/VtVqFsj8d7hTLCtMGjOVyw3IGmXb9keZTr8jGvdbft/XHr+ V7EsqIt77n6wqBkAL29HMKBqFIC856VV1JYWHY6nzypNsdYW2uM5/XkG9W0qUTZnb+964q d2eAR+TaHSiM6fDA5lrd7KZ//SVYNVw= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-320-AU8IRVTjNYqiuh7hhJ6ozg-1; Wed, 06 Dec 2023 10:07:43 -0500 X-MC-Unique: AU8IRVTjNYqiuh7hhJ6ozg-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-332e71b8841so800651f8f.0 for <linux-kernel@vger.kernel.org>; Wed, 06 Dec 2023 07:07:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701875258; x=1702480058; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/JBJQVO5XBoc9lUdPYm1Dkdtmlp9iMHA1ds5P3k5wtk=; b=mzwJnfibU39gaPoOT8TZG3ZbjLjUnCw5cWOHKNnrrLB293/tgnxy5xsizpisti53dP ScyczTXzWcGbOtbOEeVsASXqmycoQ4rou6kHDG4gvHVwbNGsa7ulMvRm1mNxtOmfe+Gl kr0f7Aq5ZwHRszsBKxGqch0wz/DTkQ/NVnXF2fi2Ge6YCot0mPpZcvC9J9/tu5CTR84n ZDZjAAtA/mq0cJyNCNZxpzO017cm+UIqFcbjcP3lPz6CQWUGXmab/BVrwmU6bK30PUl2 NuKeJLcPoqT4w5pgWVfODeUIjm6dG01LD8m4JUREdgyu4rgK2DuTV13snSrmm76N3glc XGgg== X-Gm-Message-State: AOJu0YzArj3lvFMGD4vgDHS6s/90KJz+pERYru+c+R56AwSorcyrsEHu 24CJhzmTmMdSsZ3CGHGqfFSGO5i6bfC29yy9v557srR7HcVepBg53My5ZM8Zt47fHHyIH5uRrSU 4bKlqqNrBioeSrXln2w1kGQo= X-Received: by 2002:a5d:4009:0:b0:333:5258:68ef with SMTP id n9-20020a5d4009000000b00333525868efmr692840wrp.68.1701875258351; Wed, 06 Dec 2023 07:07:38 -0800 (PST) X-Received: by 2002:a5d:4009:0:b0:333:5258:68ef with SMTP id n9-20020a5d4009000000b00333525868efmr692834wrp.68.1701875257975; Wed, 06 Dec 2023 07:07:37 -0800 (PST) Received: from klayman.redhat.com (net-2-34-30-38.cust.vodafonedsl.it. [2.34.30.38]) by smtp.gmail.com with ESMTPSA id c1-20020adfa301000000b003333a216682sm11896497wrb.97.2023.12.06.07.07.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 07:07:37 -0800 (PST) From: Marco Pagani <marpagan@redhat.com> To: Brendan Higgins <brendan.higgins@linux.dev>, David Gow <davidgow@google.com>, Shuah Khan <skhan@linuxfoundation.org>, Jinjie Ruan <ruanjinjie@huawei.com>, Rae Moar <rmoar@google.com> Cc: Marco Pagani <marpagan@redhat.com>, Richard Fitzgerald <rf@opensource.cirrus.com>, Javier Martinez Canillas <javierm@redhat.com>, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH v3] kunit: run test suites only after module initialization completes Date: Wed, 6 Dec 2023 16:07:28 +0100 Message-ID: <20231206150729.54604-1-marpagan@redhat.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 06 Dec 2023 07:07:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784545576721446474 X-GMAIL-MSGID: 1784545576721446474 |
Series |
[v3] kunit: run test suites only after module initialization completes
|
|
Commit Message
Marco Pagani
Dec. 6, 2023, 3:07 p.m. UTC
Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") fixed a wild-memory-access bug that could have happened during the loading phase of test suites built and executed as loadable modules. However, it also introduced a problematic side effect that causes test suites modules to crash when they attempt to register fake devices. When a module is loaded, it traverses the MODULE_STATE_UNFORMED and MODULE_STATE_COMING states before reaching the normal operating state MODULE_STATE_LIVE. Finally, when the module is removed, it moves to MODULE_STATE_GOING before being released. However, if the loading function load_module() fails between complete_formation() and do_init_module(), the module goes directly from MODULE_STATE_COMING to MODULE_STATE_GOING without passing through MODULE_STATE_LIVE. This behavior was causing kunit_module_exit() to be called without having first executed kunit_module_init(). Since kunit_module_exit() is responsible for freeing the memory allocated by kunit_module_init() through kunit_filter_suites(), this behavior was resulting in a wild-memory-access bug. Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") fixed this issue by running the tests when the module is still in MODULE_STATE_COMING. However, modules in that state are not fully initialized, lacking sysfs kobjects. Therefore, if a test module attempts to register a fake device, it will inevitably crash. This patch proposes a different approach to fix the original wild-memory-access bug while restoring the normal module execution flow by making kunit_module_exit() able to detect if kunit_module_init() has previously initialized the tests suite set. In this way, test modules can once again register fake devices without crashing. This behavior is achieved by checking whether mod->kunit_suites is a virtual or direct mapping address. If it is a virtual address, then kunit_module_init() has allocated the suite_set in kunit_filter_suites() using kmalloc_array(). On the contrary, if mod->kunit_suites is still pointing to the original address that was set when looking up the .kunit_test_suites section of the module, then the loading phase has failed and there's no memory to be freed. v3: - add a comment to clarify why the start address is checked v2: - add include <linux/mm.h> Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com> Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> Signed-off-by: Marco Pagani <marpagan@redhat.com> --- lib/kunit/test.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) base-commit: 33cc938e65a98f1d29d0a18403dbbee050dcad9a
Comments
On 06/12/2023 15:07, Marco Pagani wrote: > Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in > kunit_free_suite_set()") fixed a wild-memory-access bug that could have > happened during the loading phase of test suites built and executed as > loadable modules. However, it also introduced a problematic side effect > that causes test suites modules to crash when they attempt to register > fake devices. > > When a module is loaded, it traverses the MODULE_STATE_UNFORMED and > MODULE_STATE_COMING states before reaching the normal operating state > MODULE_STATE_LIVE. Finally, when the module is removed, it moves to > MODULE_STATE_GOING before being released. However, if the loading > function load_module() fails between complete_formation() and > do_init_module(), the module goes directly from MODULE_STATE_COMING to > MODULE_STATE_GOING without passing through MODULE_STATE_LIVE. > > This behavior was causing kunit_module_exit() to be called without > having first executed kunit_module_init(). Since kunit_module_exit() is > responsible for freeing the memory allocated by kunit_module_init() > through kunit_filter_suites(), this behavior was resulting in a > wild-memory-access bug. > > Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in > kunit_free_suite_set()") fixed this issue by running the tests when the > module is still in MODULE_STATE_COMING. However, modules in that state > are not fully initialized, lacking sysfs kobjects. Therefore, if a test > module attempts to register a fake device, it will inevitably crash. > > This patch proposes a different approach to fix the original > wild-memory-access bug while restoring the normal module execution flow > by making kunit_module_exit() able to detect if kunit_module_init() has > previously initialized the tests suite set. In this way, test modules > can once again register fake devices without crashing. > > This behavior is achieved by checking whether mod->kunit_suites is a > virtual or direct mapping address. If it is a virtual address, then > kunit_module_init() has allocated the suite_set in kunit_filter_suites() > using kmalloc_array(). On the contrary, if mod->kunit_suites is still > pointing to the original address that was set when looking up the > .kunit_test_suites section of the module, then the loading phase has > failed and there's no memory to be freed. > > v3: > - add a comment to clarify why the start address is checked > v2: > - add include <linux/mm.h> > > Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") > Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com> > Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> > Signed-off-by: Marco Pagani <marpagan@redhat.com> > --- For V3: Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com> Fixes this crash: https://lore.kernel.org/all/e239b94b-462a-41e5-9a4c-cd1ffd530d75@opensource.cirrus.com/ Also tested with sound/pci/hda/cirrus_scodec_test.c
On Wed, Dec 6, 2023 at 10:07 AM Marco Pagani <marpagan@redhat.com> wrote: > > Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in > kunit_free_suite_set()") fixed a wild-memory-access bug that could have > happened during the loading phase of test suites built and executed as > loadable modules. However, it also introduced a problematic side effect > that causes test suites modules to crash when they attempt to register > fake devices. > > When a module is loaded, it traverses the MODULE_STATE_UNFORMED and > MODULE_STATE_COMING states before reaching the normal operating state > MODULE_STATE_LIVE. Finally, when the module is removed, it moves to > MODULE_STATE_GOING before being released. However, if the loading > function load_module() fails between complete_formation() and > do_init_module(), the module goes directly from MODULE_STATE_COMING to > MODULE_STATE_GOING without passing through MODULE_STATE_LIVE. > > This behavior was causing kunit_module_exit() to be called without > having first executed kunit_module_init(). Since kunit_module_exit() is > responsible for freeing the memory allocated by kunit_module_init() > through kunit_filter_suites(), this behavior was resulting in a > wild-memory-access bug. > > Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in > kunit_free_suite_set()") fixed this issue by running the tests when the > module is still in MODULE_STATE_COMING. However, modules in that state > are not fully initialized, lacking sysfs kobjects. Therefore, if a test > module attempts to register a fake device, it will inevitably crash. > > This patch proposes a different approach to fix the original > wild-memory-access bug while restoring the normal module execution flow > by making kunit_module_exit() able to detect if kunit_module_init() has > previously initialized the tests suite set. In this way, test modules > can once again register fake devices without crashing. > > This behavior is achieved by checking whether mod->kunit_suites is a > virtual or direct mapping address. If it is a virtual address, then > kunit_module_init() has allocated the suite_set in kunit_filter_suites() > using kmalloc_array(). On the contrary, if mod->kunit_suites is still > pointing to the original address that was set when looking up the > .kunit_test_suites section of the module, then the loading phase has > failed and there's no memory to be freed. > Hello, I have tested this change and it looks good to me! Although, it no longer applies cleanly on the kselftest/kunit branch so it will need to be rebased. So besides the need for a rebase, Tested-by: Rae Moar <rmoar@google.com> Thanks for the fix! Rae > v3: > - add a comment to clarify why the start address is checked > v2: > - add include <linux/mm.h> > > Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") > Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com> > Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> > Signed-off-by: Marco Pagani <marpagan@redhat.com> > --- > lib/kunit/test.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 7aceb07a1af9..3263e0d5e0f6 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -16,6 +16,7 @@ > #include <linux/panic.h> > #include <linux/sched/debug.h> > #include <linux/sched.h> > +#include <linux/mm.h> > > #include "debugfs.h" > #include "hooks-impl.h" > @@ -775,12 +776,19 @@ static void kunit_module_exit(struct module *mod) > }; > const char *action = kunit_action(); > > + /* > + * Check if the start address is a valid virtual address to detect > + * if the module load sequence has failed and the suite set has not > + * been initialized and filtered. > + */ > + if (!suite_set.start || !virt_addr_valid(suite_set.start)) > + return; > + > if (!action) > __kunit_test_suites_exit(mod->kunit_suites, > mod->num_kunit_suites); > > - if (suite_set.start) > - kunit_free_suite_set(suite_set); > + kunit_free_suite_set(suite_set); > } > > static int kunit_module_notify(struct notifier_block *nb, unsigned long val, > @@ -790,12 +798,12 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val, > > switch (val) { > case MODULE_STATE_LIVE: > + kunit_module_init(mod); > break; > case MODULE_STATE_GOING: > kunit_module_exit(mod); > break; > case MODULE_STATE_COMING: > - kunit_module_init(mod); > break; > case MODULE_STATE_UNFORMED: > break; > > base-commit: 33cc938e65a98f1d29d0a18403dbbee050dcad9a > -- > 2.43.0 >
On Wed, 6 Dec 2023 at 23:07, Marco Pagani <marpagan@redhat.com> wrote: > > Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in > kunit_free_suite_set()") fixed a wild-memory-access bug that could have > happened during the loading phase of test suites built and executed as > loadable modules. However, it also introduced a problematic side effect > that causes test suites modules to crash when they attempt to register > fake devices. > > When a module is loaded, it traverses the MODULE_STATE_UNFORMED and > MODULE_STATE_COMING states before reaching the normal operating state > MODULE_STATE_LIVE. Finally, when the module is removed, it moves to > MODULE_STATE_GOING before being released. However, if the loading > function load_module() fails between complete_formation() and > do_init_module(), the module goes directly from MODULE_STATE_COMING to > MODULE_STATE_GOING without passing through MODULE_STATE_LIVE. > > This behavior was causing kunit_module_exit() to be called without > having first executed kunit_module_init(). Since kunit_module_exit() is > responsible for freeing the memory allocated by kunit_module_init() > through kunit_filter_suites(), this behavior was resulting in a > wild-memory-access bug. > > Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in > kunit_free_suite_set()") fixed this issue by running the tests when the > module is still in MODULE_STATE_COMING. However, modules in that state > are not fully initialized, lacking sysfs kobjects. Therefore, if a test > module attempts to register a fake device, it will inevitably crash. > > This patch proposes a different approach to fix the original > wild-memory-access bug while restoring the normal module execution flow > by making kunit_module_exit() able to detect if kunit_module_init() has > previously initialized the tests suite set. In this way, test modules > can once again register fake devices without crashing. > > This behavior is achieved by checking whether mod->kunit_suites is a > virtual or direct mapping address. If it is a virtual address, then > kunit_module_init() has allocated the suite_set in kunit_filter_suites() > using kmalloc_array(). On the contrary, if mod->kunit_suites is still > pointing to the original address that was set when looking up the > .kunit_test_suites section of the module, then the loading phase has > failed and there's no memory to be freed. > > v3: > - add a comment to clarify why the start address is checked > v2: > - add include <linux/mm.h> > > Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") > Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com> > Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> > Signed-off-by: Marco Pagani <marpagan@redhat.com> > --- Sorry for the delay here: there are enough subtleties here that I wanted to double check some things. I keep feeling that there has to be a nicer way of doing this, but I can't think of one, so let's go with this, since it's fixing a real issue. I'm a little hesitant about our use of the suite_set.start address as an 'is initialised' flag, and depending on it being reallocated via kunit_filter_suites(), but since we already depend on that (by always using kunit_free_suite_set()), I'm okay with it. My only request (other than this needing a rebase, probably on top of 6.8) would be to add a comment in kunit_filter_suites() noting that it must return a virtual address. That's probably something we should've done a while ago, but I can just see this requirement getting forgotten. Reviewed-by: David Gow <davidgow@google.com> Cheers, -- David > lib/kunit/test.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 7aceb07a1af9..3263e0d5e0f6 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -16,6 +16,7 @@ > #include <linux/panic.h> > #include <linux/sched/debug.h> > #include <linux/sched.h> > +#include <linux/mm.h> > > #include "debugfs.h" > #include "hooks-impl.h" > @@ -775,12 +776,19 @@ static void kunit_module_exit(struct module *mod) > }; > const char *action = kunit_action(); > > + /* > + * Check if the start address is a valid virtual address to detect > + * if the module load sequence has failed and the suite set has not > + * been initialized and filtered. > + */ > + if (!suite_set.start || !virt_addr_valid(suite_set.start)) > + return; > + > if (!action) > __kunit_test_suites_exit(mod->kunit_suites, > mod->num_kunit_suites); > > - if (suite_set.start) > - kunit_free_suite_set(suite_set); > + kunit_free_suite_set(suite_set); > } > > static int kunit_module_notify(struct notifier_block *nb, unsigned long val, > @@ -790,12 +798,12 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val, > > switch (val) { > case MODULE_STATE_LIVE: > + kunit_module_init(mod); > break; > case MODULE_STATE_GOING: > kunit_module_exit(mod); > break; > case MODULE_STATE_COMING: > - kunit_module_init(mod); > break; > case MODULE_STATE_UNFORMED: > break; > > base-commit: 33cc938e65a98f1d29d0a18403dbbee050dcad9a > -- > 2.43.0 >
On 2024-01-08 08:27, David Gow wrote: > On Wed, 6 Dec 2023 at 23:07, Marco Pagani <marpagan@redhat.com> wrote: >> >> Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in >> kunit_free_suite_set()") fixed a wild-memory-access bug that could have >> happened during the loading phase of test suites built and executed as >> loadable modules. However, it also introduced a problematic side effect >> that causes test suites modules to crash when they attempt to register >> fake devices. >> >> When a module is loaded, it traverses the MODULE_STATE_UNFORMED and >> MODULE_STATE_COMING states before reaching the normal operating state >> MODULE_STATE_LIVE. Finally, when the module is removed, it moves to >> MODULE_STATE_GOING before being released. However, if the loading >> function load_module() fails between complete_formation() and >> do_init_module(), the module goes directly from MODULE_STATE_COMING to >> MODULE_STATE_GOING without passing through MODULE_STATE_LIVE. >> >> This behavior was causing kunit_module_exit() to be called without >> having first executed kunit_module_init(). Since kunit_module_exit() is >> responsible for freeing the memory allocated by kunit_module_init() >> through kunit_filter_suites(), this behavior was resulting in a >> wild-memory-access bug. >> >> Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in >> kunit_free_suite_set()") fixed this issue by running the tests when the >> module is still in MODULE_STATE_COMING. However, modules in that state >> are not fully initialized, lacking sysfs kobjects. Therefore, if a test >> module attempts to register a fake device, it will inevitably crash. >> >> This patch proposes a different approach to fix the original >> wild-memory-access bug while restoring the normal module execution flow >> by making kunit_module_exit() able to detect if kunit_module_init() has >> previously initialized the tests suite set. In this way, test modules >> can once again register fake devices without crashing. >> >> This behavior is achieved by checking whether mod->kunit_suites is a >> virtual or direct mapping address. If it is a virtual address, then >> kunit_module_init() has allocated the suite_set in kunit_filter_suites() >> using kmalloc_array(). On the contrary, if mod->kunit_suites is still >> pointing to the original address that was set when looking up the >> .kunit_test_suites section of the module, then the loading phase has >> failed and there's no memory to be freed. >> >> v3: >> - add a comment to clarify why the start address is checked >> v2: >> - add include <linux/mm.h> >> >> Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") >> Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com> >> Reviewed-by: Javier Martinez Canillas <javierm@redhat.com> >> Signed-off-by: Marco Pagani <marpagan@redhat.com> >> --- > > Sorry for the delay here: there are enough subtleties here that I > wanted to double check some things. > > I keep feeling that there has to be a nicer way of doing this, but I > can't think of one, so let's go with this, since it's fixing a real > issue. > > I'm a little hesitant about our use of the suite_set.start address as > an 'is initialised' flag, and depending on it being reallocated via > kunit_filter_suites(), but since we already depend on that (by always > using kunit_free_suite_set()), I'm okay with it. > I have the same feeling. I spent some thinking about alternative solutions that did not require adding a flag in the module struct or restructuring significant portions of the code, but I could not think of anything better for the moment. > My only request (other than this needing a rebase, probably on top of > 6.8) would be to add a comment in kunit_filter_suites() noting that it > must return a virtual address. That's probably something we should've > done a while ago, but I can just see this requirement getting > forgotten. > Sure, I'll do it. Thanks, Marco > Reviewed-by: David Gow <davidgow@google.com> > > >> lib/kunit/test.c | 14 +++++++++++--- >> 1 file changed, 11 insertions(+), 3 deletions(-) >> >> diff --git a/lib/kunit/test.c b/lib/kunit/test.c >> index 7aceb07a1af9..3263e0d5e0f6 100644 >> --- a/lib/kunit/test.c >> +++ b/lib/kunit/test.c >> @@ -16,6 +16,7 @@ >> #include <linux/panic.h> >> #include <linux/sched/debug.h> >> #include <linux/sched.h> >> +#include <linux/mm.h> >> >> #include "debugfs.h" >> #include "hooks-impl.h" >> @@ -775,12 +776,19 @@ static void kunit_module_exit(struct module *mod) >> }; >> const char *action = kunit_action(); >> >> + /* >> + * Check if the start address is a valid virtual address to detect >> + * if the module load sequence has failed and the suite set has not >> + * been initialized and filtered. >> + */ >> + if (!suite_set.start || !virt_addr_valid(suite_set.start)) >> + return; >> + >> if (!action) >> __kunit_test_suites_exit(mod->kunit_suites, >> mod->num_kunit_suites); >> >> - if (suite_set.start) >> - kunit_free_suite_set(suite_set); >> + kunit_free_suite_set(suite_set); >> } >> >> static int kunit_module_notify(struct notifier_block *nb, unsigned long val, >> @@ -790,12 +798,12 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val, >> >> switch (val) { >> case MODULE_STATE_LIVE: >> + kunit_module_init(mod); >> break; >> case MODULE_STATE_GOING: >> kunit_module_exit(mod); >> break; >> case MODULE_STATE_COMING: >> - kunit_module_init(mod); >> break; >> case MODULE_STATE_UNFORMED: >> break; >> >> base-commit: 33cc938e65a98f1d29d0a18403dbbee050dcad9a >> -- >> 2.43.0 >>
diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 7aceb07a1af9..3263e0d5e0f6 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -16,6 +16,7 @@ #include <linux/panic.h> #include <linux/sched/debug.h> #include <linux/sched.h> +#include <linux/mm.h> #include "debugfs.h" #include "hooks-impl.h" @@ -775,12 +776,19 @@ static void kunit_module_exit(struct module *mod) }; const char *action = kunit_action(); + /* + * Check if the start address is a valid virtual address to detect + * if the module load sequence has failed and the suite set has not + * been initialized and filtered. + */ + if (!suite_set.start || !virt_addr_valid(suite_set.start)) + return; + if (!action) __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites); - if (suite_set.start) - kunit_free_suite_set(suite_set); + kunit_free_suite_set(suite_set); } static int kunit_module_notify(struct notifier_block *nb, unsigned long val, @@ -790,12 +798,12 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val, switch (val) { case MODULE_STATE_LIVE: + kunit_module_init(mod); break; case MODULE_STATE_GOING: kunit_module_exit(mod); break; case MODULE_STATE_COMING: - kunit_module_init(mod); break; case MODULE_STATE_UNFORMED: break;