From nobody Thu Sep 11 10:19:20 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D870DC00528 for ; Thu, 3 Aug 2023 09:18:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234574AbjHCJSP (ORCPT ); Thu, 3 Aug 2023 05:18:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235014AbjHCJRY (ORCPT ); Thu, 3 Aug 2023 05:17:24 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D78C430D2; Thu, 3 Aug 2023 02:17:16 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4238A219CB; Thu, 3 Aug 2023 09:17:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1691054235; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZnMoWEEpAsUPtzHKTEyf3AXNncjwngOjcJYgFuxitFU=; b=Vli1TgXK3idTK2YIoKvKoLGunLGeyq34SmTTNZ3KnmH7lPjGBV7f+Z2S9a0PNoqt7EQsAM QOujKeAmIuZ0Mx1rqIBP9RCoIA0aRyDnugN7s6/kkPZ4L7GGla/OnsrPtrAT28rmrm3Sw9 986gTtv2dW4Q73IlgvNaYkZ7nKrgVb8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1691054235; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZnMoWEEpAsUPtzHKTEyf3AXNncjwngOjcJYgFuxitFU=; b=kHOzd5ikuBiXaDMrk3M+0w3w2GPatGUoRW5JSLvdteZp4OEtG/tiIBXFN29cHlC27J3mAX A4Tp3Oxbh9IkXZDg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C0D911333C; Thu, 3 Aug 2023 09:17:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id xYAdLJpwy2TEbQAAMHmgww (envelope-from ); Thu, 03 Aug 2023 09:17:14 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id 53a1b5e5; Thu, 3 Aug 2023 09:17:14 +0000 (UTC) From: =?UTF-8?q?Lu=C3=ADs=20Henriques?= To: Theodore Ts'o , Andreas Dilger , Daniel Rosenberg , Eric Biggers Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Lu=C3=ADs=20Henriques?= Subject: [PATCH v3] ext4: fix memory leaks in ext4_fname_{setup_filename,prepare_lookup} Date: Thu, 3 Aug 2023 10:17:13 +0100 Message-Id: <20230803091713.13239-1-lhenriques@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the filename casefolding fails, we'll be leaking memory from the fscrypt_name struct, namely from the 'crypto_buf.name' member. Make sure we free it in the error path on both ext4_fname_setup_filename() and ext4_fname_prepare_lookup() functions. Fixes: 1ae98e295fa2 ("ext4: optimize match for casefolded encrypted dirs") Signed-off-by: Lu=C3=ADs Henriques Reviewed-by: Eric Biggers --- Changes since v2: - Rephrased commit log to (hopefully!) make it more clear. - Use ext4_fname_free_filename() instead of fscrypt_free_filename(). Although not strictly needed, it cleans up the fname struct, removing a dangling pointer to freed memory. Changes since v1: - Include fix to ext4_fname_prepare_lookup() as well - Add 'Fixes:' tag fs/ext4/crypto.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c index e20ac0654b3f..453d4da5de52 100644 --- a/fs/ext4/crypto.c +++ b/fs/ext4/crypto.c @@ -33,6 +33,8 @@ int ext4_fname_setup_filename(struct inode *dir, const st= ruct qstr *iname, =20 #if IS_ENABLED(CONFIG_UNICODE) err =3D ext4_fname_setup_ci_filename(dir, iname, fname); + if (err) + ext4_fname_free_filename(fname); #endif return err; } @@ -51,6 +53,8 @@ int ext4_fname_prepare_lookup(struct inode *dir, struct d= entry *dentry, =20 #if IS_ENABLED(CONFIG_UNICODE) err =3D ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname); + if (err) + ext4_fname_free_filename(fname); #endif return err; }