Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp508244pxu; Thu, 15 Oct 2020 09:14:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytdIgOaRgC5TJxFN3G9EWdKaGmVBpN3Vh6JwXoXrMmLlzTOWknBRQmuw8WZ26i2zGRAcBN X-Received: by 2002:a17:906:ae82:: with SMTP id md2mr5085075ejb.538.1602778479514; Thu, 15 Oct 2020 09:14:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602778479; cv=none; d=google.com; s=arc-20160816; b=GfYrm0kwzHgTd2PSIfjrb8omQXd5MRL+nFk3YlI14IrXkxDsJ47s3vkt4Cm4AS5DbS oVoPuN8T11xrrLV6BL+SNyGtEnQxLoGhjKBFTAqzSGf4O7jS+O6sdoFqt1vJpIimqnVy r+/zBsHUFJ91Q8sNm8zcB96WG3mRKueRBSLa4jr3CHlzjOqAUBMBJvTu02y6K6N+1nbq wWwS9cFfZZSJXqLPsKyxPDUlXDfWuW0ExRdPO4tv1T5VEj1hTnBBLHgqonmi5rK8rWmV L+ZZDd04nSMyKevF2Qlp0SOsF7kU2LYeYaTBkVzhbuhofaNAuPJ4471ajDCjkrtSxoln FjLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :sender:dkim-signature; bh=BdZDlc0VgpP2Ln7XZ/SJzkYEHvK/fd43ssyrqdMV8Lc=; b=D7hvK7wQrlLN53tBJXr39HkLahQhmBvWnZI4SZqKddYcxPknodJv2YDa1waZNIpAN1 rRVkWx3FJshNYE2J3ubWC5DUngBQSWcFDwENjvua+gbjr5T5YJndy/kTdFTrayRNMayo gtSII9HmofWgQRx09RUsGkgNCuke+KaeeBz6o28MnKIXG3yPuDCs6iGBRAp9dm9XsiTr HD+2NZdAI5aAGOQ6E6gDprp95xp46a6QfzNlAH9ea/6ypW0pwTbNLOfAU/sukScO433Q k8DyVLMl4Crn4/hOCPa6JT98G47D2ZH8v1dNKzUDAKvJbXAGjWY6UDUGPdIQXPsJNoXo 95TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mW6Dp+96; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h7si1581726ejc.443.2020.10.15.09.14.07; Thu, 15 Oct 2020 09:14:39 -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=@google.com header.s=20161025 header.b=mW6Dp+96; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388946AbgJOQMW (ORCPT + 99 others); Thu, 15 Oct 2020 12:12:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388461AbgJOQMW (ORCPT ); Thu, 15 Oct 2020 12:12:22 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09AD8C061755 for ; Thu, 15 Oct 2020 09:12:21 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id w126so2420809qka.5 for ; Thu, 15 Oct 2020 09:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=BdZDlc0VgpP2Ln7XZ/SJzkYEHvK/fd43ssyrqdMV8Lc=; b=mW6Dp+96INwoUu0vyVN9WtCsrfLyXMNru/NIX528PPsBLkjCGONPtRuIrkY5rqMuVi YPTIJms4p4xd2n5X3ZS5xAp47zvrv3sy+3ZsibOCcsBB1h2Nb9oLzrsQeKfNgXlD7EK9 XY7JBoE8kayWVwhbj7USCWhMw+b7kGKs51Z8ehZitV9gc/9YpR5eD8ktfIdqqhzm7+8i 3808CYJKywtpSXLuQ2nFgvoDHwE4UIE7At6lUy0Y2CCmGPGXNL9+mV6E7gTdeip2I6dU PHSx7IKdL6gmMh5JbICbWvf4vMbCfppO+r8pReIRMMcsNV8WtPjWi/OqYFVNWQpAWjnx w3Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=BdZDlc0VgpP2Ln7XZ/SJzkYEHvK/fd43ssyrqdMV8Lc=; b=fz4Fw0hl5DjV9T6RF2M4mg9FQ9DK6vXPUZW9g9Y8p1rfJv51kuscZEgrSezJzNKSEO f5e/cZkYm3OBr7KKzwi0M8EVQK3FNeKuuXe9PVlZ+E0KibVEF0xbwOIbdNABmSXZHrVp EOsvDZXxIDh62ttgGptDCK4C7JVdZd2RpG1PUMAsWoz62wy7WQ59jcqqlEbuHEZHcRSG y/fIfYIDstbNdkRtrySTlSeIfSLxo7Kf9nAxRLtyqBBOKjOVQpaFYxJz+pxLqjFcu9rr qKeWbSTy8qpCQ4tJG1Fh5qU0ceNbfWxMJ9OHVfhzkk4+EZQNFFahBuuPvAqjyKsdi46M SkVQ== X-Gm-Message-State: AOAM533I8HvpFq1CjPJeCGRGo4TarcjEGY3clW9pbBZPJ7jQch4zRl4O LRyOe5QAIcZ4QSZXVREPhfWvW2E8t5pE Sender: "irogers via sendgmr" X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:2:f693:9fff:fef4:4583]) (user=irogers job=sendgmr) by 2002:a0c:a345:: with SMTP id u63mr5556078qvu.17.1602778340129; Thu, 15 Oct 2020 09:12:20 -0700 (PDT) Date: Thu, 15 Oct 2020 09:12:16 -0700 Message-Id: <20201015161216.1563600-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [PATCH v2] x86/insn, tools/x86: Fix some potential undefined behavior. From: Ian Rogers To: 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 , Arnaldo Carvalho de Melo Cc: Numfor Mbiziwo-Tiapo , Ian Rogers Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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