Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1063205imm; Mon, 9 Jul 2018 16:30:31 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfw+G+vKw3Mo6E0KlcvZ9VYYgGmg78UfFUi6iMuu3p1rrxGvFYHpqra3rWLZtGkT+hc+YAZ X-Received: by 2002:a17:902:8306:: with SMTP id bd6-v6mr22574689plb.120.1531179031460; Mon, 09 Jul 2018 16:30:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531179031; cv=none; d=google.com; s=arc-20160816; b=YHyWEXoil9DoADDdc546HpMbpJhmxBXtNTIAz5srvk+tn/J/827Hf4R/r/DPRCDcnO OjUutbj/mAgZBEJNj6x/b51r9dncc0zgq3VJyGlX7TZ1o87n0apz1JYX2pgQbvbxqO7P HuWQHsUVu5E/iQ+IXPFBB14UaR8ZG2NtBdJj/WokI8ahO1GSMqBwUtzT+PddqyxexQe1 mgEBp0MGGI1Y1nVhfEqYJe/mnVJ1SJoFIYsJRsgJu8lybw1VaHLDmUEbcZwnCIchk0wV 4NOXHmA3tu+bAsC8P8vHCPm5ZJYaEICctwzs9gyCQqblAd0YH4SRF61atI8toh3RNXPm DqlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=XXCyUgn1PDivJY8kRX5o1hjgb9kqBKI9dIRpZShmcJk=; b=BwZmk3gwc0kXfOr3BSWhoWtI8QnTZjVY2vz4rDCXOMh6VYG0gP78WWLBdq7KbvEyz+ OzcxjLvB0yr/EH8t0Q5i/jfy3PGHwzX4uE+2EBI5akB6DMHQ4715jKSNR56CyMT27K6p 3FbwC0mncEOCrFyUwcuQ4FG3XSYu7e0iEa6NSnA32qcJj5DBmN3O9N8aIywC35h2Nzl8 HvCQM5bff95eXm3xYgmmdNb1tVx4H8I2NY7jo+LrisSqOQeDqCF/NZFJ57XrDNZgvELx pd12++neE3UvtLZSmutotMBtmmbMNoDttUvIvccS2r1baAyFanofQbP8u/IPzSHjLt/Z E0ig== ARC-Authentication-Results: i=1; mx.google.com; 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 d1-v6si15382717pld.515.2018.07.09.16.30.16; Mon, 09 Jul 2018 16:30:31 -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; 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 S933129AbeGIX3i (ORCPT + 99 others); Mon, 9 Jul 2018 19:29:38 -0400 Received: from nautica.notk.org ([91.121.71.147]:59294 "EHLO nautica.notk.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754502AbeGIX3h (ORCPT ); Mon, 9 Jul 2018 19:29:37 -0400 Received: by nautica.notk.org (Postfix, from userid 1001) id 7FFDFC009; Tue, 10 Jul 2018 01:29:35 +0200 (CEST) Date: Tue, 10 Jul 2018 01:29:20 +0200 From: Dominique Martinet To: Tomas Bortoli Cc: Al Viro , lucho@ionkov.net, ericvh@gmail.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, syzkaller@googlegroups.com, v9fs-developer@lists.sourceforge.net, rminnich@sandia.gov, davem@davemloft.net Subject: Re: [V9fs-developer] [PATCH] Integer underflow in pdu_read() Message-ID: <20180709232920.GA19917@nautica> References: <20180709192651.28095-1-tomasbortoli@gmail.com> <20180709193151.GI30522@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Tomas Bortoli wrote on Tue, Jul 10, 2018: > > What does cause the calls of pdu_read() in such conditions and shouldn't *that* > > be dealt with? > > Mmh I think that's caused by p9_parse_header(). That function reads the > first 7 bytes of a PDU regardless of the current offset. It then sets > the PDU length to the one read and then it restores the original offset. > Therefore, it's possible to set a size < offset here. > (to be 100% sure I'd need more debugging) It doesn't always restore the original offset; but if the packet lied and said its size is < 7 it doesn't even need to. I think as things stand it should be enough to fix it there, once the state machine is runnning there don't seem to be any way of making offset jump over size; but I'm not fussy either way, protecting in pdu_read is probably just as good. Note that there is another "min_t(uint32_t, *count, pdu->size - pdu->offset)" that needs a similar check below in the file if you want to go this way. On the other hand, pdu_write() calls come from the system so hopefully these are a bit more trustworthy, but I guess the extra check could protect against a programming error. I'm not sure what the linux kernel policy about these "redundant" low-level checks is as this is technically in the fast path. > We can prevent it in p9_parse_header(), but if the invariant offset < > size gets broken elsewhere we fall back to the underflow. Enforcing it > in pdu_read() should be the safest thing to do as it would detect *any* > bad read. The main problem is that 9p is just too trusty of what is sent to it. To further extent what was said in the other thread ("[PATCH] KASAN: slab-out-of-bounds Read in pdu_read") the extra check on pdu->size that must be smaller than actual read size holds for p9_check_zc_error as well so should probably be moved to p9_parse_header() ; but a packet that says its size is < 7 is also wrong : for trans_fd, we are guaranted to have read as much by the transport, so once again size didn't match up. Ideally, pdu->size should only ever be set by the transport who know what they have read, and p9_parse_header should check that r_size == pdu->size and error if not. Also, as TCP is a stream protocol, once we've had a single packet lie about its size we're lost in the stream with no chance of recovering so the connection should probably be aborted. For rdma/virtio there is a notion of packet so they could recover, but TCP is not as forgiving... -- Dominique Martinet | Asmadeus