Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1559315imm; Tue, 10 Jul 2018 04:07:53 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc348c5Z5mYM1LkxkHkfHbHQCmBj47c+Q2E+xtSPsTF/q8b3nB2CVEhpDxq7MhZhTcgbmxG X-Received: by 2002:a65:6455:: with SMTP id s21-v6mr15705850pgv.394.1531220873894; Tue, 10 Jul 2018 04:07:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531220873; cv=none; d=google.com; s=arc-20160816; b=woef7OzNHgxPmH3LpN/oZx4hdXsMBQC9TLNxTjmAs9ZaJvN5tvSMo8IaBPoXomLUcZ gjgXqCIlh/s3t+BaEauWEoNmvd0ogND/xL+qWO9VLyyefM6Da1TNPx3/gMMASdHAYV+D 0R8dwA/VOeOZ/VZr5GOaTtjrv/sag9/ksAxUGvLd7Ou4XJbN9tyjJZqP+Ll3/HMvdvwL 5Ro8SV8ryRQF4GPMOw1Xo5yWWTtvI7sSnejGyP9oZFVaQJ0NVqJowvfmNRManFFx9dzl HuJaWKU2e6ZSkMNt1/frpO4rVoQumckcdN0an444xmMDve429rF53wyy6BKm98KeNi+v q/ZA== 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:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject:arc-authentication-results; bh=JfMk8xiTQYxR8U1V5cKYD1SmzPjgcpI1DgC6vJDJHro=; b=T4jptRQUJnpHRe3VGkBYbMmks6Lncdt8A5h9W342x/H2UN9HM50bTY69fzut/3bYO5 B69ejxXvBosxMWg2nYqWchx4L2lzxl1Iv/h0rUL3Gf54DkZwDvrHffxuFzHpVlCKYy/L 4y+knZBMMzYlwXT9fLyNxaxbgZVpPI8nLlwfC3lJBbK48HJZVncOkmWNBm09EHGW00NH 2br66AMkMoCvEXBHrS9ozQay7q/IwzGX4gsP+IhVDlpek+3Dd1e5qhJlT3329ksznlsD 1tlYKLZqP9CK4tGEXZh4bqd1nykR0OTrovHumwWgksWPu2C7m4bHDa3KvuT+9IR+2CaO lJjA== 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 67-v6si15694351pgi.456.2018.07.10.04.07.26; Tue, 10 Jul 2018 04:07:53 -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 S933279AbeGJLGq (ORCPT + 99 others); Tue, 10 Jul 2018 07:06:46 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:9168 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751432AbeGJLGo (ORCPT ); Tue, 10 Jul 2018 07:06:44 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 42F7C6B4A7162; Tue, 10 Jul 2018 19:06:29 +0800 (CST) Received: from [10.177.253.249] (10.177.253.249) by smtp.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.382.0; Tue, 10 Jul 2018 19:06:28 +0800 Subject: Re: [V9fs-developer] [PATCH] Integer underflow in pdu_read() To: Tomas Bortoli , , , References: <20180709192651.28095-1-tomasbortoli@gmail.com> <5B440B6A.9090000@huawei.com> <36523cc7-adec-9e61-d34c-dc00806c403a@gmail.com> CC: , , , , From: piaojun Message-ID: <5B449329.1050507@huawei.com> Date: Tue, 10 Jul 2018 19:06:17 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <36523cc7-adec-9e61-d34c-dc00806c403a@gmail.com> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.253.249] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Tomas, Thanks for your explaination, and I get your point. On 2018/7/10 16:27, Tomas Bortoli wrote: > 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, right? >> 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 will have >>> a wrong result, resulting in an out-of-bound read. >>> This patch modifies also pdu_write() in the same way to prevent the same >>> 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); >>> >>> size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size) >>> { >>> - size_t len = min(pdu->size - pdu->offset, size); >>> - memcpy(data, &pdu->sdata[pdu->offset], len); >>> + size_t len = pdu->offset > pdu->size ? 0 : >>> + min(pdu->size - pdu->offset, size); >>> + if (len != 0) >>> + memcpy(data, &pdu->sdata[pdu->offset], len); >>> pdu->offset += len; >>> return size - len; >>> } >>> >>> static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size) >>> { >>> - size_t len = min(pdu->capacity - pdu->size, size); >>> - memcpy(&pdu->sdata[pdu->size], data, len); >>> + size_t len = pdu->size > pdu->capacity ? 0 : >>> + min(pdu->capacity - pdu->size, size); >>> + if (len != 0) >>> + memcpy(&pdu->sdata[pdu->size], data, len); >>> pdu->size += len; >>> return size - len; >>> } >>> > > >