Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3693320ybv; Mon, 10 Feb 2020 04:50:02 -0800 (PST) X-Google-Smtp-Source: APXvYqzPS97S9EkPlyKHbfJNG9Eby+nmegtacSHiCZVR/8T+jy/RS8zyEbUatpDS67eNzqPWqGHn X-Received: by 2002:a05:6830:200d:: with SMTP id e13mr946984otp.364.1581339002380; Mon, 10 Feb 2020 04:50:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339002; cv=none; d=google.com; s=arc-20160816; b=kkOwab5h9c+NewP6ynqNNBnn3ChZnI820FOsqSm/d69uTOVyGEFkPfrtYjxlkNiyJo 6w8MdbGPfnwuNHgdkYoStQ5BhHuy3EzJX8Y4ccKLv1K9tuWsjjrBLwOusbkbhvFsBCGr GMVvqKPLvMUDWRZZyYJ/raJXUlWVRonpVRmrRhQfhIFKy9NAcrigRUGRHJykI3B1nCVh OF3uNngFef9wzCyrzUS1bw0nzKzTRHgnoxGKHL2XH/T0w0lUDeaNSECsu/r5w9KGTemr GCIIek036PBqnr2gXg9XoGfJ9jf5/gmu3tCbhKMXRbXyz2ag7wop9fPSamxeEHCXBIQN FsBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=j55KBYd68unJBO2bkBFN9jvfNyl4+pSfyMa7Kwq9GAI=; b=oFbfW3Ofr6cPOJGcYJPGR92pQTLtJoPO5wUdapDrMJUMRF3TLGSZAtOEHA7QocNKFP JO4yJcfZTJyAolr6zG4sS0QOeXuj7I5GbfJxoe+7ysCh/yE+qm7kh7UxTWEj4n6PSnN7 +vfu5MkF7SOM+XGd07u0HEmxZXArQHLz1K19LaAIewCcMk+zoyOGrVXIBPTuj05pUEqh pOueg2OO/Pg/T7fShE1Nyeo8Ysc1GlVKbw3y8ZuRFgl+ymZ92m5CQ8dvVvWY+GfQ0g+F 9gT93RNYAMfVe8swhvQRYCCuvIajn43PPoOfTiLRwtF9M0hkTXf44mHhszfKzm7RgPgk L/Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=K9HD6282; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i2si158689otc.130.2020.02.10.04.49.50; Mon, 10 Feb 2020 04:50:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=K9HD6282; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730733AbgBJMoz (ORCPT + 99 others); Mon, 10 Feb 2020 07:44:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:41596 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729797AbgBJMkr (ORCPT ); Mon, 10 Feb 2020 07:40:47 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2B2C620873; Mon, 10 Feb 2020 12:40:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338447; bh=1iLSJo26RCT4stMBqRTZAFylN/S4Au55QZXe5a9TujI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K9HD628270EkpxLCEojoBn9Ev1ibPBqu2PMJqHqjR/CoOUnIsqGHbTbTMSOn1l2dG Y3gJL2lVpIBwLhrmOX5Zhjj5HFC5SHFoBEdf0OIKutuqvonBx+BQvPQcf67kTuHJOE uYq+CoVDLXAo4d+OHQMmtSGCag4LMsdRPkA+kbK4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Trond Myklebust , Benjamin Coddington , Anna Schumaker Subject: [PATCH 5.5 189/367] NFS: Fix memory leaks and corruption in readdir Date: Mon, 10 Feb 2020 04:31:42 -0800 Message-Id: <20200210122442.076982365@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Trond Myklebust commit 4b310319c6a8ce708f1033d57145e2aa027a883c upstream. nfs_readdir_xdr_to_array() must not exit without having initialised the array, so that the page cache deletion routines can safely call nfs_readdir_clear_array(). Furthermore, we should ensure that if we exit nfs_readdir_filler() with an error, we free up any page contents to prevent a leak if we try to fill the page again. Fixes: 11de3b11e08c ("NFS: Fix a memory leak in nfs_readdir") Cc: stable@vger.kernel.org # v2.6.37+ Signed-off-by: Trond Myklebust Reviewed-by: Benjamin Coddington Signed-off-by: Anna Schumaker Signed-off-by: Greg Kroah-Hartman --- fs/nfs/dir.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -162,6 +162,17 @@ typedef struct { bool eof; } nfs_readdir_descriptor_t; +static +void nfs_readdir_init_array(struct page *page) +{ + struct nfs_cache_array *array; + + array = kmap_atomic(page); + memset(array, 0, sizeof(struct nfs_cache_array)); + array->eof_index = -1; + kunmap_atomic(array); +} + /* * we are freeing strings created by nfs_add_to_readdir_array() */ @@ -174,6 +185,7 @@ void nfs_readdir_clear_array(struct page array = kmap_atomic(page); for (i = 0; i < array->size; i++) kfree(array->array[i].string.name); + array->size = 0; kunmap_atomic(array); } @@ -610,6 +622,8 @@ int nfs_readdir_xdr_to_array(nfs_readdir int status = -ENOMEM; unsigned int array_size = ARRAY_SIZE(pages); + nfs_readdir_init_array(page); + entry.prev_cookie = 0; entry.cookie = desc->last_cookie; entry.eof = 0; @@ -626,8 +640,6 @@ int nfs_readdir_xdr_to_array(nfs_readdir } array = kmap(page); - memset(array, 0, sizeof(struct nfs_cache_array)); - array->eof_index = -1; status = nfs_readdir_alloc_pages(pages, array_size); if (status < 0) @@ -682,6 +694,7 @@ int nfs_readdir_filler(void *data, struc unlock_page(page); return 0; error: + nfs_readdir_clear_array(page); unlock_page(page); return ret; }