Return-Path: <awaresystems.be+caf_=filtered=awaresystems.be@gmail.com>
Delivered-To: filtered@awaresystems.be
Received: from mx6.public.one.com ([10.246.18.26])
	by mail6.local.one.com (Dovecot) with LMTP id JTL5DM/hgVl/IgEAsyOMew
	for <filtered@awaresystems.be>; Wed, 02 Aug 2017 15:02:59 +0000
X-HalOne-ID: abc6895e-7793-11e7-a839-b82a72d854f0
Received: from mail-oi0-f49.google.com (unknown [209.85.218.49])
	by mx6.public.one.com (Halon) with ESMTPS
	id abc6895e-7793-11e7-a839-b82a72d854f0;
	Wed, 02 Aug 2017 15:02:59 +0000 (UTC)
Received: by mail-oi0-f49.google.com with SMTP id g131so46729537oic.3
        for <filtered@awaresystems.be>; Wed, 02 Aug 2017 08:02:59 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-original-authentication-results:x-gm-message-state:delivered-to
         :mime-version:from:date:message-id:to:subject:precedence:list-id
         :list-unsubscribe:list-archive:list-post:list-help:list-subscribe
         :content-transfer-encoding:sender:errors-to;
        bh=tEXGnW4omgOu6wLLezCjKCmaSMNOlC4ySo+o1tr4So4=;
        b=Mvnn0LR9Bgv1/fuvoLksKC2KeqD0yb+5YOqVEifouXavRJjIzbMdtMmZGjfLmWOYC/
         f1d0Jg/GkeqNVz5j2DtBILZnvaGI++xO3grjGbr+f2Y4KZQ4BIUzcCk5V/+zBUr6fhIv
         CIr1goNhYRvrNHAuAWmogZq5nKckOzKRiitNUZB2T20u9xjDJj3LcEgSIhUW3R85Zh/K
         IhvTdMARQ81IGVzoSPdlBTd19eCwIpIVQ3JzYMOJYbtzEUdsjfS66Y9gpahRe1NjOGtR
         rl2JbfcPcRhGftfZHr+OrDsIRkZn1dZg0dFkjnBaK5i2TVNNkkOJoIUyrMBkVIMvwit0
         1pag==
X-Original-Authentication-Results: mx.google.com;       spf=neutral (google.com: 94.23.4.142 is neither permitted nor denied by best guess record for domain of tiff-bounces@lists.maptools.org) smtp.mailfrom=tiff-bounces@lists.maptools.org;       dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com
X-Gm-Message-State: AIVw1134MLKjA+3rhWaZipYMO6fBLjurVFrGfa1Ys78lTyPt5015VgvH
	sYipjRi+TYvbuuxEm14THwvVqe0sIES41QtPB3TPAw==
X-Received: by 10.202.80.19 with SMTP id e19mr6326094oib.274.1501686177888;
        Wed, 02 Aug 2017 08:02:57 -0700 (PDT)
X-Forwarded-To: filtered@awaresystems.be
X-Forwarded-For: awaresystems.be@gmail.com filtered@awaresystems.be
Delivered-To: awaresystems.be@gmail.com
Received: by 10.74.68.1 with SMTP id o1csp3968836ooa;
        Wed, 2 Aug 2017 08:02:56 -0700 (PDT)
X-Received: by 10.28.109.27 with SMTP id i27mr3767792wmc.173.1501686176521;
        Wed, 02 Aug 2017 08:02:56 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1501686176; cv=none;
        d=google.com; s=arc-20160816;
        b=pDC44FjVsIrJ0ZrrzjuDpbUnmLEu95o+smZV2qdZkjoq4FjRFlcABnnHsOhiGJk+CQ
         ++mIx+PGbhdJ9sbyNt9oxXttVKPfsA9Ti+grY5L+8dwJ6TM4qyHUZc1uwn8023vOhKoS
         WJQKwOI/GKQhUgKSNR19iwGMS2aUI9LBd7bicllcD0FXr/w3WYAFoFIulEyzBsdQ1YBy
         n6422/KjD1Lbd54gIeLkkWBxCUZ/ZPaHUpGkmV4alzcr5u4rqYm7gfV0fDl3hArCIkuK
         4w2lmf1FIZRlxCqfv5lWRy5GKz/EFT79gLI9ZPUea8FY4jr3Aol4EK3POVFW+XAxkoX+
         cMVg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=errors-to:sender:content-transfer-encoding:list-subscribe:list-help
         :list-post:list-archive:list-unsubscribe:list-id:precedence:subject
         :to:message-id:date:from:mime-version:arc-authentication-results;
        bh=tEXGnW4omgOu6wLLezCjKCmaSMNOlC4ySo+o1tr4So4=;
        b=TnBFQFxRsX99jXy8KCM8GBZdu2R2tz24qA1uhkZy9aTe7L7GLSBhjJjKeV+V6OmVAX
         8nm8xTOVXHhCZT0sEaaK734dE3o3kQj+hvGW8YT21Y+w2/mzJ7Rqy3w2xDJtbRBb3ahb
         +WntZW48LyT+Tq7UjC8dreaNcKG56SA9dh3FaZuLda6jfmMAF9BdnvAoIb3qQzTZr8b7
         9V0DvrZL/dMN2ruMuDlWVKp8yv2HHyKQcbCQehaQ9CzqY4q3XbXb92Mitx85UvvDqMrw
         zSsvFNKKqz/oJtwF9zVVx7BLDX6p5BbtCemjQVZCcTWVdDM3Annqds5YZDnwPYSjIF4X
         7gHQ==
