From patchwork Wed Mar 6 18:24:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13584422 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 130F3C5475B for ; Wed, 6 Mar 2024 18:25:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D95DC6B009E; Wed, 6 Mar 2024 13:25:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D1E586B009F; Wed, 6 Mar 2024 13:25:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4C086B00A0; Wed, 6 Mar 2024 13:25:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9FDE76B009E for ; Wed, 6 Mar 2024 13:25:14 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 793A1120CDC for ; Wed, 6 Mar 2024 18:25:13 +0000 (UTC) X-FDA: 81867441306.30.03B69A8 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf13.hostedemail.com (Postfix) with ESMTP id B585220006 for ; Wed, 6 Mar 2024 18:25:11 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ougjA2pD; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3BrXoZQYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3BrXoZQYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709749511; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UUM81FzZaYB1+q8hTjh77vdQeiiRG/JwjOPLDZjum/8=; b=nuS3X/RaTWIbq3ULmZ4YnOhtj8J3hklyhacQOXGpJdmhtydJWFWGGeLRjgU7jSUO+J+aAU rCmh3Oc1Ar5FWi25z0nuj7fUMwLkq9VP0i70lcXMevZT/JL5ky632rgkMe+G6DepZyszmG 4b0TpUXTD1lIAagdhdMTY88EoL1J5NQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ougjA2pD; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3BrXoZQYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3BrXoZQYKCEk352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709749511; a=rsa-sha256; cv=none; b=AHKPlmF5Oo/agNzuw2DWyz5GNXbIZIskPkPZx45W/PKmFYZSOCIsOUBiifVQQ/nhHt3DQ4 bNr+fC7DxfS2LxsCG/tgDtxBn/0bN8FwM6HPYeGu4gpOfIKuQyLQZwmu/W3MEtv7cCWia7 2wRQSUv88zC+DlEb319q6mWsoNtYlSQ= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcd1779adbeso4114555276.3 for ; Wed, 06 Mar 2024 10:25:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709749511; x=1710354311; darn=kvack.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=UUM81FzZaYB1+q8hTjh77vdQeiiRG/JwjOPLDZjum/8=; b=ougjA2pDPDSDJpvx0+LQfy/5xLtYCGuj3VlcNqFS98lCkoRItnBDFpbmyVyVf+Vy0J StP3oHDBANY7HjE7GP6+xt4aaBk4qCoVJ6AEJl6uYy6yC52PBkoXBEhLCpA2cllfGGVK XxxtdeuyDavWX46yCNKCUX9C6NfREWaq1ll1mGab4poDJZfWRAxiprlxHXXoZxx8tI56 0buEhS5b8msJJooXHYO86lBFCyOWQ8bTgRImu2qBostnrpGT1xLUblYyphCKrenik91p pmcjLxrSDgnZKNbXgnup0w/Zzbbhbb/4UvGl4KHn+F+26MQ1K39xVu7fX95gnV6kwf2X QmXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749511; x=1710354311; 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=UUM81FzZaYB1+q8hTjh77vdQeiiRG/JwjOPLDZjum/8=; b=SVVVIONU65qjCj76gSdBRFZhvpz4AfhCVP24DCETryumHiT7v4V/ql7Y6vCdjyEbgm Jdnq6jkQmhVJbXiwt7zoTCibhhaGSvAkH8oKmRtdsFtKcgjXwPos0Zhw4zMFK5K+ST8N GCnKMFCDdRcTIepO9F43Yhc/ceZDA4vbOFBMBOTtC43trD6m9Wl+F3glyCdpDc9uRtr2 5UptsHaI6b5EbcuzCpj6TER7O5lZN2rfdLS/mzy3W/8Ss1jjB3ltLAWLHQLmW5Ced493 qKsaU632PnIigiVNnrurIlLzFrc1OYZ1N4hnK8bCKRJVHsid9duH1sMQxTIgAHTNLvE2 IyYA== X-Forwarded-Encrypted: i=1; AJvYcCU/EvruljEvOocpvKohiYORD9aSOSRD9ridkjJnlGob3qTJvbqp959f+o2KhocShF2vPTHvli9pAfwLKP2E1MSCZLo= X-Gm-Message-State: AOJu0YyrRndqk5370jyD+uAPg5jy56r7rSA2mUkY7Ji8dZ2vj3/dyTwp lewJjiqPSMHCnhVdV3zIJmhzL4Vms6ER/EqPpWobyjmwdn893pdWCqtnFJ2DwO+rkjge50lATcX Frg== X-Google-Smtp-Source: AGHT+IGqHIJUWrUsfejQErup2Z224PwJZwn/RbIXxQkXJPLPBFMv4l1nRilm/FfAADknSVQZ33UMPS4jncc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:85f0:e3db:db05:85e2]) (user=surenb job=sendgmr) by 2002:a05:6902:2492:b0:dcb:b9d7:2760 with SMTP id ds18-20020a056902249200b00dcbb9d72760mr4211044ybb.13.1709749510558; Wed, 06 Mar 2024 10:25:10 -0800 (PST) Date: Wed, 6 Mar 2024 10:24:10 -0800 In-Reply-To: <20240306182440.2003814-1-surenb@google.com> Mime-Version: 1.0 References: <20240306182440.2003814-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306182440.2003814-13-surenb@google.com> Subject: [PATCH v5 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, shakeelb@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 X-Rspamd-Queue-Id: B585220006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 91zphrrqsgoy6fqfmq5ur6n83ieiz3wb X-HE-Tag: 1709749511-147601 X-HE-Meta: U2FsdGVkX1/DwyM+guwmYMTmmOKdUkI1G4/Zytupcy87run8xPkkvCPeonOZKHf2kxy2mHq5SMXvzgvENfOYPRgP/b2sc6uX3I9aUVpheS9YOZCkhheGhJNDRAUq2nb74VLflAbIFnCYOjS9bcye8/eJw/mcrBCt5NElE65YW8s2DCsq3JBKOfG6dAOg00SQNMnQ4BV1krWYqY8QiKm8WFK4mGdTQ/w+7GB0DA///+kEwbB7PGJ6nF88DgbndCEIC1utxDAwyO3Wtjo2QKLKcfMiDI0yG+IPFG5xxk4PsGeOCXMIzsPRC4oyakLXUX5uwRpmb6eFU//GyTPl0VWeU4bQmrwTh2s0RbQiO9IiwYSCuWCYqw4m6raucoffNk/vUpgP6hpNlMRsdo2uvzOpJp8NE0xP5PfugeFE+o/oga9aBk/F/hqLw5XsCtepHDMCCdJae5o1dTQlKByS+ndKLKz/wAYrm0/UFg/rX8Ucwqju9eNkHemTAk5xTb/d2JBcaYDLIQazPMDjbfxJj2/l1yeBLnU/pXBZLDaEyuFDPYrglU81WpVRG1oevv6GT9FVILHPMiKpAOMsgRZrTsYM3u/J/AQfJUhRTYZHL6g5EQbORqAib3G6i2+qsi0ZDg78IC0hZhVKvTsAmF5KZsPmocjzwRmeDpTiLumq5BOYuVMRYQf5Kuk7cLrEaX4V0Ie2VyzR9lqOkUV3V5eo8ILAy9oNwyjUNIhTbBY41DLX3nHdPYnvowmfnMROWZs8oUWIo416jbJ5baISKzbDV3z4xgbGsXFXLCF7G1ipUhlh+T7fD8eNqaMy30hLHHkMWTp2rOnrUran5Lc5VXMKLmveCB6a8PA5QauFqJcR+AdFj1HiG4du3ntAHtptra00xbgFAqelwQ9JjOT3V0TP465Q2vs9ivPz+itkobOpt1Vv739hIGIx51lhNuOhNuTwTVf/MZi5XMHz9iGwstjx1oi OxEqCa/Q lWA6viYqPkBTZpjroM+AQ2z7a7o3CI3ZWJplnkr5K/yf5exPfgMgP/6ZY8pGcqY+NqvPuyYq/gKk78hOBBohcZL3EtX8EcF2iVoqsifkcQE40dIfIfwLl1OXzjZrbn4mquJfJzQDDG4nKImwDJUaejBH2NVE+BG9ppWwhUzldwz308XGKdbL0BTw8yAjFfgoxVwe4iPJDvMrj8nAniaFWMFEOkb78iQTljtezEVhCBzGEupAvZwiJLfrHRSa/E7ZTXQF+st+sosgIkFujKOsxjiQSUBx2K5Fv1GxLjq/8L4XlDj0qMQrTF10Aek1pk7/9O8sjw6I3ElYbwNtt8miDirzD34ETFZZtC/MsFKQqF/C6XnOKkXwXGV5Y03cmSk5XuODZQFex+NCUhyYDVbhTm8PG7/SkFaVSRwqN X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 bf5a4afbe4c5..41c37ad3d16e 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 */