Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3730440imm; Mon, 8 Oct 2018 08:37:53 -0700 (PDT) X-Google-Smtp-Source: ACcGV63MxHWu6LL/B8VIxgDJOFpYGy3jp8DuHH7UpFNmr5y6NVzSUiQk/lqYq6Ltjty68/Jr+RxQ X-Received: by 2002:a17:902:b03:: with SMTP id 3-v6mr16977800plq.141.1539013073522; Mon, 08 Oct 2018 08:37:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539013073; cv=none; d=google.com; s=arc-20160816; b=mdebi56x9udBDP10t8tG+a5sB9Th0luSuzzN4e9Xx5xTwpFSib3VjQjQmAbM7XQABa YgKdEiZW6rYYpuhauWz8irQvZ3euU4xRhZsxn+wUeNabMVf8sXTcMxoNc15a3T68ZNv3 6gkUilbi3S0jQFJbu+zyeNIZWO115DYbyEq4E+MXkunVgwZfGQvOtZu48wQGR5ijVAWN 4RVljlyOkG1OSt8fOzNL0sD1jGO2i5yvac5l98zNXFz1O1CNFOBfBGt5Gmbytt+xOaCL 6MjbM7FT+g/wGYG6PyDYLPciBEe5ajvkamujUzhFJp/5Kjuh4YBoRhFUdbqT8LDqmx24 +o0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=LiQoFOtuhDlj3K2RflJNdYtj2PExEQCSBSeJi9UzCAA=; b=Gi/gnNW05ElgZkKBony0LrncZNuuFSsPGz67zubzx5eNvBb5rS0BSE8jnHGXhmiOys iquSdo9UmWVQR8kVVIIaLQlGXDhArn3DCFCHXZ4fWf02U33//F9swcBXPpBEkbexuuo2 SEbMJOu3QuYlzGHfybVN9k5XzseWXo928OK6EFUcPK80FSrUuZAt6f+sHpncv5eLQxRA HTK2a1vHKyYWH+wSfzbhj19eiqkvwTIqvkZ9Erspr9fIPoSXBVhhFvSeG0yhwDI6o6wg I5THiy0+iX2Y8m+F5iT3BENIUz/fWOFQ+Zm6wxHSeAD9iDjvOHKUwhIDCPVNtR9F+AOn ppGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uSc+ItO0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d10-v6si17471134pgg.330.2018.10.08.08.37.38; Mon, 08 Oct 2018 08:37:53 -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; dkim=pass header.i=@kernel.org header.s=default header.b=uSc+ItO0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727194AbeJHWh7 (ORCPT + 99 others); Mon, 8 Oct 2018 18:37:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:56124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726977AbeJHWh7 (ORCPT ); Mon, 8 Oct 2018 18:37:59 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7B6B821502; Mon, 8 Oct 2018 15:25:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539012344; bh=Xo3qObIC5WyLxyurs+orkNrT7iRCZB08D+cqvrnClns=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uSc+ItO0RO7mBG9GUdttC5DcT/ITsZZ62dF4mWBgmsJ5SRkq59h8oh6fzrO2kWaZ2 msil0HwvfhzF+rEP3XV917In/M73DOaflQPiIbHanUuWNwuvC+eqAjzppaYZw7rXs0 res/CvR7FjyYA2TQw9ckzh3UAjyQ0vHWvboZwXaI= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Cong Wang , Jason Gunthorpe , Doug Ledford , Leon Romanovsky , Sasha Levin Subject: [PATCH AUTOSEL 4.18 18/58] ucma: fix a use-after-free in ucma_resolve_ip() Date: Mon, 8 Oct 2018 11:24:43 -0400 Message-Id: <20181008152523.70705-18-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181008152523.70705-1-sashal@kernel.org> References: <20181008152523.70705-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Cong Wang [ Upstream commit 5fe23f262e0548ca7f19fb79f89059a60d087d22 ] There is a race condition between ucma_close() and ucma_resolve_ip(): CPU0 CPU1 ucma_resolve_ip(): ucma_close(): ctx = ucma_get_ctx(file, cmd.id); list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) { mutex_lock(&mut); idr_remove(&ctx_idr, ctx->id); mutex_unlock(&mut); ... mutex_lock(&mut); if (!ctx->closing) { mutex_unlock(&mut); rdma_destroy_id(ctx->cm_id); ... ucma_free_ctx(ctx); ret = rdma_resolve_addr(); ucma_put_ctx(ctx); Before idr_remove(), ucma_get_ctx() could still find the ctx and after rdma_destroy_id(), rdma_resolve_addr() may still access id_priv pointer. Also, ucma_put_ctx() may use ctx after ucma_free_ctx() too. ucma_close() should call ucma_put_ctx() too which tests the refcnt and waits for the last one releasing it. The similar pattern is already used by ucma_destroy_id(). Reported-and-tested-by: syzbot+da2591e115d57a9cbb8b@syzkaller.appspotmail.com Reported-by: syzbot+cfe3c1e8ef634ba8964b@syzkaller.appspotmail.com Cc: Jason Gunthorpe Cc: Doug Ledford Cc: Leon Romanovsky Signed-off-by: Cong Wang Reviewed-by: Leon Romanovsky Signed-off-by: Doug Ledford Signed-off-by: Sasha Levin --- drivers/infiniband/core/ucma.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c index ec8fb289621f..e8edad9b2744 100644 --- a/drivers/infiniband/core/ucma.c +++ b/drivers/infiniband/core/ucma.c @@ -1753,6 +1753,8 @@ static int ucma_close(struct inode *inode, struct file *filp) mutex_lock(&mut); if (!ctx->closing) { mutex_unlock(&mut); + ucma_put_ctx(ctx); + wait_for_completion(&ctx->comp); /* rdma_destroy_id ensures that no event handlers are * inflight for that id before releasing it. */ -- 2.17.1