ARC-Authentication-Results: i=1; mx.google.com;
       spf=neutral (google.com: 94.23.4.142 is neither permitted nor denied by best guess record for domain of tiff-bounces@lists.maptools.org) smtp.mailfrom=tiff-bounces@lists.maptools.org;
       dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com
Return-Path: <tiff-bounces@lists.maptools.org>
Received: from lists.maptools.org (ks365428.kimsufi.com. [94.23.4.142])
        by mx.google.com with ESMTP id t16si26876821wrb.484.2017.08.02.08.02.33;
        Wed, 02 Aug 2017 08:02:56 -0700 (PDT)
Received-SPF: neutral (google.com: 94.23.4.142 is neither permitted nor denied by best guess record for domain of tiff-bounces@lists.maptools.org) client-ip=94.23.4.142;
Authentication-Results: mx.google.com;
       spf=neutral (google.com: 94.23.4.142 is neither permitted nor denied by best guess record for domain of tiff-bounces@lists.maptools.org) smtp.mailfrom=tiff-bounces@lists.maptools.org;
       dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com
Received: from [10.1.0.241] (localhost.localdomain [127.0.0.1])
	by lists.maptools.org (Postfix) with ESMTP id E003035C027;
	Wed,  2 Aug 2017 10:01:22 -0500 (EST)
Received: from mail-qt0-f178.google.com (mail-qt0-f178.google.com
	[209.85.216.178])
	by lists.maptools.org (Postfix) with ESMTPS id 2549E35C025
	for <tiff@lists.maptools.org>; Wed,  2 Aug 2017 10:00:58 -0500 (EST)
Received: by mail-qt0-f178.google.com with SMTP id p3so28338459qtg.2
	for <tiff@lists.maptools.org>; Wed, 02 Aug 2017 08:00:58 -0700 (PDT)
X-Received: by 10.237.61.123 with SMTP id h56mr31207463qtf.1.1501686056633;
	Wed, 02 Aug 2017 08:00:56 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.12.185.153 with HTTP; Wed, 2 Aug 2017 08:00:36 -0700 (PDT)
From: Nicolas RUFF <nicolas.ruff@gmail.com>
Date: Wed, 2 Aug 2017 17:00:36 +0200
Message-ID: <CABoQFUD-RYPQeJSCBPVdXNeA-yESnHH1QcySUPd3QA=SxMcOOA@mail.gmail.com>
To: tiff@lists.maptools.org
Subject: [Tiff] Error handling in Read/Write/Seek
X-BeenThere: tiff@lists.maptools.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: TIFF and libtiff discussion <tiff.lists.maptools.org>
List-Unsubscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=unsubscribe>
List-Archive: <http://lists.maptools.org/mailman/private/tiff>
List-Post: <mailto:tiff@lists.maptools.org>
List-Help: <mailto:tiff-request@lists.maptools.org?subject=help>
List-Subscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Sender: tiff-bounces@lists.maptools.org
Errors-To: tiff-bounces@lists.maptools.org

Hello,

Apparently TIFFReadFile / TIFFWriteFile / TIFFSeekFile return byte
count on success, and -1 on error.

I have a pathological TIFF file (generated by a fuzzer) where a
directory seek will actually end up calling TIFFSeekFile(-1,
SEEK_SET). This will pass the SeekOK() test in any case, and result in
an out-of-bound access to tiff_buffer[-1].

The quick-and-dirty fix I have is redefining the following macros:

#define ReadOK(tif, buf, size) \
(TIFFReadFile((tif),(buf),(size))==(size))
#define SeekOK(tif, off) \
(TIFFSeekFile((tif),(off),SEEK_SET)==(off))
#define WriteOK(tif, buf, size) \
(TIFFWriteFile((tif),(buf),(size))==(size))

into:

#define ReadOK(tif, buf, size) \
((TIFFReadFile((tif),(buf),(size))==(size) && (size)!=-1))
#define SeekOK(tif, off) \
((TIFFSeekFile((tif),(off),SEEK_SET)==(off) && (off)!=-1))
#define WriteOK(tif, buf, size) \
((TIFFWriteFile((tif),(buf),(size))==(size) && (size)!=-1))

This approach has two shortcomings:
1. If "size" or "off" is an expression, it will be evaluated one more time.
2. I wonder if a directory entry could be located at exact offset
0xFFFFFFFF in the input file (in which case it would be inaccessible).

The real fix would be some kind of out-of-band error reporting (a la
errno). Such a change would be transparent to all users of ReadOK /
WriteOK / SeekOK macros, but I wonder if there are people in the wild
checking return values on their own.

WDYT?
_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
