From patchwork Fri Jun 1 18:31:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 10444267 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9E6DE602BC for ; Fri, 1 Jun 2018 18:32:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8F66728D4F for ; Fri, 1 Jun 2018 18:32:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 844BC28D68; Fri, 1 Jun 2018 18:32:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4CDE528D82 for ; Fri, 1 Jun 2018 18:32:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753432AbeFASb6 (ORCPT ); Fri, 1 Jun 2018 14:31:58 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:34803 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752333AbeFASb4 (ORCPT ); Fri, 1 Jun 2018 14:31:56 -0400 Received: by mail-pg0-f65.google.com with SMTP id k2-v6so11628921pgc.1; Fri, 01 Jun 2018 11:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=J9jKgWlsSdVSFJoEE43ev7Iwk1CE2AxsqgZeR/FKRBk=; b=T57SanJfGjonlyOFckaZhAb44GXTblM2Qo3Gj+sK62Rxh8dsDdM1DAMWxNouVSgeXq RmxW0oPz/5XgsGfFfC4l5gkPeLuw8wEUieJ6/BNj9+GIt0IpzV52LEqhAQUGFl8cnt2M ScsVmhCsPDEDbpnkE3WBq6hcYc0wxnFUuI85I3hEYeeimRw/aH3TKsy/ijQIqaCTZkAB 5LDEb6/A3O2kIIn9E8KScH8zl7rw4B5mEJY0L+yz6Gn83f92a4hvxd7vQQY3ZcTMWVjD juvT7CdIdTFJ6PbrjtyLkbRbFzzNlCQgz8YRfSylcpie680V61jsfYPeANt+uyIyGRtI ZPYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=J9jKgWlsSdVSFJoEE43ev7Iwk1CE2AxsqgZeR/FKRBk=; b=RslGArpZ6pvNi7yzILg+OAX118ITIw4EIpc78A/og27C3BsZJASxVV6vwzQ2tCyB9C KYEo4aYofwdcBYlQrSpb1njwyRwwapjsYOlBoP/XLWGwC2hkO5Z4cCUX606Py17JxaSl 75iZU7T5nUMmqmtWApF7YlKj5JwWOeNbaofB0Bao+xlnpH864Rpr92dvIJRMq3sDbV4r bzud3aZNF7zV08M2VHDLDFMeX0zxb7evfsR0e5hSue/UNCYYjNb6FJ+8ull/qa3xoEq4 wPM4cZwJU+2Ku1GVoepxJXW8SNAsHpxMhnU/RCfrcfPVr5L+ABmjPAOEwBlLGA8Rc9Pg Aw4w== X-Gm-Message-State: ALKqPwdLqIkMHlhvqtvA7S0lMUVDilftInx/lAHwxNcFDjn9AdTEr3G0 Eq41645n+4+3np20GWc/vak0DzH1 X-Google-Smtp-Source: ADUXVKK9JSALd+1+SEDY8zFuGqdA+dzmDxk9TZNOHEhYnhnMoQ5cQ7YGNe+FPih3JAx5xOa6hBdFIA== X-Received: by 2002:a62:1e02:: with SMTP id e2-v6mr11948551pfe.212.1527877916154; Fri, 01 Jun 2018 11:31:56 -0700 (PDT) Received: from tw-172-25-29-37.office.twttr.net ([8.25.197.25]) by smtp.gmail.com with ESMTPSA id c12-v6sm3442505pfi.177.2018.06.01.11.31.54 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 01 Jun 2018 11:31:55 -0700 (PDT) From: Cong Wang To: linux-kernel@vger.kernel.org Cc: noamr@beyondsecurity.com, Cong Wang , Sean Hefty , Doug Ledford , Jason Gunthorpe , linux-rdma@vger.kernel.org Subject: [PATCH] infiniband: fix a possible use-after-free bug Date: Fri, 1 Jun 2018 11:31:44 -0700 Message-Id: <20180601183144.17374-1-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.9.4 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP ucma_process_join() will free the new allocated "mc" struct, if there is any error after that, especially the copy_to_user(). But in parallel, ucma_leave_multicast() could find this "mc" through idr_find() before ucma_process_join() frees it, since it is already published. So "mc" could be used in ucma_leave_multicast() after it is been allocated and freed in ucma_process_join(), since we don't refcnt it. Fix this by separating "publish" from ID allocation, so that we can get an ID first and publish it later after copy_to_user(). Fixes c8f6a362bf3e ("RDMA/cma: Add multicast communication support") Reported-by: Noam Rathaus Cc: Sean Hefty Cc: Doug Ledford Cc: Jason Gunthorpe Cc: linux-rdma@vger.kernel.org Signed-off-by: Cong Wang --- drivers/infiniband/core/ucma.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/infiniband/core/ucma.c b/drivers/infiniband/core/ucma.c index eab43b17e9cf..ec8fb289621f 100644 --- a/drivers/infiniband/core/ucma.c +++ b/drivers/infiniband/core/ucma.c @@ -235,7 +235,7 @@ static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx) return NULL; mutex_lock(&mut); - mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL); + mc->id = idr_alloc(&multicast_idr, NULL, 0, 0, GFP_KERNEL); mutex_unlock(&mut); if (mc->id < 0) goto error; @@ -1421,6 +1421,10 @@ static ssize_t ucma_process_join(struct ucma_file *file, goto err3; } + mutex_lock(&mut); + idr_replace(&multicast_idr, mc, mc->id); + mutex_unlock(&mut); + mutex_unlock(&file->mut); ucma_put_ctx(ctx); return 0;