From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85F07C433DB for ; Wed, 27 Jan 2021 20:40:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C16B860C3D for ; Wed, 27 Jan 2021 20:40:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C16B860C3D Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=nongnu.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:47410 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l4rbo-0008Uj-7c for qemu-devel@archiver.kernel.org; Wed, 27 Jan 2021 15:40:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34408) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l4rZk-0007AN-6V for qemu-devel@nongnu.org; Wed, 27 Jan 2021 15:38:04 -0500 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]:42489) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l4rZi-0003s2-F7 for qemu-devel@nongnu.org; Wed, 27 Jan 2021 15:38:03 -0500 Received: by mail-lf1-x129.google.com with SMTP id h12so4530956lfp.9 for ; Wed, 27 Jan 2021 12:38:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vtUiY4nMDeP6TOSXa2wuZ0uc6XilJPya8B0WM38Ne8I=; b=iX0M0Lj+IhTysQfKtSlr9fJCCb4TT0aZlKBPInF/xRWQBpvJBjtCVAGOtdaJdxl2PR Tm9gV3s0ZiraMzIZsum30Fs+ryyRjsGjt5gmQcoyEf49AIw0K6X7XVf8MObzvvv9eOA9 Hd70ED+jGsp9i5+rBMhiB+b1eII0IH63DFzD4b8ZQOrjBUL/bcyA3wiQ3mpvUOv8V16B aua820db9p/hpLS/nnJdzPdyKfbDAoQV1SQdb98pfCkwCCklEtJ6QnR8eqoEaNTq/nxU I6m7luDjBpg9j9qrAxcmpfSCyXw7ImuY4KuE2/iRzEKe4sAyc+OS7D1luBzsmJEpTblM X8iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vtUiY4nMDeP6TOSXa2wuZ0uc6XilJPya8B0WM38Ne8I=; b=WtFyPzuawm7jsz/qYpoQQ6AMft6KiupK2UENJMrmCp3/pBNH1kvYNrfanat6PoWDJy ohMYhYmK3ZqlDNkTnVRvsAkzIORJ0FPQoeQiu8BmqDyQS/VfpDuz8oOPVeQX7ktqJ9aV JHUhddMKE0hCAObYZxNEbLPFPo3/aiBLiQ5RCOfaG5mZUfrMaxFy/y0puFel3NfFbuCI zqmQPGXEzWfGJ08xht+eFj90f7FYcEr0hC71D/yiRNLNfCmSME+rl2r6+lG8FYG/oebO fWs7486sgb61RUBwbMPqQzi20/fWX5quwkIwcB7Mpm8Vz3n4P+ztzrs7k3AW23fZoE5s bYuQ== X-Gm-Message-State: AOAM532mTlQXK7fV0/TRBMBQhJGCRXH+GOny5FGvAvQpjo1p704TCPRR oDh9qNbBEAbrE00Z329v4osDWInj3sQ3+Y3yq8p9Bw== X-Google-Smtp-Source: ABdhPJzb78teiHi2M9vA3dCHp+0DpMMUeBKDBu9adfTDAx7p+dRBMGkZC3FXthEMeD6HHlign/a58pYuWugu1y/cOlM= X-Received: by 2002:a05:6512:2141:: with SMTP id s1mr5663393lfr.337.1611779879183; Wed, 27 Jan 2021 12:37:59 -0800 (PST) MIME-Version: 1.0 References: <20210126193237.1534208-1-wuhaotsh@google.com> <20210126193237.1534208-7-wuhaotsh@google.com> <20210126234724.GC2057975@minyard.net> In-Reply-To: <20210126234724.GC2057975@minyard.net> Date: Wed, 27 Jan 2021 12:37:46 -0800 Message-ID: Subject: Re: [PATCH 6/6] hw/i2c: Implement NPCM7XX SMBus Module FIFO Mode To: Corey Minyard Content-Type: multipart/alternative; boundary="000000000000281d0d05b9e7bfa6" Received-SPF: pass client-ip=2a00:1450:4864:20::129; envelope-from=wuhaotsh@google.com; helo=mail-lf1-x129.google.com X-Spam_score_int: -175 X-Spam_score: -17.6 X-Spam_bar: ----------------- X-Spam_report: (-17.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Patrick Venture , QEMU Developers , Havard Skinnemoen , CS20 KFTing , qemu-arm , IS20 Avi Fishman , Doug Evans Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: wuhaotsh--- via --000000000000281d0d05b9e7bfa6 Content-Type: text/plain; charset="UTF-8" On Tue, Jan 26, 2021 at 3:47 PM Corey Minyard wrote: > On Tue, Jan 26, 2021 at 11:32:37AM -0800, wuhaotsh--- via wrote: > > + > > +static void npcm7xx_smbus_read_byte_fifo(NPCM7xxSMBusState *s) > > +{ > > + uint8_t received_bytes = NPCM7XX_SMBRXF_STS_RX_BYTES(s->rxf_sts); > > + > > + if (received_bytes == 0) { > > + npcm7xx_smbus_recv_fifo(s); > > + return; > > + } > > + > > + s->sda = s->rx_fifo[s->rx_cur]; > > + s->rx_cur = (s->rx_cur + 1u) % NPCM7XX_SMBUS_FIFO_SIZE; > > + --s->rxf_sts; > > This open-coded decrement seems a little risky. Are you sure in every > case that s->rxf_sts > 0? There's no way what's running in the VM can > game this and cause a buffer overrun? One caller to this function seems > to protect against this, and another does not. > s->rxf_sts is uint8_t so it's guaranteed to be >=0. In the case s->rxf_sts == 0, NPCM7XX_SMBRXF_STS_RX_BYTES(s->rxf_sts) is also 0, so it'll take the if-branch and return without running --s->rxf_sts. I'll probably add "g_assert(s->rxf_sts > 0)" to clarify. > > Other than this, I didn't see any issues with this patch. > > -corey > --000000000000281d0d05b9e7bfa6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On = Tue, Jan 26, 2021 at 3:47 PM Corey Minyard <minyard@acm.org> wrote:
On Tue, Jan 26, 2021 at 11:32:37AM -0800, wuhaotsh---= via wrote:
> +
> +static void npcm7xx_smbus_read_byte_fifo(NPCM7xxSMBusState *s)
> +{
> +=C2=A0 =C2=A0 uint8_t received_bytes =3D NPCM7XX_SMBRXF_STS_RX_BYTES(= s->rxf_sts);
> +
> +=C2=A0 =C2=A0 if (received_bytes =3D=3D 0) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 npcm7xx_smbus_recv_fifo(s);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
> +=C2=A0 =C2=A0 }
> +
> +=C2=A0 =C2=A0 s->sda =3D s->rx_fifo[s->rx_cur];
> +=C2=A0 =C2=A0 s->rx_cur =3D (s->rx_cur + 1u) % NPCM7XX_SMBUS_FI= FO_SIZE;
> +=C2=A0 =C2=A0 --s->rxf_sts;

This open-coded decrement seems a little risky.=C2=A0 Are you sure in every=
case that s->rxf_sts > 0?=C2=A0 There's no way what's running= in the VM can
game this and cause a buffer overrun?=C2=A0 One caller to this function see= ms
to protect against this, and another does not.
s->r= xf_sts is uint8_t so it's guaranteed to be >=3D0.
In the c= ase s->rxf_sts =3D=3D 0,=C2=A0 NPCM7XX_SMBRXF_STS_RX_BYTES(s->rxf_sts= ) is also 0, so it'll take the if-branch and return without running --s= ->rxf_sts.
I'll probably add "g_assert(s->rxf_sts = > 0)" to clarify.

Other than this, I didn't see any issues with this patch.

-corey
--000000000000281d0d05b9e7bfa6--