Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp580533img; Fri, 22 Mar 2019 04:28:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqyiTBmNiLau16Zh6KT8wPfv01Rk8YfplgIEJWcZfznwC79uPLTrAnOxqx0zj55u3uFph8si X-Received: by 2002:a17:902:758f:: with SMTP id j15mr8998763pll.211.1553254126780; Fri, 22 Mar 2019 04:28:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553254126; cv=none; d=google.com; s=arc-20160816; b=XR3/UlLzGLX21jdNsc3gc9z23PNIxE4rnWZXCaFLw/LWvkhdHvHzUxWMBrQx8rTTE4 2Cv2xZ1rfhl+4tpQgJQjVtfPrW18eo+Xs7Ad/0+P8Fsc6wKADweYvQxrSJc6MQgEo9FQ Frl995FqjgxpzqbdrKdK7EfOYPWWqUt77JKcPitHMQ7OjITGKM9FUH/VD8JKAh67NKpo riB1au8rMemAMHEn6SdMfbl1uzGMvd/6yaGU4jXSoGcozslm/QPhTIhLH1LRmV4gL8Bh FocN7ZjgGyh1hHs/HmwnxP7rdEBm1UyW4YPoh/p7HaCMYxYZMjeFjysK9YvXxUC6HOBj VOvA== 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=xZxljoylPu5Ffh6gK7tXTqsupU686r7Zl4ZTrrxE8oo=; b=H5bZmzfeThDIGKUCGYjCjubOSbm/58we0kF7DPTtiKFRWewAISlVHn22h5sDRYlYDS RqGnPTlOadPLS3tOOKBGvzJcfDsq0QCA5b15VolrR8/itiyD4itLgPXtxMxLCqFJ640G snjMa78UXrfs2JP3KAG1IlOlAJ1mYQFQ6yYYuDhTSNVfVONzOKIzP206po4uE/ArJ1kw h8HL1pJKwJAaNOwrE27IYIISarvihGRXQZW3SQ4WyCPnlRKCw8ofln6zEF4/J+R1ycHc 50X+7NTuOVVf6MCf8LiOCz54amK68lgjmfCJI1Qlm/hAJs8DrFSzqFsD/tso+PdBgCQl Nl+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0qnJLnhv; 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 n6si6701212pgj.96.2019.03.22.04.28.31; Fri, 22 Mar 2019 04:28:46 -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=0qnJLnhv; 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 S1729510AbfCVL1F (ORCPT + 99 others); Fri, 22 Mar 2019 07:27:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:54784 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729497AbfCVL1D (ORCPT ); Fri, 22 Mar 2019 07:27:03 -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 A7B5C20657; Fri, 22 Mar 2019 11:27:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553254022; bh=LjjXYsu0PWOUfAgeTCYH9hX5gxdmY10x7Dp7firFYiE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0qnJLnhvM3/Kih41yZKHyLdrkfgQI5pID9Z67W7vcjR1zXr8uUzeKAbkJuHuQaSqA e196sTZr2xousC1rqgMM5OuBxfa2Mabnzoldlk3xEEc6egoJTEIxVf7YIPAXRClP3x QNBFB4tgxRdwS/BJyzzy3DNr822oJKxwK71pzvbQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, NeilBrown , "J. Bruce Fields" Subject: [PATCH 3.18 129/134] nfsd: fix memory corruption caused by readdir Date: Fri, 22 Mar 2019 12:15:42 +0100 Message-Id: <20190322111219.860961453@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111210.465931067@linuxfoundation.org> References: <20190322111210.465931067@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 3.18-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 @@ -892,6 +892,7 @@ encode_entry(struct readdir_cd *ccd, con } else { xdr_encode_hyper(cd->offset, offset64); } + cd->offset = NULL; } /*