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=-11.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, NICE_REPLY_A,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 061E2C433E7 for ; Fri, 16 Oct 2020 01:33:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A78E62083B for ; Fri, 16 Oct 2020 01:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602812017; bh=Gyq0Uk2XEfl9xmWYnLkSM9zvUTO4aNfnfhPhd5Y8Dyw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:List-ID:From; b=ejs96s3G+9Zjo5hQYyhR355q9fkSza8RD+07U+2WRK1bBIkLCyYvHdaHOdZTHVdeM KTVqM2zKKvz6IzdbG0ELqZeg4HqUXdt091ls+iNz6KmELRKeE3ZFeW60E0v9X+z2J4 lyPtEoYeIVb0yNe1olc6zMnHcr6szF8dqKU0aC78= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389091AbgJPBdg (ORCPT ); Thu, 15 Oct 2020 21:33:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:39050 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388978AbgJPBdg (ORCPT ); Thu, 15 Oct 2020 21:33:36 -0400 Received: from devnote2 (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CC97120760; Fri, 16 Oct 2020 01:33:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602812015; bh=Gyq0Uk2XEfl9xmWYnLkSM9zvUTO4aNfnfhPhd5Y8Dyw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=1Mlbr9r5bcAnUqGUfWwMD8DGKhMJgxyOMR+rHZeKITFbo1+QN6xLqfguFM4Au1Ffa x7WVxZVJFg2GS/N1H9LufkM09Jo4ykEZBVbBg4Lu61FnoV6qMy8jBXwQgKLEmzoMEY oiJdHOWokM8FXajrQsmzrA/Zp+s3sZuqD2NbOnGI= Date: Fri, 16 Oct 2020 10:33:30 +0900 From: Masami Hiramatsu To: Ian Rogers Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , "Peter Zijlstra (Intel)" , Josh Poimboeuf , linux-kernel@vger.kernel.org, Adrian Hunter , Arnaldo Carvalho de Melo , Numfor Mbiziwo-Tiapo Subject: Re: [PATCH v2] x86/insn, tools/x86: Fix some potential undefined behavior. Message-Id: <20201016103330.668783f6d4e19ec2ac2ca1f8@kernel.org> In-Reply-To: <20201015161216.1563600-1-irogers@google.com> References: <20201015161216.1563600-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 Thu, 15 Oct 2020 09:12:16 -0700 Ian Rogers wrote: > 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/ > > v2. removes the validate_next check and merges the 2 changes into one as > requested by Masami Hiramatsu Looks good to me. This may be OK on x86, but now this code will be run on other arches for cross compilation. Acked-by: Masami Hiramatsu Thank you, > > Signed-off-by: Ian Rogers > Signed-off-by: Numfor Mbiziwo-Tiapo > --- > 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 404279563891..be88ab250146 100644 > --- a/arch/x86/lib/insn.c > +++ b/arch/x86/lib/insn.c > @@ -20,10 +20,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); r; }) > + ({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); r; }) > > #define __peek_nbyte_next(t, insn, n) \ > - ({ t r = *(t*)((insn)->next_byte + n); r; }) > + ({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(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 0151dfc6da61..92358c71a59e 100644 > --- a/tools/arch/x86/lib/insn.c > +++ b/tools/arch/x86/lib/insn.c > @@ -20,10 +20,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); r; }) > + ({ t r; memcpy(&r, insn->next_byte, sizeof(t)); insn->next_byte += sizeof(t); r; }) > > #define __peek_nbyte_next(t, insn, n) \ > - ({ t r = *(t*)((insn)->next_byte + n); r; }) > + ({ t r; memcpy(&r, (insn)->next_byte + n, sizeof(t)); r; }) > > #define get_next(t, insn) \ > ({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); }) > -- > 2.28.0.1011.ga647a8990f-goog > -- Masami Hiramatsu