From patchwork Thu Jan 28 13:12:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 1432792 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DRLW00Yfcz9sVv for ; Fri, 29 Jan 2021 00:12:59 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 148B182774; Thu, 28 Jan 2021 14:12:52 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 1BFF18275B; Thu, 28 Jan 2021 14:12:47 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 97BF28274D for ; Thu, 28 Jan 2021 14:12:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=nsaenzjulienne@suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5D31EAE8D; Thu, 28 Jan 2021 13:12:44 +0000 (UTC) From: Nicolas Saenz Julienne To: andriy.shevchenko@linux.intel.com, sjg@chromium.org Cc: trini@konsulko.com, mbrugger@suse.com, u-boot@lists.denx.de, Nicolas Saenz Julienne Subject: [PATCH v2 1/2] stdio: Introduce stdio_valid() Date: Thu, 28 Jan 2021 14:12:39 +0100 Message-Id: <20210128131240.13190-2-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210128131240.13190-1-nsaenzjulienne@suse.de> References: <20210128131240.13190-1-nsaenzjulienne@suse.de> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean stdio_valid() will confirm that a struct stdio_dev pointer is indeed valid. Signed-off-by: Nicolas Saenz Julienne Reviewed-by: Simon Glass --- Changes since v1: - Properly document function common/stdio.c | 11 +++++++++++ include/stdio_dev.h | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/common/stdio.c b/common/stdio.c index abf9b1e915..69b7d2692d 100644 --- a/common/stdio.c +++ b/common/stdio.c @@ -157,6 +157,17 @@ static int stdio_probe_device(const char *name, enum uclass_id id, return 0; } +bool stdio_valid(struct stdio_dev *dev) +{ + struct stdio_dev *sdev; + + list_for_each_entry(sdev, &devs.list, list) + if (sdev == dev) + return true; + + return false; +} + struct stdio_dev *stdio_get_by_name(const char *name) { struct list_head *pos; diff --git a/include/stdio_dev.h b/include/stdio_dev.h index 48871a6a22..e9d610a840 100644 --- a/include/stdio_dev.h +++ b/include/stdio_dev.h @@ -98,6 +98,17 @@ struct list_head *stdio_get_list(void); struct stdio_dev *stdio_get_by_name(const char *name); struct stdio_dev *stdio_clone(struct stdio_dev *dev); +/** + * stdio_valid() - Validate stdio device exists + * + * Verifies that dev is contained in the global list of stdio devices. + * + * @dev: Stdio device pointer + * + * returns 'true' when valid, 'false' otherwise + */ +bool stdio_valid(struct stdio_dev *dev); + int drv_lcd_init(void); int drv_video_init(void); int drv_keyboard_init(void); From patchwork Thu Jan 28 13:12:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Saenz Julienne X-Patchwork-Id: 1432793 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DRLW95gYSz9sVv for ; Fri, 29 Jan 2021 00:13:09 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 10139827BF; Thu, 28 Jan 2021 14:12:54 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 85C1782797; Thu, 28 Jan 2021 14:12:49 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id F403982757 for ; Thu, 28 Jan 2021 14:12:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=nsaenzjulienne@suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id BCBA0AEA3; Thu, 28 Jan 2021 13:12:44 +0000 (UTC) From: Nicolas Saenz Julienne To: andriy.shevchenko@linux.intel.com, sjg@chromium.org Cc: trini@konsulko.com, mbrugger@suse.com, u-boot@lists.denx.de, Nicolas Saenz Julienne Subject: [PATCH v2 2/2] console: Don't start/stop console if stdio device invalid Date: Thu, 28 Jan 2021 14:12:40 +0100 Message-Id: <20210128131240.13190-3-nsaenzjulienne@suse.de> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210128131240.13190-1-nsaenzjulienne@suse.de> References: <20210128131240.13190-1-nsaenzjulienne@suse.de> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Don't start/stop an stdio device that might have been already freed. Signed-off-by: Nicolas Saenz Julienne Fixes: 70c2525c0d3c ("IOMUX: Stop dropped consoles") Reviewed-by: Simon Glass --- Changes since v1: - Add comment stating this should be properly fixed common/console.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/console.c b/common/console.c index f3cc45cab5..570f26d149 100644 --- a/common/console.c +++ b/common/console.c @@ -252,6 +252,15 @@ static bool console_needs_start_stop(int file, struct stdio_dev *sdev) { int i, j; + /* + * TODO: This is a workaround to avoid accessing freed memory: + * console_stop() might be called on an stdio_dev that has already been + * de-registered, due to the fact that stdio_deregister_dev() + * doesn't update the global console_devices array. + */ + if (!stdio_valid(sdev)) + return false; + for (i = 0; i < ARRAY_SIZE(cd_count); i++) { if (i == file) continue;