Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795054460; b=OVG+FVN+rpJWrwSQTfKRAsS9ob9NfWaVyK0G7qOZqBLoy3WL5OxeEEDhJN/yw9X1iV9A Dmfp2zsMxbOxLwzPKzHNNiIkx/nN73sORmPUmpQODSMXNQHyYaJjLOhEbINfPC2boDOQV w2TrXoi1Ni0birS/3uIwUIJuJ7QhP5lZOuvUZANlQw28vC7QEUNMR/OQKdFL0V/2RJ1gu hueZU85lwXHgzlaGCVOhRLl/jPCTKPeeD7drDAdQni7sYjsuKojp3mZSJ4HGjiHuAzcMB kOdUsXXWgP2cLERC5T4QVPpxjYUAnryswrYPQqQ8YPyI5IGCNW5SgrQ4ixRmKLcWDCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795054460; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=4C4CsiAcB0vDRDS5apwiVDDKZAi+gnJCQEjlCZ+fO3E=; b=CfqRX5wpfxA529Yk/9wZdGe2Zr0Pf4ZxaZKUdRpnT3oA9WUL0yvyevHE9hVQERefkJ4k VtVHO94n6UiNKwh2i38GVZdB6Ai4ALbbcAd1ao8gLFg7N9WIb3Oqc9pCCtMIzvqZeAFL9 YVUtjiLqFFK8QEeR48y14hY+AQJt+RmYHZ8NbP14elVZUOqdSGfajVgDHkMcJ/BtBBBZ+ qkK7uPQeASb0uQpH3zuA15d2jR8LtZHmKChqW/3+7PZG1Rzk0Lh+atZOVnNbgPGxD2TNP aUwy+Cq+O1pD6OXa7hnFqTbSnPTHvchiAWuxJ5WH7IPBE0LhindT4OieNJmJwz8YpUw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=aC5bkw9h; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7F9D606A325; Thu, 19 Nov 2026 02:14:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=aC5bkw9h Received: by smtp.kernel.org (Postfix) with ESMTPSA id 123EC1F000C4; Thu, 19 Nov 2026 02:14:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795054460; bh=4C4CsiAcB0vDRDS5apwiVDDKZAi+gnJCQEjlCZ+fO3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=aC5bkw9hrsbZG5/zUxMwvYugR3EfLAoKFn8foZLxHUGLn9qJhHZwlI49MiVCK9SME h9nlI5+JD+Oi3HoY5OkrjssHATB2GG+iUZiJpVDa6F8eFjzmYvco0E9mJPvYuKIelh hl2g5eKyt7VIHGPif12HZKGxQ0MHGds+uuCmfbOc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Francis Perron , AutonomousCodeSecurity@microsoft.com, "Cen Zhang (Microsoft)" , John Johansen , Sasha Levin Subject: [PATCH 5.15 1281/2214] apparmor: fix query_data() output overlapping the lookup key Date: Wed, 18 Nov 2026 15:49:17 +0100 Message-ID: <20261118150102.833163018@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150009.797899913@linuxfoundation.org> References: <20261118150009.797899913@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cen Zhang (Microsoft) [ Upstream commit d30844001360a403a511cf24fc75ecaa6a7a7544 ] In query_data(), buf and query point into the same multi_transaction buffer: buf at the start, query a few bytes in, and the lookup key right after the label string that query points to. Reusing the buffer is by design and is safe as long as the query is fully parsed before buf is written to, but the key is not: it is dereferenced on every iteration of label_for_each_confined(), after output has already been written. So when the label has more than one profile with a data table, the data of the first matching profile is written to buf while the key is still needed, and overwrites it if the data is large enough. The later lookups then run on a corrupted key. Besides, if the data has no NUL byte from the key's position to the end of the allocation, strhash() -> strlen() -> jhash() reads past the 4096-byte slab object, triggering an out-of-bound read: BUG: KASAN: slab-out-of-bounds in jhash+0x595/0x5e0 Read of size 1 at addr ff1100001159d000 by task exploit/155 CPU: 0 UID: 1000 PID: 155 Comm: exploit Not tainted 7.3.0-rc7 #1 Call Trace: jhash+0x595/0x5e0 aa_write_access+0x880/0x1400 vfs_write+0x26f/0xd20 ksys_write+0x115/0x210 Writing to the .access file is unprivileged (0666), but triggering this requires loaded profiles that carry a data table, which needs CAP_MAC_ADMIN and is not the case on default distribution policies. Fix by copying the key with kstrdup() before writing any output to buf, so the key stays valid across all loop iterations regardless of what gets written to the overlapping buffer. Fixes: 317d9a054e1c ("apparmor: update query interface to support label queries") Cc: stable@vger.kernel.org # v4.13+ Cc: Francis Perron Reported-by: AutonomousCodeSecurity@microsoft.com Signed-off-by: Cen Zhang (Microsoft) Signed-off-by: John Johansen [ sashal: adjusted context for label critical-section helpers without needput arguments ] Signed-off-by: Sasha Levin --- security/apparmor/apparmorfs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/security/apparmor/apparmorfs.c b/security/apparmor/apparmorfs.c index 65b16c6f61b7d..74b85d540d41e 100644 --- a/security/apparmor/apparmorfs.c +++ b/security/apparmor/apparmorfs.c @@ -739,7 +739,7 @@ static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, static ssize_t query_data(char *buf, size_t buf_len, char *query, size_t query_len) { - char *out; + char *out, *keydup; const char *key; struct label_it i; struct aa_label *label, *curr; @@ -760,11 +760,18 @@ static ssize_t query_data(char *buf, size_t buf_len, if (buf_len < sizeof(bytes) + sizeof(blocks)) return -EINVAL; /* not enough space */ + /* buf overlaps query, keep a copy of the key for the later lookups */ + keydup = kstrdup(key, GFP_KERNEL); + if (!keydup) + return -ENOMEM; + curr = begin_current_label_crit_section(); label = aa_label_parse(curr, query, GFP_KERNEL, false, false); end_current_label_crit_section(curr); - if (IS_ERR(label)) + if (IS_ERR(label)) { + kfree(keydup); return PTR_ERR(label); + } /* We are going to leave space for two numbers. The first is the total * number of bytes we are writing after the first number. This is so @@ -782,13 +789,14 @@ static ssize_t query_data(char *buf, size_t buf_len, if (!profile->data) continue; - data = rhashtable_lookup_fast(profile->data, &key, + data = rhashtable_lookup_fast(profile->data, &keydup, profile->data->p); if (data) { if (out + sizeof(outle32) + data->size > buf + buf_len) { aa_put_label(label); + kfree(keydup); return -EINVAL; /* not enough space */ } outle32 = __cpu_to_le32(data->size); @@ -800,6 +808,7 @@ static ssize_t query_data(char *buf, size_t buf_len, } } aa_put_label(label); + kfree(keydup); outle32 = __cpu_to_le32(out - buf - sizeof(bytes)); memcpy(buf, &outle32, sizeof(outle32)); -- 2.53.0