Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1973582rwo; Thu, 3 Aug 2023 02:51:37 -0700 (PDT) X-Google-Smtp-Source: APBJJlFFWP5PtkeebqvcimNPA2YfYdFY/oz8ZMS2hEkapIEpfS/stc0qZmkHAaujeGbLT8lYopZU X-Received: by 2002:a05:6358:419b:b0:134:ce45:b782 with SMTP id w27-20020a056358419b00b00134ce45b782mr9535180rwc.21.1691056297140; Thu, 03 Aug 2023 02:51:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691056297; cv=none; d=google.com; s=arc-20160816; b=ksfNpyxnZ9dXbi9pDsWEu+MNwptxABH+zRCLYY2TMX/3MiTfoE+Dowlb+3E+h4mak9 6geuWzl54iewGkXXyg0El+KIahMA4wRoV2FfT4Qs7DRA13clSLATlOPC9Mvz6OsKTQD3 cAyrBPsGxiGClF8H0EOs3E71PduAklAUID87Is8Yv4IuM8dJcXolvYMC3CU9MFcW+te8 5puE9DIOZHpFcY2hR15FCGVw1DOZNgIPGePH0Q5d5VVn49HR5+5ziMFyJ9BPpSLbz+6Z JS0KFuDUEgRWn6HBiImb88Q/1qwhAECKSw4wBCm22yAAHai/oF4vWjeMYzDrAqVIPTbS 3fXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=ZWdsGao+jmR1CNun7RA+yXja8or+75Lznkue4lBq63c=; fh=UZfhiU2pgwvZdBOe9fFEhnfTQX0/6KsvXKQubWsfhik=; b=UyOLjSGcFeHCxuf+ao1wVWxrDJtj4CqdEyRcssh5JRr6lsmaT4lulTBlmV7KVoiSAY 7S83VrznKKmYGZbyL8yiBPfsMu2VlYpkVANaXeOME3WTYqQr8ULzF/oHAj9sExv5b+46 Zna8KDbXEywuJH8ERn4BVZjt9VU3RwVdVS91FqRkZD75Hr74xS2NJKHDcaXlSRurL04o o6psQ+GP5oxB9OtppjavEvHRYapT0XvdArfoO46OgQjTi5u7buyDgstd6btPLqN8XDha Jte9xt+SQrRtgtHOAIL3aOg7ZAWjTHfQUdwgSd3gaBZWjDwK5qCkCW1sMBayWbokStBJ cKtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b="Ig/AE/Kg"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z9-20020a637e09000000b0055fdd303745si12064398pgc.421.2023.08.03.02.51.24; Thu, 03 Aug 2023 02:51:37 -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=@weissschuh.net header.s=mail header.b="Ig/AE/Kg"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233979AbjHCH3b (ORCPT + 99 others); Thu, 3 Aug 2023 03:29:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232240AbjHCH2y (ORCPT ); Thu, 3 Aug 2023 03:28:54 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEBCA30DB; Thu, 3 Aug 2023 00:28:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1691047729; bh=EWfkz4+JzA39y3BqrykKODSeqlkEjyKnDKBLfiY+GuE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ig/AE/KgUWfFxj21F5PavNG2qCm5JBdo5042nfha90GMAczwMIY/O9NlmvnbZ3knW EYEIYxbeaLYAtO44FmwMBEp0S1ZD63RBN6MlSBjfWdy+2tRezwF3ocvxK5Tix/jYyu Neftr8nUENgEomhNYVegLkI+kIsTnJPG2mMrsehw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 03 Aug 2023 09:28:56 +0200 Subject: [PATCH v3 12/14] selftests/nolibc: prevent out of bounds access in expect_vfprintf MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20230803-nolibc-warnings-v3-12-bcc1a096ae02@weissschuh.net> References: <20230803-nolibc-warnings-v3-0-bcc1a096ae02@weissschuh.net> In-Reply-To: <20230803-nolibc-warnings-v3-0-bcc1a096ae02@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Yuan Tan , Zhangjin Wu , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1691047727; l=1067; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=EWfkz4+JzA39y3BqrykKODSeqlkEjyKnDKBLfiY+GuE=; b=gJ18WhH9OWXlk9KTnh/w/CexA9VLB/UrRUJQFDwfWfARdOCmeWSBQgfbsGXRx1smLI4+Fcbre zkHVNQjYOHjCWMsimBUOfvx+Y+h5C9RE2QBpLg8ZUpLIRN+YKslVwht X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, 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 If read() fails and returns -1 (or returns garbage for some other reason) buf would be accessed out of bounds. Only use the return value of read() after it has been validated. Signed-off-by: Thomas Weißschuh --- tools/testing/selftests/nolibc/nolibc-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 63f09800057d..0145a44af990 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1055,7 +1055,6 @@ static int expect_vfprintf(int llen, int c, const char *expected, const char *fm lseek(fd, 0, SEEK_SET); r = read(fd, buf, sizeof(buf) - 1); - buf[r] = '\0'; fclose(memfile); @@ -1065,6 +1064,7 @@ static int expect_vfprintf(int llen, int c, const char *expected, const char *fm return 1; } + buf[r] = '\0'; llen += printf(" \"%s\" = \"%s\"", expected, buf); ret = strncmp(expected, buf, c); -- 2.41.0