Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2342335imm; Thu, 11 Oct 2018 08:48:21 -0700 (PDT) X-Google-Smtp-Source: ACcGV62B5LUj2PvKMNX9V0f53AS0ZIadGh9+lKTArqrjmLpuWCHwVY9e/RtV/67pDaZ8zorPRREH X-Received: by 2002:a17:902:bccc:: with SMTP id o12-v6mr2134893pls.202.1539272901348; Thu, 11 Oct 2018 08:48:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539272901; cv=none; d=google.com; s=arc-20160816; b=rlKBuKUYet6IKz14NShYTrecZW37RhyPQSyNXcn/oJkzgU/81MOgPRlS/nBZHLz89Y uUyEhxxwFBOHh7Oef6P7iSHWheJKZzv65GbDAu2A/jyLoglQMumYsmEUfse2DIFnb37i BM4k6Af63qRWP2wqAuCoEBfD6BCE/A3lC0eXqi8XDFlez42l5NMY6dp9sDKydn4HMmAq ydmT0tlasOYtwhMh3hWsjpQUKB9cIVRPnfCWV36LYzZn4MrUNIkj+094YhwAGBdqTxpJ ej91pEH4pRGg9IoVRH8LfjJzuhgeo2c4OkJrH5pzc8Ak2g7N7CZswnnXQejpzROTEx+d 6xjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3Jw/lhPcL3YVb98R8iLc4qoUOCc1mDyDBrEw9Ujgc0E=; b=rfKou362PC5wZ2t8OvfjM6262/Ca8YcIqcIOeEkihoEhCEYQA0XYdBLeC8lpiN1wrY aVXrYka0/z0RvOGEMLjt9CMjiKgjk2q6QJ20KHbjpHI3gJk9O5xvFqDHkXWcR9i1qXuT riQCzP990DcORbE/Y0XaohB+NiqCvKLNCOWxmpvb+JT6Gebc/JCN0UiTZ4LaZBBweoX9 mOz/tNg24nIG0D/N6fthXcEzeFa7nM6F6KIs0P2v4rkkRD5LBpBGsMkryc/iniggLdtS rqUEWyfwHYS+gN3+zGW5RJoe2cwcmi/JCamvNAW8z8WoRVlDFSFU0EdjXM2+WQCHYZCY ABCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BaF8bf+8; 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 186-v6si28975188pfg.163.2018.10.11.08.48.06; Thu, 11 Oct 2018 08:48:21 -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=BaF8bf+8; 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 S1731675AbeJKXOh (ORCPT + 99 others); Thu, 11 Oct 2018 19:14:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:47740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726882AbeJKXOg (ORCPT ); Thu, 11 Oct 2018 19:14:36 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 88B7021476; Thu, 11 Oct 2018 15:46:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539272811; bh=VgAgntmVdZamr+4ZstGIeXOGbaLqpeg/9MTqNSgYV6U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BaF8bf+8H0kmmkikvws1yIAjelcbAc6zmWZKZnK9JXi6oHEcOmeBXahuae/pqrGwb /HQ17BWgwepr6kao0OzDUUNspi+gqzHIIesYnxJFnANQdv8m/UWb1OjGm+iFchQx01 oVqBlvuvyRMlrAj68g+8TtZybb1/Hvlx2s2Wh1PE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+cfe3c1e8ef634ba8964b@syzkaller.appspotmail.com, Jason Gunthorpe , Doug Ledford , Leon Romanovsky , Cong Wang , Leon Romanovsky Subject: [PATCH 4.14 40/45] ucma: fix a use-after-free in ucma_resolve_ip() Date: Thu, 11 Oct 2018 17:40:07 +0200 Message-Id: <20181011152510.676306450@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181011152508.885515042@linuxfoundation.org> References: <20181011152508.885515042@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cong Wang commit 5fe23f262e0548ca7f19fb79f89059a60d087d22 upstream. 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: Greg Kroah-Hartman --- drivers/infiniband/core/ucma.c | 2 ++ 1 file changed, 2 insertions(+) --- a/drivers/infiniband/core/ucma.c +++ b/drivers/infiniband/core/ucma.c @@ -1742,6 +1742,8 @@ static int ucma_close(struct inode *inod 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. */