From patchwork Thu Dec 3 15:44:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matan Barak X-Patchwork-Id: 7761321 Return-Path: X-Original-To: patchwork-linux-rdma@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 70FEBBEEE1 for ; Thu, 3 Dec 2015 15:47:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8BF362047B for ; Thu, 3 Dec 2015 15:47:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A34AB20465 for ; Thu, 3 Dec 2015 15:47:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760652AbbLCPr0 (ORCPT ); Thu, 3 Dec 2015 10:47:26 -0500 Received: from [193.47.165.129] ([193.47.165.129]:39459 "EHLO mellanox.co.il" rhost-flags-FAIL-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1760651AbbLCPrY (ORCPT ); Thu, 3 Dec 2015 10:47:24 -0500 Received: from Internal Mail-Server by MTLPINE1 (envelope-from matanb@mellanox.com) with ESMTPS (AES256-SHA encrypted); 3 Dec 2015 17:46:49 +0200 Received: from rsws33.mtr.labs.mlnx (dev-r-vrt-064.mtr.labs.mlnx [10.212.64.1]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id tB3FknqB002430; Thu, 3 Dec 2015 17:46:49 +0200 From: Matan Barak To: Eli Cohen Cc: linux-rdma@vger.kernel.org, Doug Ledford , Eran Ben Elisha , Yann Droneaud , Matan Barak Subject: [PATCH for-next V1 2/5] IB/core: Add ib_is_udata_cleared Date: Thu, 3 Dec 2015 17:44:20 +0200 Message-Id: <1449157463-25313-3-git-send-email-matanb@mellanox.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1449157463-25313-1-git-send-email-matanb@mellanox.com> References: <1449157463-25313-1-git-send-email-matanb@mellanox.com> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Extending core and vendor verb commands require us to check that the unknown part of the user's given command is all zeros. Adding ib_is_udata_cleared in order to do so. Signed-off-by: Matan Barak Reviewed-by: Moshe Lazer --- include/rdma/ib_verbs.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h index 31fb409..0ad89e3 100644 --- a/include/rdma/ib_verbs.h +++ b/include/rdma/ib_verbs.h @@ -1947,6 +1947,73 @@ static inline int ib_copy_to_udata(struct ib_udata *udata, void *src, size_t len return copy_to_user(udata->outbuf, src, len) ? -EFAULT : 0; } +#define IB_UDATA_ELEMENT_CLEARED(type, ptr, len, expected) \ + ({type v; \ + typeof(ptr) __ptr = ptr; \ + \ + ptr = (void *)ptr + sizeof(type); \ + len -= sizeof(type); \ + !copy_from_user(&v, __ptr, sizeof(v)) && (v == expected); }) + +static inline bool ib_is_udata_cleared(struct ib_udata *udata, + u8 cleared_char, + size_t offset, + size_t len) +{ + const void __user *p = udata->inbuf + offset; +#ifdef CONFIG_64BIT + u64 expected = cleared_char; +#else + u32 expected = cleared_char; +#endif + + if (len > USHRT_MAX) + return false; + + if (len && (uintptr_t)p & 1) + if (!IB_UDATA_ELEMENT_CLEARED(u8, p, len, expected)) + return false; + + expected = expected << 8 | expected; + if (len >= 2 && (uintptr_t)p & 2) + if (!IB_UDATA_ELEMENT_CLEARED(u16, p, len, expected)) + return false; + + expected = expected << 16 | expected; +#ifdef CONFIG_64BIT + if (len >= 4 && (uintptr_t)p & 4) + if (!IB_UDATA_ELEMENT_CLEARED(u32, p, len, expected)) + return false; + + expected = expected << 32 | expected; +#define IB_UDATA_CLEAR_LOOP_TYPE u64 +#else +#define IB_UDATA_CLEAR_LOOP_TYPE u32 +#endif + while (len >= sizeof(IB_UDATA_CLEAR_LOOP_TYPE)) + if (!IB_UDATA_ELEMENT_CLEARED(IB_UDATA_CLEAR_LOOP_TYPE, p, len, + expected)) + return false; + +#ifdef CONFIG_64BIT + expected = expected >> 32; + if (len >= 4 && (uintptr_t)p & 4) + if (!IB_UDATA_ELEMENT_CLEARED(u32, p, len, expected)) + return false; +#endif + expected = expected >> 16; + if (len >= 2 && (uintptr_t)p & 2) + if (!IB_UDATA_ELEMENT_CLEARED(u16, p, len, expected)) + return false; + + expected = expected >> 8; + if (len) + if (!IB_UDATA_ELEMENT_CLEARED(u8, p, len, expected)) + return false; + + return true; +} + /** * ib_modify_qp_is_ok - Check that the supplied attribute mask * contains all required attributes and no attributes not allowed for