From mboxrd@z Thu Jan 1 00:00:00 1970 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 39F5F15CAB for ; Tue, 8 Nov 2022 13:43:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2C72C433D6; Tue, 8 Nov 2022 13:43:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1667915021; bh=bw+BIZtdFfM8Mk8vKMHji2geImt997I56AlXRqLRbIY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wVfw+CIpIm7a/q2dtVT+mGfIqIL47jAGTvbmQLNvCIoLpwrxGi1FbzErFaIFZlCE/ mVKjDby84rpSV83l3CKA20Pyw+07IJtcEbCrvEfBQ06swaFVYyzFMTyHuDJqSZfn25 MjvmL+GCIOlbftQSRMvm9Wy8aZUppIJGmS9uVKzQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Gaosheng Cui , Serge Hallyn , Paul Moore Subject: [PATCH 4.14 27/40] capabilities: fix potential memleak on error path from vfs_getxattr_alloc() Date: Tue, 8 Nov 2022 14:39:12 +0100 Message-Id: <20221108133329.393252089@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221108133328.351887714@linuxfoundation.org> References: <20221108133328.351887714@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Gaosheng Cui commit 8cf0a1bc12870d148ae830a4ba88cfdf0e879cee upstream. In cap_inode_getsecurity(), we will use vfs_getxattr_alloc() to complete the memory allocation of tmpbuf, if we have completed the memory allocation of tmpbuf, but failed to call handler->get(...), there will be a memleak in below logic: |-- ret = (int)vfs_getxattr_alloc(mnt_userns, ...) | /* ^^^ alloc for tmpbuf */ |-- value = krealloc(*xattr_value, error + 1, flags) | /* ^^^ alloc memory */ |-- error = handler->get(handler, ...) | /* error! */ |-- *xattr_value = value | /* xattr_value is &tmpbuf (memory leak!) */ So we will try to free(tmpbuf) after vfs_getxattr_alloc() fails to fix it. Cc: stable@vger.kernel.org Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities") Signed-off-by: Gaosheng Cui Acked-by: Serge Hallyn [PM: subject line and backtrace tweaks] Signed-off-by: Paul Moore Signed-off-by: Greg Kroah-Hartman --- security/commoncap.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/security/commoncap.c +++ b/security/commoncap.c @@ -398,8 +398,10 @@ int cap_inode_getsecurity(struct inode * &tmpbuf, size, GFP_NOFS); dput(dentry); - if (ret < 0 || !tmpbuf) - return ret; + if (ret < 0 || !tmpbuf) { + size = ret; + goto out_free; + } fs_ns = inode->i_sb->s_user_ns; cap = (struct vfs_cap_data *) tmpbuf;