Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3698666imm; Mon, 18 Jun 2018 02:31:47 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI4MTJIw6bVOEavr7jirr1bxXky7W7Ea6HDW6t4ohq4x1v3sWwzvo5/S1BCvZgMPxJLnUIq X-Received: by 2002:a63:24c4:: with SMTP id k187-v6mr10279525pgk.434.1529314307596; Mon, 18 Jun 2018 02:31:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529314307; cv=none; d=google.com; s=arc-20160816; b=cnrWncieqw0zeG//bDWt3OycZZi0plw8B0CGzsRa8BW8H05gS+F5yze/u5KkCY0tO/ vFdoiGednNFtw0Ng5ewo70R7XNRt0L/crhpUreVhW53ebNMDXi/kTaRsJckRMrValg5W aA44KJINW+jQebIoonu+Qy7zOrCEpafVTdYaFVkTSPA+chcrM7c8+YCsmjPuvOblhpaQ CKdd9IJ41jF4HBQ58CpaYiu+SVWJU8waxFNILPfSKISh6wF9Pa4rvzarrqbaF0IM2uFz wFOobz0LsT9zIoUZowEzKVMdAm+WHpp/6sXmIu5H0dklOG7TiJ9tFg3QyWZzUMLSwehj 62lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=X25NJ7UTMBoXC9r6waZPy0AzWsGoF79Ap/SHicRIjdI=; b=hK1yEyhEN8/PGnNI7sJLjTCa917j1uGCOkXBjyHGh4nLbDCR/wOsDnbz5wqbRllYM1 QEckU+2dpdJ5SJbwQywbbL5jgEr3WUsDFqns1++bvJ9o8SV/9bNr196nKUJu3cjjkX7f KvfYD2mNWLakPqatERnmLDvPz4vBzK2vOUmqQpmGZ8Q7iywzHnUnTepnIKOJLFJerUFH CfqSjGdvC7/Q78v6IxoExpOgEbV03MBSlZfCFg2MAIh1DoQsjvEhsBlsI3J6ippysSFF AUVQZrf7wLhdhC32HSLNNgjLQK/skN/E2yXyVUiHorFGOF/RUSI0wUnOoutr09r0EWrT djWQ== ARC-Authentication-Results: i=1; mx.google.com; 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 f7-v6si16217544plj.122.2018.06.18.02.31.34; Mon, 18 Jun 2018 02:31:47 -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; 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 S966005AbeFRJa4 (ORCPT + 99 others); Mon, 18 Jun 2018 05:30:56 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:56174 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935926AbeFRIZF (ORCPT ); Mon, 18 Jun 2018 04:25:05 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 3AC8FBAD; Mon, 18 Jun 2018 08:25:05 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jack Morgenstein , Leon Romanovsky , Doug Ledford , Sasha Levin Subject: [PATCH 4.16 176/279] IB/mlx4: Fix integer overflow when calculating optimal MTT size Date: Mon, 18 Jun 2018 10:12:41 +0200 Message-Id: <20180618080616.213734209@linuxfoundation.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180618080608.851973560@linuxfoundation.org> References: <20180618080608.851973560@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jack Morgenstein [ Upstream commit b03bcde962606d2ee59a4e9dd470db9ad53c5418 ] When the kernel was compiled using the UBSAN option, we saw the following stack trace: [ 1184.827917] UBSAN: Undefined behaviour in drivers/infiniband/hw/mlx4/mr.c:349:27 [ 1184.828114] signed integer overflow: [ 1184.828247] -2147483648 - 1 cannot be represented in type 'int' The problem was caused by calling round_up in procedure mlx4_ib_umem_calc_optimal_mtt_size (on line 349, as noted in the stack trace) with the second parameter (1 << block_shift) (which is an int). The second parameter should have been (1ULL << block_shift) (which is an unsigned long long). (1 << block_shift) is treated by the compiler as an int (because 1 is an integer). Now, local variable block_shift is initialized to 31. If block_shift is 31, 1 << block_shift is 1 << 31 = 0x80000000=-214748368. This is the most negative int value. Inside the round_up macro, there is a cast applied to ((1 << 31) - 1). However, this cast is applied AFTER ((1 << 31) - 1) is calculated. Since (1 << 31) is treated as an int, we get the negative overflow identified by UBSAN in the process of calculating ((1 << 31) - 1). The fix is to change (1 << block_shift) to (1ULL << block_shift) on line 349. Fixes: 9901abf58368 ("IB/mlx4: Use optimal numbers of MTT entries") Signed-off-by: Jack Morgenstein Signed-off-by: Leon Romanovsky Signed-off-by: Doug Ledford Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/infiniband/hw/mlx4/mr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/drivers/infiniband/hw/mlx4/mr.c +++ b/drivers/infiniband/hw/mlx4/mr.c @@ -346,7 +346,7 @@ int mlx4_ib_umem_calc_optimal_mtt_size(s /* Add to the first block the misalignment that it suffers from. */ total_len += (first_block_start & ((1ULL << block_shift) - 1ULL)); last_block_end = current_block_start + current_block_len; - last_block_aligned_end = round_up(last_block_end, 1 << block_shift); + last_block_aligned_end = round_up(last_block_end, 1ULL << block_shift); total_len += (last_block_aligned_end - last_block_end); if (total_len & ((1ULL << block_shift) - 1ULL))