From patchwork Thu Jun 22 17:03:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Gruenbacher X-Patchwork-Id: 9804879 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1230360234 for ; Thu, 22 Jun 2017 17:03:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F259D2869A for ; Thu, 22 Jun 2017 17:03:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E670E2869C; Thu, 22 Jun 2017 17:03:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 959CA286F2 for ; Thu, 22 Jun 2017 17:03:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752289AbdFVRDv (ORCPT ); Thu, 22 Jun 2017 13:03:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:6293 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751927AbdFVRDv (ORCPT ); Thu, 22 Jun 2017 13:03:51 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A888240F0D; Thu, 22 Jun 2017 17:03:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A888240F0D Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=agruenba@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A888240F0D Received: from nux.redhat.com (ovpn-116-35.ams2.redhat.com [10.36.116.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 79F675C7A9; Thu, 22 Jun 2017 17:03:49 +0000 (UTC) From: Andreas Gruenbacher To: linux-xfs@vger.kernel.org Cc: Andreas Gruenbacher , fstests@vger.kernel.org, Jan Kara Subject: [PATCH] xfs_io: add seek consistency checks Date: Thu, 22 Jun 2017 19:03:48 +0200 Message-Id: <1498151028-18433-1-git-send-email-agruenba@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 22 Jun 2017 17:03:50 +0000 (UTC) Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When seeking for data and holes, the lseek result must be greater than or equal to the start offset. Furthermore, assuming that the file doesn't change under us, when switching between SEEK_HOLE and SEEK_DATA, the seek position must increase monotonically. Warn and abort if this is not the case. Signed-off-by: Andreas Gruenbacher Reviewed-by: Christoph Hellwig --- io/seek.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/io/seek.c b/io/seek.c index d06375d..871b472 100644 --- a/io/seek.c +++ b/io/seek.c @@ -147,7 +147,10 @@ seek_f( * decide if we want to display that type of entry. */ if (flag & SEEK_HFLAG) { + current = HOLE; offset = lseek(file->fd, start, SEEK_HOLE); + if (offset != -1 && offset < start) + goto bad_result; if ((start == offset) || !(flag & SEEK_DFLAG)) { /* * this offset is a hole or are only displaying holes. @@ -155,7 +158,6 @@ seek_f( * data, then we will fall through below to * initialize the data search. */ - current = HOLE; goto found_hole; } } @@ -163,6 +165,8 @@ seek_f( /* The offset is not a hole, or we are looking just for data */ current = DATA; offset = lseek(file->fd, start, SEEK_DATA); + if (offset != -1 && offset < start) + goto bad_result; found_hole: /* @@ -203,8 +207,15 @@ found_hole: current ^= 1; /* alternate between data and hole */ start = offset; offset = lseek(file->fd, start, seekinfo[current].seektype); + if (offset != -1 && offset <= start) + goto bad_result; } return 0; + +bad_result: + fprintf(stderr, "Invalid seek result: lseek(, %lld, SEEK_%s) = %lld\n", + (long long)start, seekinfo[current].name, (long long)offset); + return 0; } void