From patchwork Mon Feb 14 17:51:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Rix X-Patchwork-Id: 12745962 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3146EC433F5 for ; Mon, 14 Feb 2022 17:51:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357260AbiBNRvz (ORCPT ); Mon, 14 Feb 2022 12:51:55 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:40202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346837AbiBNRvy (ORCPT ); Mon, 14 Feb 2022 12:51:54 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D6E1FC40 for ; Mon, 14 Feb 2022 09:51:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644861106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=sRClfikrb2SAycFi2aWUxJvEEU6PW+e2USfmkK1Iaug=; b=UVRN1nDA2L4V0QTsPM2TJIgUNYbVNPM+/2zZDl5RfW2OIb7WrYH1CHXtB3Chd6t0FSQKAy hz9E6JNXzVgSGOh7mqL8XvU2CBP/xs3e888ewcWhN/agkNFTBop+z4OvtqrDKowK1GRxdJ nAyIfDY3nRArpNDEuW35SFd9I4VQs0Y= Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-329-CRpa71_qML-0NkRs0u3b-Q-1; Mon, 14 Feb 2022 12:51:44 -0500 X-MC-Unique: CRpa71_qML-0NkRs0u3b-Q-1 Received: by mail-ot1-f70.google.com with SMTP id q4-20020a056830018400b0059eb82177ceso10792760ota.1 for ; Mon, 14 Feb 2022 09:51:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=sRClfikrb2SAycFi2aWUxJvEEU6PW+e2USfmkK1Iaug=; b=gdbzHOYPznQSeSMCPGLxUk4Mn5lIuXaFYQ0c1Sl5a3Ik1kH/jbO+oRf7ai+dDe70ve fzrYNRcp5IFX+a9KgNy7T2ENlpbXSk3m1RdJBOb44RxwZ4/+5yuMw9+gHHwY/TJSqMuM naOGZtNvrTcqI7lwLVOzAyCqh+lZLD/RHZMc3Lcs4CKzXghPP1QWpJWZcMk8S9ONNCqm WwovRqJtARbgIpyNPgtmvGJDeXCv8Vjg5liYEk110DXeOg1i7U/mNNqh6UAGZYGwYt6K oCD3ESHJv+ywwD3206Y06gtt4RiurjslKJESCEx4qyzJFvKRPLp5KJPeXBx1/53p4vBn v48w== X-Gm-Message-State: AOAM531snOUAiv1UdiIOxOCuCJ3LmkNu/6x+nRUpyQoMWJSv29r912+n vlm4whrqa6VJPviDhReEzkTW/41YxikiGFBceI4bNzxuhwN98J3lMi+rthBKbT6XGfwOskmowv3 Mk0/06x3ZajTgeM44 X-Received: by 2002:a9d:6e84:: with SMTP id a4mr18935otr.43.1644861103174; Mon, 14 Feb 2022 09:51:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJzcnHxTDhHzI4mHvMfEWEZ41qbHYDvsaNz05h/swvvbijMzbRD2lKpXlDtrTUoDdBuBcdn0Yg== X-Received: by 2002:a9d:6e84:: with SMTP id a4mr18930otr.43.1644861102975; Mon, 14 Feb 2022 09:51:42 -0800 (PST) Received: from localhost.localdomain.com (024-205-208-113.res.spectrum.com. [24.205.208.113]) by smtp.gmail.com with ESMTPSA id w20sm12910024ooe.3.2022.02.14.09.51.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 09:51:42 -0800 (PST) From: trix@redhat.com To: jk@codeconstruct.com.au, matt@codeconstruct.com.au, davem@davemloft.net, kuba@kernel.org, nathan@kernel.org, ndesaulniers@google.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Tom Rix Subject: [PATCH] mctp: fix use after free Date: Mon, 14 Feb 2022 09:51:38 -0800 Message-Id: <20220214175138.2902947-1-trix@redhat.com> X-Mailer: git-send-email 2.26.3 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org From: Tom Rix Clang static analysis reports this problem route.c:425:4: warning: Use of memory after it is freed trace_mctp_key_acquire(key); ^~~~~~~~~~~~~~~~~~~~~~~~~~~ When mctp_key_add() fails, key is freed but then is later used in trace_mctp_key_acquire(). Add an else statement to use the key only when mctp_key_add() is successful. Fixes: 4a992bbd3650 ("mctp: Implement message fragmentation & reassembly") Signed-off-by: Tom Rix --- net/mctp/route.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/net/mctp/route.c b/net/mctp/route.c index 17e3482aa770..0c4c56e1bd6e 100644 --- a/net/mctp/route.c +++ b/net/mctp/route.c @@ -419,13 +419,14 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb) * this function. */ rc = mctp_key_add(key, msk); - if (rc) + if (rc) { kfree(key); + } else { + trace_mctp_key_acquire(key); - trace_mctp_key_acquire(key); - - /* we don't need to release key->lock on exit */ - mctp_key_unref(key); + /* we don't need to release key->lock on exit */ + mctp_key_unref(key); + } key = NULL; } else {