From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 054BFC433ED for ; Wed, 28 Apr 2021 17:26:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE9856143A for ; Wed, 28 Apr 2021 17:26:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241612AbhD1R1N (ORCPT ); Wed, 28 Apr 2021 13:27:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47217 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241602AbhD1R1K (ORCPT ); Wed, 28 Apr 2021 13:27:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619630784; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gG7pVgMu1dn0fnwqtPsC3vyUDfxQRZUfZPSBpjAVrzc=; b=GWfdqDfnvH9tDGe9ldzevIZcM6OHLJLwsAKW3AaTCCuLZ1UUV9dgbuZ5W/bojJPahVvigh 936LOkvRP8NtSLj5kmqarDs/UNKPe3rcP2LrShLcYd3AsrXisCxP6K4FmrFUDxrb6q7G22 alO66e7WYR//epDZqDhy2/0BI/cKmMU= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-585-vGzJbsVYMvm_Nyj534Hrtw-1; Wed, 28 Apr 2021 13:26:23 -0400 X-MC-Unique: vGzJbsVYMvm_Nyj534Hrtw-1 Received: by mail-qv1-f69.google.com with SMTP id f17-20020a05621400d1b029019a6fc802f7so28630243qvs.7 for ; Wed, 28 Apr 2021 10:26:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=gG7pVgMu1dn0fnwqtPsC3vyUDfxQRZUfZPSBpjAVrzc=; b=naVQrqYNwAyU5UpUq94m5FHAe05U/XJnHOB4+xLTKmcTZ750WbnW9uFR85LUDqiDvd /co0jXYpYpCB6OmLuEt3d7/+473okY9gUd1Oa3E2vG1elbjGFnIIgllBag06abO5KtwY KejG5cV9yy1tYNAjPhlx3Crio1tZzWg6gkBZxlTyZU/zQBhyw+BmUJbybbrciGrNYvsI UEx1DBMHNKLTBPP7T925JJge3peOOPvbPsVhZzw1FeU9KCk5RFs3UUwVmq51KL3NXYXx H6xPcYUtUX3pfZoiSgfhSjN/KzzMl2ZcsLvGHE7DIserUUDgDeLeUu7MZtLxspp7Uh77 VAlA== X-Gm-Message-State: AOAM532p4uw6wSZkKD5nGbm/tLIyM06DUehlll7jmg+UtrEsypmFL92K d7LLzEMHPTThOuebJTnnNPwWaTFgA9i1gsaXZ9EBBoaev4yRxurxw/NJ2t/9WGXHYmtMhYTXMj4 N15M4mX20EPlcd+6/IhvY3cv+ X-Received: by 2002:ac8:490c:: with SMTP id e12mr27530984qtq.351.1619630779648; Wed, 28 Apr 2021 10:26:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwu2q1hIZae88kkzy/WSBPDkP1/cW/HkadfScxYQv9g52d1g+ZXvDeMNI33o6wcG2j8+Yaqzg== X-Received: by 2002:ac8:490c:: with SMTP id e12mr27530954qtq.351.1619630779252; Wed, 28 Apr 2021 10:26:19 -0700 (PDT) Received: from localhost.localdomain (075-142-250-213.res.spectrum.com. [75.142.250.213]) by smtp.gmail.com with ESMTPSA id m14sm426385qtq.59.2021.04.28.10.26.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Apr 2021 10:26:18 -0700 (PDT) Subject: Re: [PATCH] bus: fsl-mc: fix improper free of mc_dev To: Nick Desaulniers Cc: stuyoder@gmail.com, laurentiu.tudor@nxp.com, Nathan Chancellor , Greg KH , LKML , clang-built-linux References: <20210427183624.2790305-1-trix@redhat.com> From: Tom Rix Message-ID: <975e8af4-d1b6-0440-7447-1e06e514ca8a@redhat.com> Date: Wed, 28 Apr 2021 10:26:16 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/27/21 12:19 PM, Nick Desaulniers wrote: > On Tue, Apr 27, 2021 at 11:36 AM wrote: >> From: Tom Rix >> >> Clang static analysis reports this error >> >> fsl-mc-bus.c:891:2: warning: Attempt to free released memory >> kfree(mc_dev); >> ^~~~~~~~~~~~~ >> >> In this block of code >> >> if (strcmp(obj_desc->type, "dprc") == 0) { >> .. >> mc_bus = kzalloc(..) >> mc_dev = &mc_bus->mc_dev; > Thanks for the patch. > > Aren't the allocations for mc_bus and mc_dev mutually exclusive based > on that conditional? If so... > >> mc_dev is not alloc-ed, so it should not be freed. >> Old handler triggers a false positive from checkpatch, so add a >> comment and change logic a bit. >> >> Fixes: a042fbed0290 ("staging: fsl-mc: simplify couple of deallocations") >> Signed-off-by: Tom Rix >> --- >> drivers/bus/fsl-mc/fsl-mc-bus.c | 4 +++- >> 1 file changed, 3 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/bus/fsl-mc/fsl-mc-bus.c b/drivers/bus/fsl-mc/fsl-mc-bus.c >> index 380ad1fdb745..fb3e1d8a7f63 100644 >> --- a/drivers/bus/fsl-mc/fsl-mc-bus.c >> +++ b/drivers/bus/fsl-mc/fsl-mc-bus.c >> @@ -887,8 +887,10 @@ int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, >> >> error_cleanup_dev: >> kfree(mc_dev->regions); >> + /* mc_dev is only allocated when it is not part of mc_bus */ >> + if (!mc_bus) >> + kfree(mc_dev); >> kfree(mc_bus); >> - kfree(mc_dev); > The error handling here seems quite wrong (regardless of your patch). > mc_dev->regions is allocated by fsl_mc_device_get_mmio_regions() IIUC. > Wouldn't the first `goto error_cleanup_dev;` taken end up passing an > uninitialized pointer to kfree()? On the first goto, mc_dev->regions, because of the kzalloc, the value would be mc_bus->mc_dev.regions , should be 0 or mc_dev->regions, which also should be 0 and kfree handles 0. > > what if `strcmp(obj_desc->type, "dprc") == 0` is false? We allocate > `mc_dev`, but then call kfree on `mc_bus`? mc_bus is initialized to NULL, which makes the call to kfree safe. The original handler was if (mc_bus)   kfree(mc_bus) else   kfree(mc_dev) I tried this first, which works, but checkpatch throw a warning for kfree(mc_bus). This change makes the 'else' with the !mc_bus > I think it would be safer to locally save the result of > `strcmp(obj_desc->type, "dprc") == 0`, then check that throughout this the local mc_bus is only set in this block, so I don't think another local is needed. > function, including the error handling at the end, or use multiple > labels to unwind the allocations correctly. The goto's could be finer grained because some of the mc_dev->regions are known to be unallocated. Changing these would not be a fix and it could be argued the simpler, less efficent error handling works as designed. Tom > >> return error; >> } >> -- > > -- > Thanks, > ~Nick Desaulniers >