Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4631331pxj; Wed, 12 May 2021 09:37:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJziZ4QJ63L4ee6MyUN/sAyO7TH09OjC6Ol2C/kmvqIBeLLVfltfXx0EH3PGOTjOZODfv6AZ X-Received: by 2002:a17:906:1984:: with SMTP id g4mr37635222ejd.525.1620837479157; Wed, 12 May 2021 09:37:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620837479; cv=none; d=google.com; s=arc-20160816; b=jAV1n5ibVHV2oSrtEYfeUUBqDC+7JiF9IAEb7QQ/6rR2CipL6AAqqFy5xoYFSIRvns 5traB3H4O8XZruZlQH6NDgI+9FHKGUjVExfcb0Si1zBovPCyw6NCmv19ExL2r86lZ132 tM8v7oUIPU8XVLnZxFAWpKcV00gG1dXYvOi+bCuS4/C76N3GagIENBd6JxIcRFmSPcvo aYrJf6InKKQhljjSFWA0e0FWHSwb4iglsWMWko2HdjGkVx88IH3UNq8hibDRH9ALkjFB kM5liUiucaETMIMG6EBLu+8CQpnN/TFqxM9lu6HxeU6dDDMeteqjxxtGZQX3LAYEnDRO hsHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fYf9o85seDBRP4bxbsm1/A2Dh6YBNWcVYpV51bMmCB0=; b=LdPDz9UXe4Y6JWTfLcp9wHiqDtln2ZvjTM+pJH+e8n9cYunTLAZxdH+Pk2WbBRVOQu 8Nd3aZ8A/syTE8jm2Wz4bctEnwfjr/NykkT7teKa5muGRE6KHdtEtuk8QWVayiWNo9aY 86zCoxkSz9C283RA+jHTiWFRYhVI8nuHfnsiZGqnwzeQh0+PbZdAIghLS2ZUuP59gePn ZnMddsFDLW8KGf9YaXzSNoYfHrrteDC0ehLs4ZdT9OZRCAry8OHYSzh+sa5DcudQ5YLA Zq40T8YP7sCMX6KBHHfSU/uDg0lAV+8FV60q9CJZj7YPyHR7sJrmKAVzicxxy4eixMe3 Cefw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=J2Em7w2D; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x11si129460edd.384.2021.05.12.09.37.34; Wed, 12 May 2021 09:37:59 -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=@linuxfoundation.org header.s=korg header.b=J2Em7w2D; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243298AbhELQhJ (ORCPT + 99 others); Wed, 12 May 2021 12:37:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:42370 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236618AbhELPpd (ORCPT ); Wed, 12 May 2021 11:45:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 54EC86195B; Wed, 12 May 2021 15:23:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620833001; bh=33ODD0I24enfDgzrAF6996/yVOfNCM+RxRup5ieFN4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J2Em7w2D0PYQEUbPQA5lQ0T8A71XfpkRc4J+83AJxGLzbli9f4KaLHgN9izC7SwWN Ec+wLhwQhmDXNoCjiHpaHoun0faTzTIwhOq5SafRL7V0wgKDuz1Cts+ofWwkkTPYat DL0MqbzZAnwRNYYtpSkIpDL+Tb32FA6SEOTDxM80= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lv Yunlong , Bernard Metzler , Jason Gunthorpe , Sasha Levin Subject: [PATCH 5.10 508/530] RDMA/siw: Fix a use after free in siw_alloc_mr Date: Wed, 12 May 2021 16:50:18 +0200 Message-Id: <20210512144836.458620389@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lv Yunlong [ Upstream commit 3093ee182f01689b89e9f8797b321603e5de4f63 ] Our code analyzer reported a UAF. In siw_alloc_mr(), it calls siw_mr_add_mem(mr,..). In the implementation of siw_mr_add_mem(), mem is assigned to mr->mem and then mem is freed via kfree(mem) if xa_alloc_cyclic() failed. Here, mr->mem still point to a freed object. After, the execution continue up to the err_out branch of siw_alloc_mr, and the freed mr->mem is used in siw_mr_drop_mem(mr). My patch moves "mr->mem = mem" behind the if (xa_alloc_cyclic(..)<0) {} section, to avoid the uaf. Fixes: 2251334dcac9 ("rdma/siw: application buffer management") Link: https://lore.kernel.org/r/20210426011647.3561-1-lyl2019@mail.ustc.edu.cn Signed-off-by: Lv Yunlong Reviewed-by: Bernard Metzler Signed-off-by: Jason Gunthorpe Signed-off-by: Sasha Levin --- drivers/infiniband/sw/siw/siw_mem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/sw/siw/siw_mem.c b/drivers/infiniband/sw/siw/siw_mem.c index 34a910cf0edb..61c17db70d65 100644 --- a/drivers/infiniband/sw/siw/siw_mem.c +++ b/drivers/infiniband/sw/siw/siw_mem.c @@ -106,8 +106,6 @@ int siw_mr_add_mem(struct siw_mr *mr, struct ib_pd *pd, void *mem_obj, mem->perms = rights & IWARP_ACCESS_MASK; kref_init(&mem->ref); - mr->mem = mem; - get_random_bytes(&next, 4); next &= 0x00ffffff; @@ -116,6 +114,8 @@ int siw_mr_add_mem(struct siw_mr *mr, struct ib_pd *pd, void *mem_obj, kfree(mem); return -ENOMEM; } + + mr->mem = mem; /* Set the STag index part */ mem->stag = id << 8; mr->base_mr.lkey = mr->base_mr.rkey = mem->stag; -- 2.30.2