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=-9.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,T_DKIMWL_WL_HIGH,URIBL_BLOCKED, USER_AGENT_GIT 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 07EA6C04AB5 for ; Mon, 3 Jun 2019 18:50:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CCFAD26E5A for ; Mon, 3 Jun 2019 18:50:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="HwEsH4E4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726112AbfFCSuX (ORCPT ); Mon, 3 Jun 2019 14:50:23 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46593 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726055AbfFCSuX (ORCPT ); Mon, 3 Jun 2019 14:50:23 -0400 Received: by mail-pg1-f196.google.com with SMTP id v9so8769089pgr.13 for ; Mon, 03 Jun 2019 11:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=2Xopmdxw630udg8sc3WFoukd+r5XBzX5qUOtBDIPHjI=; b=HwEsH4E4OqacLS9nL2wxeBGHw/sTvuB7qRWQKfGmoIBgg43ue4GHUkpkQjO9f17D4Y cjaFnx5qPIX3WHoztruDoI63uW4BDV4eDKr9DcCRYQO3B0UPLPqwZQU7yqcDlPPEdlPb MpMt5mpZQCHEOju38TirhrKRbl/Cj/Px1jQqs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=2Xopmdxw630udg8sc3WFoukd+r5XBzX5qUOtBDIPHjI=; b=r/YQ2Qu/MHrJMuk6daMxgMAT3K9GCzV8VJYN/BYPAcA4uCTZuSWBPMktISmUtzVOPD DQbft/EBvUxNd5eTG0jd7ryTgoBHuz3816h2Jef7GU8baJG4bHppWNY2uQnzV0vXkQ2c V6hvyDKrQirA6oQdBXHeFoPKfuua0ROLyy4wQdbEDk2IsA/x1L858cfhgoL6IErh06jI 4SRb8I0t0NjnnNTsvL2LxgR1q3yBsVcNkVpG4Nw4WrtMEDlZZ8ZvqhqW+mWB2pXYkFrD 9UXKssOz7O2FGSO6Zu1iMPWWWgARn0/CwKMGwlBCLoz7asxcqGiX5DNgAqZgFnsHbeRC L5ig== X-Gm-Message-State: APjAAAWk8GWk56uzyYrOXZ/MnYWe3Fowp4mCHAuN/T2EFzGYn/LXw56F e5Mkibjwz5GdUI0pV2aAlryCTw== X-Google-Smtp-Source: APXvYqzUGScpnRCh0F7iBXFTNWvOCU1e+3zVwqagUPqg0MBVDCf7q/cYYPI9T3xTmSQb5EK5Emebnw== X-Received: by 2002:a62:ed1a:: with SMTP id u26mr6293453pfh.229.1559587822316; Mon, 03 Jun 2019 11:50:22 -0700 (PDT) Received: from localhost ([2620:15c:202:1:75a:3f6e:21d:9374]) by smtp.gmail.com with ESMTPSA id l12sm12750412pgq.26.2019.06.03.11.50.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Jun 2019 11:50:21 -0700 (PDT) From: Matthias Kaehlcke To: Greg Kroah-Hartman , Sasha Levin Cc: stable@vger.kernel.org, clang-built-linux@googlegroups.com, Nick Desaulniers , Rasmus Villemoes , Ido Schimmel , Will Deacon , Vadim Pasternak , Andrey Ryabinin , Jacek Anaszewski , Pavel Machek , Andrew Morton , Linus Torvalds , Matthias Kaehlcke Subject: [PATCH 4.14] include/linux/bitops.h: sanitize rotate primitives Date: Mon, 3 Jun 2019 11:50:15 -0700 Message-Id: <20190603185015.45362-1-mka@chromium.org> X-Mailer: git-send-email 2.22.0.rc1.311.g5d7573a151-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Rasmus Villemoes commit ef4d6f6b275c498f8e5626c99dbeefdc5027f843 upstream The ror32 implementation (word >> shift) | (word << (32 - shift) has undefined behaviour if shift is outside the [1, 31] range. Similarly for the 64 bit variants. Most callers pass a compile-time constant (naturally in that range), but there's an UBSAN report that these may actually be called with a shift count of 0. Instead of special-casing that, we can make them DTRT for all values of shift while also avoiding UB. For some reason, this was already partly done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes these patterns as rotates, so for example __u32 rol32(__u32 word, unsigned int shift) { return (word << (shift & 31)) | (word >> ((-shift) & 31)); } compiles to 0000000000000020 : 20: 89 f8 mov %edi,%eax 22: 89 f1 mov %esi,%ecx 24: d3 c0 rol %cl,%eax 26: c3 retq Older compilers unfortunately do not do as well, but this only affects the small minority of users that don't pass constants. Due to integer promotions, ro[lr]8 were already well-defined for shifts in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16] (only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set, word << 16 is undefined). For consistency, update those as well. Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk Signed-off-by: Rasmus Villemoes Reported-by: Ido Schimmel Tested-by: Ido Schimmel Reviewed-by: Will Deacon Cc: Vadim Pasternak Cc: Andrey Ryabinin Cc: Jacek Anaszewski Cc: Pavel Machek Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Matthias Kaehlcke --- Hi Greg and Sasha, Please pick this patch for 4.14. Issues were observed on arm32 with newer kernels built with clang for rotations with a shift count of zero. Thanks Matthias --- include/linux/bitops.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index d03c5dd6185d..fc64a43690f8 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -79,7 +79,7 @@ static __always_inline unsigned long hweight_long(unsigned long w) */ static inline __u64 rol64(__u64 word, unsigned int shift) { - return (word << shift) | (word >> (64 - shift)); + return (word << (shift & 63)) | (word >> ((-shift) & 63)); } /** @@ -89,7 +89,7 @@ static inline __u64 rol64(__u64 word, unsigned int shift) */ static inline __u64 ror64(__u64 word, unsigned int shift) { - return (word >> shift) | (word << (64 - shift)); + return (word >> (shift & 63)) | (word << ((-shift) & 63)); } /** @@ -99,7 +99,7 @@ static inline __u64 ror64(__u64 word, unsigned int shift) */ static inline __u32 rol32(__u32 word, unsigned int shift) { - return (word << shift) | (word >> ((-shift) & 31)); + return (word << (shift & 31)) | (word >> ((-shift) & 31)); } /** @@ -109,7 +109,7 @@ static inline __u32 rol32(__u32 word, unsigned int shift) */ static inline __u32 ror32(__u32 word, unsigned int shift) { - return (word >> shift) | (word << (32 - shift)); + return (word >> (shift & 31)) | (word << ((-shift) & 31)); } /** @@ -119,7 +119,7 @@ static inline __u32 ror32(__u32 word, unsigned int shift) */ static inline __u16 rol16(__u16 word, unsigned int shift) { - return (word << shift) | (word >> (16 - shift)); + return (word << (shift & 15)) | (word >> ((-shift) & 15)); } /** @@ -129,7 +129,7 @@ static inline __u16 rol16(__u16 word, unsigned int shift) */ static inline __u16 ror16(__u16 word, unsigned int shift) { - return (word >> shift) | (word << (16 - shift)); + return (word >> (shift & 15)) | (word << ((-shift) & 15)); } /** @@ -139,7 +139,7 @@ static inline __u16 ror16(__u16 word, unsigned int shift) */ static inline __u8 rol8(__u8 word, unsigned int shift) { - return (word << shift) | (word >> (8 - shift)); + return (word << (shift & 7)) | (word >> ((-shift) & 7)); } /** @@ -149,7 +149,7 @@ static inline __u8 rol8(__u8 word, unsigned int shift) */ static inline __u8 ror8(__u8 word, unsigned int shift) { - return (word >> shift) | (word << (8 - shift)); + return (word >> (shift & 7)) | (word << ((-shift) & 7)); } /** -- 2.22.0.rc1.311.g5d7573a151-goog