Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp599025lqp; Thu, 21 Mar 2024 09:52:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXOqXbwQAPSn6GZ+wyTJs6Z95/y9+OF5ew/9CsbH8A2ta8B86KrzxsGIZONKwZa/hOtrwjHNbeiqO6awxWMOZIbPwFK+9aofGcwSwejBg== X-Google-Smtp-Source: AGHT+IF2NJhF6pDgknNfI11dTdRzZ5J3OlCnvkTDeBxjDZoncCuRL3SiMXun3Qx241Ajc6JhUCSb X-Received: by 2002:ac2:505c:0:b0:513:d3cb:249f with SMTP id a28-20020ac2505c000000b00513d3cb249fmr4212952lfm.52.1711039960498; Thu, 21 Mar 2024 09:52:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711039960; cv=pass; d=google.com; s=arc-20160816; b=itt/J329AYJkPM24Be3I/bsvYPG/f6aYVFnmFOLRWiIoN/XVO39wngirAgPk5VA702 cOsnOR/meKvcj+wmOsT+gRHJOw489xUh6EJHJSquystF4Euo8Jx90DrneM9BfTpg4WO4 SpOhADMmiMTfhGFEJW9HbmRGwAQBAieXExIPM8gc0KaYQ9k5R2Cvh/073/Ff1ufoVho/ u4UBcgl04DSvKkPQwuGWgIAp2UcfqccItXWgWyHoT4FNKD4XgCsWAbk6LLyNM7XxNg5Y 41Qlqpftg9IHNgKDijha0ZAbfHsHnytQTHml6vl3zFi3aaCzml+EWyXhSBsGuX3LcQUm TEuw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=LQ90PfNulEINqNY+MaQbbDhVRZQhbHdS/nw/zX5Mh3o=; fh=mWnk770amC+o7uHM7Om0nJDUBt0LUaGiSm9jxSaosVE=; b=EXl/7HIMmXFAlwoyms3rIrtS9WGVNZtNLAH1Co6vpjlxwe5Ju6tcdp0/OGs4/DXebZ w88rjhaQ/DtQFDH5g2c64ehNPc2gy8hre9BS6baJOD8FHK4lABRKPuyvXSgLarn9x0Sm ftHIsY8BKEFWSCQrRdwEpXAPHfcYE/Oplw3+xbIyTEN6g9/1tzUNm4mP1fqCtnZR3Wqa kYN7M1ULim6hsrY7HKwKnhWNPbNRA4/PqxR0tOx8Bz9vAPEnLOPei8pte6/73Y0U7Kr3 agbm4aMHhSRev4MgMxNyB78DQ4Qf3i9Xhqx76T0dUM4SZBh8ZaNJEXXOagPWTlwBsLQb oRLw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VLdzFuYV; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110366-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110366-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f1-20020a170906494100b00a4677942cfbsi84009ejt.324.2024.03.21.09.52.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 09:52:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110366-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=VLdzFuYV; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110366-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110366-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5F8341F25B30 for ; Thu, 21 Mar 2024 16:42:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6DF4D13774A; Thu, 21 Mar 2024 16:37:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VLdzFuYV" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2B58136673 for ; Thu, 21 Mar 2024 16:37:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039060; cv=none; b=SnF25aWgSHCAe+Hdg2RJKQqMV9H1LFho9BRHUPgqw/yanR11AclAlTMeD8OuROe02+CA8F/f0bnDKx5X0DbFVmJ3nX+l62R9iuZG+9CXno7Pwo2c/Si6ojRIkHz6L5/eDanasV53sahba04xCQGhp1/jVao8mUxd0plK3z1mz2w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039060; c=relaxed/simple; bh=+Wbn8cbgVpizkhOtX6sgZbjCyWBIalHOvLdju46wytw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IwfMZgfJ5d1r3ma9NHOL2AAv2ETsGADF8kkk7bRxyoCjWbBgefVkJ2k2oM6kU7RZ5K7pZ8i/YhJ2XDFq4Z5ejcJQ9qfNVIHkhoeJXINyAGT5Ns+q/VHvIzmOS+lmgQR9rYwhHZv4O5wiqkVlni4Y+e6F5EUgS1ej7MSL0KbMiUE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VLdzFuYV; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6dbdcfd39so2286534276.2 for ; Thu, 21 Mar 2024 09:37:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711039056; x=1711643856; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LQ90PfNulEINqNY+MaQbbDhVRZQhbHdS/nw/zX5Mh3o=; b=VLdzFuYVlDnKvbI2NWinPW4x+pQ3NECzSUAnYPnRknvDrbyQnfTOgwPiyqGY/dZoek 2cMH9kG8+ayl0dYbdHnip+W5KKhh98W01vb3MQmPNdsYhqBSgBjTHYAB+HlTJkP3YIa0 dFpV59kt/n8psw5XjUyrsCHQUPlUnOKEV4tMUfzGrBphEMp/0cyiERcOgUO7MsIc9XDi fbPo3ZwVLJDAqzo2v54BP54aTT/AzsLMF6uDea/dHXvctQpC/AEu4S8cDYOWO0fqR9Fi G/UzVLW06lqao5GKwPKxT+cSjIGI++P3PkrqGKG8jP2W0SiGzU6wXk4L1MCRAYo9/i2n NuhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711039056; x=1711643856; 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=LQ90PfNulEINqNY+MaQbbDhVRZQhbHdS/nw/zX5Mh3o=; b=fnGDkP5lqCjXDbvuqIFzRznJUjAzaMeys+OTrHdg3BKVR778d4wlq4G9thjPg2EnOU Oghhv7XZ1BVZkyPXlj2j5gkxT/VfJCcyUJiewwdPsFs43KqxDLdQ9wstrvh7W644T+he 9sPB4zEUBtxv/Jmebq3fVnuGIsH8897j/KyT7gUo6JaeQhoHoaZ83DhB1M+FqjwBBRCy HcT50DbBR+JUwZg74vKbw4+v0x+iMcaM5tTAce4CNfB8yNyucBF0q46OvNG0j7KaE1Xw /nt60MFfi/dKVUPafiKm8f1DsLTG/bFld7rg30W8ldGGNP3sKCww/SR+tR3GUttQLsrk Lcjw== X-Forwarded-Encrypted: i=1; AJvYcCX1AXORYVbnrc/QrhuQ6ssfNY7wKM/rTIjnjMo6scpTz8Yv0tDTcr+r2S8BNwDaQotq73o75d52CzD0PtGKtrXk2AUaYa8L0ND8CZHH X-Gm-Message-State: AOJu0YwW5U7E0e7hV6c5bLnh4rSM80dr+Uop5/I9kI9H3AKJe1e05a/Y LrJ0T1qZqaZOTqkfjCpgactFKRFPUqrxF/DwCj+o9xyVoD1fKDftPE+ptPeAoepx0bbvxJkCsjH bGw== X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:a489:6433:be5d:e639]) (user=surenb job=sendgmr) by 2002:a05:6902:1144:b0:dcb:abcc:62be with SMTP id p4-20020a056902114400b00dcbabcc62bemr5818428ybu.6.1711039055775; Thu, 21 Mar 2024 09:37:35 -0700 (PDT) Date: Thu, 21 Mar 2024 09:36:34 -0700 In-Reply-To: <20240321163705.3067592-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240321163705.3067592-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240321163705.3067592-13-surenb@google.com> Subject: [PATCH v6 12/37] lib: prevent module unloading if memory is not freed From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, jhubbard@nvidia.com, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, songmuchun@bytedance.com, jbaron@akamai.com, aliceryhl@google.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Skip freeing module's data section if there are non-zero allocation tags because otherwise, once these allocations are freed, the access to their code tag would cause UAF. Signed-off-by: Suren Baghdasaryan --- include/linux/codetag.h | 6 +++--- kernel/module/main.c | 27 +++++++++++++++++++-------- lib/codetag.c | 11 ++++++++--- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/linux/codetag.h b/include/linux/codetag.h index c44f5b83f24d..bfd0ba5c4185 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -35,7 +35,7 @@ struct codetag_type_desc { size_t tag_size; void (*module_load)(struct codetag_type *cttype, struct codetag_module *cmod); - void (*module_unload)(struct codetag_type *cttype, + bool (*module_unload)(struct codetag_type *cttype, struct codetag_module *cmod); }; @@ -71,10 +71,10 @@ codetag_register_type(const struct codetag_type_desc *desc); #if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) void codetag_load_module(struct module *mod); -void codetag_unload_module(struct module *mod); +bool codetag_unload_module(struct module *mod); #else static inline void codetag_load_module(struct module *mod) {} -static inline void codetag_unload_module(struct module *mod) {} +static inline bool codetag_unload_module(struct module *mod) { return true; } #endif #endif /* _LINUX_CODETAG_H */ diff --git a/kernel/module/main.c b/kernel/module/main.c index ffa6b3e9cb43..2d25eebc549d 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1211,15 +1211,19 @@ static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) return module_alloc(size); } -static void module_memory_free(void *ptr, enum mod_mem_type type) +static void module_memory_free(void *ptr, enum mod_mem_type type, + bool unload_codetags) { + if (!unload_codetags && mod_mem_type_is_core_data(type)) + return; + if (mod_mem_use_vmalloc(type)) vfree(ptr); else module_memfree(ptr); } -static void free_mod_mem(struct module *mod) +static void free_mod_mem(struct module *mod, bool unload_codetags) { for_each_mod_mem_type(type) { struct module_memory *mod_mem = &mod->mem[type]; @@ -1230,20 +1234,27 @@ static void free_mod_mem(struct module *mod) /* Free lock-classes; relies on the preceding sync_rcu(). */ lockdep_free_key_range(mod_mem->base, mod_mem->size); if (mod_mem->size) - module_memory_free(mod_mem->base, type); + module_memory_free(mod_mem->base, type, + unload_codetags); } /* MOD_DATA hosts mod, so free it at last */ lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); - module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); + module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA, unload_codetags); } /* Free a module, remove from lists, etc. */ static void free_module(struct module *mod) { + bool unload_codetags; + trace_module_free(mod); - codetag_unload_module(mod); + unload_codetags = codetag_unload_module(mod); + if (!unload_codetags) + pr_warn("%s: memory allocation(s) from the module still alive, cannot unload cleanly\n", + mod->name); + mod_sysfs_teardown(mod); /* @@ -1285,7 +1296,7 @@ static void free_module(struct module *mod) kfree(mod->args); percpu_modfree(mod); - free_mod_mem(mod); + free_mod_mem(mod, unload_codetags); } void *__symbol_get(const char *symbol) @@ -2298,7 +2309,7 @@ static int move_module(struct module *mod, struct load_info *info) return 0; out_enomem: for (t--; t >= 0; t--) - module_memory_free(mod->mem[t].base, t); + module_memory_free(mod->mem[t].base, t, true); return ret; } @@ -2428,7 +2439,7 @@ static void module_deallocate(struct module *mod, struct load_info *info) percpu_modfree(mod); module_arch_freeing_init(mod); - free_mod_mem(mod); + free_mod_mem(mod, true); } int __weak module_finalize(const Elf_Ehdr *hdr, diff --git a/lib/codetag.c b/lib/codetag.c index 54d2828eba25..408062f722ce 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -5,6 +5,7 @@ #include #include #include +#include struct codetag_type { struct list_head link; @@ -206,12 +207,13 @@ void codetag_load_module(struct module *mod) mutex_unlock(&codetag_lock); } -void codetag_unload_module(struct module *mod) +bool codetag_unload_module(struct module *mod) { struct codetag_type *cttype; + bool unload_ok = true; if (!mod) - return; + return true; mutex_lock(&codetag_lock); list_for_each_entry(cttype, &codetag_types, link) { @@ -228,7 +230,8 @@ void codetag_unload_module(struct module *mod) } if (found) { if (cttype->desc.module_unload) - cttype->desc.module_unload(cttype, cmod); + if (!cttype->desc.module_unload(cttype, cmod)) + unload_ok = false; cttype->count -= range_size(cttype, &cmod->range); idr_remove(&cttype->mod_idr, mod_id); @@ -237,6 +240,8 @@ void codetag_unload_module(struct module *mod) up_write(&cttype->mod_lock); } mutex_unlock(&codetag_lock); + + return unload_ok; } #else /* CONFIG_MODULES */ -- 2.44.0.291.gc1ea87d7ee-goog