Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp841843pxu; Thu, 15 Oct 2020 19:03:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVVtYbw4SD7bHh6ljk4EiHtHhDi3MC9thFdymQI7YyhbBNuXS10eJz7Jc7eOLrS1I0Vuq4 X-Received: by 2002:a17:906:60d3:: with SMTP id f19mr1367174ejk.141.1602813829861; Thu, 15 Oct 2020 19:03:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602813829; cv=none; d=google.com; s=arc-20160816; b=OKg6gqnbtVm03j77y89saobzyWtX5GVehjBbSF74xj0mLXeQ15o+Lu8kRNR55EjC3Z 4agDRCaH90DjmP+T9Zzzn5pZd62yiTfwbwh8WGko6ZCrcTpxQLNfnn/WE6V9Rq3s54AJ TdI/y8YvL7LxSkA+n37SXUQN5goLP+agcYtxbMMyCw6eDyDdWFPPtYACMoCet0b0+x9E sKBIZI24fNHWVw64toLDFoeD4VKu3zfurlmLoXPHL7m3Xhel97JWx7WvDBLJmuS0if0f Rpl2p9nMJR2xikvnr814LEUCKHRiE3tOmKAZW9gAMRkdgxvb/IeXGAx0QKO1pQDkWh4e lThg== 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=E8XdpikzyRRcXO72r0jGo+e9VNdni9uGBVwBLT9waDM=; b=R0JzTvUHGckn3NAjC0fsrwQBEELq7N15DxOVPxtwQtPmSsHPe2XdmPWDIqqaab4icn srhOvHOOha9tP7cBY3P+7SBiK0deQJXTRdtPAkCJA2CpiPthe2y+FJgK5DLdDWd0tUhX 595RSAVCs8g85LoktbAZ+rvTVOshZaVw60uSN4RYLAobQP+sq0ODFJlE1eilp6DlWrVR Ss4dO9FrpqblkJnAraYmWrBHRVe6EupygXVzTUs3cjTz5IzgeK90i3+Rn1T+FIfH3H1R TYBkMvbxjfQ2i8vK+gASlj06ck/DxJTAQk8v4GoUfkrDxFbEJkxe3kz/sRzUx4oglRmj nOGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1Mlbr9r5; 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 fy1si647723ejb.649.2020.10.15.19.03.27; Thu, 15 Oct 2020 19:03:49 -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=default header.b=1Mlbr9r5; 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 S2389007AbgJPBdg (ORCPT + 99 others); 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