From patchwork Tue Aug 7 12:27:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Honggang LI X-Patchwork-Id: 10558651 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C392413AC for ; Tue, 7 Aug 2018 12:27:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3262C29891 for ; Tue, 7 Aug 2018 12:27:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 265CD298AE; Tue, 7 Aug 2018 12:27:53 +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.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI 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 B4FDC29891 for ; Tue, 7 Aug 2018 12:27:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389074AbeHGOl5 (ORCPT ); Tue, 7 Aug 2018 10:41:57 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:43154 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2389013AbeHGOl5 (ORCPT ); Tue, 7 Aug 2018 10:41:57 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 56A718160F82; Tue, 7 Aug 2018 12:27:50 +0000 (UTC) Received: from dhcp-129-124.nay.redhat.com (unknown [10.66.129.124]) by smtp.corp.redhat.com (Postfix) with ESMTP id B89272026D66; Tue, 7 Aug 2018 12:27:48 +0000 (UTC) From: Honggang LI To: hal@dev.mellanox.co.il Cc: linux-rdma@vger.kernel.org, Honggang Li Subject: [PATCH] osm_sa_mcmember_record.c: log before cleanup mgrp to avoid use-after-free Date: Tue, 7 Aug 2018 20:27:42 +0800 Message-Id: <20180807122742.9938-1-honli@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 07 Aug 2018 12:27:50 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 07 Aug 2018 12:27:50 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'honli@redhat.com' RCPT:'' 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 From: Honggang Li Issue was found by Coverity. Error: USE_AFTER_FREE (CWE-825): [#def11] opensm-3.3.20/opensm/osm_sa_mcmember_record.c:1340: freed_arg: "osm_mgrp_cleanup" frees "p_mgrp". opensm-3.3.20/opensm/osm_multicast.c:184:2: freed_arg: "free" frees parameter "mgrp". opensm-3.3.20/opensm/osm_sa_mcmember_record.c:1342: deref_after_free: Dereferencing freed pointer "p_mgrp". |# 1340| osm_mgrp_cleanup(sa->p_subn, p_mgrp); |# 1341| CL_PLOCK_RELEASE(sa->p_lock); |# 1342|-> OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1B12: " |# 1343| "validate_more_comp_fields, validate_port_caps, " |# 1344| "or JoinState = 0 failed for MGID: %s port 0x%016" PRIx64 Error: USE_AFTER_FREE (CWE-825): [#def12] opensm-3.3.20/opensm/osm_sa_mcmember_record.c:1357: freed_arg: "osm_mgrp_cleanup" frees "p_mgrp". opensm-3.3.20/opensm/osm_multicast.c:184:2: freed_arg: "free" frees parameter "mgrp". opensm-3.3.20/opensm/osm_sa_mcmember_record.c:1360: deref_after_free: Dereferencing freed pointer "p_mgrp". |# 1358| CL_PLOCK_RELEASE(sa->p_lock); |# 1359| memset(gid_str, 0, sizeof(gid_str)); |# 1360|-> OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1B14: " |# 1361| "Cannot join port 0x%016" PRIx64 " to MGID %s - " |# 1362| Signed-off-by: Honggang Li --- opensm/osm_sa_mcmember_record.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/opensm/osm_sa_mcmember_record.c b/opensm/osm_sa_mcmember_record.c index 9b94993e..20c3d006 100644 --- a/opensm/osm_sa_mcmember_record.c +++ b/opensm/osm_sa_mcmember_record.c @@ -1345,10 +1345,7 @@ static void mcmr_rcv_join_mgrp(IN osm_sa_t * sa, IN osm_madw_t * p_madw) || !validate_port_caps(sa->p_log, p_mgrp, p_physp) || !(join_state != 0)) { char gid_str[INET6_ADDRSTRLEN]; - /* since we might have created the new group we need to cleanup */ - if (is_new_group) - osm_mgrp_cleanup(sa->p_subn, p_mgrp); - CL_PLOCK_RELEASE(sa->p_lock); + memset(gid_str, 0, sizeof(gid_str)); OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1B12: " "validate_more_comp_fields, validate_port_caps, " "or JoinState = 0 failed for MGID: %s port 0x%016" PRIx64 @@ -1356,6 +1353,10 @@ static void mcmr_rcv_join_mgrp(IN osm_sa_t * sa, IN osm_madw_t * p_madw) inet_ntop(AF_INET6, p_mgrp->mcmember_rec.mgid.raw, gid_str, sizeof gid_str), cl_ntoh64(portguid), p_port->p_node->print_desc); + /* since we might have created the new group we need to cleanup */ + if (is_new_group) + osm_mgrp_cleanup(sa->p_subn, p_mgrp); + CL_PLOCK_RELEASE(sa->p_lock); osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_REQ_INVALID); goto Exit; } @@ -1363,9 +1364,6 @@ static void mcmr_rcv_join_mgrp(IN osm_sa_t * sa, IN osm_madw_t * p_madw) /* verify that the joining port is in the partition of the group */ if (!osm_physp_has_pkey(sa->p_log, p_mgrp->mcmember_rec.pkey, p_physp)) { char gid_str[INET6_ADDRSTRLEN]; - if (is_new_group) - osm_mgrp_cleanup(sa->p_subn, p_mgrp); - CL_PLOCK_RELEASE(sa->p_lock); memset(gid_str, 0, sizeof(gid_str)); OSM_LOG(sa->p_log, OSM_LOG_ERROR, "ERR 1B14: " "Cannot join port 0x%016" PRIx64 " to MGID %s - " @@ -1374,6 +1372,9 @@ static void mcmr_rcv_join_mgrp(IN osm_sa_t * sa, IN osm_madw_t * p_madw) inet_ntop(AF_INET6, p_mgrp->mcmember_rec.mgid.raw, gid_str, sizeof(gid_str))); + if (is_new_group) + osm_mgrp_cleanup(sa->p_subn, p_mgrp); + CL_PLOCK_RELEASE(sa->p_lock); osm_sa_send_error(sa, p_madw, IB_SA_MAD_STATUS_REQ_INVALID); goto Exit; }