Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp5225642yba; Wed, 10 Apr 2019 14:21:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzU9jqnUU2aSkTESKUfAvV/HGLUPYnIXoybiYjS9/NryOOdDi1JAE33y+brND3BGmo9YPzD X-Received: by 2002:a65:414a:: with SMTP id x10mr43707142pgp.237.1554931293041; Wed, 10 Apr 2019 14:21:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554931293; cv=none; d=google.com; s=arc-20160816; b=IcnRf1FRKabVJusElIr8826/u43xN1D0+r0U7OSLSD3/fpHadAJPFb7jVX+mbm9keD yILOhOOu0ueFouL02An1Zle5GNbyE/2caNAT6706Ckscxx0Z85zHrTBlXCSM+SwCTj8M q9BkVsqRmH7i8dqjYPO9MD3qfq8S0EEFFZj4ozg7TXLNC0lrKM0vVAaoxExd4NGxFkrj RyEA8xVznVz76oh87sFLM3IIGDazDRfuoOeQ7Udqq/Kc9frbUcB9725H5KfikATiAQ1u 8K2WSZOvmfhF0biWfvSktqyr0i/XJmkoaWOg6oQ/GKNrtg/H1vyVQYR8JU54ETTo1ltT Dbgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=AQdyv44o0lMaGsIrdTDQzwqqE/OiS9gGi6TMQRjBRUk=; b=Ny9+z0I2cCRR8vtRKnY5PNlb0NGsfuWMdLQNi5rp82x2yMOHYjMnsgCkH1s9BwcyZT wHJdNTMDHDsoYi/SjXyY5TahV/EniZjN9xPZez1Aqu/PxMDihCpn95JjdpT2wlAz86uw rDn+usdgImvXl7Hbpml7UfcfYuDO3VEypifSZQvLMZzDCHD6NCrcRlS/6QIwG88TG05E v5CdtWvkz3ILklRLrVWVGDVkk+GFoSNxEiYHquC3SfqzDTA5TV5XsKtcKAntT4faAGey 8EG7kM5HDXXj/vx6Z/eTfBLTxECg+2tG5su7qbYOvbMwee4GbFygkSi8z1+4n8l7Zhae Fvrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=BBz++SQN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h7si26032664pgj.363.2019.04.10.14.21.16; Wed, 10 Apr 2019 14:21:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=BBz++SQN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726670AbfDJVTP (ORCPT + 99 others); Wed, 10 Apr 2019 17:19:15 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:41912 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726022AbfDJVTP (ORCPT ); Wed, 10 Apr 2019 17:19:15 -0400 Received: by mail-ed1-f66.google.com with SMTP id u2so3309841eds.8 for ; Wed, 10 Apr 2019 14:19:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AQdyv44o0lMaGsIrdTDQzwqqE/OiS9gGi6TMQRjBRUk=; b=BBz++SQNUnZ0EGaXv4WmfJwoV7K/q4gb20DYijx0E4QvVwiLqXutESPb3Ow0rwQPT+ y83bZHxSDBJBC/6naNdskwdrAhezX/R/P2BRl4/GfFnyH8LKG3qPLLs2lmsRv7WFuTi/ 4InjpR97BfMbnYHoJOT65Os1iAiF2mF6nrznE= 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=AQdyv44o0lMaGsIrdTDQzwqqE/OiS9gGi6TMQRjBRUk=; b=bURH/vUoY0vMfyir6rqyzLHas8vwiAibL/DsVvJwfVQ/veX2eJdOVnrN9CASCFva44 O/5oDDrjF3A5fmv7a2emnjQ5ZxPKLYoLudDZOv2+qmM75zykDf8YmQi/pUonbL8jB2GQ XcdO7Z5UtR5mWDaHHeBQPSlYx8lNhDVTcuNpi/k8btkxynBehDHwRzK6KsEMQtZA/Gs7 xCrj0M+QpwpjT3BbihTHrfc349a9WHULrdCd2scNZ58JwkGq6UMxft8H3VrbcQhEKXpq Qw0iDSsJoKpSRTGdYLpDiJEuzSL8l+qaosLvtYcLMYBe6k/eYF9J+5evN50YLGcNJkU6 ItVA== X-Gm-Message-State: APjAAAX8CvqTZ8/xs1U0R0mOId9Fo9kk+O+sYmMNRy5eitFsu49iBkDw aL3K39A3+PkpP3jULVfa8jfOwQ== X-Received: by 2002:a17:906:1856:: with SMTP id w22mr24951377eje.130.1554931153448; Wed, 10 Apr 2019 14:19:13 -0700 (PDT) Received: from prevas-ravi.prevas.se (ip-5-186-118-63.cgn.fibianet.dk. [5.186.118.63]) by smtp.gmail.com with ESMTPSA id d14sm1539386edj.57.2019.04.10.14.19.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Apr 2019 14:19:10 -0700 (PDT) From: Rasmus Villemoes To: Andrew Morton , Vineet Gupta , Will Deacon , Anthony Yznaga , Rasmus Villemoes Cc: Andrey Ryabinin , Pavel Machek , Ido Schimmel , Vadim Pasternak , linux-kernel@vger.kernel.org Subject: [PATCH] bitops.h: sanitize rotate primitives Date: Wed, 10 Apr 2019 23:19:06 +0200 Message-Id: <20190410211906.2190-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Reported-by: Ido Schimmel Cc: Vadim Pasternak Signed-off-by: Rasmus Villemoes --- 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 602af23b98c7..cf074bce3eb3 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -60,7 +60,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)); } /** @@ -70,7 +70,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)); } /** @@ -80,7 +80,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)); } /** @@ -90,7 +90,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)); } /** @@ -100,7 +100,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)); } /** @@ -110,7 +110,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)); } /** @@ -120,7 +120,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)); } /** @@ -130,7 +130,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.20.1