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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4192DC433FE for ; Sat, 25 Sep 2021 04:39:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E31B6127A for ; Sat, 25 Sep 2021 04:39:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234446AbhIYEl0 (ORCPT ); Sat, 25 Sep 2021 00:41:26 -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