Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B64A1A3BA1 for ; Wed, 26 Feb 2025 01:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740535191; cv=none; b=DmRCz0OVqTJFgV6CY15mIzeiuA105s/09j3hM29nvq0hRb8IIaCW74bhYuusaLqkJw3W3coqu+2zlrk3ZmkTp+RflXC0vegy0BenFdN9DdGloBceC4+XQG0GQzRMmxcXkye8Vj60MAdAuG2IzY3FKDXda+ynV9WOE6fyhuEOJQI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740535191; c=relaxed/simple; bh=YmVTk1kP4NcQiKUVNWhAzAdqCxcUpchGc4X67KeEbqw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=VURmlm606lTzl2bwhuP3JV2GhqAWv0SCx/XEsd03H0NDL8APzTEOs4mEwkLc0UmOIn3CC3+k1OgY7fUxxf2/SROTL0yYOfOc4XZDl2jL9dTE/fsXzW3kNGQ/syyIRWXFxspGehsNbp8Z97e+gWwhbnADwHggH/JoAq6vcWcRyHY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=VdQ/ccBE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="VdQ/ccBE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EAB0C4CEDD; Wed, 26 Feb 2025 01:59:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1740535191; bh=YmVTk1kP4NcQiKUVNWhAzAdqCxcUpchGc4X67KeEbqw=; h=From:To:Cc:Subject:Date:Reply-to:From; b=VdQ/ccBEIwrNpRNDCpGU/yh52s8F9KXDr0paC2e16EgVig+GmMZ4KMX28T22HYeui 1hnnauOKRQH/dtTyRCoOaQuX74nuGdliVOWC+9X2Q4iNkymhJgJ9/iaYaF8a3Dw3Xn YHmtZTYUbeP0rPJa8MUVCTtlD/N8zjwZuZsbS6Kk= From: Greg Kroah-Hartman To: linux-cve-announce@vger.kernel.org Cc: Greg Kroah-Hartman Subject: CVE-2022-49236: bpf: Fix UAF due to race between btf_try_get_module and load_module Date: Wed, 26 Feb 2025 02:57:21 +0100 Message-ID: <2025022624-CVE-2022-49236-e3f7@gregkh> X-Mailer: git-send-email 2.48.1 Precedence: bulk X-Mailing-List: linux-cve-announce@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Reply-to: , X-Developer-Signature: v=1; a=openpgp-sha256; l=5812; i=gregkh@linuxfoundation.org; h=from:subject:message-id; bh=YmVTk1kP4NcQiKUVNWhAzAdqCxcUpchGc4X67KeEbqw=; b=owGbwMvMwCRo6H6F97bub03G02pJDOn7SgKyHwq9vrA4UC45+fmRvmKVPHkTCda3rCv2aks/F Z10JvFkRywLgyATg6yYIsuXbTxH91ccUvQytD0NM4eVCWQIAxenAExk+WmGedqLE9TOb1uyMbDq 3LKVRutYgt8EuzPMD7WanPd/9fpTCXcr4wXPv3WpX3JMAQA= X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 Content-Transfer-Encoding: 8bit Description =========== In the Linux kernel, the following vulnerability has been resolved: bpf: Fix UAF due to race between btf_try_get_module and load_module While working on code to populate kfunc BTF ID sets for module BTF from its initcall, I noticed that by the time the initcall is invoked, the module BTF can already be seen by userspace (and the BPF verifier). The existing btf_try_get_module calls try_module_get which only fails if mod->state == MODULE_STATE_GOING, i.e. it can increment module reference when module initcall is happening in parallel. Currently, BTF parsing happens from MODULE_STATE_COMING notifier callback. At this point, the module initcalls have not been invoked. The notifier callback parses and prepares the module BTF, allocates an ID, which publishes it to userspace, and then adds it to the btf_modules list allowing the kernel to invoke btf_try_get_module for the BTF. However, at this point, the module has not been fully initialized (i.e. its initcalls have not finished). The code in module.c can still fail and free the module, without caring for other users. However, nothing stops btf_try_get_module from succeeding between the state transition from MODULE_STATE_COMING to MODULE_STATE_LIVE. This leads to a use-after-free issue when BPF program loads successfully in the state transition, load_module's do_init_module call fails and frees the module, and BPF program fd on close calls module_put for the freed module. Future patch has test case to verify we don't regress in this area in future. There are multiple points after prepare_coming_module (in load_module) where failure can occur and module loading can return error. We illustrate and test for the race using the last point where it can practically occur (in module __init function). An illustration of the race: CPU 0 CPU 1 load_module notifier_call(MODULE_STATE_COMING) btf_parse_module btf_alloc_id // Published to userspace list_add(&btf_mod->list, btf_modules) mod->init(...) ... ^ bpf_check | check_pseudo_btf_id | btf_try_get_module | returns true | ... ... | module __init in progress return prog_fd | ... ... V if (ret < 0) free_module(mod) ... close(prog_fd) ... bpf_prog_free_deferred module_put(used_btf.mod) // use-after-free We fix this issue by setting a flag BTF_MODULE_F_LIVE, from the notifier callback when MODULE_STATE_LIVE state is reached for the module, so that we return NULL from btf_try_get_module for modules that are not fully formed. Since try_module_get already checks that module is not in MODULE_STATE_GOING state, and that is the only transition a live module can make before being removed from btf_modules list, this is enough to close the race and prevent the bug. A later selftest patch crafts the race condition artifically to verify that it has been fixed, and that verifier fails to load program (with ENXIO). Lastly, a couple of comments: 1. Even if this race didn't exist, it seems more appropriate to only access resources (ksyms and kfuncs) of a fully formed module which has been initialized completely. 2. This patch was born out of need for synchronization against module initcall for the next patch, so it is needed for correctness even without the aforementioned race condition. The BTF resources initialized by module initcall are set up once and then only looked up, so just waiting until the initcall has finished ensures correct behavior. The Linux kernel CVE team has assigned CVE-2022-49236 to this issue. Affected and fixed versions =========================== Issue introduced in 5.12 with commit 541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c and fixed in 5.15.33 with commit 51b82141fffa454abf937a8ff0b8af89e4fd0c8f Issue introduced in 5.12 with commit 541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c and fixed in 5.16.19 with commit d7fccf264b1a785525b366a5b7f8113c756187ad Issue introduced in 5.12 with commit 541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c and fixed in 5.17.2 with commit 0481baa2318cb1ab13277715da6cdbb657807b3f Issue introduced in 5.12 with commit 541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c and fixed in 5.18 with commit 18688de203b47e5d8d9d0953385bf30b5949324f Please see https://www.kernel.org for a full list of currently supported kernel versions by the kernel community. Unaffected versions might change over time as fixes are backported to older supported kernel versions. The official CVE entry at https://cve.org/CVERecord/?id=CVE-2022-49236 will be updated if fixes are backported, please check that for the most up to date information about this issue. Affected files ============== The file(s) affected by this issue are: kernel/bpf/btf.c Mitigation ========== The Linux kernel CVE team recommends that you update to the latest stable kernel version for this, and many other bugfixes. Individual changes are never tested alone, but rather are part of a larger kernel release. Cherry-picking individual commits is not recommended or supported by the Linux kernel community at all. If however, updating to the latest release is impossible, the individual changes to resolve this issue can be found at these commits: https://git.kernel.org/stable/c/51b82141fffa454abf937a8ff0b8af89e4fd0c8f https://git.kernel.org/stable/c/d7fccf264b1a785525b366a5b7f8113c756187ad https://git.kernel.org/stable/c/0481baa2318cb1ab13277715da6cdbb657807b3f https://git.kernel.org/stable/c/18688de203b47e5d8d9d0953385bf30b5949324f