From patchwork Mon Feb 12 21:38:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13554050 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 B1726C4829D for ; Mon, 12 Feb 2024 21:40:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04CF06B0093; Mon, 12 Feb 2024 16:40:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3D796B0095; Mon, 12 Feb 2024 16:40:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB68E6B0096; Mon, 12 Feb 2024 16:40:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C53A06B0093 for ; Mon, 12 Feb 2024 16:40:01 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 688F0A201D for ; Mon, 12 Feb 2024 21:40:01 +0000 (UTC) X-FDA: 81784469802.19.7AB8DDD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf26.hostedemail.com (Postfix) with ESMTP id B3D0F140003 for ; Mon, 12 Feb 2024 21:39:59 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uID8xtGr; spf=pass (imf26.hostedemail.com: domain of 3LpDKZQYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3LpDKZQYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707773999; 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=9alS6gtOXFejwBrY9h+FRijZZu8X6fVY6jwbxDwAezo=; b=Eqx1EdiCZJtO++mSUXTFuyWIUO1ag6DS2f8WmCiUEz4Gc2y74TD4Bn77Jo+fggT++EDZ4a /S3qXPIBCYZJwi5lnHjOeS/6/Ac0kt7Z467RgVm+cM28gzh/SI2+SNhURA0xoGEEBJewkq mhpUv9Ll2cT8a6sM2+PCOZaFpsrc+XU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707773999; a=rsa-sha256; cv=none; b=A+u5uA3ezqmCk5/xdjaFD2InxiXK01S77bEGRLUjEei8fsBbSpaMIlfQxbBWeZRbP/iHgz rRBSXOPIcoBxRQL6siohB+pL7B+MlN8P3HaEOw5GRPWohzW1te/nUsyCyMNqybE139PYgK r72kxQhBK1nI3zNieNDHj3lGQpNJSBw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uID8xtGr; spf=pass (imf26.hostedemail.com: domain of 3LpDKZQYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3LpDKZQYKCK0fheRaOTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5efe82b835fso84262837b3.0 for ; Mon, 12 Feb 2024 13:39:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707773999; x=1708378799; 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=9alS6gtOXFejwBrY9h+FRijZZu8X6fVY6jwbxDwAezo=; b=uID8xtGrXQVnEXNHTcNLxTS9O/0avr/sQkLrTKc9Ke7yl0i8vLm/iK2EbDYKU2w4rh X/NvSWuSYKi/mpWl/3uyPXi8+y6cjOWZT7yV1oQsbhlNSsINq9euARHBF4K9n8DygX7N ow/Z35dr9HvYyMmar/yqpiJ2WeVuC4sr8v50mqFlXoWhp3ZLX3JebS0ZlCOVuyTVHG6Y 5wsGPoXAIfJ8xDjWsdQuIdz5vvsJcnPZYDOq/go3McJG61+YLwWrbMtG8WuoqNUaJzsM lt1+qMNcpzFxe3Q/W7ciOVo1wjT57FjRDvmSZ+GEjdxinaPc3f+8SRaOMW6WQDW7KhEA nxVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707773999; x=1708378799; 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=9alS6gtOXFejwBrY9h+FRijZZu8X6fVY6jwbxDwAezo=; b=MgYa88pNHkH/yY7CkIU2Lxi4AKmz3kXQyA/LmxSLalQ4uys75bDXkvNBBjrXBGVI6O REqfG9sMGdPsb8SMNiZ1cfLiYx9FoOp20kNxiueZItviAj+6WD8cXmzXu9vpRlhDYQM7 cE2mWi/fumANfJGykkgk3Lxrl7b0xAHCRvqkTuYhMsmZ2Xfhv1t4kW5sRVt68LceLwJf Kyh3QxuZ5ijITlyYyM0p1Ms+c7opb6Ch/UXPq4FDNIufV1nk7CGu6Y4E64/Lj1YfD0c8 HiUmMnhInZO6eC6U4pK+dg/CHxMnU6LB7JQ50M8PIZaYZmzqlFeClxJjTIsZa4aydTiG g6Xg== X-Gm-Message-State: AOJu0Yzpxk59EHoQf+cX5QXupBV4Zc6gwYv9VIWGp7xK07aojC3NPuM0 pyrc2AYZfny0PTIlLh0gavC6UTorPVnH6COEkptJU7rf4RT0vqMK15WJPE+zfR8ZJjss7hClodc SyQ== X-Google-Smtp-Source: AGHT+IFW0cHHZhA7bV0V2zEhIEFhckUZZIzrQEg30Nhl0ohuGdOhgEJdFPgUoRF4LKS4dpvAz4HDei+0kVU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:b848:2b3f:be49:9cbc]) (user=surenb job=sendgmr) by 2002:a0d:e844:0:b0:604:a67c:7f8d with SMTP id r65-20020a0de844000000b00604a67c7f8dmr2200694ywe.5.1707773998774; Mon, 12 Feb 2024 13:39:58 -0800 (PST) Date: Mon, 12 Feb 2024 13:38:58 -0800 In-Reply-To: <20240212213922.783301-1-surenb@google.com> Mime-Version: 1.0 References: <20240212213922.783301-1-surenb@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240212213922.783301-13-surenb@google.com> Subject: [PATCH v3 12/35] 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, 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, 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, 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: B3D0F140003 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 41o39ierq1cfdfegczs3djkmo788tb9a X-HE-Tag: 1707773999-944780 X-HE-Meta: U2FsdGVkX1/W/A555moEZ8T7d1ZAVbiWBcYZgMFfCUWxFMBdO4wdRwtfs46H3T+rK4FyvzcUXp+KloFb5UG7iTdHu7y5SbTp/SE5CfS26sjnwHpw28nimk+6/nWrEbacGlwsFJ/daJiNSBdy/R/FCpWPxsOYebOFUCXQElp4aWZWnzuC1vrrPQb8+KkmWMoNr/0yJMnClCdKb+u0m4x2ZljlkKy+CvMco5yfzrhzsjYCkDtYwCUV4Av6iYiphC25yussNx5Qs/76mScdeZXQQTnez5ce7AXnITPq0kzc3CJxTQW6JTmgSYHjP6utOcvyRiBxjxVDWLJtR3AGNk6OxEFWtiSo7sPUocWOJjypFT2X+aSfGnhsuOX0beXRNqMAFj5ANG2zfrkXe+5XF+4mkyvCUuQLQWaRwdQ/VhQb8cVAbIvIrscHoP855cHQ50PyhyWOHPyGqYaRUxbEl2AAC6wa1PokLNw1x2eblHRK2qfI5WZ7uNohvFKKjc/1HA9x4ki8wbLdw+Gd2LYxKsPBZLvDkP2FpIYw04wiU2L73LNZ88tYaX3JOuWhA3bPbcT6mw+f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Wxs/KkNEN5+SByP71Z7lJmcw9lhc= 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 | 23 +++++++++++++++-------- lib/codetag.c | 11 ++++++++--- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/include/linux/codetag.h b/include/linux/codetag.h index 386733e89b31..d98e4c8e86f0 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -44,7 +44,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); }; @@ -74,10 +74,10 @@ codetag_register_type(const struct codetag_type_desc *desc); #ifdef CONFIG_CODE_TAGGING 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 f400ba076cc7..658b631e76ad 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,23 @@ 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); mod_sysfs_teardown(mod); /* @@ -1285,7 +1292,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 +2305,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 +2435,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 4ea57fb37346..0ad4ea66c769 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -5,6 +5,7 @@ #include #include #include +#include struct codetag_type { struct list_head link; @@ -219,12 +220,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) { @@ -241,7 +243,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); @@ -250,4 +253,6 @@ void codetag_unload_module(struct module *mod) up_write(&cttype->mod_lock); } mutex_unlock(&codetag_lock); + + return unload_ok; }