From patchwork Mon Jul 24 12:40:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 13324576 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E03B3C0015E for ; Mon, 24 Jul 2023 12:41:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231124AbjGXMlH (ORCPT ); Mon, 24 Jul 2023 08:41:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231147AbjGXMlD (ORCPT ); Mon, 24 Jul 2023 08:41:03 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8053910D5 for ; Mon, 24 Jul 2023 05:41:01 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C8AC520438; Mon, 24 Jul 2023 12:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690202459; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=QGG6yARk8oD9OpOPL20nYB14tUUNPvr6doBPFl+MbNg=; b=T8Wt6D7NyC1QJGtBCepOa5nAphS3RifRW6PbPLiOd6AUBkc84wSB5NlQXyt21pfGMf7bA4 NRSr3Csuutk+nQFsM/pUpfFxAE90JyS/KvuIEAkGJb8pzipM68hcH9VE+d+sOO9W3pQ5Ju kV/sFPGxGphFpS3qv4EUnyFJRZS2IVI= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 869AC138E8; Mon, 24 Jul 2023 12:40:59 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id xVDhHltxvmR8HQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 12:40:59 +0000 From: Oliver Neukum To: gregkh@linuxfoundation.org, stern@rowland.harvard.edu, liulongfang@huawei.com, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [PATCH] USB: hub: make sure stale buffers are not enumerated Date: Mon, 24 Jul 2023 14:40:57 +0200 Message-ID: <20230724124057.12975-1-oneukum@suse.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Quoting Alan Stern on why we cannot just check errors: The operation carried out here is deliberately unsafe (for full-speed devices). It is made before we know the actual maxpacket size for ep0, and as a result it might return an error code even when it works okay. This shouldn't happen, but a lot of USB hardware is unreliable. Therefore we must not ignore the result merely because r < 0. If we do that, the kernel might stop working with some devices. He is absolutely right. However, we must make sure that in case we read nothing or a short answer, the buffer contains nothing that can be misinterpreted as a valid answer. So we have to zero it before we use it for IO. Reported-by: liulongfang Signed-off-by: Oliver Neukum --- drivers/usb/core/hub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index a739403a9e45..9772716925c3 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -4873,7 +4873,8 @@ hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, } #define GET_DESCRIPTOR_BUFSIZE 64 - buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); + /* zeroed so we don't operate on a stale buffer on errors */ + buf = kzalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); if (!buf) { retval = -ENOMEM; continue;