Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1938997iob; Thu, 5 May 2022 11:19:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXdNKhk2sIbxlrYHet+QHejHDwH/gwX4+/J+jcG7Y0rQ3oZEL4SewLfoYt7FcCCPZYxhGa X-Received: by 2002:a17:90a:dc0c:b0:1da:125a:ec84 with SMTP id i12-20020a17090adc0c00b001da125aec84mr7590771pjv.137.1651774748381; Thu, 05 May 2022 11:19:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651774748; cv=none; d=google.com; s=arc-20160816; b=uZCvpA3bSQgN+nl8UJCqApSuDiEEnUk6Vpa9Lv5/R7W5nV5g0+uNBkv0YvV9657SjI d2y/0Vbc7O31jvgWUqaVRw4Rh4MjtJrq63HT8hKB53G9t2X5XrfkoSstGZH3neZ7BNgf 26mKG7ywOczui6UhRKv+bWXMtaw+jzIkUdZFbbK5y2WawqDYORQaf8sk2s2iGEy+iYZ/ /ze1V07UCpD7VV+WF+RpVrVfUMfxeuR9owUMyJLSYuPg7rbYmcwFS5YqrX5YvjOiFLes geYMnf8RVmER/NGZ4qya1tRmatHZ5oe+iNcI8jv/z1Jtd76y1VOd01y61cdbtRSlbD0v WtEQ== 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=NaagyVKKVrpl9Udhlp3R01+dPXIPApm/aZ5Y/gLDWWeuGmygX8VCUIyi2GpLW5xZsO GgBDoc+O7cP8plF7nNrb0jtHcwyS+O88l2iYrrtgTREW9vtgNXcPB6bNdG+Yc5+qJicA 5hDK82veMH3ZNuwwbYnTHyKhWAre0gqWKG6TMJ76AoKhrIHsGHWuMhTuc02N4LegEeMp mpTHd5+Ima8FvTvFCv6I3kaaW9XObUieg3ZOX9gzqpubTLmrss/sr9Z/m3/SmDjoXDOO g8Dx31WCjDTg37Xw6nQbRGILMvjLqB9P67PNe2iSChOGe8CmJIoubXQQgiGa6wnIhLCD kQ/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EKJZQtLA; 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 u184-20020a6385c1000000b003c2648bfbf4si2093186pgd.3.2022.05.05.11.18.49; Thu, 05 May 2022 11:19:08 -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=EKJZQtLA; 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 S1358048AbiEDRPh (ORCPT + 99 others); Wed, 4 May 2022 13:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355475AbiEDRE2 (ORCPT ); Wed, 4 May 2022 13:04:28 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA4254EDDC; Wed, 4 May 2022 09:53:04 -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 898F861505; Wed, 4 May 2022 16:53:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2629C385AA; Wed, 4 May 2022 16:53:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683184; bh=+J6mkW7g7FZT8T1bJ9fP3KdetTDCgArgqcBdHrc4ShY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EKJZQtLAZNRWK/v83KC4GPQ6jTjUB2hvyODqxhTI5yDbfw55NLq/McCB1iMhfQh6a Aa8FraV3zcplmY2oHMJ/Z/qNKH1mMz99GzMedbvv3yxThzjlBdQC4bf0xMqM0g6ar5 910fsZ0O4Jq9o7+4FbAPa29N0IwKRYl8z+dArulI= 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.15 040/177] hex2bin: fix access beyond string end Date: Wed, 4 May 2022 18:43:53 +0200 Message-Id: <20220504153056.515747012@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153053.873100034@linuxfoundation.org> References: <20220504153053.873100034@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;