Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp670244ybz; Fri, 1 May 2020 06:32:31 -0700 (PDT) X-Google-Smtp-Source: APiQypJW4WHunPu183AzQ/z+e4bWyAtG1rMwu8CnODyHQENwj6fWgho4BGxyulH1fYKXGUamnbe1 X-Received: by 2002:aa7:d606:: with SMTP id c6mr3587059edr.107.1588339951603; Fri, 01 May 2020 06:32:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588339951; cv=none; d=google.com; s=arc-20160816; b=iUh9mICZfGxlVA9lcV/BzjpiNBFRoEcV3tNB2SonmyEoRUfwmYhKyt/0q6F++M3VDY z8NbScOUFh/0+BggqXebpUHM++sAhp6YsSMOUJsMVhtVoX0CBMmocHKIz6vnLDlmRpO8 Zru/YTZfwCuv+ImMRKWq2n9ruYoazvmN2++ltPllzPWRqIuudVtm6pzvDGiUGuNmwXF7 2qt1RpjTiIAaA7SNPsLMFjV8n0csRWppC0MZ8J9zAYtYZa5JMSkuFymMS5cfdg/mHazN FDG43IXpo6XaHvsHscaqz05TTb43g/xdwT9Pk2L1j2XZyb5NT87ZpK8kpzv54kD+nN2Z 9lDQ== 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=qtyWlxc3to/ABxb10wqIzySRthnkovAsih12KQNSvqQ=; b=VKWTUP5LW/754rkUiREbsgK0xvLMyHOTZpBP9EERhwrY2XddE9nSs+nqzukcDiNRcV HV58qKYClJgnpLAa+6D3ehodnsHC3VLgAoK1tqg1bY51uCMpVqMIscm7LbOrFg0LWNKr /vkvXDuVv5PqbxOP2vDoCzboF3xC1AHwm/z8rFP3bSF1gtuGpplM72lTlNSkyKNe1pRo 57I9Bqam1PbYBFV6P0OLyk7T416fUODtTQWl0ip/hDZCfKxSDZVyY94ZVqmHpqYRLYKi BpcbNzTTzcNvs0jP98RCyPbqkniHvxPuDiQi4odDP9mfWUUYtMbxbEH9o4JlNsc6CHsf 6z5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Y4NhUao0; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i13si1732895ejh.439.2020.05.01.06.32.08; Fri, 01 May 2020 06:32:31 -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=@kernel.org header.s=default header.b=Y4NhUao0; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729780AbgEAN31 (ORCPT + 99 others); Fri, 1 May 2020 09:29:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:52904 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729771AbgEAN3Y (ORCPT ); Fri, 1 May 2020 09:29:24 -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 09A63208D6; Fri, 1 May 2020 13:29:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588339763; bh=EjXI4xnL4Cjprb4Aa93w3O2TXHlJhQK22RWF6FH/Ry8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y4NhUao0BwI6I/ItMi5qLrRT5G3C9Pln2CqVj3n/kcNuzNA2XVRQlhDPDlrAQeA+Y CC6CKFhhsoZCp6NUJPjistvfRuD5Iwz7XSAsyVpawIqbyEA9tiEu9L+1Hqb1KTs/ZQ S198ZvisAhvnGA9Ptce4hl4ZUnqRV3bCRz275VLI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jason Gunthorpe , Leon Romanovsky , Kees Cook Subject: [PATCH 4.9 38/80] overflow.h: Add arithmetic shift helper Date: Fri, 1 May 2020 15:21:32 +0200 Message-Id: <20200501131525.845641274@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200501131513.810761598@linuxfoundation.org> References: <20200501131513.810761598@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: Jason Gunthorpe commit 0c66847793d1982d1083dc6f7adad60fa265ce9c upstream. Add shift_overflow() helper to assist driver authors in ensuring that shift operations don't cause overflows or other odd conditions. Signed-off-by: Jason Gunthorpe Signed-off-by: Leon Romanovsky [kees: tweaked comments and commit log, dropped unneeded assignment] Signed-off-by: Kees Cook Signed-off-by: Greg Kroah-Hartman --- include/linux/overflow.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -202,4 +202,35 @@ #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */ +/** check_shl_overflow() - Calculate a left-shifted value and check overflow + * + * @a: Value to be shifted + * @s: How many bits left to shift + * @d: Pointer to where to store the result + * + * Computes *@d = (@a << @s) + * + * Returns true if '*d' cannot hold the result or when 'a << s' doesn't + * make sense. Example conditions: + * - 'a << s' causes bits to be lost when stored in *d. + * - 's' is garbage (e.g. negative) or so large that the result of + * 'a << s' is guaranteed to be 0. + * - 'a' is negative. + * - 'a << s' sets the sign bit, if any, in '*d'. + * + * '*d' will hold the results of the attempted shift, but is not + * considered "safe for use" if false is returned. + */ +#define check_shl_overflow(a, s, d) ({ \ + typeof(a) _a = a; \ + typeof(s) _s = s; \ + typeof(d) _d = d; \ + u64 _a_full = _a; \ + unsigned int _to_shift = \ + _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \ + *_d = (_a_full << _to_shift); \ + (_to_shift != _s || *_d < 0 || _a < 0 || \ + (*_d >> _to_shift) != _a); \ +}) + #endif /* __LINUX_OVERFLOW_H */