Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1003838yba; Thu, 4 Apr 2019 02:11:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqwvePeNWIQQXdReUYM2YanIKe9HjPByDho3CYeZtnyO0yHKqHKzuE7qU80A5xojIEedZEH6 X-Received: by 2002:a63:e556:: with SMTP id z22mr4593736pgj.290.1554369105153; Thu, 04 Apr 2019 02:11:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554369105; cv=none; d=google.com; s=arc-20160816; b=DC1H5Kyl8moeBGV/aBoTpyat5exul3zE4jyh16OK2vP8SMuH9uN4CqKnWCr8Zk4Mg+ h9GHJaiEc1HEMtSL5e2ztgyxasAP+12FX3qoyAsvTzotVNdDMwI7puDF58Mtknsgs5RY jNaYU3jjHzBQGmAMXRy0MM1QstoyUOqDni1z1lg2Ky0om4lN9jVEpfbI5HdDYNkITEl3 +wQYbfcZ/tNsIENGYTE8edrzMFX3HV5Dba+J0UUU+PDa3oCgJUQ6CTQLdBvckLy0MUtk fmWFig9GgP9VJPCf1HbeyRi8QtrQub1Z4qGqf5i1hUB5j+ho9rIHIMO0HGg87q7jS8cL MmzA== 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=cnhDVCxbbkrjgp5lOvhEnV8z9MhaCXKgJBbQ/be5AME=; b=hb02V7p4YJJpf+th+N4ktTwsfhz5uBsJOi3OIRJB8VTk9ny76PHFRNOGFsLh26Gnsy Yf7G4fKqRzA/X7L62P9fFsabxapxxoge3fGy5faTdDdZdlcJE6lhuzdH8xYe23/I/+nC dm4gotfjQPPJ3FRMOf/CNvcz9lTPBNKGUq9oe9H3ERjHfdS1nV/YQzhLVahXTOczXs6o 3IB5wIEY4oE0kGCqQOysrHlQ/uXF9QGUVXnRbXEjwQ0HNMODzDbqeMIF7iu5Duxs+AwM tFL2qH/iUV6c0+Xd9vD61U/KY9Ky6ow/g2yIVo0qATEd2UmIGPIF45sAhE1y5oSAwcDJ F0GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ba74eqmw; 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 e2si15308209plt.197.2019.04.04.02.11.29; Thu, 04 Apr 2019 02:11:45 -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=Ba74eqmw; 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 S1732891AbfDDJJr (ORCPT + 99 others); Thu, 4 Apr 2019 05:09:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:49072 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732877AbfDDJJq (ORCPT ); Thu, 4 Apr 2019 05:09:46 -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 37FE020652; Thu, 4 Apr 2019 09:09:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554368985; bh=F3/fTFECdWSUpLHBhH99MC/R4nGnko2CDEIVbz5aLPU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ba74eqmwWQzLN1d9PR0nb0db2q+QGgsooQkpr8ttCZoa1QOh8sHm1NHtPymKLD4az AtPrhDGjonexHQiYdISKvUKEkDGCojkFyHGipXTvcaPVyNvvY3M7dA6Yq0g1ULnFDd uqid8BrYZgOUwnt5DrXLBFf8wTMdifiziV6pLJ5I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ming Lei , Carlos Maiolino , Jens Axboe , Sasha Levin Subject: [PATCH 5.0 055/246] fs: fix guard_bio_eod to check for real EOD errors Date: Thu, 4 Apr 2019 10:45:55 +0200 Message-Id: <20190404084620.942520655@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084619.236418459@linuxfoundation.org> References: <20190404084619.236418459@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. ------------------ [ Upstream commit dce30ca9e3b676fb288c33c1f4725a0621361185 ] guard_bio_eod() can truncate a segment in bio to allow it to do IO on odd last sectors of a device. It already checks if the IO starts past EOD, but it does not consider the possibility of an IO request starting within device boundaries can contain more than one segment past EOD. In such cases, truncated_bytes can be bigger than PAGE_SIZE, and will underflow bvec->bv_len. Fix this by checking if truncated_bytes is lower than PAGE_SIZE. This situation has been found on filesystems such as isofs and vfat, which doesn't check the device size before mount, if the device is smaller than the filesystem itself, a readahead on such filesystem, which spans EOD, can trigger this situation, leading a call to zero_user() with a wrong size possibly corrupting memory. I didn't see any crash, or didn't let the system run long enough to check if memory corruption will be hit somewhere, but adding instrumentation to guard_bio_end() to check truncated_bytes size, was enough to see the error. The following script can trigger the error. MNT=/mnt IMG=./DISK.img DEV=/dev/loop0 mkfs.vfat $IMG mount $IMG $MNT cp -R /etc $MNT &> /dev/null umount $MNT losetup -D losetup --find --show --sizelimit 16247280 $IMG mount $DEV $MNT find $MNT -type f -exec cat {} + >/dev/null Kudos to Eric Sandeen for coming up with the reproducer above Reviewed-by: Ming Lei Signed-off-by: Carlos Maiolino Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- fs/buffer.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/buffer.c b/fs/buffer.c index 48318fb74938..cab7a026876b 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -3027,6 +3027,13 @@ void guard_bio_eod(int op, struct bio *bio) /* Uhhuh. We've got a bio that straddles the device size! */ truncated_bytes = bio->bi_iter.bi_size - (maxsector << 9); + /* + * The bio contains more than one segment which spans EOD, just return + * and let IO layer turn it into an EIO + */ + if (truncated_bytes > bvec->bv_len) + return; + /* Truncate the bio.. */ bio->bi_iter.bi_size -= truncated_bytes; bvec->bv_len -= truncated_bytes; -- 2.19.1