Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp78149lqi; Wed, 6 Mar 2024 10:29:09 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXMNO2UzuqwFK+ZylGTVPCTOQJra/M5Vtvs6YTt0F03OWBSAaun7+Rs9ifVPl3lYYWLGp98Biqp026QnNCsLEGgRrZmyHwkXG+rwRnQ9g== X-Google-Smtp-Source: AGHT+IHWkqdNCw6v2vY+C/wOI2nb9/t+8e1AAAzdmtFX8CRKQ83e6Z2hT6Cr4KHQcX4LwL5h2DIm X-Received: by 2002:a05:6359:2309:b0:17b:f04e:7e0a with SMTP id lk9-20020a056359230900b0017bf04e7e0amr5259644rwb.20.1709749749038; Wed, 06 Mar 2024 10:29:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709749749; cv=pass; d=google.com; s=arc-20160816; b=ff9OKlSPUjvxAH1qEkEU+Wffdcc77RmMgX7vHQv+LnG6rhsfLpdM81knBSOP4bFHRQ 3M+nc7ELwFgyUBB0EQJzlyaNc9h3o2YiQQYsKM1ZRhuHQrwjRbFGN3/H4+blVkAB7AsW Lqm6Yr9SZQGmBwLxIyIfOdoEtH2ONa+NNdLgvSeygDFlljX49VZ1111XkM1iCeVdOLJK h1gr16cIMSC9puz+l0ol6IM/m1+w0GnBOJs3aBvVC3E7q64kuexjALJg8HULOI//hZFy uprm8w0GEnfBORDeR9KK9+oNBSOHYQKCJGZ7yUo9fuikv53wseHYHoPKbwriCGiGhCL6 K+uw== 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=UUM81FzZaYB1+q8hTjh77vdQeiiRG/JwjOPLDZjum/8=; fh=WGx97AMa4KSB3kVqt50bA2ifs6WU9mXQqBZc8quNX40=; b=dTcDtZhMDjAQoidzjE07FSPRyMOTuK8Fk9VYd4BnCLOeJwtAsZuxJXPHpzhp1A3mhf Z13R6jzuNLmKISNfGFgURbffkJpWGNMyW1ebZAlxtzFbVE709nTLISNqk7344P9GjvpM ZbH7VTu8zPAbrIjFu09992681+ace0CQLMDnwfYALrHheiq9S5fqLn9hDW5KVaxjm0nz RgyWAizZqhgAQAoOcaPeaR+QdHtfg3lFamHj0G95sisQSlF+P5+LKRqgM0tpFPUUQyx9 +ncXw96c1ahwEofSr5EMZT20G1ZxwIKgjLXrOgaT2e4CGnGwGGPb+U7ORvUZsaynq6Pt pg0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aKGRNZD4; 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-94404-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94404-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id pi18-20020a0562144a9200b0068caccd5f77si15114677qvb.214.2024.03.06.10.29.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 10:29:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94404-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aKGRNZD4; 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-94404-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94404-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id AD00D1C20B99 for ; Wed, 6 Mar 2024 18:29:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E327F146E69; Wed, 6 Mar 2024 18:25:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aKGRNZD4" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 8CA38145654 for ; Wed, 6 Mar 2024 18:25:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749514; cv=none; b=VxKRyR+HmvZAvd/AVv/N8g6D24FGQLpP8BcPy0BaqiNB7JVaM9Y2KjmvNfyOxiu4AARVToqq9BmG8OV2xoE2X3fYIkdTJufO4117pcFbsA70T8n6SkXudOpxIOYVco+PWObNLx7UlKH+FiGvd12Q82gnqhVNfKvT+lzEXi+0vZs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749514; c=relaxed/simple; bh=yct4qnU2TlVHTPT2LuakdSxlKoSb3v4n02iScNGM8ZI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LsPy6kKF/04BqUo67v8d1zJi91nj9ChpvcNcQZcTlSyAOk8wf/4jUsD9u1tdBmz8A16MwYGEbbULnh1Qytgiawr2JIIOON0kbaAxl7/ijdeZZZP/iZ9NYd+71fvspmymgTbCWEHOXg+XltIDDem4B1a8GfFieWV6t+oR9CgZems= 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=aKGRNZD4; arc=none smtp.client-ip=209.85.219.202 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-f202.google.com with SMTP id 3f1490d57ef6-dc64f63d768so3712656276.2 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=1709749510; x=1710354310; 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=UUM81FzZaYB1+q8hTjh77vdQeiiRG/JwjOPLDZjum/8=; b=aKGRNZD4gVFqjaJsShFb1e3SlgKf5jmo7gyCneTdJCHv0mNY4+YqeIGbr80KJXR4gc nrabNlL4ESZLQi42DqFRy9tyItdPC3udhCUp3NIbL20ZEEaJ+HORCD8tve2d69D24wSS 0qGWzHMTOhr36dMBuZqAzyyH2ilsQlKetq2rH9y4S5y7JL3LANo8mu6VHC9LW8q4JQ3N 1r6AQcJ15j/qzaJ9nninLCUYhWtRTk14CxHOw8vO9Gp0erT6a1ZOSyXJfX4GSF48JVhx HYlUNaeK1LyG32sPrN/Sx8Ur1zWgq8OWZTE+mL9yo4Ob74CHE42/JBk6B4ewU5PkazyQ 11Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749510; x=1710354310; 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=DQKx73cYo91wajHoKgscQd6wKbWwPlajJxn/NseqB+tcLN0kj66U3i60M3ze19PP78 WiMSYW4CT1RyC9zxEHxTm29VrK593GC1qiEnh4S24VLO+OQjrmPSpfjq3wq2sD9vSwVq 237PsDHN809C1zkoXKeTrON4rE0xkm5F6Ls8++IQZxRN7zRmREv5Tn5OXTY7uCZF7mOY 6hcK+q7yej6lIqb/z4z7XmQc4jB+dNCPWrFEJ5MzIJw0NRi4fWxVIpqmu+zv5DPlz3mz znxdeDxrK/LOMMQ1JnLIvDZkMFORDImdczlqdbFw53rQYE211fRbvyaY/zfXIIdxfxUC vZ3w== X-Forwarded-Encrypted: i=1; AJvYcCW/w9Zo8FY1PxI9k+3ndkuR76S5Jnqc7p4FTNaMG23YxXAo09tw/mzbY3LC9WAPH36ItxJTdXbIr84ZADu8BnxdaeKCkNJyxcV09IrH X-Gm-Message-State: AOJu0Yxe2wJC6AxcRrGmjYqKvfXaZ1MKNaCNEDIRxY8igD7wk14nA50P ua09ZItiF3DpvKPXd8IQa+KcqdOI49euUqJt+7t+H/KvF4ncaRvYLqT/AKAf5sTPAaRFqGaEhr3 kkA== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 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 */ -- 2.44.0.278.ge034bb2e1d-goog