Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1714497ybt; Thu, 25 Jun 2020 12:18:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzd8ZkagNsn/ogjTAgzi9dlugoLJ5ZUMa/n0C+GtrG8FqDjvmJt+kE8FEDgRjKsXxFlUW5y X-Received: by 2002:a17:906:1ed2:: with SMTP id m18mr29785792ejj.529.1593112685522; Thu, 25 Jun 2020 12:18:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593112685; cv=none; d=google.com; s=arc-20160816; b=sJd6ketDcWRn/NwfOR6EbKqx2uiEmIspVK/4IcCLFDR35biU6O5mDQcPJTsfg/G8kv XiKMLTv9WxC9WR1rpLsiba5BieCGtSbyCaPd+0SQeIlFhUb0I22S4y4LcgDxcAfqDw/5 kQXMqZpTaUeYdkb2I11SBSR3Fw84xvPyWq7sKR5Ic8z0LlhMlH9FZ/Jzbn66LGPhxDzm wreYWDaYMzwUHL98ypNIEwHYnFLnEbC+2VHBM89HSmcyYmP/smEyqidStEKm2n6HaS24 iBO4H1TM5o9I72c6+eIQUdQM81vW8XnIq41gtj1Ao9ducfKhEPkBLM9bBCuJz4BlnlMC zGbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=XGJuA0l04Boa2lC029FVtNehot89KSoZOwPqiKXFYNA=; b=mNgN1Vhe1mhUv+LjN2Fa8kWMboX2TT4EEJHYxJo6aegx0k6lkgKuFEW+rWFpf8yHE/ WW6jNl2OzPdTkhln11uOIqsY2gbMB+9gdBEKSDz/6XKNSinZfYgPNekyEMAaQ3JOyvZa psvx2vxEzUM7w1jfdWoxSBproUW8V3fzeK7QOfkZy8GMl6PpmcX13JtO6DAwiwlUN4bL /M55xs2dNqd216J1gpk8BEiE++9J3h/Uy14NLcDb7xezKfLs9YoadnTP9Ba0UzhvNfdj hUw0CyoSNMcL1Cnv5GRccLDJ18RbXGNGgNqJD/C1eTyN7va8CODsR/tDzGdrFWAP1Ry4 MEnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=K1lbL3iV; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l61si4612944edl.512.2020.06.25.12.17.42; Thu, 25 Jun 2020 12:18:05 -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=@kernel.org header.s=default header.b=K1lbL3iV; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406153AbgFYTAs (ORCPT + 99 others); Thu, 25 Jun 2020 15:00:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:42288 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390431AbgFYTAs (ORCPT ); Thu, 25 Jun 2020 15:00:48 -0400 Received: from localhost (unknown [213.57.247.131]) (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 7F90620679; Thu, 25 Jun 2020 19:00:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593111647; bh=YkfmNwRTqOcDyN/i3vS+1r+tulnHjjdBmDirQ+SLQ6w=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=K1lbL3iVZT7SpBv0OEozOH0fVRO8/BuAfb9RFc8HtvswzuDfoaC6xqbpPi+WXBqrU cAv8xiWw27C2V9eZ6RlnrKZgkDs7dEkpa8txpq+TVjXtVI4O94xD8sQAKj6z3w+dX+ nCS+6WlJ0QgvlsO0WP45hjJ63EaQEgcFJ56TfJHg= Date: Thu, 25 Jun 2020 22:00:43 +0300 From: Leon Romanovsky To: Divya Indi Cc: linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, Jason Gunthorpe , Kaike Wan , Gerd Rausch , =?iso-8859-1?Q?H=E5kon?= Bugge , Srinivas Eeda , Rama Nichanamatlu , Doug Ledford Subject: Re: [PATCH v4] IB/sa: Resolving use-after-free in ib_nl_send_msg Message-ID: <20200625190043.GF1446285@unreal> References: <1592964789-14533-1-git-send-email-divya.indi@oracle.com> <20200625100904.GE1446285@unreal> <372b8c22-bac9-e737-bd54-0d9e2901de65@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <372b8c22-bac9-e737-bd54-0d9e2901de65@oracle.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 25, 2020 at 10:11:07AM -0700, Divya Indi wrote: > Hi Leon, > > Please find my comments inline - > > On 6/25/20 3:09 AM, Leon Romanovsky wrote: > > On Tue, Jun 23, 2020 at 07:13:09PM -0700, Divya Indi wrote: > >> Commit 3ebd2fd0d011 ("IB/sa: Put netlink request into the request list before sending")' > >> - > >> 1. Adds the query to the request list before ib_nl_snd_msg. > >> 2. Moves ib_nl_send_msg out of spinlock, hence safe to use gfp_mask as is. > >> > >> However, if there is a delay in sending out the request (For > >> eg: Delay due to low memory situation) the timer to handle request timeout > >> might kick in before the request is sent out to ibacm via netlink. > >> ib_nl_request_timeout may release the query causing a use after free situation > >> while accessing the query in ib_nl_send_msg. > >> > >> Call Trace for the above race: > >> > >> [] ? ib_pack+0x17b/0x240 [ib_core] > >> [] ib_sa_path_rec_get+0x181/0x200 [ib_sa] > >> [] rdma_resolve_route+0x3c0/0x8d0 [rdma_cm] > >> [] ? cma_bind_port+0xa0/0xa0 [rdma_cm] > >> [] ? rds_rdma_cm_event_handler_cmn+0x850/0x850 > >> [rds_rdma] > >> [] rds_rdma_cm_event_handler_cmn+0x22c/0x850 > >> [rds_rdma] > >> [] rds_rdma_cm_event_handler+0x10/0x20 [rds_rdma] > >> [] addr_handler+0x9e/0x140 [rdma_cm] > >> [] process_req+0x134/0x190 [ib_addr] > >> [] process_one_work+0x169/0x4a0 > >> [] worker_thread+0x5b/0x560 > >> [] ? flush_delayed_work+0x50/0x50 > >> [] kthread+0xcb/0xf0 > >> [] ? __schedule+0x24a/0x810 > >> [] ? __schedule+0x24a/0x810 > >> [] ? kthread_create_on_node+0x180/0x180 > >> [] ret_from_fork+0x47/0x90 > >> [] ? kthread_create_on_node+0x180/0x180 > >> .... > >> RIP [] send_mad+0x33d/0x5d0 [ib_sa] > >> > >> To resolve the above issue - > >> 1. Add the req to the request list only after the request has been sent out. > >> 2. To handle the race where response comes in before adding request to > >> the request list, send(rdma_nl_multicast) and add to list while holding the > >> spinlock - request_lock. > >> 3. Use non blocking memory allocation flags for rdma_nl_multicast since it is > >> called while holding a spinlock. > >> > >> Fixes: 3ebd2fd0d011 ("IB/sa: Put netlink request into the request list > >> before sending") > >> > >> Signed-off-by: Divya Indi > >> --- > >> v1: > >> - Use flag IB_SA_NL_QUERY_SENT to prevent the use-after-free. > >> > >> v2: > >> - Use atomic bit ops for setting and testing IB_SA_NL_QUERY_SENT. > >> - Rewording and adding comments. > >> > >> v3: > >> - Change approach and remove usage of IB_SA_NL_QUERY_SENT. > >> - Add req to request list only after the request has been sent out. > >> - Send and add to list while holding the spinlock (request_lock). > >> - Overide gfp_mask and use GFP_NOWAIT for rdma_nl_multicast since we > >> need non blocking memory allocation while holding spinlock. > >> > >> v4: > >> - Formatting changes. > >> - Use GFP_NOWAIT conditionally - Only when GFP_ATOMIC is not provided by caller. > >> --- > >> drivers/infiniband/core/sa_query.c | 41 ++++++++++++++++++++++---------------- > >> 1 file changed, 24 insertions(+), 17 deletions(-) > >> > >> diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c > >> index 74e0058..9066d48 100644 > >> --- a/drivers/infiniband/core/sa_query.c > >> +++ b/drivers/infiniband/core/sa_query.c > >> @@ -836,6 +836,10 @@ static int ib_nl_send_msg(struct ib_sa_query *query, gfp_t gfp_mask) > >> void *data; > >> struct ib_sa_mad *mad; > >> int len; > >> + unsigned long flags; > >> + unsigned long delay; > >> + gfp_t gfp_flag; > >> + int ret; > >> > >> mad = query->mad_buf->mad; > >> len = ib_nl_get_path_rec_attrs_len(mad->sa_hdr.comp_mask); > >> @@ -860,36 +864,39 @@ static int ib_nl_send_msg(struct ib_sa_query *query, gfp_t gfp_mask) > >> /* Repair the nlmsg header length */ > >> nlmsg_end(skb, nlh); > >> > >> - return rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_LS, gfp_mask); > >> -} > >> + gfp_flag = ((gfp_mask & GFP_ATOMIC) == GFP_ATOMIC) ? GFP_ATOMIC : > >> + GFP_NOWAIT; > > I would say that the better way will be to write something like this: > > gfp_flag |= GFP_NOWAIT; > > You mean gfp_flag = gfp_mask|GFP_NOWAIT? [We dont want to modify the gfp_mask sent by caller] > > #define GFP_ATOMIC (__GFP_HIGH|__GFP_ATOMIC|__GFP_KSWAPD_RECLAIM) > #define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS) > #define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM) > > If a caller passes GFP_KERNEL, "gfp_mask|GFP_NOWAIT" will still have __GFP_RECLAIM, > __GFP_IO and __GFP_FS set which is not suitable for using under spinlock. Ahh, sorry I completely forgot about spinlock part. Thanks > > Thanks, > Divya > > > > > Thanks