Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2618000ybz; Mon, 27 Apr 2020 00:47:22 -0700 (PDT) X-Google-Smtp-Source: APiQypJBRn6CoDP1mz1tT8nSwNkwoR/GUotqCfIr6+kHA3+Ol+7kDwZE17/98gI1Dy4RP3prCf20 X-Received: by 2002:a05:6402:2076:: with SMTP id bd22mr17680536edb.334.1587973642310; Mon, 27 Apr 2020 00:47:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587973642; cv=none; d=google.com; s=arc-20160816; b=UfWaduQUy+Ek9JJAQrC5y2z2a3fzE57DPxcfd7JvXCNCHsLlD1b4CUEz1z3lOGEs3V ky/9seZ8bWEDfQsst68GdPdzurNj0pMf4JGWaa2Ivvjbr/rFAt5IwsE/doSgID4GZNKD WQpBbDU3Ep308IHTNNDoJp22A3+/OIHq4rw+xp0smcgwXj1v+t/xYqQLiQPz/0r9Kuz6 86F/DLzQsuIOkwCEp1NKnUS8JAV2+jyIKIQNxZruna0tBI10WxxjKEmMM+2SDucMMKI6 CRT2h2A/zpX/Z45Wti+YvfF+LrciJweoaH0eTpe+EKPjMlXtIAWEJhPPqNrNP3TENHf9 wayw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qpydbUORH6esAS5gi+cl+9hT95SHJdDoYerkBD0BMSQ=; b=b74MCHNywS3AawchESNOz0PKO1S39Fk0UjPbePjnxlbF4UkTPklA6IuMVnUwpwdYhN 4Tp+mJcSPpc5yk1R+VxngG3oXrUcbD6qCcCz/aH9v5iQrS5udGZ1LRyPsgAvBdhps4as orpHutzAa5zr11Mh+fQhluh4HVtYuVHjauu+GR9aEBJc8jQE+1WGAnpUBiyMfMgrFvRx Zm+qH51XkBXOIAyiP6oJDgPw7X5Ow+j+TnBrpzOTdp3URKfDH9n9fih/P0eGDL4++mt7 QG5WchBJe2+vyDXzoZ2xlbBl69mVLiPM43rKKso4fxXs3v/JuMI8rUQTzDVtBzaGvT+Y /mxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=oGC3hWAK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si7180222edy.66.2020.04.27.00.46.59; Mon, 27 Apr 2020 00:47:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=oGC3hWAK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726697AbgD0Hmi (ORCPT + 99 others); Mon, 27 Apr 2020 03:42:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726658AbgD0Hmg (ORCPT ); Mon, 27 Apr 2020 03:42:36 -0400 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA7DFC061A10 for ; Mon, 27 Apr 2020 00:42:35 -0700 (PDT) Received: by mail-wr1-x443.google.com with SMTP id g13so19261742wrb.8 for ; Mon, 27 Apr 2020 00:42:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qpydbUORH6esAS5gi+cl+9hT95SHJdDoYerkBD0BMSQ=; b=oGC3hWAK5PSMXwewUJVSLEn3ogH9c7x5GKyIHrpt9Un7/DBlDORpuf62nVnG49Srxq CBPr6rjegtfdy4HmaesOJcdbArkYcmrSclkKdby2Szkv2vZysdmhW/l8xsDWAw2iyPnq jfboZFx7R4wbNNSHjoAWW5eMpNn76nkOspz55Y5+OFrUkDYZ8p0wXuqCEi45cqbxXUBG TeFDLDb0OfgPptEIe47XWWQm/bN+bVLTlgFBc4gekSc6hGd2QXs2dBk0V1KbvDBHqQW+ 3g1EWvZ+w2B1/oRR4QmdxkGkGcsOVE2LCqMRuxP+CV8IuHcnno6CdC3EKkUvtBdSkHpL gEZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qpydbUORH6esAS5gi+cl+9hT95SHJdDoYerkBD0BMSQ=; b=AMy/xZAZLbM9qg6oJOmPhndWqplfG9NDigiBcQKciWiPt/gwjAOrCZZGxFchR7Coz8 xOQ8UZiZF8f07nnnalM3KACQ07R13e7jz7gUhXUaIOiPdcZ5fienc+e8OQ5T/EixoIuE CKSEUhebCo8yiU6+NLIOiYNp0JTUpew2HDLEOsFiiAlbWOVwJAhENfS0qhnFvS2sYzd6 dS+IOVXTcYhyDRAMjOuCerOezZjMo4MbP/CBuoWYDpFnq/Wf9aU6lzqHgKMQloA9a9eH hz1aGuL6VjNpVnsFyKt8y8WTCGtv/QtqzpG9S/7Hjf5ud6rFz5RybF7YBlFJHof5eCpG UfNg== X-Gm-Message-State: AGi0PubG07B+tWrWlD7pgE9ZT5Z8rRHC1kU/D29Z6Y4pMCX2YVZZ6EE0 riMwrDan5eaSghIY6BVvdkc5tA== X-Received: by 2002:a05:6000:12c7:: with SMTP id l7mr26755993wrx.239.1587973354636; Mon, 27 Apr 2020 00:42:34 -0700 (PDT) Received: from maco2.ams.corp.google.com (a83-162-234-235.adsl.xs4all.nl. [83.162.234.235]) by smtp.gmail.com with ESMTPSA id x132sm15091658wmg.33.2020.04.27.00.42.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 00:42:34 -0700 (PDT) From: Martijn Coenen To: axboe@kernel.dk, hch@lst.de, ming.lei@redhat.com Cc: narayan@google.com, zezeozue@google.com, kernel-team@android.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, maco@google.com, bvanassche@acm.org, Chaitanya.Kulkarni@wdc.com, jaegeuk@kernel.org, Martijn Coenen Subject: [PATCH v3 1/9] loop: Factor out loop size validation Date: Mon, 27 Apr 2020 09:42:14 +0200 Message-Id: <20200427074222.65369-2-maco@android.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog In-Reply-To: <20200427074222.65369-1-maco@android.com> References: <20200427074222.65369-1-maco@android.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ensuring we don't truncate loff_t when casting to sector_t is done in multiple places; factor it out. Signed-off-by: Martijn Coenen --- drivers/block/loop.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index f1754262fc94..f812f11649d3 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -228,15 +228,30 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) blk_mq_unfreeze_queue(lo->lo_queue); } +/** + * loop_validate_size() - validates that the passed in size fits in a sector_t + * @size: size to validate + */ +static int +loop_validate_size(loff_t size) +{ + if ((loff_t)(sector_t)size != size) + return -EFBIG; + else + return 0; +} + static int figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { + int err; loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - sector_t x = (sector_t)size; struct block_device *bdev = lo->lo_device; - if (unlikely((loff_t)x != size)) - return -EFBIG; + err = loop_validate_size(size); + if (err) + return err; + if (lo->lo_offset != offset) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) @@ -1003,9 +1018,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, !file->f_op->write_iter) lo_flags |= LO_FLAGS_READ_ONLY; - error = -EFBIG; size = get_loop_size(lo, file); - if ((loff_t)(sector_t)size != size) + error = loop_validate_size(size); + if (error) goto out_unlock; error = loop_prepare_queue(lo); if (error) -- 2.26.2.303.gf8c07b1a785-goog