From patchwork Wed Aug 1 21:25:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10553047 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E962213BF for ; Wed, 1 Aug 2018 21:26:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D79B22A4BD for ; Wed, 1 Aug 2018 21:26:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C95492B603; Wed, 1 Aug 2018 21:26:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A9C92A4BD for ; Wed, 1 Aug 2018 21:26:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732285AbeHAXNb (ORCPT ); Wed, 1 Aug 2018 19:13:31 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:33650 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731956AbeHAXNb (ORCPT ); Wed, 1 Aug 2018 19:13:31 -0400 Received: by mail-pl0-f66.google.com with SMTP id 6-v6so32036plb.0 for ; Wed, 01 Aug 2018 14:25:47 -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:in-reply-to:references; bh=y9gTulMBBTJUGk4SgomdwgBXUzuch8+LlOsBDLCewZM=; b=j/jNjm5JRFOT94MonXpmYjJKgZir+FR3iMvTkV6k0nOKrMPMDe2R3AZKSeg6I15yPq +6BMwMwyZc+vSxqyl4sGXOw3/QViDrrYHJ6IYnJMLLjfKSCozDF7CvXSMcZybUWfI3+O OkC9W4AUbIpRfmFKEoAYB2Buq1mj7ntD4SUH0= 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:in-reply-to :references; bh=y9gTulMBBTJUGk4SgomdwgBXUzuch8+LlOsBDLCewZM=; b=IBa5I89XSoHx8Lu366TkVSZA7XfgxFErBimykKXheTepLT7u/+llbirrIJBNVkAMx5 QEUnj3iLjtVyMF4+h/E5xUcLwdNEfRUxiwH5m7gz7JTGlpfOK8onET6GYAxE8dz+wPfc 25z1wgjUjmSBLABLc0MGI7NSmjWVXm19PWqVVtI8S75pSaIAIKXQUwCS8fATwlW5LgWg IGMNKJVf6mIWWoAZfSTtdAYZy33/j/LuVUbFytkgT7BQog3hPrEzjdlCD9RIlX3M69KV IX6M5V9ZEseQCc9wm/k/2dhGx04TKcsJLbiinju0ruqe6ZvBnlY4mvMI/Z0Aa8oUcJI/ AGEQ== X-Gm-Message-State: AOUpUlEtOxkZxnvyrAzGxCtWQSCJQqUT+ihU7l/q5qn3An1Fw3MLe71d 39SJzsW4Tnx4qIwb1T7M7vajWg== X-Google-Smtp-Source: AAOMgpdDAYXDfZEss0ZXGDMzIDpNnyKoPKWUZEtfZuJi9PA1TdpJfu7LelCM4irW85ma0rpRTFBykQ== X-Received: by 2002:a17:902:ab95:: with SMTP id f21-v6mr37594plr.264.1533158747265; Wed, 01 Aug 2018 14:25:47 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id a15-v6sm43167pfe.32.2018.08.01.14.25.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 01 Aug 2018 14:25:45 -0700 (PDT) From: Kees Cook To: Jason Gunthorpe Cc: Kees Cook , Leon Romanovsky , Rasmus Villemoes , Leon Romanovsky , Bart Van Assche , Doug Ledford , Dan Carpenter , Randy Dunlap , linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH rdma-next v4 1/3] overflow.h: Add arithmetic shift helper Date: Wed, 1 Aug 2018 14:25:39 -0700 Message-Id: <20180801212541.22889-2-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180801212541.22889-1-keescook@chromium.org> References: <20180801212541.22889-1-keescook@chromium.org> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe 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 --- include/linux/overflow.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 8712ff70995f..40b48e2133cb 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -202,6 +202,37 @@ #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); \ +}) + /** * array_size() - Calculate size of 2-dimensional array. *