From patchwork Mon May 2 20:51:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Tomlin X-Patchwork-Id: 12834725 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 0751DC433EF for ; Mon, 2 May 2022 20:51:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387362AbiEBUyp (ORCPT ); Mon, 2 May 2022 16:54:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387493AbiEBUyo (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.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E462C642E 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-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-202-VAf3sqOPNg-xzO_PRHCxfg-1; Mon, 02 May 2022 16:51:12 -0400 X-MC-Unique: VAf3sqOPNg-xzO_PRHCxfg-1 Received: by mail-wm1-f70.google.com with SMTP id bh7-20020a05600c3d0700b003940829b48dso232466wmb.1 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=1kz2DXTykTOL7QtUpks4X6ySkk8Op3H+4M1Yb0mc674iyqjPKAy5V5MfxSQTOL9noe hHqoYpXR38RpR5LsF81PJg2UcYPNo6RoRnmHy7hFht7uod+46rhUXuTKnC8IRm7W+jkr fyQx+7hwCyUqR2cQQcl6GTcwhMsfz4ZqdRDe3WMWRg+ER9KBDS6kEN4gNVu+FhEPq8BE +1CIBy97G6B468qylZ/MZnKFprQE3aQ+h4BvRH2t7q1UcVX35EuyPSCbspCwt8Uoli6U YFBOI/y+jE8VzTuLZ3ZXeBz1WCuf9ReLHyCrJpESk4MVp09qy1ViQqnIELCr1BxmSyHQ aE5w== X-Gm-Message-State: AOAM532dbEdT2k54inQpmS7mkomG90U7pOJjrf1U2iO0qMpD9oVRzDDu FdRPtoL8poc5us6pBA603HcQNLXTpATdIlzPYvXxbeNM8N9SGrmMZc65p7/+0GOYG0DAjUJp+Vl vEkStNNdFc9ITDBQe33lPQxiN X-Received: by 2002:a7b:c190:0:b0:38e:7e47:744a with SMTP id y16-20020a7bc190000000b0038e7e47744amr679516wmi.38.1651524671477; 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 Precedence: bulk List-ID: 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 *sechdr, unsigned int section); char *module_flags(struct module *mod, char *buf); +size_t module_flags_taint(unsigned long taints, char *buf); 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 *mod) } #endif /* CONFIG_MODULE_UNLOAD */ -static size_t module_flags_taint(struct module *mod, char *buf) +size_t module_flags_taint(unsigned long taints, char *buf) { size_t l = 0; int i; for (i = 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++] = taint_flags[i].c_true; } @@ -974,7 +974,7 @@ static ssize_t show_taint(struct module_attribute *mattr, { size_t l; - l = module_flags_taint(mk->mod, buffer); + l = module_flags_taint(mk->mod->taints, buffer); buffer[l++] = '\n'; return l; } @@ -2993,7 +2993,7 @@ char *module_flags(struct module *mod, char *buf) mod->state == MODULE_STATE_GOING || mod->state == MODULE_STATE_COMING) { buf[bx++] = '('; - bx += module_flags_taint(mod, buf + bx); + bx += module_flags_taint(mod->taints, buf + bx); /* Show a - for module-is-being-unloaded */ if (mod->state == MODULE_STATE_GOING) buf[bx++] = '-'; From patchwork Mon May 2 20:51:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Tomlin X-Patchwork-Id: 12834726 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 3FFB8C433EF for ; Mon, 2 May 2022 20:51:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387513AbiEBUyv (ORCPT ); Mon, 2 May 2022 16:54:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387505AbiEBUys (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 DE31A6565 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-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-502-qnWNM9a5MA28R2p34F2PzQ-1; Mon, 02 May 2022 16:51:16 -0400 X-MC-Unique: qnWNM9a5MA28R2p34F2PzQ-1 Received: by mail-wm1-f71.google.com with SMTP id c62-20020a1c3541000000b0038ec265155fso218180wma.6 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=idIhFyo7FE4kQw12VfjAqbhHU8m39fsreiJQWN19vQA+wRTdZkNk0kULzyh4j/2t3t YsrxkU9p0BslxE7+PeR26FrKeUq0yDbEPsUJ0Mlxzpsq7l49mY5Qy7Vq4XCVRdobiC/3 t0W/VXHtpC/pTB6RZs6cDs1vHEp+Ou0pVVVaRmWkLQ9ty516eQVp5pDLFmGpdWYP4GTV qhC5m2Dw3lhRkpWlQginCZY45SqLb7XipD7QtAeBCSDtbLwNERXjYIiTtDsAiexUUgGY 8FPp8jc/X3bzmljylok8dxf2Qy9wDoMdsq2f7CeVEhpVsj4hsJ7RlSTqWH0FmZbwa28j Xpdg== X-Gm-Message-State: AOAM5327iFr7qcm7ljn1I5fB0Nk3eNNO3uuKHKhzBA8pwiqqGYCTh0S3 fGiCG/u2bU3dS3vrffUSMc9TXdTxA2o5vLw1c2X+wdoUVOdOnuqINWvemwUbthaNcrHxhWhBh2q K8SwZZKpXuEAjmG80APAvkoop X-Received: by 2002:a05:600c:6009:b0:394:33ef:86c8 with SMTP id az9-20020a05600c600900b0039433ef86c8mr661400wmb.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 Precedence: bulk List-ID: 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 #ifndef ARCH_SHF_SMALL #define ARCH_SHF_SMALL 0 @@ -102,6 +103,17 @@ long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr, char *module_flags(struct module *mod, char *buf); size_t module_flags_taint(unsigned long taints, char *buf); +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 } -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 = 0; core_param(nomodule, modules_disabled, bint, 0); From patchwork Mon May 2 20:52:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Tomlin X-Patchwork-Id: 12834727 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 76D7BC433EF for ; Mon, 2 May 2022 20:53:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387537AbiEBU4j (ORCPT ); Mon, 2 May 2022 16:56:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387558AbiEBU4f (ORCPT ); Mon, 2 May 2022 16:56:35 -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 34D27E0A1 for ; Mon, 2 May 2022 13:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651524781; 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=SNknfSGBJexqkcXqGrdMuobS4XnVBPXZUFsa/Lf7sELw28KJJh7O5cmhXr3cTMRlY6Dukv aqFuP+aunOGh5UP0bE2EoxT7YWK1SeDA+J9/cn0padK/U8iMsr4EpcfmOOHRVr8Wd2V4Dn 3mHmW2z8u6e02F7LqG0HerLM/Hjaulc= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-78-m6b_jMn9OlGgc6Vg7ZMIXg-1; Mon, 02 May 2022 16:53:00 -0400 X-MC-Unique: m6b_jMn9OlGgc6Vg7ZMIXg-1 Received: by mail-wm1-f72.google.com with SMTP id d13-20020a05600c3acd00b0038ff865c043so229631wms.3 for ; Mon, 02 May 2022 13:53:00 -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=cg6R5Wxn4jRBsfHU5DyL8cY7/Tq6qz/CRJawlzuCl464r69Yt2mDecganpo/Ty1/w2 6LWyLjpgzFlQ2kliYJgTk9TwFJpu9uT3zwoOV0WAqFWionc2olZ7oZgVrthLWSb0N4YH tvTcW+8Bm0s19nME7/W4/PPdOqNEsd4vNPF3Z31OqzjWiyUZcHlgHkuEJ6dO+AO9VcU/ IS+LZ8frAo4wuoIKl9UxoUb6b5Ced8jBV0tkrp/oUiuRFWFj74aU83085m4alJ3MH1hw iv97QHayc/64i6+/GRQ2uu9IaiXzUSu6XIjM4UkkB6mULTn67m7akLmWLgaE4kAi+c/6 HEdg== X-Gm-Message-State: AOAM5304z5YbLmtAKCUFq/Xy66dC+Ejm10f42NruQwVBj/i25Kv7WGwu wYno5hOcFTKrMx4kF3eFnGgSwzIXZRsd4WKaGRzKjFWD/EW4w1kkQt6B6aDBr+P01WMzQ9vPqq7 kEU1OmRES2QM9a1TDSEmtp5ZF X-Received: by 2002:adf:e7c1:0:b0:20a:b724:cedd with SMTP id e1-20020adfe7c1000000b0020ab724ceddmr10626711wrn.409.1651524773902; 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 Precedence: bulk List-ID: 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. +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) += procfs.o obj-$(CONFIG_SYSFS) += sysfs.o obj-$(CONFIG_KGDB_KDB) += kdb.o obj-$(CONFIG_MODVERSIONS) += version.o +obj-$(CONFIG_MODULE_UNLOAD_TAINT_TRACKING) += 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 } +#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 buglist. */ synchronize_rcu(); + if (try_add_tainted_module(mod)) + pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n", + mod->name); mutex_unlock(&module_mutex); /* 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 = kmalloc(sizeof(*mod_taint), GFP_KERNEL); + if (unlikely(!mod_taint)) + return -ENOMEM; + strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN); + mod_taint->taints = mod->taints; + list_add_rcu(&mod_taint->list, &unloaded_tainted_modules); + mod_taint->count = 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 = module_flags_taint(mod_taint->taints, buf); + buf[l++] = '\0'; + pr_cont(" %s(%s):%llu", mod_taint->name, buf, + mod_taint->count); + } + } +}