Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2172286pxb; Fri, 24 Sep 2021 23:21:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyncdw5XhTsV2b02dVy5MKTg9U7MJew6492ExWzUekHcRB9s4ojj5wJnZYdtNQZecDfLaxG X-Received: by 2002:a5e:d618:: with SMTP id w24mr12272044iom.178.1632550869950; Fri, 24 Sep 2021 23:21:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632550869; cv=none; d=google.com; s=arc-20160816; b=NS3KrFik8uvIcP3eTruyI/SxHFB18JIhoul4vpNZ9SDgf4ylklvXwEZhVDRDklj3JR rJ6NE6AITZrpcfbBtO7uCrmJinjBXnMEQxG7NJ7+n3qj14SlcfsN14hBzUtQqs+6BSPW tCegcdMOT4Gi9+hZV8Z14Hrl7uUefzHUtmsYOsAtft9as0vYAcBAbUniFA1GFxE/yydT oZRodXJM93qrAgVuT1JYAjI+ynT4PKZqQfN7UY4wRhWmuqQZ6ZPh8neHTBYSWLi0jSzc JbnPk8+P+JSUhPTNfWh5s7pzfQOLmigw4q+MpMhue+xuvPD79jPjafc91+yvMj1YNGxJ e0tQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=cB2tCUEIW8iXGZnzhSQC0Kt5ScCAX/4k4a/3iNHpn64=; b=RcgTfjgbRlii+kdWpRvkFOOon8r+5vh485rmfI71X7HtA0RbboHE6pVQXFv88VdJzA Qx0EOWu9BElDF+4J3PNpBDWTS4DlR8gTd9lPYWlkj0cyXKsIBX4JBK/QEhNm3WYJCTuB hG1To5wczY4GAgnnrSHIvkd63PaQRKR8QuR9CTueLahahR4WmxHN3Btyg7362XZfTQRc AFNObpiFdU4UxTkvFT8ZbJvnA67vCWPdSMH7JgoS3HqTfnmQIQYirSHAtd+cYqVd+Ctz ylZTAXe/mJGypCLdhh00BXvrH5ikJ88x24VYEDIdYQtS4ndyul99+rDGw9bW0bPaQT/l FWKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MPFZfxfL; 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 a24si12375236ioc.90.2021.09.24.23.20.59; Fri, 24 Sep 2021 23:21:09 -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=MPFZfxfL; 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 S1346180AbhIXTEN (ORCPT + 99 others); Fri, 24 Sep 2021 15:04:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:50674 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346035AbhIXTEJ (ORCPT ); Fri, 24 Sep 2021 15:04:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 32BE060E97; Fri, 24 Sep 2021 19:02:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1632510156; bh=4NxIqTflfhfqaUe2RRhcz/Q/LBzaoiPooJpvQFfcryU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MPFZfxfLd/csjrEKgFH3vmou92f1gYHFt5SbX+JcGDiMjWrDWDhjU3TAoVa1Pe2uG yB/KAd1f6QnK0Kf+2gEUb+cTM9NwLpUEZJV3PeA6FlGjRrFcPw+2Z9DNplNcpKfFqn IQlr2jyMTeUY/PyAK7IJN4179R9pOc6RLCF8Tq8PqVr7fX768aF7p6Z5JPnvdOcbVW /Yg/W0TtJMYXgDegI7jOQszqFLq4bkAc2tR3jknESXgVThMi7HiDoqbjUDIdAsvYC6 pH70cixcpBM2NZfxq/1wdf2fJyM9slnw3GEDLTVL9P8YuAcQacQQUBypB5sZQJyc/R bZuygnwbV9KKg== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id BB923410A1; Fri, 24 Sep 2021 16:02:33 -0300 (-03) Date: Fri, 24 Sep 2021 16:02:33 -0300 From: Arnaldo Carvalho de Melo To: Ian Rogers Cc: 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: References: <20210923161843.751834-1-irogers@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210923161843.751834-1-irogers@google.com> X-Url: http://acmel.wordpress.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. - 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