From nobody Sun May 10 19:51:42 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 643D0C433F5 for ; Mon, 2 May 2022 20:51:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387509AbiEBUys (ORCPT ); Mon, 2 May 2022 16:54:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387404AbiEBUyo (ORCPT ); Mon, 2 May 2022 16:54:44 -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 D38655FDE for ; Mon, 2 May 2022 13:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651524674; 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=fyk1qjVRfuvYLqg1SExRln/WT9DH+HXSu9T9RqEZXqo=; b=BdR6EmYKuNLmnxFq51wlobIBBYGMPYhTk6ayQsPDGA0CbshZ412Gp6jq0deWuFrYAnUERB Y73v+gJlzYJtNebSs3Jz5LnEJm2KosYliNk1YqbjAwL+7AjgemXVSleXq6W5tWTmR6t/5k KqHDM6hA5H7utEtLj61j0SZmbM6aO0g= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-549-RLfwxk6tPRS3LyLkKYmuEw-1; Mon, 02 May 2022 16:51:12 -0400 X-MC-Unique: RLfwxk6tPRS3LyLkKYmuEw-1 Received: by mail-wr1-f71.google.com with SMTP id y13-20020adfc7cd000000b0020ac7c7bf2eso5686434wrg.9 for ; Mon, 02 May 2022 13:51:12 -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=fyk1qjVRfuvYLqg1SExRln/WT9DH+HXSu9T9RqEZXqo=; b=EqOIbQfs8TXmbXyMba0kgLKoN0wNqHznlYYPMY3fZtNl+AH4KfJM7ZpI6P7rELZm+q JM1cGChsXEbAyRIUZft8HUBoTd5ZQSODixsd0JkcTmdlP9+UsO9EqU8WD0zOLltCZi7F bn2gFGBtoCsxKZ+GnA7U41KUD9fQYQc5penfSiW/BETLfSrOHz216hUzSF06Vn/+Jj7K ugjKmmd4k3vt4wr5A1iUNwz2EDLsZy5yNrJDYCDLUXVUaRQvOAP5srXsgCa96p9L1rVY 7dJWyYuFZPSxbyltn4nDmrzC6fUmc/xLFVPD+ur8UGqnOqcio6xLnATif8J7TTvtelLF R2LQ== X-Gm-Message-State: AOAM533YWo/CCz2slKHI7Q7lwWEAxdjswB0y1gNiANkZliij/0ItWDm0 /gFixTprceRpNFC+JW/DOHGF2Ccem7bjJs/VcaJzXa4Sj5JbyLoMrgg7ifQHfl8CRWyU8wMTOh+ mvewwQNteFm400GtQHGlUUJQ= X-Received: by 2002:a7b:c190:0:b0:38e:7e47:744a with SMTP id y16-20020a7bc190000000b0038e7e47744amr679523wmi.38.1651524671478; Mon, 02 May 2022 13:51:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxKsvS2IxsQjCeX7CT8ullWyqn23kIb1dUIoGUy6s0v5x+VIfJ9cUyBrwRp2d/3CxqpeULGAA== X-Received: by 2002:a7b:c190:0:b0:38e:7e47:744a with SMTP id y16-20020a7bc190000000b0038e7e47744amr679499wmi.38.1651524671272; Mon, 02 May 2022 13:51:11 -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 q14-20020adfaa4e000000b0020c5253d91fsm8177336wrd.107.2022.05.02.13.51.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 13:51:09 -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 v5 1/3] module: Make module_flags_taint() accept a module's taints bitmap and usable outside core code Date: Mon, 2 May 2022 21:51:03 +0100 Message-Id: <20220502205105.919523-2-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220502205105.919523-1-atomlin@redhat.com> References: <20220502205105.919523-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. Furthermore, it is now possible to access a given module's taint flags data outside of non-essential code yet does remain for internal use only. This is in preparation for module unload taint tracking support. Signed-off-by: Aaron Tomlin --- kernel/module/internal.h | 1 + kernel/module/main.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 3e23bef5884d..abbd1c5ef264 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -100,6 +100,7 @@ int cmp_name(const void *name, const void *sym); long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *s= echdr, unsigned int section); char *module_flags(struct module *mod, char *buf); +size_t module_flags_taint(unsigned long taints, char *buf); =20 static inline unsigned long kernel_symbol_value(const struct kernel_symbol= *sym) { diff --git a/kernel/module/main.c b/kernel/module/main.c index 05a42d8fcd7a..7dbdd098b995 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) +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 Sun May 10 19:51:42 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 6CE3DC433EF for ; Mon, 2 May 2022 20:51:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387527AbiEBUyy (ORCPT ); Mon, 2 May 2022 16:54:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387507AbiEBUys (ORCPT ); Mon, 2 May 2022 16:54:48 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 003F76586 for ; Mon, 2 May 2022 13:51:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651524677; 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=/RrE130eSMlvd7DvNif1xecZQXGFe2pTq64QlGNfROg=; b=UAD49mW00URn2Cio8csmV5cC/LRQtfnHOvHcLC2n/Km2yvojA0KtFCr3P+XB9iaaYYXDcL 4jRb2ZgodYIhBvMnI8wyblqzlJguy5SNZfLlrj+oxMuxEiiTjlZ41A8mMhRLB19t3VBbuI gd8jVcIL7FKPexejBDO3QsanR8X6eLs= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-116-Uc2B5DjjOMSqoit7RFsq_w-1; Mon, 02 May 2022 16:51:16 -0400 X-MC-Unique: Uc2B5DjjOMSqoit7RFsq_w-1 Received: by mail-wm1-f69.google.com with SMTP id z23-20020a05600c221700b003942fd37764so214300wml.8 for ; Mon, 02 May 2022 13:51:15 -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=/RrE130eSMlvd7DvNif1xecZQXGFe2pTq64QlGNfROg=; b=bsX/kxXJfFg9ye5IkuJHtNXu5zNWOLpruoq983e2kpZH9tQar5fBG3QVYGm6WxIRol 6qM1WpCWIBhnT69/69cNwng+1xz02qMd70q1oSyHs/ISpV8clkA9R8wWdUOPmI9AK4DI wmNgI7o+bdeLVw2EyCTZmTHL6DuzQT1MjP47QbQ92GZFR2TnN5/VecEINGBDf+1M20Ge iC0zfudJsEx3ZeWpndOu6hhE5sxHb8jb+bR9uqsbo+BwjKLDZ1FnhYgM20lFyrl713Dn i7LMPbagDisd3xpQpj7Oc6yxIfHdvoVf7qZINIzifc8YydOdYjpsLtEI2XgQH8kX/973 ov6w== X-Gm-Message-State: AOAM5311gf1xLk+uVPDyfRx8uzB+7ZLPsxFhq0k5KQXBv9L3vhzyesiZ 5Mn7q2mFg/5eoOudc58/jVYEkkoUb9tCk3mwJMQPQf9qk9ZV4wV0dF5A9YQpgADUAZa2aTd3Y9Z XBj6YLHVogXJq2L2SwUTsoPk= X-Received: by 2002:a05:600c:6009:b0:394:33ef:86c8 with SMTP id az9-20020a05600c600900b0039433ef86c8mr661408wmb.83.1651524673930; Mon, 02 May 2022 13:51:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkos8YnAEh9YSvm+XhUHPRClq8ESwoIPdhprAcYE9bzhNBVujcte0JM5awEg64V0rA4vlgxA== X-Received: by 2002:a05:600c:6009:b0:394:33ef:86c8 with SMTP id az9-20020a05600c600900b0039433ef86c8mr661386wmb.83.1651524673646; Mon, 02 May 2022 13:51:13 -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 e23-20020adf9bd7000000b0020c5253d8ccsm8068190wrc.24.2022.05.02.13.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 13:51:12 -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 v5 2/3] module: Move module_assert_mutex_or_preempt() to internal.h Date: Mon, 2 May 2022 21:51:04 +0100 Message-Id: <20220502205105.919523-3-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220502205105.919523-1-atomlin@redhat.com> References: <20220502205105.919523-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. This patch migrates module_assert_mutex_or_preempt() to internal.h. So, the aforementiond function can be used outside of main/or core module code yet will remain restricted for internal use only. Signed-off-by: Aaron Tomlin --- kernel/module/internal.h | 12 ++++++++++++ kernel/module/main.c | 11 ----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index abbd1c5ef264..0bdf64c9dfb5 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -10,6 +10,7 @@ #include #include #include +#include =20 #ifndef ARCH_SHF_SMALL #define ARCH_SHF_SMALL 0 @@ -102,6 +103,17 @@ long module_get_offset(struct module *mod, unsigned in= t *size, Elf_Shdr *sechdr, char *module_flags(struct module *mod, char *buf); size_t module_flags_taint(unsigned long taints, char *buf); =20 +static inline void module_assert_mutex_or_preempt(void) +{ +#ifdef CONFIG_LOCKDEP + if (unlikely(!debug_locks)) + return; + + WARN_ON_ONCE(!rcu_read_lock_sched_held() && + !lockdep_is_held(&module_mutex)); +#endif +} + static inline unsigned long kernel_symbol_value(const struct kernel_symbol= *sym) { #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS diff --git a/kernel/module/main.c b/kernel/module/main.c index 7dbdd098b995..7a0484900320 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -118,17 +118,6 @@ static void mod_update_bounds(struct module *mod) #endif } =20 -static void module_assert_mutex_or_preempt(void) -{ -#ifdef CONFIG_LOCKDEP - if (unlikely(!debug_locks)) - return; - - WARN_ON_ONCE(!rcu_read_lock_sched_held() && - !lockdep_is_held(&module_mutex)); -#endif -} - /* Block module loading/unloading? */ int modules_disabled =3D 0; core_param(nomodule, modules_disabled, bint, 0); --=20 2.34.1 From nobody Sun May 10 19:51:42 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 E257DC433F5 for ; Mon, 2 May 2022 20:53:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387530AbiEBU4j (ORCPT ); Mon, 2 May 2022 16:56:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387547AbiEBU4e (ORCPT ); Mon, 2 May 2022 16:56:34 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 53146E088 for ; Mon, 2 May 2022 13:52:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651524778; 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=XGglu3dvrLHqMUiIqOc49nbqCKUgmOSaGECzqLEx8mk=; b=U0mUbjxAWZGq64HxU1Mo7COSd3AI8BXjKF2I41vfh7Cr89zmInKljeyXKGOdqqMewYSG0l 90Up6t9pQBOePNbFzU/bxpOF7fHUAyp//ViQGcozJV+jMjzTq3zdfIrhdQXQbz0C1XJlKf k5mgcQbwKaB0f1v9Y7RTuB4yBTt781c= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-164-APTNGBHLOGGum_etedfHLw-1; Mon, 02 May 2022 16:52:57 -0400 X-MC-Unique: APTNGBHLOGGum_etedfHLw-1 Received: by mail-wm1-f71.google.com with SMTP id c62-20020a1c3541000000b0038ec265155fso220280wma.6 for ; Mon, 02 May 2022 13:52:57 -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=XGglu3dvrLHqMUiIqOc49nbqCKUgmOSaGECzqLEx8mk=; b=m6DFYFtfm76r1SrY/9C6Ybs/FCJyp4GWYle5wZkueEhn0WWjHBiJOnCh5gfW5POSUn wb2aL/XqIe+WO47C/xmvUIPKwYhDxA7TzhzBoJuKGHRmzt9ykJo0HIMnr82uZEsMYRZH BA280LlzhChPK+XbkQI+VyuWEWFEx7FgTvW2K6td68WeriBnNrR5VN1rYa+rYffwiHOp p/s/8oKBb3jlgO+JPZrScXYMT4tzZcj5JNSMiNDqt8+qlk5tcfWls9pR26dkwp+6UOCW r8GsChdulhiQ3aSYXpZOiIcyJ4Eic8mOlo6JP4DblmSooVBsQwRY7iBZctYGhaLC+eqr OOkw== X-Gm-Message-State: AOAM5319+5cktifT/em/fM7aeM67Tlsyf8PwpvL6kAPlEdxzURsShlLi 9plM7Rb7ZSgSsdXzkPenvnUB3DkyR6AR4dHur3oExdrE071VP7muStUL0Pb7K4GJP1Knh3sUktk e8FEhVIRj0DZhmQyjfJGQZeY= X-Received: by 2002:adf:e7c1:0:b0:20a:b724:cedd with SMTP id e1-20020adfe7c1000000b0020ab724ceddmr10626705wrn.409.1651524773901; Mon, 02 May 2022 13:52:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvuJf+abT6YgA0yP5v5smYS9QrTBUKnxy+RoVT5ra6EC1CCqPgthzK3N3gMQIkac8lXN5VJA== X-Received: by 2002:adf:e7c1:0:b0:20a:b724:cedd with SMTP id e1-20020adfe7c1000000b0020ab724ceddmr10626696wrn.409.1651524773701; Mon, 02 May 2022 13:52:53 -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 e7-20020adfa447000000b0020c5253d8fasm7677512wra.70.2022.05.02.13.52.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 13:52:52 -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 v5 3/3] module: Introduce module unload taint tracking Date: Mon, 2 May 2022 21:52:52 +0100 Message-Id: <20220502205252.921173-1-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220502205105.919523-1-atomlin@redhat.com> References: <20220502205105.919523-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 --- init/Kconfig | 11 ++++++++ kernel/module/Makefile | 1 + kernel/module/internal.h | 21 ++++++++++++++ kernel/module/main.c | 5 ++++ kernel/module/tracking.c | 61 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 kernel/module/tracking.c 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/Makefile b/kernel/module/Makefile index d1ca799c12e2..948efea81e85 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -18,3 +18,4 @@ obj-$(CONFIG_PROC_FS) +=3D procfs.o obj-$(CONFIG_SYSFS) +=3D sysfs.o obj-$(CONFIG_KGDB_KDB) +=3D kdb.o obj-$(CONFIG_MODVERSIONS) +=3D version.o +obj-$(CONFIG_MODULE_UNLOAD_TAINT_TRACKING) +=3D tracking.o diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 0bdf64c9dfb5..bc5507ab8450 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -145,6 +145,27 @@ static inline bool set_livepatch_module(struct module = *mod) #endif } =20 +#ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING +struct mod_unload_taint { + struct list_head list; + char name[MODULE_NAME_LEN]; + unsigned long taints; + u64 count; +}; + +int try_add_tainted_module(struct module *mod); +void print_unloaded_tainted_modules(void); +#else /* !CONFIG_MODULE_UNLOAD_TAINT_TRACKING */ +static inline int try_add_tainted_module(struct module *mod) +{ + return 0; +} + +static inline void print_unloaded_tainted_modules(void) +{ +} +#endif /* CONFIG_MODULE_UNLOAD_TAINT_TRACKING */ + #ifdef CONFIG_MODULE_DECOMPRESS int module_decompress(struct load_info *info, const void *buf, size_t size= ); void module_decompress_cleanup(struct load_info *info); diff --git a/kernel/module/main.c b/kernel/module/main.c index 7a0484900320..6c3b4a846645 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1190,6 +1190,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. */ @@ -3125,6 +3128,8 @@ void print_modules(void) continue; pr_cont(" %s%s", mod->name, module_flags(mod, buf)); } + + print_unloaded_tainted_modules(); preempt_enable(); if (last_unloaded_module[0]) pr_cont(" [last unloaded: %s]", last_unloaded_module); diff --git a/kernel/module/tracking.c b/kernel/module/tracking.c new file mode 100644 index 000000000000..7f8133044d09 --- /dev/null +++ b/kernel/module/tracking.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module taint unload tracking support + * + * Copyright (C) 2022 Aaron Tomlin + */ + +#include +#include +#include +#include +#include +#include +#include "internal.h" + +static LIST_HEAD(unloaded_tainted_modules); + +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)) { + if (!strcmp(mod_taint->name, mod->name) && + 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; +} + +void print_unloaded_tainted_modules(void) +{ + struct mod_unload_taint *mod_taint; + char buf[MODULE_FLAGS_BUF_SIZE]; + + 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); + } + } +} --=20 2.34.1