From patchwork Mon May 1 16:54:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227650 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 332FCC77B61 for ; Mon, 1 May 2023 16:55:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF56B900011; Mon, 1 May 2023 12:55:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CA5C1900002; Mon, 1 May 2023 12:55:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B46CB900011; Mon, 1 May 2023 12:55:45 -0400 (EDT) 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 A6A74900002 for ; Mon, 1 May 2023 12:55:45 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7F480A0700 for ; Mon, 1 May 2023 16:55:45 +0000 (UTC) X-FDA: 80742287850.03.D19EBDD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf21.hostedemail.com (Postfix) with ESMTP id BC4DA1C0019 for ; Mon, 1 May 2023 16:55:43 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=QWt5E67j; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3Du9PZAYKCFUFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Du9PZAYKCFUFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682960143; 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=KlR34XlXux9ljlJXPX99aK4k/cmOetFB9Q46atx2T7Y=; b=K2AUkeMXpsABDG4f7SSrljjbLwOxkVNB4PlYDXAKaLxI6QvQCLL5ub51b1SD+XeaD+7jhb K0ep5Z77JPqtLJDCAkB4XjfGeZNVKqDp6ld/SxvvNoN1MBoMcM6wGZjHt4ztCIrlH4qYQZ hziuSvs+xDtw11jMUCtfG49dQEpLprQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=QWt5E67j; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3Du9PZAYKCFUFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Du9PZAYKCFUFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682960143; a=rsa-sha256; cv=none; b=jGoMiYTh6gnT9XoemZELdkcRC4gh1FKXoYn2uKYtOus/MlkVX8OvLTXToD56uOBoLXrmwN CIGmqJ6at4fWApNd6x1Ja9FNMWUWhcCRP3VytnqBy+WarrK/d6YU81Q8mzeNum7F0wbssX yEyIlgB8zzmoD8JfQfR30E3Ph73e3dk= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-559f142fce7so40198147b3.3 for ; Mon, 01 May 2023 09:55:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682960143; x=1685552143; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KlR34XlXux9ljlJXPX99aK4k/cmOetFB9Q46atx2T7Y=; b=QWt5E67jzX3Q9n5FFMEQTPcgcYXDs3S4iywG6hR5j3a3JlBUkYCc4DtOpxxkkjLFnu 4D+SF9vM2W+aSyyk8kJjTG69VNYflbotzNlQqwEogeuQynY5M8r1b7VoJRcfxutnecbL LuU9GuJRo0TkDfrLBSlYcY4SPMYXeI4QMPcJoAM+FNu7Tta0lyhA6wvriGeN5RlC9+S0 iffTz/pkSCC3kF9yyYctejaCym7L71sJ6nP1Y94qsXlQPHEO6LhbQR95ce2BCueglN+Q qfFo6LHwZ424yz+z+s5ncB2VWclW4myqX2EcpXj3e8kJr6XkScT1PFtCKYGoTWUopffl ivjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682960143; x=1685552143; 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=KlR34XlXux9ljlJXPX99aK4k/cmOetFB9Q46atx2T7Y=; b=LjE+tBTCmkjylPhdPeaXaDiHo6bbtjVsxfXC+UWo0CnD2HwuMXNkgTH4kohjJ+kJc+ vowsQAQnjy5j0vTK1T1/ZbddoK1qGa81vTu3MflGIghfwfBIyKexXg4OrzDVwtlgxaBn DEcfl7i1AOzb+tkSVwvNwXGFC9LvZ7wid4YZSZf1wY/KNYqhAPxxjJvr2POwv1MqaQjS vItB3Ht22zbdErXGgoLiq3nf2Ra996kps1cuoGLVtxKukdK9onfLvOUAMkbmugGNiNP1 uf+8w2rbN2KBhrsA9qajjwknmP8EecDr1/XYBsM7m2X3YMgQ38FKgaP7ZsHBFcXPkFse 71LA== X-Gm-Message-State: AC+VfDyerQLzCnYj/kMb+W98knbOtpDT/QwV970Inxsc8mF9C3xJCv6z QqdtA+od61n15tWB1vqER+OVWL/D4kU= X-Google-Smtp-Source: ACHHUZ4HzBM+CNluGDUwOIwZDncQGZjVanEzUdQYw0pkU2rekMEn9D+HYHGjdxqKqeVKj47azkDUlaXgHqg= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a81:b3c8:0:b0:559:e792:4e87 with SMTP id r191-20020a81b3c8000000b00559e7924e87mr4661945ywh.7.1682960142770; Mon, 01 May 2023 09:55:42 -0700 (PDT) Date: Mon, 1 May 2023 09:54:25 -0700 In-Reply-To: <20230501165450.15352-1-surenb@google.com> Mime-Version: 1.0 References: <20230501165450.15352-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501165450.15352-16-surenb@google.com> Subject: [PATCH 15/40] 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, 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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: BC4DA1C0019 X-Stat-Signature: s5yr4px9dt7k9tdax67df69f7x3psxsg X-HE-Tag: 1682960143-471536 X-HE-Meta: U2FsdGVkX1+K0ZznrJcQLlATy32kY1n6l7nKW02DVa7o52Nz+7w5p8DT4vUqHsNbkZlDPYgwAdCNksvg2zQ359NJbTRNKWzQD+gvw5SDmshCTg9nQ0DwIhd4mxGnh19IFaH/c/7d8BRWVJDJpWlAFtLWrYJA2MTB+SkPFIE6v0WcRT+ZWqVERqwAxemc2zaYBtPZ04OZeTavy3VezkhYjkYomOgDgz1CGQTvEujqmlG4iaZ48Up4utv+knnWKfvCosJBJXz4yPMhL05oEKre2GCsIwTrgNSuujXTr66S95mQKaQTL5vHPR4nxvxu+SfxAsaf4YQH64cVoc3EsYRweI4jvDRuMXYwLfxVZlDTi3mJgTXAOd0YWu53CCbaVZLgFDah/J+4IRcPMichfCg8CLUrKTVc+wQmjxrNUb75+qA0RYRAsWVSrxAMBPwQHQ9TnCYRsx0yzgQ6S59W2zXtIErmFWlfcGszmAaV7ixKmfc0isaPk5mDAyuyCWRa95Q5REE4OgMLo2EEfqBqAEk0RXS3EZAv0zUFqDnG/7VZ80sZrWs8D4203KBuGPC7k1ODYpj1pPOAW4uWGyhSFSImhqEDv15Cn5/bGjHvThKh1z85tyfaTxY+I0zxMoE/LEKtqAqmQbJvk4Xm8yZeGx/eoXANHpUhuJIT27Daqljv53XNQf9dZHxHhBeZb6IFtVbFRny2g2nKEoNKphv945i/7/SLmTcM/+dOK8sipkspLk/FtLMTQ0tJtCgDtjBBS2DgFUC83UR9Kzh7aorfhLLL+2BTR7glNSsixPbBJ62hChzWjnbv6eNdWt0dAEzkGAE681fwBXTrjBHhqt0AnSUt63mO9ZudPAirYWI7wCVhG8ucwK3eIZdnT1/a5LRDzVPFTeblZJXf3UOJ2eh9gDueBGV7BemkYupSBFvo0YRWcc+RxiHrx+5qKEm8lXDpPjmewiUl1UKKv0+VY6kKYUZ cKTb8y1X cg1dSqJNNg8FpjPiOGDXTbrOfaBOuBRm/0RZ/sn9/XizYxLrg92YUExQtQYv8Nh9YlQMxmknEltzLccbl8BvgWneT39a+WVspNqXovcsV3DQKvxNTVMWWt5DPVR3QByiv5l4RErVyBnUDqiARfrHwfEEdGWpmZTbqxljhUe4eC4UBycwmzuWvRsYK0K7ae1NZ/hv8W00ag5v006nUTfZVzZKvJTMhdTwwdSj1mPrrCO1ly68dEm2SjsVemgXbovNq0uBDwiwOGVtCuhqfm/mL4vdaLqAL1V+G2muG0/sYucGDfbk8mypNC2XZpKhoiGBGEyI8Regtut3GozmLs4EDSn4SAb25lrEXpfrJbHisd/7N98Y3zARrUUcFkKizldfYLT9fwg2YFLwLjtt8IgRg3YCFKzXvDseh82tU64OPVBMKk3AGuMlAbJfMxoN1GHrBCbCDmV7MgV4i8BzM0vEbpKqQSRf19VhgquLm1ukTIpr6Of1TB2ICh0ZPCZbQJzmvet2Uq7EzT9R/wIxOt7uC7s7BeyOcHcaAkRAkFcMxPlzWiH+HUdW7CmiBA+tPxXW5XEOsl/qOEIFn6AyP+wgPiwMgEYxGxgE2hcxDwkaFdmk98c9lwfqHRCAQoNvI2EPzr6w0Afm/hXksDfsuXvnzGGSx9g/0RddiiKxciy1e3ME5LIyxC+fiIJeuxQ== 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: 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 4232e7bff549..9ff56f2bb09d 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1218,15 +1218,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]; @@ -1237,20 +1241,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); /* @@ -1292,7 +1299,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) @@ -2294,7 +2301,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; } @@ -2424,7 +2431,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; }