Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp2272889pxb; Fri, 8 Oct 2021 04:41:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpOjyEFReP8ouM4xlp1HIufzuImCZSLDbqiF3YHP7CW+U6kxtVEqubEiVPlAnDKwaqo3FV X-Received: by 2002:a17:90b:3851:: with SMTP id nl17mr11157640pjb.12.1633693292685; Fri, 08 Oct 2021 04:41:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633693292; cv=none; d=google.com; s=arc-20160816; b=pxmU5/IIgKuhJvjmUI9KwXa7ppmL0ft3dQ5LiXmE8TPHCMdrC1hPj2HAsuUxUYRC97 7uVqijOr+L9myCdZ1/AWWcBWsXfKuwWQiuknHxUK48HKczx2OwDchK7gFxdUDD+/O/pC rSdVr3GHe5CFCaE6uhJoFFjb4c7j/I7zV/hMl+eo7xwJ6xe7efeFHbgivojmotURh1fn hLs7u6yMIJPKUlg0e7xMPxDHnoJGGS+kD0ojQZU+djV/CPGIuzzY/vnfB1R+O+htx773 Ku4T9LlWP0WGn9fwF0olnBPKkqoDbwSFADhxPjqTKiWkTol0H2bX/Y7PadFHFue+910V 2Zqg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kpol/XZvuvAp0BR+gK17byGhp5JZljxGNNKnNrr4lLc=; b=PXa8x3BBDKNstDdoP29pD2D/LY6c+GOtluaWuSzZei1rOqb547mYzhPU8thIvz71rg D1dOItriVYXjpAUg7tVr6E/Zrl8gWK2tkHieem/uCgN9JJcbfWGVq9y8WxF2sX4wVCMk RjoRAwimjAZgWUIt2dd5SHG2qFEe7EQeB8iWj0yK5RnakmBr8Cf4+G7/sAHbWPODsYlE PN5wD3XFxYBqJfFnc8heq4S7K8di27bN1usibnQjEpzIYAwsGRHHTEQfww29kSwLbkDI EwsPD8FVTNXY5Xggu5fWWiDiY5HClfFziu6lgu5iINiMsyV9b6FY9hiXqO+IDcPmL2bj KKUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="U/MbRydj"; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oo13si4550342pjb.120.2021.10.08.04.41.18; Fri, 08 Oct 2021 04:41:32 -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=@linuxfoundation.org header.s=korg header.b="U/MbRydj"; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242625AbhJHLmd (ORCPT + 99 others); Fri, 8 Oct 2021 07:42:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:39486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243255AbhJHLjG (ORCPT ); Fri, 8 Oct 2021 07:39:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CFE71615E0; Fri, 8 Oct 2021 11:33:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633692797; bh=CEQN41l6ZcHJHjaCHHfGMtXSoAkYU/pYnEf/L9HH98w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U/MbRydj3M2mTrCYHQovtNBpdmPRI77IWj+PUEnZhk20IouWej/pUR9kqYLS4O4Jb pB1nT5RN4EO6INQ+DnULrFTP88ZD/BCSjwgQ+78KCqTN3S2pdiaJ7hjqXfXIhLq59H aLDPAKxyuAWaRYqhVXnDC2Mds2K9HaJj5Y7IOgyY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Numfor Mbiziwo-Tiapo , Ian Rogers , Borislav Petkov , Masami Hiramatsu , Sasha Levin Subject: [PATCH 5.14 35/48] x86/insn, tools/x86: Fix undefined behavior due to potential unaligned accesses Date: Fri, 8 Oct 2021 13:28:11 +0200 Message-Id: <20211008112721.194413986@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211008112720.008415452@linuxfoundation.org> References: <20211008112720.008415452@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Numfor Mbiziwo-Tiapo [ Upstream commit 5ba1071f7554c4027bdbd712a146111de57918de ] Don't perform unaligned loads in __get_next() and __peek_nbyte_next() as these are forms of undefined behavior: "A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined." (from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) These problems were identified using the undefined behavior sanitizer (ubsan) with the tools version of the code and perf test. [ bp: Massage commit message. ] Signed-off-by: Numfor Mbiziwo-Tiapo Signed-off-by: Ian Rogers Signed-off-by: Borislav Petkov Acked-by: Masami Hiramatsu Link: https://lkml.kernel.org/r/20210923161843.751834-1-irogers@google.com Signed-off-by: Sasha Levin --- 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