Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp435278ybi; Fri, 7 Jun 2019 10:26:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqz9mF6tSfxFcCAdty7zbENV4zkvlubMzCMhT26z1GCsLIG7widD2wetdPclUgCsQkxgqfCY X-Received: by 2002:aa7:8145:: with SMTP id d5mr61069824pfn.11.1559928404670; Fri, 07 Jun 2019 10:26:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559928404; cv=none; d=google.com; s=arc-20160816; b=y8EjtDbZrYZnus0Jh87mu+g+dZTv9XG4c9IeJCSNqqvs1HHnOkZDEB1qV+L//E5ubx NiiFCotZ0U9Vnm6+30r/D17mGMlcItKmGbg30ukYGEr3r/be4rnR6JRqOhj4sstB4Bpb 3ABJjJa7zoDMDEzbLNXvL28x4RV6+asOZH0Egc1VsEyIIx7120ex3iY8F0/OqhCadI6u 3LmEtGcOLoAH+OysZl/uZ8T0+xwoGmca45A2o7NoP3HW3BUfyW+3AcL98pg6IlknzeHV 7wGwQT4cGYD1DjvS6n80d/e/Ug+PW5qrUdLc5i/+P4Vfp6oAod8qrfzciF43q6Mwcsnx Dl0Q== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=X3narSNUucvLkj/KyQ54MIyFCvIpHxas2+e5RIi5SOc=; b=RPykrMvQf9OH8aB9BPU4na8gemZiRaySibMFUQ5Ja0ygkNjXVVcVFabfzi6P+PPOlf pAnvlyGkcTKgG+ZC1KDzUuELCYDAU8j4H+V2Mn98KbrlmQP+VNH2wb9MfPhebwn55YaU TdfKFBCzjHFrVuL/qW1jM3e560wA98qucwu2dR7l/hlAQJmEeM32dWzmMFVZhBKujY5j 29KsYDJ5kgA/1IRrky8b17c2G7/9duagkwh/lTqPcoqvcVmTZkW5RNVfEvlT/UuhgN2i rhsD+HEn+WIBJ9AwnrHXScvcWpd+FcREaJrlu/48nIam3fmocEJCn5a48rkIC3rEVTML ybtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wTzmSkJg; 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 o17si2591754pge.519.2019.06.07.10.26.28; Fri, 07 Jun 2019 10:26:44 -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=@kernel.org header.s=default header.b=wTzmSkJg; 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 S1730190AbfFGPlA (ORCPT + 99 others); Fri, 7 Jun 2019 11:41:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:50454 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730178AbfFGPk6 (ORCPT ); Fri, 7 Jun 2019 11:40:58 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5161B21473; Fri, 7 Jun 2019 15:40:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559922057; bh=KlwBN7wTpPzY3aeol3PGzhNPWo4vwzk6cXWEBS4kLsQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wTzmSkJgTpJ7OYojBoHplDOTXH0Lm1vrC+/a63VFzGJ7PsLU41/AWgr6sR92h+l2k suFCQemEyILexTTBSWqSXHWhTFIaIukMp9rRgyywdeVp9ZiyyKhATr1f6iAhf2ZVN7 eNk+mfdQH8Nd92qhqct/m6sVCVt4OI/XYOR3NQl0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Rasmus Villemoes , Ido Schimmel , Will Deacon , Vadim Pasternak , Andrey Ryabinin , Jacek Anaszewski , Pavel Machek , Andrew Morton , Linus Torvalds , Matthias Kaehlcke Subject: [PATCH 4.14 22/69] include/linux/bitops.h: sanitize rotate primitives Date: Fri, 7 Jun 2019 17:39:03 +0200 Message-Id: <20190607153851.091848129@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190607153848.271562617@linuxfoundation.org> References: <20190607153848.271562617@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 Signed-off-by: Greg Kroah-Hartman --- include/linux/bitops.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -59,7 +59,7 @@ static __always_inline unsigned long hwe */ static inline __u64 rol64(__u64 word, unsigned int shift) { - return (word << shift) | (word >> (64 - shift)); + return (word << (shift & 63)) | (word >> ((-shift) & 63)); } /** @@ -69,7 +69,7 @@ static inline __u64 rol64(__u64 word, un */ static inline __u64 ror64(__u64 word, unsigned int shift) { - return (word >> shift) | (word << (64 - shift)); + return (word >> (shift & 63)) | (word << ((-shift) & 63)); } /** @@ -79,7 +79,7 @@ static inline __u64 ror64(__u64 word, un */ static inline __u32 rol32(__u32 word, unsigned int shift) { - return (word << shift) | (word >> ((-shift) & 31)); + return (word << (shift & 31)) | (word >> ((-shift) & 31)); } /** @@ -89,7 +89,7 @@ static inline __u32 rol32(__u32 word, un */ static inline __u32 ror32(__u32 word, unsigned int shift) { - return (word >> shift) | (word << (32 - shift)); + return (word >> (shift & 31)) | (word << ((-shift) & 31)); } /** @@ -99,7 +99,7 @@ static inline __u32 ror32(__u32 word, un */ static inline __u16 rol16(__u16 word, unsigned int shift) { - return (word << shift) | (word >> (16 - shift)); + return (word << (shift & 15)) | (word >> ((-shift) & 15)); } /** @@ -109,7 +109,7 @@ static inline __u16 rol16(__u16 word, un */ static inline __u16 ror16(__u16 word, unsigned int shift) { - return (word >> shift) | (word << (16 - shift)); + return (word >> (shift & 15)) | (word << ((-shift) & 15)); } /** @@ -119,7 +119,7 @@ static inline __u16 ror16(__u16 word, un */ static inline __u8 rol8(__u8 word, unsigned int shift) { - return (word << shift) | (word >> (8 - shift)); + return (word << (shift & 7)) | (word >> ((-shift) & 7)); } /** @@ -129,7 +129,7 @@ static inline __u8 rol8(__u8 word, unsig */ static inline __u8 ror8(__u8 word, unsigned int shift) { - return (word >> shift) | (word << (8 - shift)); + return (word >> (shift & 7)) | (word << ((-shift) & 7)); } /**