From nobody Mon May 11 00:05:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62305C433EF for ; Wed, 20 Apr 2022 11:53:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378291AbiDTLzx (ORCPT ); Wed, 20 Apr 2022 07:55:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378275AbiDTLzt (ORCPT ); Wed, 20 Apr 2022 07:55:49 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E0EC4249A for ; Wed, 20 Apr 2022 04:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1650455582; 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: in-reply-to:in-reply-to:references:references; bh=wj4nXdv1p55S6gdjYiynG1yQGRwuN4zNBoal8sio/r8=; b=RRfnzZG5B4Uzka+smO4lo+eWMWUH+LYzsw/rQX25ngzWH06yY0FG/UMakEH9SEpws7JmEC nOCBLnYPfqDrFsminex800rzT0reOYbnF1NuMrYZAl+bFpMHp9F0Ki4N7xLaWSJJuFo2gA deKb5P3WF0OHcJ5F5rAyv1mPQAW5p90= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-497-QdcohxnSN7qZIOBowjK7JA-1; Wed, 20 Apr 2022 07:53:01 -0400 X-MC-Unique: QdcohxnSN7qZIOBowjK7JA-1 Received: by mail-wm1-f70.google.com with SMTP id v191-20020a1cacc8000000b0038ce818d2efso826976wme.1 for ; Wed, 20 Apr 2022 04:53:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wj4nXdv1p55S6gdjYiynG1yQGRwuN4zNBoal8sio/r8=; b=VRDMG+2Na8cXh/jmauw4kuKKkcurjSEsGIYEKYYDKcnO27NC2XNpe4rL1ECnISaipB nBuC52Ms6heiHBJPwq0fF/LZ7H6zjagdY0bCvNE8ejo93frZVwCmfsnMUsAH+Kr/LY8V l9Lyn9TRzEyWKbqpyi+DwWz3VLvnvbY9CHlFS9geoRNPX9/xeQUyx5KK9enlDTRJdXVm ZvTe1AgmTCSh5TgeDWDeWrlss+QGTvyTqGtEh/TCntUhuwf0WPyrkoLPMhAi+9XnVSgY UC2/NYqIqYir4Rllqtvx/2xYwcBKzvN7jdreVWUHw/mFPYsgECfq6Jl2Ztqz6oke+C+m ZKrQ== X-Gm-Message-State: AOAM532cB/XuH8y5jTJGWSuU24OXfJQZA162UyTl/nT9d04u3Xb7FysE ppNO6duJDOPzLsHLCgEvMoRrJkUS8pdq3X9wuaySBGxOk+Q3Ycz9CPOr+pqZRpJeupNYgTO8A/2 Et/1mT+wbAIcysKALTQ6O0QU= X-Received: by 2002:a05:600c:3ac6:b0:38e:d79e:d9a with SMTP id d6-20020a05600c3ac600b0038ed79e0d9amr3231379wms.200.1650455580566; Wed, 20 Apr 2022 04:53:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyptGP8duMmQo1LImHknI95oJYwmBAN9GbqtwZdsmtM8wxSj1PX+FCWEX4Pfh1PWJZNJ5TyOQ== X-Received: by 2002:a05:600c:3ac6:b0:38e:d79e:d9a with SMTP id d6-20020a05600c3ac600b0038ed79e0d9amr3231366wms.200.1650455580418; Wed, 20 Apr 2022 04:53:00 -0700 (PDT) Received: from localhost (cpc111743-lutn13-2-0-cust979.9-3.cable.virginm.net. [82.17.115.212]) by smtp.gmail.com with ESMTPSA id v14-20020a7bcb4e000000b0034492fa24c6sm18866730wmj.34.2022.04.20.04.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 04:52:59 -0700 (PDT) From: Aaron Tomlin To: mcgrof@kernel.org Cc: cl@linux.com, pmladek@suse.com, mbenes@suse.cz, christophe.leroy@csgroup.eu, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, atomlin@atomlin.com, ghalat@redhat.com, oleksandr@natalenko.name, neelx@redhat.com Subject: [PATCH v3 1/2] module: Make module_flags_taint() accept a module's taints bitmap directly Date: Wed, 20 Apr 2022 12:52:56 +0100 Message-Id: <20220420115257.3498300-2-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220420115257.3498300-1-atomlin@redhat.com> References: <20220420115257.3498300-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. The purpose of this patch is to modify module_flags_taint() to accept a module's taints bitmap as a parameter and modifies all users accordingly. This is in preparation for module unload taint tracking support. Signed-off-by: Aaron Tomlin --- kernel/module/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 05a42d8fcd7a..ea78cec316dd 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -890,13 +890,13 @@ static inline int module_unload_init(struct module *m= od) } #endif /* CONFIG_MODULE_UNLOAD */ =20 -static size_t module_flags_taint(struct module *mod, char *buf) +static size_t module_flags_taint(unsigned long taints, char *buf) { size_t l =3D 0; int i; =20 for (i =3D 0; i < TAINT_FLAGS_COUNT; i++) { - if (taint_flags[i].module && test_bit(i, &mod->taints)) + if (taint_flags[i].module && test_bit(i, &taints)) buf[l++] =3D taint_flags[i].c_true; } =20 @@ -974,7 +974,7 @@ static ssize_t show_taint(struct module_attribute *matt= r, { size_t l; =20 - l =3D module_flags_taint(mk->mod, buffer); + l =3D module_flags_taint(mk->mod->taints, buffer); buffer[l++] =3D '\n'; return l; } @@ -2993,7 +2993,7 @@ char *module_flags(struct module *mod, char *buf) mod->state =3D=3D MODULE_STATE_GOING || mod->state =3D=3D MODULE_STATE_COMING) { buf[bx++] =3D '('; - bx +=3D module_flags_taint(mod, buf + bx); + bx +=3D module_flags_taint(mod->taints, buf + bx); /* Show a - for module-is-being-unloaded */ if (mod->state =3D=3D MODULE_STATE_GOING) buf[bx++] =3D '-'; --=20 2.34.1 From nobody Mon May 11 00:05:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66935C433EF for ; Wed, 20 Apr 2022 11:53:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378297AbiDTLz5 (ORCPT ); Wed, 20 Apr 2022 07:55:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378272AbiDTLzw (ORCPT ); Wed, 20 Apr 2022 07:55:52 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4E5E04248E for ; Wed, 20 Apr 2022 04:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1650455585; 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: in-reply-to:in-reply-to:references:references; bh=XyEpFOsVjUMPpghKZCBH2maNdoYb/HCQD9/+9CauZP8=; b=RwYQVmmUtwVwaTlP7CMyhz9J22zfRruBL6q7j/xJD7OH6kxzbZ6AwwJfxHagS7K+ewwOAL 3X+CQzelY4ZGUUYlz06GwURUnl3fFe+RDYB10DnwFaGsaeGNg6WPo8H4U9BueGYvjb1Hla QIchotB4F6np39BMrs/yZDmBQv6Ob/k= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-651-51GZh-IwO-WK3FW4c46ONw-1; Wed, 20 Apr 2022 07:53:04 -0400 X-MC-Unique: 51GZh-IwO-WK3FW4c46ONw-1 Received: by mail-wr1-f70.google.com with SMTP id p18-20020adf9592000000b00207bc12decbso324230wrp.21 for ; Wed, 20 Apr 2022 04:53:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XyEpFOsVjUMPpghKZCBH2maNdoYb/HCQD9/+9CauZP8=; b=5jvd1CcoHTvUjbxVAbViMbT1fbGQdNmWLcAAIN+iTgxIC6MOX/n2kTzibPTydcd4UX C4o2aq0kqonsFBPAPOugg1M7HU13LjRJYRTkeRafz7hkY8qt7YgN70TLm5+itLxdO9nr TrBhWCwg5urqVb7xZ8ivbo4jNDhJIY5F0awB8N2rJdWFeIo57IUaagtYYOPdPyAmgvUR 0qGAQKCzWMwIIHrriHTGRkz4ywnlJvPfkWQX3HKVR3FHBaXjIzOiFNAJmNIk7nY00kAL 2jO/y9naYRRcDIIUXaLnyXV/R1prKFucwQkfktSM1yfBhPKWq/BQKKoAtm6w7O8GNtEk RSxg== X-Gm-Message-State: AOAM533FrJUj+dDLRqpP9eUNkINEEN9JzK8O4Ud5JdRAplF4ETz8tTtl nIERxkZ/XHx/axCTeDyL2MUlvrKHnRSnbnZXqsHk+fF755XqLU9lidDhl0Wqd+Xdzeij5FeuDuM UcwlzddekVn7VZH16dvZT9Cs= X-Received: by 2002:adf:f783:0:b0:207:a8ce:c155 with SMTP id q3-20020adff783000000b00207a8cec155mr15340435wrp.258.1650455583063; Wed, 20 Apr 2022 04:53:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3mdcDdgnw8wBFd4oSvz2RFBCBuLcli0LAQbYaimimhCAkyCaDc/C/mRRY0k5Jc+1/dIwhWA== X-Received: by 2002:adf:f783:0:b0:207:a8ce:c155 with SMTP id q3-20020adff783000000b00207a8cec155mr15340418wrp.258.1650455582802; Wed, 20 Apr 2022 04:53:02 -0700 (PDT) Received: from localhost (cpc111743-lutn13-2-0-cust979.9-3.cable.virginm.net. [82.17.115.212]) by smtp.gmail.com with ESMTPSA id v11-20020a056000144b00b0020a9c02f60dsm6333233wrx.50.2022.04.20.04.53.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 04:53:01 -0700 (PDT) From: Aaron Tomlin To: mcgrof@kernel.org Cc: cl@linux.com, pmladek@suse.com, mbenes@suse.cz, christophe.leroy@csgroup.eu, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, atomlin@atomlin.com, ghalat@redhat.com, oleksandr@natalenko.name, neelx@redhat.com Subject: [PATCH v3 2/2] module: Introduce module unload taint tracking Date: Wed, 20 Apr 2022 12:52:57 +0100 Message-Id: <20220420115257.3498300-3-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220420115257.3498300-1-atomlin@redhat.com> References: <20220420115257.3498300-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently, only the initial module that tainted the kernel is recorded e.g. when an out-of-tree module is loaded. The purpose of this patch is to allow the kernel to maintain a record of each unloaded module that taints the kernel. So, in addition to displaying a list of linked modules (see print_modules()) e.g. in the event of a detected bad page, unloaded modules that carried a taint/or taints are displayed too. A tainted module unload count is maintained. The number of tracked modules is not fixed. This feature is disabled by default. Signed-off-by: Aaron Tomlin Reported-by: kernel test robot --- init/Kconfig | 11 ++++++++ kernel/module/main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/init/Kconfig b/init/Kconfig index ddcbefe535e9..6b30210f787d 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -2118,6 +2118,17 @@ config MODULE_FORCE_UNLOAD rmmod). This is mainly for kernel developers and desperate users. If unsure, say N. =20 +config MODULE_UNLOAD_TAINT_TRACKING + bool "Tainted module unload tracking" + depends on MODULE_UNLOAD + default n + help + This option allows you to maintain a record of each unloaded + module that tainted the kernel. In addition to displaying a + list of linked (or loaded) modules e.g. on detection of a bad + page (see bad_page()), the aforementioned details are also + shown. If unsure, say N. + config MODVERSIONS bool "Module versioning support" help diff --git a/kernel/module/main.c b/kernel/module/main.c index ea78cec316dd..d7cc64172dd1 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -68,6 +68,16 @@ */ DEFINE_MUTEX(module_mutex); LIST_HEAD(modules); +#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING +static LIST_HEAD(unloaded_tainted_modules); + +struct mod_unload_taint { + struct list_head list; + char name[MODULE_NAME_LEN]; + unsigned long taints; + u64 count; +}; +#endif =20 /* Work queue for freeing init sections in success case */ static void do_free_init(struct work_struct *w); @@ -150,6 +160,41 @@ int unregister_module_notifier(struct notifier_block *= nb) } EXPORT_SYMBOL(unregister_module_notifier); =20 +#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING +static int try_add_tainted_module(struct module *mod) +{ + struct mod_unload_taint *mod_taint; + + module_assert_mutex_or_preempt(); + + list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, list, + lockdep_is_held(&module_mutex)) { + size_t len =3D strlen(mod_taint->name); + + if (len =3D=3D strlen(mod->name) && !memcmp(mod_taint->name, mod->name, = len) && + mod_taint->taints & mod->taints) { + mod_taint->count++; + goto out; + } + } + + mod_taint =3D kmalloc(sizeof(*mod_taint), GFP_KERNEL); + if (unlikely(!mod_taint)) + return -ENOMEM; + strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN); + mod_taint->taints =3D mod->taints; + list_add_rcu(&mod_taint->list, &unloaded_tainted_modules); + mod_taint->count =3D 1; +out: + return 0; +} +#else /* MODULE_UNLOAD_TAINT_TRACKING */ +static int try_add_tainted_module(struct module *mod) +{ + return 0; +} +#endif + /* * We require a truly strong try_module_get(): 0 means success. * Otherwise an error is returned due to ongoing or failed @@ -1201,6 +1246,9 @@ static void free_module(struct module *mod) module_bug_cleanup(mod); /* Wait for RCU-sched synchronizing before releasing mod->list and buglis= t. */ synchronize_rcu(); + if (try_add_tainted_module(mod)) + pr_err("%s: adding tainted module to the unloaded tainted modules list f= ailed.\n", + mod->name); mutex_unlock(&module_mutex); =20 /* Clean up CFI for the module. */ @@ -3126,6 +3174,9 @@ struct module *__module_text_address(unsigned long ad= dr) void print_modules(void) { struct module *mod; +#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING + struct mod_unload_taint *mod_taint; +#endif char buf[MODULE_FLAGS_BUF_SIZE]; =20 printk(KERN_DEFAULT "Modules linked in:"); @@ -3136,6 +3187,20 @@ void print_modules(void) continue; pr_cont(" %s%s", mod->name, module_flags(mod, buf)); } +#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING + if (!list_empty(&unloaded_tainted_modules)) { + printk(KERN_DEFAULT "Unloaded tainted modules:"); + list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, + list) { + size_t l; + + l =3D module_flags_taint(mod_taint->taints, buf); + buf[l++] =3D '\0'; + pr_cont(" %s(%s):%llu", mod_taint->name, buf, + mod_taint->count); + } +#endif + } preempt_enable(); if (last_unloaded_module[0]) pr_cont(" [last unloaded: %s]", last_unloaded_module); --=20 2.34.1