Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1430900imm; Tue, 10 Jul 2018 01:28:31 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdsSUPdg9WLCWNYItnx0kq35/j+8YSAu09ZphibmhkbA2uzbpbxdgbEwiJFpFI6+ww+6WG2 X-Received: by 2002:a63:314f:: with SMTP id x76-v6mr21452136pgx.373.1531211311517; Tue, 10 Jul 2018 01:28:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531211311; cv=none; d=google.com; s=arc-20160816; b=tkLZsR3oww1VecU5oJe1PEQEEQxKk7iUFDqxQRIL40/NZp1cCcU0vf4MHQVbXyGAff BSyjIdu1842zR/jisYbo5tllYGB1mEjox1DRN6Cp2l/zfUy8fxIeDP2RN2zsIJvx8D6m aY6L+u21Up2gCwXOME5cRNHZ8y+1PAFgdMrgnrqFY4SwASmmlRNCu8gP8hqjt5yljUoz q0q4qHHnHUby3dKxm8vLCRjAvuqgbkWE1WcYLLmrge1LGLbmMjCweAABm6XzzZKby+lH 7SEihlx73VXyU3YBS8Dp4Vc0qoAo1XD2IljwSp23aWdVaJ0qUm+Fc4DwHwX8XToANetG N3QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:openpgp:from:references:cc:to:subject :dkim-signature:arc-authentication-results; bh=P6HfmlM/cBK46V8lQXuLn9AZXlb7U0QMuTLQ12qwUZc=; b=ODF7RxP7KN+QrlV3YG38WClmZu/XjaS9x/hNiiGB9zPud3baNvSQedcEA9WcnPuuY+ hOVYCYdL0rWUsvK9XG8Aika8xmLXaEfJLWzLVOVG6bn4RkecXUrpbM5smNvIv3dH879P rUg57gAi3me7jq3lkf7n9Vb22t3ggN5UDvbEDdXunUTYKdcy1s9q/nh115T1mBvgTMQE t9HSUfvU/E0xFl8m0zbYBhkcY6H63Uhk9+5R9ZMLrDZyYKGw2xkagdl6A7GWsCRSIxru 2q0jywehXzxyI8VInl27vWqa3FJOj3ndyppAg7jXZUC7D0dI1vB/usTqZtpTAtqaqCMO lJ7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MQUr5ehx; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v14-v6si13008912pga.270.2018.07.10.01.28.16; Tue, 10 Jul 2018 01:28: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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MQUr5ehx; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751329AbeGJI1h (ORCPT + 99 others); Tue, 10 Jul 2018 04:27:37 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:43476 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751082AbeGJI1f (ORCPT ); Tue, 10 Jul 2018 04:27:35 -0400 Received: by mail-wr1-f67.google.com with SMTP id b15-v6so13625590wrv.10; Tue, 10 Jul 2018 01:27:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=P6HfmlM/cBK46V8lQXuLn9AZXlb7U0QMuTLQ12qwUZc=; b=MQUr5ehxcpAATCZvoEuCxgFrw5p/aKl5vDTmuHLNCeFANW5f+aF+qNKveAOOmFa3/c YfSc3PbwyMn3vm4ScOaGxZh97SVrMzcx0kDkWULCDIK50AKrrnPSzkb6/WLtutWTwpwW o7m1yM0+CASUDSXIoQditvKogJPH7kfo9IxU2CKpQ57KlFLLWWsJKESak7j+9Id67+Ot wy3J/0fJd7fhsC2D+CSwSSdc0DTCf7ZERmuKqXXdBD0uaizgZX9JZGX0mODUywB/3C9B psICLFPoBRBCH+vG56Vy+HjrtXxAkopYBWwVzTaCLMth1ePjtMdA5wXLfkXAYVSQiXwv htmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-transfer-encoding:content-language; bh=P6HfmlM/cBK46V8lQXuLn9AZXlb7U0QMuTLQ12qwUZc=; b=pzQyBTBYL4w/2DHIMmZA+B6U8lUvtoQQvho8yWmt5tWYsvZHz2J4vr7yjQyvi/UvJi JInP7chbpY7u97skYDxwNrDAqIwEPVBpGSz7TtyDHwKWb5ThKT4TAdtji1SN5Um0+mNm qPqHonY8vj0eH5ZIprB8K+DP7X3yI+gkMj5aBqfn8XAPpesXwEcdYFDXx7ZUuCLl9dWT Gn00FjIcJ/E2eMzotoj2hBYfOTvz6xqhjbAA4xC4woMyauqaQ28LT+uq+YZXrj44A9YJ hupzEirMF2b+qpviyA+GrpAr4obS03rtOC8im3hN38gdGKdl8ygc18dNbGDIjuwrsc82 1jWw== X-Gm-Message-State: APt69E1cahLEz+whBwQQqPQOqw2iMnp+2JZk9PjIXa+zRRLRoBQAW3DC 70j/yerOZyBoXQgKkoQo0yo= X-Received: by 2002:adf:bc03:: with SMTP id s3-v6mr18338778wrg.211.1531211254464; Tue, 10 Jul 2018 01:27:34 -0700 (PDT) Received: from [192.168.8.10] ([185.175.214.210]) by smtp.gmail.com with ESMTPSA id w9-v6sm10186881wrr.77.2018.07.10.01.27.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Jul 2018 01:27:33 -0700 (PDT) Subject: Re: [V9fs-developer] [PATCH] Integer underflow in pdu_read() To: piaojun , ericvh@gmail.com, rminnich@sandia.gov, lucho@ionkov.net Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, syzkaller@googlegroups.com, v9fs-developer@lists.sourceforge.net, davem@davemloft.net References: <20180709192651.28095-1-tomasbortoli@gmail.com> <5B440B6A.9090000@huawei.com> From: Tomas Bortoli Openpgp: preference=signencrypt Autocrypt: addr=tomasbortoli@gmail.com; prefer-encrypt=mutual; keydata= xsFNBFpCTZMBEADNZ1+Ibh0Z4pgGRcd1aOUMbe/YfHktmajjcoTnKmZZunjoUVAl8waeLITd BC2c8i1wHzHcnthrmb1izs5XlG6PZnl8n5tjysSNbwggzS1NcEK1qgn5VjNlHQ5aRMUwCC51 kicBiNmlQk2UuzzWwdheRGnaf+O1MNhC0GBeEDKQAL5obOU92pzflv6wWNACr+lHxdnpyies mOnRMjH16NjuTkrGbEmJe+MKp0qbjvR3R/dmFC1wczniRMQmV5w3MZ/N9wRappE+Atc1fOM+ wP7AWNuPvrKg4bN5uqKZLDFH7OFpxvjgVdWM40n0cQfqElWY9as+228Sltdd1XyHtUWRF2VW O1l5L0kX0+7+B5k/fpLhXqD3Z7DK7wRXpXmY59pofk7aFdcN97ZK+r6R7mqrwX4W9IpsPhkT kUyg3/Dx/khBZlJKFoUP325/hoH684bSiPEBroel9alB7gTq2ueoFwy6R3q5CMUw3D+CZWHA 3xllu46TRQ/Vt2g0cIHQNPoye2OWYFJ6kSEvaLpymjNDJ9ph2EuHegonDfOaYSq34ic2BcdB JkCgXRLP5K7KtRNJqqR+DM8xByeGmQv9yp6S97el+SiM9R53RhHawJZGz0EPl+2Q6+5m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jfC2+PM5I0xebbxtnuTewRchVVfm7UWgLAy11pV3xM4wMSJOuqVMOz jYpWKYxDTpvsZ0ginUUY993Gb8k/CxjABEMUGVHhQPZ0OzjHIKS6cTzN6ue8bB+CGOLCaQp1 C0NRT5Tn9zpLxtf5nBExFd/zVENY5vAV2ZbKQdemO54O7j6B9DSgVRrm83GCZxbL4d+qTYBF 3tSCWw/6SG1F3q9gR9QrSC2YRjCmhijUVEh6FhZwB58TNZ1sEEttrps8TDa5tUd9 Message-ID: <36523cc7-adec-9e61-d34c-dc00806c403a@gmail.com> Date: Tue, 10 Jul 2018 10:27:32 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <5B440B6A.9090000@huawei.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jun, Intuitively, if you have a packet of size x and you read at an offset y, when y>x you are off the packet. That's an out out bound read. In this specific code when offset > size, the available length estimation will fail as there will be an underflow resulting from offset-size (it'll give a big big number) that breaks the out-of-bound control put in place (if offset-size is a big big number, the asked size to read will be probably smaller and therefore allowed). These definitions might help: https://cwe.mitre.org/data/definitions/787.html https://cwe.mitre.org/data/definitions/125.html Tomas > Hi Tomas, > > It looks like pdu->size should always be greater than pdu->offset, righ= t? > My question may be very easy for you, please help explaining. > > Thanks, > Jun > > On 2018/7/10 3:26, Tomas Bortoli wrote: >> The pdu_read() function suffers from an integer underflow. >> When pdu->offset is greater than pdu->size, the length calculation wil= l have >> a wrong result, resulting in an out-of-bound read. >> This patch modifies also pdu_write() in the same way to prevent the sa= me >> issue from happening there and for consistency. >> >> Signed-off-by: Tomas Bortoli >> Reported-by: syzbot+65c6b72f284a39d416b4@syzkaller.appspotmail.com >> --- >> net/9p/protocol.c | 12 ++++++++---- >> 1 file changed, 8 insertions(+), 4 deletions(-) >> >> diff --git a/net/9p/protocol.c b/net/9p/protocol.c >> index 931ea00c4fed..f1e2425f920b 100644 >> --- a/net/9p/protocol.c >> +++ b/net/9p/protocol.c >> @@ -55,16 +55,20 @@ EXPORT_SYMBOL(p9stat_free); >> =20 >> size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size) >> { >> - size_t len =3D min(pdu->size - pdu->offset, size); >> - memcpy(data, &pdu->sdata[pdu->offset], len); >> + size_t len =3D pdu->offset > pdu->size ? 0 : >> + min(pdu->size - pdu->offset, size); >> + if (len !=3D 0) >> + memcpy(data, &pdu->sdata[pdu->offset], len); >> pdu->offset +=3D len; >> return size - len; >> } >> =20 >> static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_= t size) >> { >> - size_t len =3D min(pdu->capacity - pdu->size, size); >> - memcpy(&pdu->sdata[pdu->size], data, len); >> + size_t len =3D pdu->size > pdu->capacity ? 0 : >> + min(pdu->capacity - pdu->size, size); >> + if (len !=3D 0) >> + memcpy(&pdu->sdata[pdu->size], data, len); >> pdu->size +=3D len; >> return size - len; >> } >>