Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp623741img; Fri, 22 Mar 2019 05:22:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqy4B5rKj9UaDeD6oIlZs1i7o9exJtjywMN++LGArhqE8w9fRf+eWB6fbRdPHhSmaNOmKh29 X-Received: by 2002:a62:480d:: with SMTP id v13mr9023864pfa.125.1553257354703; Fri, 22 Mar 2019 05:22:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553257354; cv=none; d=google.com; s=arc-20160816; b=Q0WZpIr1kuznOJvVhkpoptfYE7D7k7VkuXRdd6HKNyEwE86l7lnHPLXjBusizauNnJ D50TexMzAjhcEJPBSIx0wlgkTBZdhUQg1/MUzEargafG6jttNctDPNzsi+cafSLGQu26 chhmtvoYc7vFTg03txPd5HDzaOS+vPLF65Sw6v/LpLGpxhccZXJItKBjzcIUANUIJC0c fVixCVvrdO5DEoBnksFS8q4BECujrXVDjQUaKEXmXAk77dzxk1aMDnn10HzpIL3OIxR0 W6HoYe3X28R5YvI3agN6ocrMRI2JTw56CFBQMKn8CtUP1ICL27XtZE0/RLZYOFpqZ0E4 bfdA== 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=HKjOF3LwwbZcZcRBREyW469KrulRBm75kjYMbJ6InGY=; b=uQJoYqUV8nw2zAbIGUXDpOnVSORt1Ii36kCbmgCzF3W0WnlU0cnMUHhpUKpDzNHnhm VHFgnvOG/lY/YjqaRamzsscmjqYcRkOToV4qtWfYMF9IxSiqkgoBIM4epUSOM+if+mPI jVkxqRyHfpgHptvfHDRD6gvsKwuFC9XyC5hgdEHtg3Cvg8sXUnxh6Q3WJWxKN8kyj9tm 2MqDo39YPJkKjzi7Cpa1pkG6cfO9l5gcshhUXwxM0uKhIEwQwrNMznhu8o0irY92H4vh yqEM9azBgR9pXCgq8r0epK9m8QwZa4ZNIdo9QBdZNdiQMqsYCnwEkE6sP6j+vKVPOLR+ s+bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cAdRwKPj; 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 t9si6826189pgc.164.2019.03.22.05.22.19; Fri, 22 Mar 2019 05:22: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=cAdRwKPj; 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 S2390403AbfCVMVk (ORCPT + 99 others); Fri, 22 Mar 2019 08:21:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:60674 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390890AbfCVMVh (ORCPT ); Fri, 22 Mar 2019 08:21:37 -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 E856B2054F; Fri, 22 Mar 2019 12:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553257296; bh=cbfvLIGZ+jVcCOxilYEZdGUuZsDK4epX1dvDpz0ZjV0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cAdRwKPjRa8aLm1uZY6+sBmyYXJ0i4nHaSQ6Hn3Wq8lMCFMl+zfbX/E6VBpf5y1xi NdhEnGriJe1nAR6r2qozmRpOa1UgtlDllqlUm2uUqDlidb0EKvQuiPjXly3twkU2SG kRd8rsZG2qoUjTdiIKYu6zenLDO0WIVt7IiJjpjI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, NeilBrown , "J. Bruce Fields" Subject: [PATCH 5.0 188/238] nfsd: fix memory corruption caused by readdir Date: Fri, 22 Mar 2019 12:16:47 +0100 Message-Id: <20190322111309.327041880@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111258.383569278@linuxfoundation.org> References: <20190322111258.383569278@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 5.0-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 @@ -463,8 +463,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); } @@ -533,6 +544,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 @@ -921,6 +921,7 @@ encode_entry(struct readdir_cd *ccd, con } else { xdr_encode_hyper(cd->offset, offset64); } + cd->offset = NULL; } /*