Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2185123pxb; Fri, 24 Sep 2021 23:48:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx31I3+htHzGszarpSuB0tmDqz91TMiMaF5AXfssStOrG1WrdW/M90z+ABHF5H8L4X+NxLw X-Received: by 2002:a5d:8d1a:: with SMTP id p26mr12278311ioj.141.1632552503322; Fri, 24 Sep 2021 23:48:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632552503; cv=none; d=google.com; s=arc-20160816; b=jG2fM3jvk2+m1kCOQXcJVuLKj+pyNCWR8lzsShpbVtrRxQjN80IHPf/3Vn7JymzLgO OvcErj2ZKT6MYPzj9xKs/REG6lRBREPZNPpAOXGml+GEUb/1WpWJWPFgaTnrE6pIgOKe 2Z2iuUdiVZqC/HJ5FAdpYQBIZuBtXRjRkZcKVwhYUNZhVgGVkBokFFjtUlw3vwgvqn2d UaOeZAus1d/9fJuFU610+7knQqUtXeWIu9xYqtdP2dRrI2ESiTgapvIAsAFzMwcBew3r MtvrtBIIUNwZnYDXc6SHIgj3HumLFuMRNYJ/dHQuv4Ytr7KmtgihG6wGFwcUvgN+C4ar YGFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=MpjMDXeEzviTJwTaUQEDTQTS2aoJm5eI7x4MOdMFOCI=; b=W5sn9Ka6Mk7z65MY+mTzhNYG8lDft2Zo+EPiO5rueAZ11FSC/UVDDJ8d7xNDvlAy+C TityhnZoPR4a0jXcxWJOWPGw62eVU5S0IBtWB75sATX2372EbHeXTOHwO0fnUwaGzBG2 RvEsmLMTZyW6UJKM6vqP4H60ry2wvvj4MnW10xCv6/z2ZPrwsLYm+KCs9mv/v7h4R2a4 KL/AEtVhwxjQDItfzdcryLU9zzCgAFkENYepMh8FL8qNs/64k3CNR/1onIYtJZ0PDKld MBzimRErm7cI1T8Xjyp7i0TvYOwNVc1sP3PoNDm/fYYYZOWGiKKAte1CymcxkqIByEjU McZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=q+iv1oDn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t21si6126955jac.10.2021.09.24.23.48.12; Fri, 24 Sep 2021 23:48:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=q+iv1oDn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232741AbhIYElZ (ORCPT + 99 others); Sat, 25 Sep 2021 00:41:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:52574 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229516AbhIYElW (ORCPT ); Sat, 25 Sep 2021 00:41:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 37C0360F70; Sat, 25 Sep 2021 04:39:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1632544787; bh=zPdgx5ZT+r+BrD+V07Yxv3l9pb88yMRgA9tQRZuqifU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=q+iv1oDnkD1W6a+1we9hREN83qPFMaVwkUYlIv1M/mlkTCpm0YV4vIamA61eg+Lsp w+5734SOezlppCF5YOsnM3iTnjeYjxbXUjStIWoViZT/dKOjVdx2EHoQzK5r0Y+m7j giinQj8jfrmXMpTSIAPrfCsLRYEBfjU//MxcguqAUXJCmtEQigFDGS0jNtOQfoIfL3 RDN20aJGPTcmNrlNQJjFfVbdiHds9EbN3/c1z0c1S/00OqGmfuADFUuL6oKcdYxHii 7U1Qd3orKLB3002Gbhut8tiEYFlEjfcX+WNjyDfBwb9D8ncxORQD4Q4JzxFV76lT21 wpnUXkoFYjBYg== Date: Sat, 25 Sep 2021 13:39:44 +0900 From: Masami Hiramatsu To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , "Peter Zijlstra (Intel)" , Masami Hiramatsu , Josh Poimboeuf , linux-kernel@vger.kernel.org, Adrian Hunter , David Laight , Numfor Mbiziwo-Tiapo Subject: Re: [PATCH v4] x86/insn, tools/x86: Fix some potential undefined behavior. Message-Id: <20210925133944.a0648549c28b047bd9aeaeff@kernel.org> In-Reply-To: References: <20210923161843.751834-1-irogers@google.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 24 Sep 2021 16:02:33 -0300 Arnaldo Carvalho de Melo wrote: > Em Thu, Sep 23, 2021 at 09:18:43AM -0700, Ian Rogers escreveu: > > From: Numfor Mbiziwo-Tiapo > > > > Don't perform unaligned loads in __get_next and __peek_nbyte_next as > > these are forms of undefined behavior. > > > > These problems were identified using the undefined behavior sanitizer > > (ubsan) with the tools version of the code and perf test. Part of this > > patch was previously posted here: > > https://lore.kernel.org/lkml/20190724184512.162887-4-nums@google.com/ > > Masami, if you're ok, just process it including the tools/ bit. Hi Arnaldo, This version updates the tools/ too, so I think this is OK. (do I need re-Ack?) Thank you, > > - Arnaldo > > > v4. Fixes a typo. > > > > v3. Is a rebase picking up a fix for big endian architectures. > > > > v2. removes the validate_next check and merges the 2 changes into one as > > requested by Masami Hiramatsu > > > > Signed-off-by: Ian Rogers > > Signed-off-by: Numfor Mbiziwo-Tiapo > > Acked-by: Masami Hiramatsu > > --- > > arch/x86/lib/insn.c | 4 ++-- > > tools/arch/x86/lib/insn.c | 4 ++-- > > 2 files changed, 4 insertions(+), 4 deletions(-) > > > > diff --git a/arch/x86/lib/insn.c b/arch/x86/lib/insn.c > > index 058f19b20465..c565def611e2 100644 > > --- a/arch/x86/lib/insn.c > > +++ b/arch/x86/lib/insn.c > > @@ -37,10 +37,10 @@ > > ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr) > > > > #define __get_next(t, insn) \ > > - ({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); leXX_to_cpu(t, r); }) > > + ({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); leXX_to_cpu(t, r); }) > > > > #define __peek_nbyte_next(t, insn, n) \ > > - ({ t r = *(t*)((insn)->next_byte + n); leXX_to_cpu(t, r); }) > > + ({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); leXX_to_cpu(t, r); }) > > > > #define get_next(t, insn) \ > > ({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); }) > > diff --git a/tools/arch/x86/lib/insn.c b/tools/arch/x86/lib/insn.c > > index c41f95815480..797699462cd8 100644 > > --- a/tools/arch/x86/lib/insn.c > > +++ b/tools/arch/x86/lib/insn.c > > @@ -37,10 +37,10 @@ > > ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr) > > > > #define __get_next(t, insn) \ > > - ({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); leXX_to_cpu(t, r); }) > > + ({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); leXX_to_cpu(t, r); }) > > > > #define __peek_nbyte_next(t, insn, n) \ > > - ({ t r = *(t*)((insn)->next_byte + n); leXX_to_cpu(t, r); }) > > + ({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); leXX_to_cpu(t, r); }) > > > > #define get_next(t, insn) \ > > ({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); }) > > -- > > 2.33.0.464.g1972c5931b-goog > > -- > > - Arnaldo -- Masami Hiramatsu