Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3130626ybl; Sun, 2 Feb 2020 15:26:58 -0800 (PST) X-Google-Smtp-Source: APXvYqw32cdO3CbIj3LRq4kEP0BK/JQgyj2paIYnhvVkUkGnnk9L53DJ64HzPApNPygWWFik6FSh X-Received: by 2002:a9d:7696:: with SMTP id j22mr16485039otl.188.1580686018819; Sun, 02 Feb 2020 15:26:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580686018; cv=none; d=google.com; s=arc-20160816; b=cCAkSmu6ik0omhJqnkvxlItuHvtSDWVclJgTQ3abH4GOuGxg0As/pFE4FNthpw8db5 Nb3zq4xo7K2+h0DzhvicB2hlOLMrJVawOTB3Q6vKyROS1HM8DOgcJRxI1RO59Lz0+Kdh zNjVNYVHWRmXK10d4S1J/JU0oz+yFS1nIM5ob1XfEpqXKxnTniMspp5dI+R+pl3zISuu Fb/y+LUjyngT0ouamN/TCGoMqC+MV3iA0Bfnx9DgWn+QvOY7PfBZWsBwBbQdErsj9+pC g9rSvPEke34Nn5ClCa0t3q3FB67NHvQWC7A5QRgzo6BEadMmPGcLAR0fVVCuhDLhrrIS 7YYw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aKCdiCvf1CCHY7Wz5wj2AxA/hIzhbB4Gaz8M9nQKtTs=; b=Vezg0JkRUyuJSjJxMsBni90Mim3GPh1XY2laP1Wa2YdUkKukbPnNmkxx3SHQ9ZlZTG cOChruY7sSaZfRo0wBqRWk7ZF4ituTSD+Oqlx8pILPBVsVj2Cm4E1CCMBc9gDwJ6HIJ5 /8ERoRwhm83i/O8CNEa8s1x+CR+l2iIZGpUzlgrv4JGSrLfsmA24nH5Q0E9Om3NY1fnV YPJjYuJREwGEbuIFgMyYgzZ7hIX6xCWoWxn5cnFnGI5Naj4QV72tDNr3Aarjx3iZ9L4i xd7p/Q9bfk+j7N6SnZPLiSnwkH3tZUCJDsgupF8wQsm8qzgJ1/kjUNKz8Bsqrg+PQWEU sEhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LzkfmkYU; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w13si8221666oti.291.2020.02.02.15.26.36; Sun, 02 Feb 2020 15:26:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-nfs-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=@gmail.com header.s=20161025 header.b=LzkfmkYU; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726150AbgBBW4K (ORCPT + 99 others); Sun, 2 Feb 2020 17:56:10 -0500 Received: from mail-yw1-f68.google.com ([209.85.161.68]:37122 "EHLO mail-yw1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726443AbgBBW4K (ORCPT ); Sun, 2 Feb 2020 17:56:10 -0500 Received: by mail-yw1-f68.google.com with SMTP id l5so11952325ywd.4 for ; Sun, 02 Feb 2020 14:56:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aKCdiCvf1CCHY7Wz5wj2AxA/hIzhbB4Gaz8M9nQKtTs=; b=LzkfmkYUhNa7kpQ+d9SqUE7PkBWbV5siaQELkviYXiq5/UufpvC3mEwVvDoacqU9En IYdmsmeTEiIn4gci/E3kQ9t8RQVBZa8e/2Dhyi1+eaBFlBDX9wcC1O1gDGjFosqPosC0 vlpc7Bf8r8iC0KXHO2npx/+omZrMli/IB4Bpz5UD9crO6bHA6WYf2RsLTxse4xQ6Vvp4 nBqiTpi1Wk59k59DG0AMsXm3D9NtsuaTyIamX056Fpzv/oJ2MbPhmGA09BnnUIni/Ym5 JUGqHq/BKxugMjwQz8W2P3SRaL2svxkDCUuspvcihD/VCmO5RS5KYnhD78CAvYXqybdF Altg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aKCdiCvf1CCHY7Wz5wj2AxA/hIzhbB4Gaz8M9nQKtTs=; b=q3HA6hzqDBzPwoBUU6C07KPM+4zupWyFqN8wzfB5Xh2JwmnwcxaCJyvb9xDqMfYwDF 3X4JZXzAK1r/w1bfgWDChBANZDM6hh657CL/8DP5rZbM9MGCwQ0UnsN5PSdts7XrLkXN 7xHtS5fSbcAlfUKcLmS+93DZupZfob9IwMjvGb+j24IjzpoHLYXgaqxtY1vc+iWUHubX I9AwfAJb0ijbRIBbbPqb7nki4703u2k0eFM5FWoJLkLO9YmqNWpYAhKVYAnMicCWN614 B8zit6sGLagDStk1QLZlquXdgGyoR23Pb92s0mnWtUVs+n1ch10VPMLuyuAU113o4sHv uNIw== X-Gm-Message-State: APjAAAWVEyfGibtHJhlalvid0O+V0lv/CvJ8vIPwfQnKaaH/cvaRF51B NmZMuwER4DOzOlaNLPPsJw== X-Received: by 2002:a25:b989:: with SMTP id r9mr15966116ybg.366.1580684167678; Sun, 02 Feb 2020 14:56:07 -0800 (PST) Received: from localhost.localdomain (c-68-40-189-247.hsd1.mi.comcast.net. [68.40.189.247]) by smtp.gmail.com with ESMTPSA id u136sm7185529ywf.101.2020.02.02.14.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Feb 2020 14:56:07 -0800 (PST) From: Trond Myklebust X-Google-Original-From: Trond Myklebust To: Anna Schumaker Cc: Benjamin Coddington , Dai Ngo , linux-nfs@vger.kernel.org Subject: [PATCH 1/4] NFS: Fix memory leaks and corruption in readdir Date: Sun, 2 Feb 2020 17:53:53 -0500 Message-Id: <20200202225356.995080-2-trond.myklebust@hammerspace.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200202225356.995080-1-trond.myklebust@hammerspace.com> References: <20200202225356.995080-1-trond.myklebust@hammerspace.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org 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 --- fs/nfs/dir.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c index 76404f53cf21..ba0d55930e8a 100644 --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -163,6 +163,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() */ @@ -175,6 +186,7 @@ void nfs_readdir_clear_array(struct page *page) array = kmap_atomic(page); for (i = 0; i < array->size; i++) kfree(array->array[i].string.name); + array->size = 0; kunmap_atomic(array); } @@ -613,6 +625,8 @@ int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, 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; @@ -629,8 +643,6 @@ int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, } 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) @@ -685,6 +697,7 @@ int nfs_readdir_filler(void *data, struct page* page) unlock_page(page); return 0; error: + nfs_readdir_clear_array(page); unlock_page(page); return ret; } -- 2.24.1