Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp660021img; Fri, 22 Mar 2019 06:06:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwSqgx5WeBA541zGOhXJe4r18iV9pz3mestDpvWOIT9N0jfZ8WgPa5fOBUnjNTu+9nBXoZH X-Received: by 2002:a63:5b4b:: with SMTP id l11mr8812870pgm.82.1553259994369; Fri, 22 Mar 2019 06:06:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553259994; cv=none; d=google.com; s=arc-20160816; b=fEt0cL4xovQEtoQzFbQ37IgBiUWpg8sgYEK1sEzsWtOvqtRId0zzUW7vd3sX8/K/ga pSHjmSS9u5A/sBmwP4rPH5WljVM7sn/Qz4wwU/qxrGzEo5zLJ6N+iC/OweBAT3BYT0VK FHqwu2I0y+cHfpo4AiVr6MimmOLtI6A57SRtRrGcRBEIIWuRBmCPqDfxkDGl8eYuLQ8Y RMBUVphSOisjj123uAQqEukru7Y9b82Bk0O+0mPRfkCVQrl6xGPulgDg3HkHQDpcqNtp Otfg7t+Hs5f/3Yc77FNy7GdwDeFCpIEfRnTBJAC3Pm3NbidZOMf8CasQ9jAS9Fdsj5et 4iWg== 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=0IJoiWs2Zo5LM9DZdzROEO4bhrI5iFeArS3ZXyVk9lg=; b=o/DuvzAGaRV2JGkIWOJy6lvpMa5LXb2XHhkvynRp3KKPg+NRQlDL8jxVDj5E2x2MO4 nIh2dTIqzgHQ8kBDl3gFyjdpiV3nc/ipwII9lTmlEB4ahRPGvEtC7pJtks//Dui9Gic1 12XhkAMSI/FptmQQWSgJ8FJ+jRuQ83QJYLz+Tobzd1EZgbyDMBeqyP3F6IfEqie6csRS G7E/jrfTfvNOmdhy2cWibyjTGGhOY78bSFovBhYr+o6gMDMWB+oQpc0FUPOygKHBrgfC Xb889y+nYQMQbuPLEZMQzjfb/NQNFCLi5z9p9YoHJoKKXxUdxDwrTox0NMfRpjH2lzPE I1hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KNChdOFI; 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 j19si6683092pgk.546.2019.03.22.06.06.16; Fri, 22 Mar 2019 06:06:34 -0700 (PDT) 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=KNChdOFI; 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 S1731018AbfCVLip (ORCPT + 99 others); Fri, 22 Mar 2019 07:38:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:40278 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730452AbfCVLio (ORCPT ); Fri, 22 Mar 2019 07:38:44 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 20EA82082C; Fri, 22 Mar 2019 11:38:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553254722; bh=yhzijB6uv78EPi1NSOj5avCXEiMuEgGBnsZQuERjaLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KNChdOFIOL0IlCO7AgSbV4oTU8+22ezku8kH1czOIJQDcXAJH7tQ5IfbpuRG4f7mM 9LNO1T3bgSfuJ+X3O4EOEMXAlxElrSlRI3AkzE8H9Xcisif+ArP/FCEQb1UOycclit yx3E+Z3sIPlYTpv3xc5vT989yJVVZLP3A+9P4QkI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, NeilBrown , "J. Bruce Fields" Subject: [PATCH 4.4 222/230] nfsd: fix memory corruption caused by readdir Date: Fri, 22 Mar 2019 12:16:00 +0100 Message-Id: <20190322111252.494344395@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111236.796964179@linuxfoundation.org> References: <20190322111236.796964179@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: NeilBrown commit b602345da6cbb135ba68cf042df8ec9a73da7981 upstream. If the result of an NFSv3 readdir{,plus} request results in the "offset" on one entry having to be split across 2 pages, and is sized so that the next directory entry doesn't fit in the requested size, then memory corruption can happen. When encode_entry() is called after encoding the last entry that fits, it notices that ->offset and ->offset1 are set, and so stores the offset value in the two pages as required. It clears ->offset1 but *does not* clear ->offset. Normally this omission doesn't matter as encode_entry_baggage() will be called, and will set ->offset to a suitable value (not on a page boundary). But in the case where cd->buflen < elen and nfserr_toosmall is returned, ->offset is not reset. This means that nfsd3proc_readdirplus will see ->offset with a value 4 bytes before the end of a page, and ->offset1 set to NULL. It will try to write 8bytes to ->offset. If we are lucky, the next page will be read-only, and the system will BUG: unable to handle kernel paging request at... If we are unlucky, some innocent page will have the first 4 bytes corrupted. nfsd3proc_readdir() doesn't even check for ->offset1, it just blindly writes 8 bytes to the offset wherever it is. Fix this by clearing ->offset after it is used, and copying the ->offset handling code from nfsd3_proc_readdirplus into nfsd3_proc_readdir. (Note that the commit hash in the Fixes tag is from the 'history' tree - this bug predates git). Fixes: 0b1d57cf7654 ("[PATCH] kNFSd: Fix nfs3 dentry encoding") Fixes-URL: https://git.kernel.org/pub/scm/linux/kernel/git/history/history.git/commit/?id=0b1d57cf7654 Cc: stable@vger.kernel.org (v2.6.12+) Signed-off-by: NeilBrown Signed-off-by: J. Bruce Fields Signed-off-by: Greg Kroah-Hartman --- fs/nfsd/nfs3proc.c | 16 ++++++++++++++-- fs/nfsd/nfs3xdr.c | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) --- a/fs/nfsd/nfs3proc.c +++ b/fs/nfsd/nfs3proc.c @@ -430,8 +430,19 @@ nfsd3_proc_readdir(struct svc_rqst *rqst &resp->common, nfs3svc_encode_entry); memcpy(resp->verf, argp->verf, 8); resp->count = resp->buffer - argp->buffer; - if (resp->offset) - xdr_encode_hyper(resp->offset, argp->cookie); + if (resp->offset) { + loff_t offset = argp->cookie; + + if (unlikely(resp->offset1)) { + /* we ended up with offset on a page boundary */ + *resp->offset = htonl(offset >> 32); + *resp->offset1 = htonl(offset & 0xffffffff); + resp->offset1 = NULL; + } else { + xdr_encode_hyper(resp->offset, offset); + } + resp->offset = NULL; + } RETURN_STATUS(nfserr); } @@ -499,6 +510,7 @@ nfsd3_proc_readdirplus(struct svc_rqst * } else { xdr_encode_hyper(resp->offset, offset); } + resp->offset = NULL; } RETURN_STATUS(nfserr); --- a/fs/nfsd/nfs3xdr.c +++ b/fs/nfsd/nfs3xdr.c @@ -898,6 +898,7 @@ encode_entry(struct readdir_cd *ccd, con } else { xdr_encode_hyper(cd->offset, offset64); } + cd->offset = NULL; } /*