From patchwork Wed Nov 6 19:02:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 11230881 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0357815AB for ; Wed, 6 Nov 2019 19:02:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D547A217F9 for ; Wed, 6 Nov 2019 19:02:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SRRkmXEw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727414AbfKFTCs (ORCPT ); Wed, 6 Nov 2019 14:02:48 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:31195 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727411AbfKFTCr (ORCPT ); Wed, 6 Nov 2019 14:02:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573066966; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kxdmMHHUCiwD411i+Ffa8FviPIgnmJlk/z+ts63ZbWE=; b=SRRkmXEwJNvL5e7dhX6bb/gdDYlKNsZnJKlx9z7uiExWrY7Y3NnHiuJ1DpfF1ghq0dFx8d aip312DtXPHLrIaTUmxXw9lXDFXXU17g8xYkq7BapJpqFND1ZJQ/TjtoUrB+ZnPZVegG9E xPd3GWQQS7JILyhz6j4aoIyxQthTHUI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-220-7sL43AozOKCWRjovtywQqw-1; Wed, 06 Nov 2019 14:02:44 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E41131005500; Wed, 6 Nov 2019 19:02:43 +0000 (UTC) Received: from max.com (unknown [10.40.206.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id B7A5160872; Wed, 6 Nov 2019 19:02:42 +0000 (UTC) From: Andreas Gruenbacher To: Alexander Viro , linux-fsdevel@vger.kernel.org Cc: Christoph Hellwig , Andreas Gruenbacher Subject: [PATCH] fs: Fix overflow in block_page_mkwrite Date: Wed, 6 Nov 2019 20:02:39 +0100 Message-Id: <20191106190239.20860-1-agruenba@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-MC-Unique: 7sL43AozOKCWRjovtywQqw-1 X-Mimecast-Spam-Score: 0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On architectures where ssize_t is wider than pgoff_t, the expression ((page->index + 1) << PAGE_SHIFT) can overflow. Rewrite to use the page offset, which we already compute here anyway. Signed-off-by: Andreas Gruenbacher Reviewed-by: Christoph Hellwig --- fs/buffer.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index 86a38b979323..da3f33b70249 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -2459,21 +2459,21 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf, struct page *page = vmf->page; struct inode *inode = file_inode(vma->vm_file); unsigned long end; - loff_t size; + loff_t offset, size; int ret; lock_page(page); size = i_size_read(inode); - if ((page->mapping != inode->i_mapping) || - (page_offset(page) > size)) { + offset = page_offset(page); + if (page->mapping != inode->i_mapping || offset > size) { /* We overload EFAULT to mean page got truncated */ ret = -EFAULT; goto out_unlock; } /* page is wholly or partially inside EOF */ - if (((page->index + 1) << PAGE_SHIFT) > size) - end = size & ~PAGE_MASK; + if (offset > size - PAGE_SIZE) + end = offset_in_page(size); else end = PAGE_SIZE;