From patchwork Tue Oct 24 13:46:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13434583 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 34B2DC00A8F for ; Tue, 24 Oct 2023 13:47:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E9FA06B0272; Tue, 24 Oct 2023 09:47:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E03A96B0273; Tue, 24 Oct 2023 09:47:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C06FF6B0274; Tue, 24 Oct 2023 09:47:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A7DBF6B0272 for ; Tue, 24 Oct 2023 09:47:15 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7B2F0C03C6 for ; Tue, 24 Oct 2023 13:47:15 +0000 (UTC) X-FDA: 81380481630.14.7680D19 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf02.hostedemail.com (Postfix) with ESMTP id AB81380018 for ; Tue, 24 Oct 2023 13:47:13 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Y52j4L1O; spf=pass (imf02.hostedemail.com: domain of 34Mo3ZQYKCIU130nwkpxxpun.lxvurw36-vvt4jlt.x0p@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=34Mo3ZQYKCIU130nwkpxxpun.lxvurw36-vvt4jlt.x0p@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=1698155233; 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=+qMxws3YMU5LIbVoF9EJC7SgnlaxtiNdFL01V96iMG0=; b=g0OU9aHq1bf3EFcVN3N2CBf4V7Ncsfr27RndxGYV4Qx5lyK6dLZjcfy1SdTitwmiRQHn94 sp9M5UKTMOjsv1/oUUKx2kMwS7ElS/vy/Pg2X1IiN1U13qEYZZlJ1t2gVshwWI6VFze7Wj nsGYZXDc7h+5ETzh5sj0lmktmgw4Pok= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698155233; a=rsa-sha256; cv=none; b=UG2+qz/vuHY23lH3+Z4+bgVhD6ONK8W92gbGvLzGiZXP8aiPluQg4PRg4cm4Uz7c8vvcgE 0kJTpXbDjyrt7DysvYqrTOhnyEUjf9CK44uyEA9n8GN4gvZbLpSTrQiimL6bdJD0Pzx8/y IOHAZfBsoeIW7eoXl71O/X6WXE+fNVY= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Y52j4L1O; spf=pass (imf02.hostedemail.com: domain of 34Mo3ZQYKCIU130nwkpxxpun.lxvurw36-vvt4jlt.x0p@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=34Mo3ZQYKCIU130nwkpxxpun.lxvurw36-vvt4jlt.x0p@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d9c4ae201e0so4420305276.1 for ; Tue, 24 Oct 2023 06:47:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698155233; x=1698760033; 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=+qMxws3YMU5LIbVoF9EJC7SgnlaxtiNdFL01V96iMG0=; b=Y52j4L1OvSijsyPSgNbX2CjqrI4gElom2n8coYv1DycrukENpDw8dlceVfr7pCo+jf +FEWTwd4UP0OzZzb9ocKxD4QBsi13qZ2/+wGkB/bUJU9CD3RfI/wX7C3hy+DZjtERQEC B7ggMOgreL/jKWu9T6CD8BElH5tJsCV4A6bMaTGyGp/ia9PsTnVKGwNKIQtMVIUC1TeF EOJE5vk14AKndnCiw2Rlzj4xe7vw5J62sB4Y6cCM83FW7w766j4BKTFmvBxgY0SNAXNf i6xjD+dfE73GV0a4hCC2N+FtlR4H8Rl5fHT5vGtb+3QGFXlw/PhxfsCIjHe76kv2JPmP CdLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698155233; x=1698760033; 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=+qMxws3YMU5LIbVoF9EJC7SgnlaxtiNdFL01V96iMG0=; b=dDNSqW6XEoy7SXyxzHVvjcdDm2S3ikMEYWWQY97P4W7raytVgGKamwYWGE64uQEvbP LqZGZ0nsPTC8cX3+FlWIqoE888c03UfdZWv/szM33ivsDMNnz6dTLEkKFwps21RzgSPi 9+Sol/Ikfp24gCVd7xEhtIoKFb0y6DLQKe9aIUYL1AcKM3Pw9xp3VYDaJnoJ8HSKcjhs vjq+p2b3miDF+h8oxKp9D8X/xwOszi3Iniq7Leo2MXHuuODFSkg1GtbzkS8XCAPOiMS1 x9Zj9ud045j6w9asoufMumxiBRWE1bO5/3H0di/K9AOtINjx0Mes/ZkU/3bAkggsc4JT hQmw== X-Gm-Message-State: AOJu0YyeMcItk/3sithcfqI36DLDPaOZUCTPF98Fw+iGpIbg8EvxFxWZ hCQdeYMvZVNRn6rN6kjMo7NPp32kwYk= X-Google-Smtp-Source: AGHT+IH+f+GT4bbDivhKX/Rj+lmLO8Xm8nWs8jzCh/R0o0BCAeKtB84tFP9Lg2d1/1Vq2LZxNMEv6GNyFyw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:45ba:3318:d7a5:336a]) (user=surenb job=sendgmr) by 2002:a25:b28d:0:b0:d9b:59c3:6eef with SMTP id k13-20020a25b28d000000b00d9b59c36eefmr419344ybj.0.1698155232720; Tue, 24 Oct 2023 06:47:12 -0700 (PDT) Date: Tue, 24 Oct 2023 06:46:11 -0700 In-Reply-To: <20231024134637.3120277-1-surenb@google.com> Mime-Version: 1.0 References: <20231024134637.3120277-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231024134637.3120277-15-surenb@google.com> Subject: [PATCH v2 14/39] 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, ldufour@linux.ibm.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: AB81380018 X-Rspam-User: X-Stat-Signature: smhne5ed8awgpj3hkwbrceuppqmdaxj9 X-Rspamd-Server: rspam03 X-HE-Tag: 1698155233-188611 X-HE-Meta: U2FsdGVkX181g7df3vtuCT/DgoNyddSS3LR0CG4WY5ch6iKVJkX9AtGEY7ubqyqn1bhMZaWKl3wvXVRkxgorjRI1otaVUX4ihj41br9pXUfx481Gkq7s0PtL8vgrl6Y6RMq4sDFacHLRsnaMjD7C3eprqCk6H8mhjZc5hgWslYgvlwRSvNWxVsa0q6pIQuulH6GlcXk0NUtKSLK/J5kkmnrcekC7KzrSEceCOxOd14Z8s3/yRXPmfCDxHb8GHKs9PY3LcefLY00sLlOcduj4tlh7sOgw9vOgzQH4e0x69PRAaXYXRuSVF0bs8BUaAFxDA1iSZ0ByLWXZOJN8oPZf6KX5OrYgRSyj0y+LiAz2vVmYD54NoXBIMqkHgch5Ih2POAUUPO4MBwt82v8k5+QoyQ/ban0O9TAksGtW2SsVRHKWEGSQlKAqC4KqXEDlEUbBagUcgN1wTtBZAuhnRodIFLOOkolPjzgg9CTEoxw0ZRRb2bx2a8oZq+AxBjwUi2e1Lw5mwD5lLw8pcYqRYwd+LoSFRaQCKAa9IIIuQyuZq1cG+ErXqYHTZWZecW5KbsmSbhhsrrroQty7sskwtB5NzpV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dM= 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 c0d3f562c7ab..079f40792ce8 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) @@ -2295,7 +2302,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; } @@ -2425,7 +2432,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; }