Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1504661ybt; Thu, 9 Jul 2020 08:34:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVJezrXNncwc5OtDWbhq8s5zU+Xp+/E4HCdvHcWe1k4FM6euzUFVM18sVdr6LtW8HaWm8B X-Received: by 2002:aa7:d58c:: with SMTP id r12mr76252276edq.160.1594308864726; Thu, 09 Jul 2020 08:34:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594308864; cv=none; d=google.com; s=arc-20160816; b=QiG8oXgw6cbnofA8wBRYAypsDn7XPjIlqqCxJOPtxyXDo49c8wk4Has8d4CaePMJqi A8C7dAD2sNilN0NStvMMeEV3Z4eP8YDmDn7yZrn1PlESF1kxv1RKdR2eyYdcabD7A49L D3xHU6iocXmfjN61T4xIQ7uS7+FFTrwWFx5Un2nWmO1irCfu6rr7tUrArfOZwohDbig6 IgojIT/bW7lbyfK4uZvgt9EiHKmOgm9yrgHF7X7gnBRSxyzbAyfnjDmClhYA3/WnSSRQ 6CJbmagETFw59/9On0jfv/rLM64izTvxY5/rztw27PCX9KuGpnpw8yUk7K0eCgXjAyg5 N+tg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=ZF+I2jn+WkRqYcMn9LbEqt6wxaWBW8Jnk837a3yZsFE=; b=FnZ8z5TuzIY4ZPJwSfJ88lsFQlS+O5sXnz/8T6ujWCRAWQ3T53wyqmhRVK3KTBXd+q NR8P8itWCdpSvWXBUYDYThZMrNeTT6Q3/KGjrD6FKzbaB2LXDfRfVOkYVwwSX75nfAGM lvk0scqTpQ7+wYz8pELipUsrvzqS3PTWAk75R8pS4wa2a/Zov3zb2j7NbhjnGM+6DyCC B8ELlw0Ix9EydoGnQqRsN6Bhfg7dinZtTv/j0wEfIiVsrJL3dXPZ17uMdvBUnbck9gRG jlOfgNd3fGxaYgFcLtwd4PWKY8jm4976ACPtRxOOZz/n5mb6HYLo4MpLP91a4DliDNFg T09A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OQF13Um3; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lg17si2078363ejb.440.2020.07.09.08.34.00; Thu, 09 Jul 2020 08:34:24 -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=@redhat.com header.s=mimecast20190719 header.b=OQF13Um3; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726844AbgGIPbf (ORCPT + 99 others); Thu, 9 Jul 2020 11:31:35 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:44799 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726460AbgGIPbf (ORCPT ); Thu, 9 Jul 2020 11:31:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594308694; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc; bh=ZF+I2jn+WkRqYcMn9LbEqt6wxaWBW8Jnk837a3yZsFE=; b=OQF13Um3Rc42bWSeRZhEXagbwvOMkjDtZ4QAO4YjyJao0p6+QvmhBYq75s959rSxtAwoSs 174sNGpnGomsJHw9kkNLU2WI653qCKABGruvRZ3rCmBbwn+iN2sUAw+Nwr7z2ELU3+mDx8 w8xg64FW4xf3yl9IZagpje9kBs4f1LA= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-342-vegBVbeLPvy1uXhzQKPOGg-1; Thu, 09 Jul 2020 11:31:32 -0400 X-MC-Unique: vegBVbeLPvy1uXhzQKPOGg-1 Received: by mail-qt1-f197.google.com with SMTP id t36so1878390qtc.16 for ; Thu, 09 Jul 2020 08:31:32 -0700 (PDT) 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=ZF+I2jn+WkRqYcMn9LbEqt6wxaWBW8Jnk837a3yZsFE=; b=dFwxp8Ir290+v1lJbAWYvWRmgI37PQJXb4yjP3nTEfcJxQHHJP4XvcxGcFC1+GzIws KI5pTdBPqLTvHzi6NsgIvs4c2UKphsj9eoLwJjwJ0ZyeUEF7tfIVPljMRzIiYkymRC63 vxyvYU3xhdAZYjFXqTkPGyBgmS1q1iZUVn50L7nJDPJDuJWCqspG/h2KVQFM8oUpJCXX uEC+zVTwppyAuydmurKWeYOSh2GAW29q5+pHUutPc/6w13EOdFXJrvMg8yTnUm0/V7lL NEd8xWXRl3C/0dsVCvVxhtd4vQDIm76hdMkVeH4Hi+IqWGZN9+RxqhBM+e1WYPi+4663 RN9Q== X-Gm-Message-State: AOAM5322JTjPbfdoq+fO7RSU2rF7SYiaHwJvWOVli2sGFH7Hc0wirGZA VB/VK7GkcYcIAGlB0JPeARMYPv6zK1fDG4ikHojwdZPm6n47x0VwwO4JO4ORy/dCb+8YKOg+twb 2bJebagV1dHjAjr3FWMBo0Qwi X-Received: by 2002:ac8:6d16:: with SMTP id o22mr67048631qtt.155.1594308691787; Thu, 09 Jul 2020 08:31:31 -0700 (PDT) X-Received: by 2002:ac8:6d16:: with SMTP id o22mr67048603qtt.155.1594308691540; Thu, 09 Jul 2020 08:31:31 -0700 (PDT) Received: from trix.remote.csb (075-142-250-213.res.spectrum.com. [75.142.250.213]) by smtp.gmail.com with ESMTPSA id l3sm3832360qtn.69.2020.07.09.08.31.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2020 08:31:30 -0700 (PDT) From: trix@redhat.com To: stuyoder@gmail.com, laurentiu.tudor@nxp.com, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Tom Rix Subject: [PATCH v3] bus: fsl-mc: fix invalid free in fsl_mc_device_add Date: Thu, 9 Jul 2020 08:31:19 -0700 Message-Id: <20200709153119.5051-1-trix@redhat.com> X-Mailer: git-send-email 2.18.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tom Rix clang static analysis flags this error fsl-mc-bus.c:695:2: warning: Attempt to free released memory [unix.Malloc] kfree(mc_dev); ^~~~~~~~~~~~~ The problem block of code is mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL); if (!mc_bus) return -ENOMEM; mc_dev = &mc_bus->mc_dev; mc_bus's structure contains a mc_dev element, freeing it later is not appropriate. So check if mc_bus was allocated before freeing mc_dev This is a case where checkpatch WARNING: kfree(NULL) is safe and this check is probably not required + if (mc_bus) + kfree(mc_bus); is wrong. Fixes: a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations") Signed-off-by: Tom Rix --- v1: add a comment to explain freeing uniqueness v2: add gregkh's suggestion to comment. drivers/bus/fsl-mc/fsl-mc-bus.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/bus/fsl-mc/fsl-mc-bus.c b/drivers/bus/fsl-mc/fsl-mc-bus.c index 40526da5c6a6..839d96d03f0d 100644 --- a/drivers/bus/fsl-mc/fsl-mc-bus.c +++ b/drivers/bus/fsl-mc/fsl-mc-bus.c @@ -691,8 +691,16 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, error_cleanup_dev: kfree(mc_dev->regions); - kfree(mc_bus); - kfree(mc_dev); + /* + * mc_bus allocates a private version of mc_dev + * it is not appropriate to free the private version. + * Which means we have to check the pointer before freeing it. + * Do not remove this check. + */ + if (mc_bus) + kfree(mc_bus); + else + kfree(mc_dev); return error; } -- 2.18.1