Received: by 10.223.185.116 with SMTP id b49csp6085528wrg; Wed, 28 Feb 2018 03:50:37 -0800 (PST) X-Google-Smtp-Source: AG47ELvGKv++h1KN/gEJzZGNVunR7Byg+BitVHXdJ/21WcM9zdO5f6Rp8iYVfs61ia+UjjLFaZ7P X-Received: by 2002:a17:902:a613:: with SMTP id u19-v6mr12539454plq.92.1519818637486; Wed, 28 Feb 2018 03:50:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519818637; cv=none; d=google.com; s=arc-20160816; b=nY9CQG9WpQVRQcabvhst+6El4N/bpavd714FIbrKwGP4sRnzW6WirsgjOfVib3M86h iPoDv9q7koWSB/Yjx5qz9+nImrtRlbY4lam7aS9sNCddFJvtQeZquEM+bkfGcfy01Jku tD/Q+hMvZ5VWrraeOpcjUP1EKz1QpC6KxHZEAiZVLuatxWdK+zmbpWlOJSRglk78KGTi 8xRkA0Yu2vWHd+JDbWCWkEhcYXRxlEEt+kpBa/0VoVO/9lOKpAao/sT0i3UOq5V2/7U1 aq6+bE5L4ia2AsgF5IMa/MxydjF6CK1kofUNXfKT4435IvioS3DVqUxKUBbkEU0qd3yr jcmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=CnZN4QFCScoGaGyQqFgoq+Jhld7IOlR7IPpoUG4ZM40=; b=fdKjO3kvKgteDpS6X0xLNe5OH09DLcZoIyVYSZxeiK1CM1f0YC9mrI5JSaZ8hTGvKa OHRC5nwVL6brO6lgHW2rIrBoSSA+9jtcay5nEiS2pEpphVOxSXbGNamysBZX/eNEaA2H KrKOehawD+y4UxjZ08t8/8Cg9/oOtgaoBveppht3B2dR9ec/TF0P+k6wNRzTV1nx6TEF N6Qjkek+xJbmAIKHIP8rOcYUWgpBZEX1wo2u46HUOB5ph4JNfNdCHOLmMtQ/kVYHChf1 KIYn75YfW7TJjF1P0sN/yJPcEmN6XiqxSAWEozUuNlTTPdvh2I66wCB7DERXU95UYVzF tNyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ElgQJ7+h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a13si1119873pfc.255.2018.02.28.03.50.19; Wed, 28 Feb 2018 03:50:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ElgQJ7+h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752241AbeB1Ltj (ORCPT + 99 others); Wed, 28 Feb 2018 06:49:39 -0500 Received: from mail-wr0-f194.google.com ([209.85.128.194]:41116 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752147AbeB1Lti (ORCPT ); Wed, 28 Feb 2018 06:49:38 -0500 Received: by mail-wr0-f194.google.com with SMTP id f14so2073669wre.8 for ; Wed, 28 Feb 2018 03:49:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=CnZN4QFCScoGaGyQqFgoq+Jhld7IOlR7IPpoUG4ZM40=; b=ElgQJ7+hhfITxiNpnChfX5SABUh8i83B1j21HZPPTJF0eBTVbAyAmjQ3xuTrq8dmOX TrLcy7D5L5yiLV1i0XD2xavM0Q3b+Y+GM/8PmKYVY9w7MnsZ4vxsRTbZatAEVu501uFM n787J6nVrPJ96yObl80Za2iHTft40kA0Ujo0Xldbk7jsKcSZJEqq8m9FloywH9WUHELG axO0eqIpO7ZuFL+y5WZBz4NoWT1SyTrrKfbIO8mKtrrzCQEhxOq+lIAMFqf6lW3meNIS B0QshO7vwx+xtzifvbdKQKO5pxCffyBt7O70nNVRDot8N7TIeBZqxtZLL8bUV2UWa5Hg 1vZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=CnZN4QFCScoGaGyQqFgoq+Jhld7IOlR7IPpoUG4ZM40=; b=GpQ1XVkQgQ4LVLH+W6fxN2Q7Z4UXhh09Z+lnvx7wNXESLHXDfzL0+m7htbr8/rAmWM NE/TT5UtshnA/peJHNZmkabTuq4mtZ2GLVsb9kDdXrk4BxA7bYMhrXvfsxUQEsgP48UE c+EMNG41c4vF7ODwTLdTO8U0EQjjRCJJqok+mSK8V0/UgFVgolOSa3hylcTJ5lZyk3r5 MqUsg25wuz0rMU1J994CiiQJU5O7YtxMQsJZDzc7+GreRn8fJg8Zi+flniqKMtEGFX7B NpivlL7n0aeaILDlICwuLxjlH9syliqFEMxJkSaUQjXnCx9rqs0MOp0NrqnFKRFQCQ3r ryAw== X-Gm-Message-State: APf1xPA8ADLWwlLD7Vsie9io+qVbdtgOJxxy068vUy0JkgwsRywxZb59 a/V/35vMpyqqGakf8/FhGK95ySXouuBTY3qWiRfCGA4m X-Received: by 10.223.132.197 with SMTP id 63mr1564598wrg.166.1519818576957; Wed, 28 Feb 2018 03:49:36 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.185.67 with HTTP; Wed, 28 Feb 2018 03:49:35 -0800 (PST) In-Reply-To: <20180227021150-mutt-send-email-mst@kernel.org> References: <20180215213312.29234-1-marcandre.lureau@redhat.com> <20180215213312.29234-9-marcandre.lureau@redhat.com> <20180227021150-mutt-send-email-mst@kernel.org> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Wed, 28 Feb 2018 12:49:35 +0100 Message-ID: Subject: Re: [PATCH v15 08/11] fw_cfg: handle fw_cfg_read_blob() error To: "Michael S. Tsirkin" Cc: open list , slp@redhat.com, bhe@redhat.com, somlo@cmu.edu, xiaolong.ye@intel.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi On Tue, Feb 27, 2018 at 1:20 AM, Michael S. Tsirkin wrote: > On Thu, Feb 15, 2018 at 10:33:09PM +0100, Marc-Andr=C3=A9 Lureau wrote: >> fw_cfg_read_blob() may fail, but does not return error. This may lead >> to undefined behaviours, such as a memcmp(sig, "QEMU") on uninitilized >> memory. > > I don't think that's true - there's a memset there that > will initialize the memory. probe is likely the only > case where it returns a slightly incorrect data. Right, I'll update the commit message. >> Return an error if ACPI locking failed. Also, the following >> DMA read/write extension will add more error paths that should be >> handled appropriately. >> >> Signed-off-by: Marc-Andr=C3=A9 Lureau >> --- >> drivers/firmware/qemu_fw_cfg.c | 32 ++++++++++++++++++++++---------- >> 1 file changed, 22 insertions(+), 10 deletions(-) >> >> diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_c= fg.c >> index f6f90bef604c..5e6e5ac71dab 100644 >> --- a/drivers/firmware/qemu_fw_cfg.c >> +++ b/drivers/firmware/qemu_fw_cfg.c >> @@ -59,8 +59,8 @@ static void fw_cfg_sel_endianness(u16 key) >> } >> >> /* read chunk of given fw_cfg blob (caller responsible for sanity-check= ) */ >> -static void fw_cfg_read_blob(u16 key, >> - void *buf, loff_t pos, size_t count) >> +static ssize_t fw_cfg_read_blob(u16 key, >> + void *buf, loff_t pos, size_t count) >> { >> u32 glk =3D -1U; >> acpi_status status; >> @@ -73,7 +73,7 @@ static void fw_cfg_read_blob(u16 key, >> /* Should never get here */ >> WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n"); >> memset(buf, 0, count); >> - return; >> + return -EINVAL; >> } >> >> mutex_lock(&fw_cfg_dev_lock); > > Wouldn't something like -EBUSY be more appropriate? In theory, it would be a general failure right? I don't think we want the caller to retry. I think in EINVAL fits better, but I don't think it matters much this or EBUSY. >> @@ -84,6 +84,7 @@ static void fw_cfg_read_blob(u16 key, >> mutex_unlock(&fw_cfg_dev_lock); >> >> acpi_release_global_lock(glk); >> + return count; >> } >> >> /* clean up fw_cfg device i/o */ >> @@ -165,8 +166,9 @@ static int fw_cfg_do_platform_probe(struct platform_= device *pdev) >> } >> >> /* verify fw_cfg device signature */ >> - fw_cfg_read_blob(FW_CFG_SIGNATURE, sig, 0, FW_CFG_SIG_SIZE); >> - if (memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) !=3D 0) { >> + if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig, >> + 0, FW_CFG_SIG_SIZE) < 0 || >> + memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) !=3D 0) { >> fw_cfg_io_cleanup(); >> return -ENODEV; >> } >> @@ -326,8 +328,7 @@ static ssize_t fw_cfg_sysfs_read_raw(struct file *fi= lp, struct kobject *kobj, >> if (count > entry->size - pos) >> count =3D entry->size - pos; >> >> - fw_cfg_read_blob(entry->select, buf, pos, count); >> - return count; >> + return fw_cfg_read_blob(entry->select, buf, pos, count); >> } >> >> static struct bin_attribute fw_cfg_sysfs_attr_raw =3D { >> @@ -483,7 +484,11 @@ static int fw_cfg_register_dir_entries(void) >> struct fw_cfg_file *dir; >> size_t dir_size; >> >> - fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count, 0, sizeof(files_co= unt)); >> + ret =3D fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count, >> + 0, sizeof(files_count)); >> + if (ret < 0) >> + return ret; >> + >> count =3D be32_to_cpu(files_count); >> dir_size =3D count * sizeof(struct fw_cfg_file); >> >> @@ -491,7 +496,10 @@ static int fw_cfg_register_dir_entries(void) >> if (!dir) >> return -ENOMEM; >> >> - fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(files_count), dir_si= ze); >> + ret =3D fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, >> + sizeof(files_count), dir_size); >> + if (ret < 0) >> + goto end; >> >> for (i =3D 0; i < count; i++) { >> ret =3D fw_cfg_register_file(&dir[i]); >> @@ -499,6 +507,7 @@ static int fw_cfg_register_dir_entries(void) >> break; >> } >> >> +end: >> kfree(dir); >> return ret; >> } >> @@ -539,7 +548,10 @@ static int fw_cfg_sysfs_probe(struct platform_devic= e *pdev) >> goto err_probe; >> >> /* get revision number, add matching top-level attribute */ >> - fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev)); >> + err =3D fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev)); >> + if (err < 0) >> + goto err_probe; >> + >> fw_cfg_rev =3D le32_to_cpu(rev); >> err =3D sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); >> if (err) > > I think that this is the only case where it's not doing the right thing r= ight now in > that it shows 0 as the revision to the users. Is it worth failing probe > here? We could just skip the attribute, could we not? I think it's best to fail the probe if we have a read failure at that time. --=20 Marc-Andr=C3=A9 Lureau