Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp2654898iob; Fri, 6 May 2022 07:39:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwT2celQHiHdLvPEMeAUnEfPoGu6WctqrdiwYuq0cyIqFA9nraajfFAOUmBgTqyD80A4cJz X-Received: by 2002:a63:e51:0:b0:3a4:9d22:1fd5 with SMTP id 17-20020a630e51000000b003a49d221fd5mr3108227pgo.586.1651847982634; Fri, 06 May 2022 07:39:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651847982; cv=none; d=google.com; s=arc-20160816; b=ZQwsiRT0ZK0n1D6wHcJwVVlqVFmWIz3C1tqNiWKCKVOir4q1AjuPyW1ejCYqfdh9dR t2iYSjgDgA8e9Hn+SSoFnnMwkXf5Hj5wd3eg6Vnn/nZc2sML6GaAB0v08nC6YZ+1IZJ4 m1esn06DjXUOcibjF0ZKoCaaG2VzqfxXLtQtyWmSt/TydzElJj25oQ2pm24dg3ZDprlu sw8+NmkwbcsEKQ2FBbQAVaIpIGFYd2Ch1ZVkL3I7T3zGKt9LcEEP0YBVQQmVMnlML07y yKm7CxWCKj2CY7XrtxhkyXj+SaKyD2BQMkL9nDflErWtc1/OlQbeaUXWJv46uBlUagzj XsWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gf1HHdqHUd672dNLSZlWTc3P/aQGVhVSG6NMJlWMUC8=; b=WCwa+pdGRn+b8wMrJBlWzLcgPCoaAO3tKwxvO1rDbUDkZL1HEm39qpM7RFPJml0KwE wxyFDQZ8dwCJAP7m+otZ5u/b6EbfymFiNFWcQntS1EokojAPBSEye++PAPqstdBXp5od fEhvNT/wB3YOaCRQKIvUlmLLU3YpPEbXI3Auqq+4h9JP7n8YTCX2CB9r9WlbrqJ8OBj7 jZnhXdSaik1yqUkTEbX43Am7vAoBgGnVkjjLQs31I0edc1FSzhGhO58OW0sw7YbdJzBx nRoe22hjyg81aokiVPA3Kx/udqOcnaJ4lIBnS70bNuPnf0siog3hvinmeXzCcuwovRUt 8jtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dpxJGPDj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e7-20020a656bc7000000b003a9f944b0cfsi5348742pgw.558.2022.05.06.07.39.25; Fri, 06 May 2022 07:39:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dpxJGPDj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354959AbiEDRDd (ORCPT + 99 others); Wed, 4 May 2022 13:03:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354597AbiEDQ6y (ORCPT ); Wed, 4 May 2022 12:58:54 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC1C71EECF; Wed, 4 May 2022 09:50:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 53DD0617BD; Wed, 4 May 2022 16:50:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A17CC385AA; Wed, 4 May 2022 16:50:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683043; bh=+J6mkW7g7FZT8T1bJ9fP3KdetTDCgArgqcBdHrc4ShY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dpxJGPDje5iDDKJQJvdzneBXOHIkLJRksauD7x82UYx8q8n2QZ3i/WaazynUP6Xyl +QvexhRAocqftmWzXna4oGdgWvwqaJ6P+YechbPvdGPd0DflG2onHHlkNbBc4iDEaU SRUgTLUt31iRGbuyckN1mGo8dq7EtsdNMyEB4bS0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikulas Patocka , Andy Shevchenko , Linus Torvalds Subject: [PATCH 5.10 034/129] hex2bin: fix access beyond string end Date: Wed, 4 May 2022 18:43:46 +0200 Message-Id: <20220504153023.967318541@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153021.299025455@linuxfoundation.org> References: <20220504153021.299025455@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mikulas Patocka commit e4d8a29997731b3bb14059024b24df9f784288d0 upstream. If we pass too short string to "hex2bin" (and the string size without the terminating NUL character is even), "hex2bin" reads one byte after the terminating NUL character. This patch fixes it. Note that hex_to_bin returns -1 on error and hex2bin return -EINVAL on error - so we can't just return the variable "hi" or "lo" on error. This inconsistency may be fixed in the next merge window, but for the purpose of fixing this bug, we just preserve the existing behavior and return -1 and -EINVAL. Signed-off-by: Mikulas Patocka Reviewed-by: Andy Shevchenko Fixes: b78049831ffe ("lib: add error checking to hex2bin") Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- lib/hexdump.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -63,10 +63,13 @@ EXPORT_SYMBOL(hex_to_bin); int hex2bin(u8 *dst, const char *src, size_t count) { while (count--) { - int hi = hex_to_bin(*src++); - int lo = hex_to_bin(*src++); + int hi, lo; - if ((hi < 0) || (lo < 0)) + hi = hex_to_bin(*src++); + if (unlikely(hi < 0)) + return -EINVAL; + lo = hex_to_bin(*src++); + if (unlikely(lo < 0)) return -EINVAL; *dst++ = (hi << 4) | lo;