Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3368434pxb; Sun, 31 Jan 2021 13:41:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJw2iwVKwqNfOnD6tqrrVDlVX2dVE+itIu/mNdjp04jmzwNn4Jjufle6fjUFePb2MWlziobv X-Received: by 2002:aa7:c390:: with SMTP id k16mr15578418edq.280.1612129282951; Sun, 31 Jan 2021 13:41:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612129282; cv=none; d=google.com; s=arc-20160816; b=W5tSU8UaHC8rl+VUEp6EESBnAYcebRvHKMAu5U4v8aE61igtuYz3lV4et/uR9cnAd1 HA3HZXCvFRa7f7b32ipRbv+HtMfTyhISteaZUC5jN9hCexeHMVtMLNlHAMjKsf7IGXUr SvcMttnbJabTsZ5WQwd30cMW1cpLMzZ1himtnWtRvvOei1CYssb5ZzhVClPhgKmHVLa8 ouPaF1QgYd7Hso2//t60dq6SOss99CqD1c4+o+qBktIwuGXki7WmeDqgVsA0KXgl+Fuj lxcz/mxC3qPFYKBkvMxYZyZt9+uid6lEazh+GyoVUOh6pOG4XlxCxcc759xkS/Gyg9o6 3g3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=BTdfeZUlmDAGnUPUg1Sx+tWBlHdEKi++bIDvsDvVpgY=; b=IqD7Q0xzke459n3bU4Wr9FGlELwX32EFzoCcN7ojc0XRF0FWrh0cAjqPalsjZ+sfEd aoDxkt2J3jTd/YI9JumitRG6KDeeHZwRQ7EUQ6pMJaf7Bae3CCqrsyof/TvwJMR4iyTL j46cjl4se6z3YdGdorUyOGD6E6nd+SzfsgAFMmw+kFtUTvyByGCgRc5ODxJM6wFADwL7 u8ykL4vkfUOZBKl0P0ni7FDNFhZMorkeawb9D8pHimydvZGBOUlobqG/ce9M/r2Z/ty2 TKDVO9rMJ34hQeGPlM2URuB+2NzVTsxh1eCeuxK5r+M4z8Z+4osNj71BO5Rc3L+UOeKf uqiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B8xQ6At5; 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 u22si9062729ejc.544.2021.01.31.13.40.58; Sun, 31 Jan 2021 13:41:22 -0800 (PST) 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=B8xQ6At5; 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 S229692AbhAaVGY (ORCPT + 99 others); Sun, 31 Jan 2021 16:06:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:32934 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231453AbhAaVAc (ORCPT ); Sun, 31 Jan 2021 16:00:32 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612126745; 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=BTdfeZUlmDAGnUPUg1Sx+tWBlHdEKi++bIDvsDvVpgY=; b=B8xQ6At5iBbNkz8F1PmFCvLWCDGMui4HDa1Cntw0o4bCKjeDZs6fcYJ/gdYeO081ULacAg 9/sE73MOXZKMw6IX5qLCn8eFprAoXoXSMTxEcdIGOQvZ5T5r+J8Juwu8zIVNW52K50AoOi yfOEqjbnDpvyYtmCxjTBdKXuTdWv8Qg= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-342-gn9vdxpeNjSU_FJO3xXkPg-1; Sun, 31 Jan 2021 15:59:03 -0500 X-MC-Unique: gn9vdxpeNjSU_FJO3xXkPg-1 Received: by mail-ed1-f69.google.com with SMTP id a24so6900009eda.14 for ; Sun, 31 Jan 2021 12:59:03 -0800 (PST) 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-language :content-transfer-encoding; bh=BTdfeZUlmDAGnUPUg1Sx+tWBlHdEKi++bIDvsDvVpgY=; b=tM3mX0UR+NPEEMTG8y+QncYGqGPsE24JNpemYEO0BQmRC1CquVN0xgWNtMqvMQHKT3 XIFHKxcbflxjlvba7OXGEWlZT/n3M1G9vSqg8If27NgfpbJC0YuzalfsnuZNfAOtNffc rCC9TmUJikHBgrZlzWOdPmuI5HXB7qAOgDQVz2QANYmVyCnbJNyuomF9r0khh2FXttd7 L+UvIdEr8Ws1JtkDV5SH1b6Re5XeOgnWoLEjMkc/FsXWVmFiQvtTP8pSdEg0FUx4clzq jW4Zall+qh/Bouz7RO21GmpSWVk6g7pmLvBBrnPqpiMg0wM3VP5XtcC6cvhqyXQ3MF2F bvBQ== X-Gm-Message-State: AOAM532PG4HvutewZhKyrBr/4+ViULvLye2O5B2WkIPjjQKyVm8oAwmM +MdZFZWRrdvneAw7vlm86PuM37Ptrblg95oMzh/do68GOFzJZcVKOUr8YVCsYcJqJQt5oHnRvnp b7xdgfke6y+lAl7o1SE6P6uCU X-Received: by 2002:a17:906:69c2:: with SMTP id g2mr14105193ejs.249.1612126741663; Sun, 31 Jan 2021 12:59:01 -0800 (PST) X-Received: by 2002:a17:906:69c2:: with SMTP id g2mr14105189ejs.249.1612126741520; Sun, 31 Jan 2021 12:59:01 -0800 (PST) Received: from x1.localdomain (2001-1c00-0c1e-bf00-37a3-353b-be90-1238.cable.dynamic.v6.ziggo.nl. [2001:1c00:c1e:bf00:37a3:353b:be90:1238]) by smtp.gmail.com with ESMTPSA id cw21sm7601846edb.85.2021.01.31.12.59.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 31 Jan 2021 12:59:00 -0800 (PST) Subject: Re: [PATCH] platform/x86: dell-wmi-sysman: fix a NULL pointer dereference To: "Limonciello, Mario" , Mark Gross Cc: LKML , "platform-driver-x86@vger.kernel.org" References: <20210129172654.2326751-1-mario.limonciello@dell.com> <0da9ca30-53b1-8d34-4fc7-62edb6423b26@redhat.com> From: Hans de Goede Message-ID: Date: Sun, 31 Jan 2021 21:59:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 1/31/21 3:04 PM, Limonciello, Mario wrote: > > >> -----Original Message----- >> From: Hans de Goede >> Sent: Saturday, January 30, 2021 15:44 >> To: Limonciello, Mario; Mark Gross >> Cc: LKML; platform-driver-x86@vger.kernel.org >> Subject: Re: [PATCH] platform/x86: dell-wmi-sysman: fix a NULL pointer >> dereference >> >> >> [EXTERNAL EMAIL] >> >> Hi, >> >> On 1/29/21 6:26 PM, Mario Limonciello wrote: >>> An upcoming Dell platform is causing a NULL pointer dereference >>> in dell-wmi-sysman initialization. Validate that the input from >>> BIOS matches correct ACPI types and abort module initialization >>> if it fails. >>> >>> This leads to a memory leak that needs to be cleaned up properly. >>> >>> Signed-off-by: Mario Limonciello >>> --- >>> drivers/platform/x86/dell-wmi-sysman/sysman.c | 8 +++++++- >>> 1 file changed, 7 insertions(+), 1 deletion(-) >>> >>> diff --git a/drivers/platform/x86/dell-wmi-sysman/sysman.c >> b/drivers/platform/x86/dell-wmi-sysman/sysman.c >>> index dc6dd531c996..38b497991071 100644 >>> --- a/drivers/platform/x86/dell-wmi-sysman/sysman.c >>> +++ b/drivers/platform/x86/dell-wmi-sysman/sysman.c >>> @@ -419,13 +419,19 @@ static int init_bios_attributes(int attr_type, const >> char *guid) >>> return retval; >>> /* need to use specific instance_id and guid combination to get right >> data */ >>> obj = get_wmiobj_pointer(instance_id, guid); >>> - if (!obj) >>> + if (!obj || obj->type != ACPI_TYPE_PACKAGE) { >>> + release_attributes_data(); >> >> All calls of init_bios_attributes() have the following error handling: >> >> ret = init_bios_attributes(INT, DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID); >> if (ret) { >> pr_debug("failed to populate integer type attributes\n"); >> goto fail_create_group; >> } >> >> ... >> >> fail_create_group: >> release_attributes_data(); >> >> So that added release_attributes_data() call is not necessary. If you can >> respin >> this patch Monday with the release_attributes_data(); addition dropped, then >> I will try to get this to Linus in time for 5.11 . >> >> Or I can fix this up locally if you agree with dropping the unnecessary >> release_attributes_data() call. >> > > Yes, please go ahead and drop the unnecessary call locally. Ok, I've merged this into my review-hans branch and I will push this out to for-next as soon as a local build has finished. I'll also include this in my last fixes pull-req for Linus later this week. While working on this I did notice that the function in question still has a bunch of further issues. But since this patch fixes a crash and has been tested I've decided to move forward with it as is (with the duplicate release_attributes_data() call dropped). The further issues can be fixed with follow-up patches. So the other issues which I noticed are: 1. Calling release_attributes_data() in the error-path here: obj = get_wmiobj_pointer(instance_id, guid); if (!obj || obj->type != ACPI_TYPE_PACKAGE) { return -ENODEV; } Is not necessary as discussed, but the added obj->type != ACPI_TYPE_PACKAGE which I assume triggers to fix the reported crash, means that obj is not NULL in which case we should free it. So this should become: obj = get_wmiobj_pointer(instance_id, guid); if (!obj || obj->type != ACPI_TYPE_PACKAGE) { kfree(obj); return -ENODEV; } Note that the kfree() will be a no-op when obj == NULL. This means that with just the current fix merged there is a small memleak on machines where we hit the error-path. I've decided that we can live with that, since the alternative is the crash or me pushing something untested. 2. There is a while below this if, which gets a new obj pointer: obj = get_wmiobj_pointer(instance_id, guid); if (!obj || obj->type != ACPI_TYPE_PACKAGE) { kfree(obj); return -ENODEV; } elements = obj->package.elements; mutex_lock(&wmi_priv.mutex); while (elements) { ... nextobj: kfree(obj); instance_id++; obj = get_wmiobj_pointer(instance_id, guid); elements = obj ? obj->package.elements : NULL; } This is missing a check for the obj->type for the new obj when going into a second (or higher) iteration of the loop. This check can be added by moving the original check to inside the loop like this: obj = get_wmiobj_pointer(instance_id, guid); mutex_lock(&wmi_priv.mutex); while (obj) { if (obj->type != ACPI_TYPE_PACKAGE) { err = ENODEV; goto err_attr_init; } elements = obj->package.elements; ... nextobj: kfree(obj); instance_id++; obj = get_wmiobj_pointer(instance_id, guid); } 3. Functions like populate_enum_data() (and the others) index the elements array with an index > 0 without checking the package length and also make assumptions about the types embedded in the package without checking them. 4. The err_attr_init exit path of init_bios_attributes() calls release_attributes_data() but that call does not just cleanup the results of that single init_bios_attributes() call but also of all previous init_bios_attributes() calls as such it makes more sense to leave the calling of release_attributes_data() to the caller. Either way calling it twice once from the err_attr_init exit path and then again in sysman_init() feels wrong, even though I think it does no harm. Regards, Hans > > Thank you > >> Regards, >> >> Hans >> >> >> >> >>> return -ENODEV; >>> + } >>> elements = obj->package.elements; >>> >>> mutex_lock(&wmi_priv.mutex); >>> while (elements) { >>> /* sanity checking */ >>> + if (elements[ATTR_NAME].type != ACPI_TYPE_STRING) { >>> + pr_debug("incorrect element type\n"); >>> + goto nextobj; >>> + } >>> if (strlen(elements[ATTR_NAME].string.pointer) == 0) { >>> pr_debug("empty attribute found\n"); >>> goto nextobj; >>> >