Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5388347rwb; Tue, 1 Aug 2023 01:28:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlFv5EFAt+LeflVKGQJ9tQBiNbl4Dq0lMwYooKcFOyJ/c08Mdr/bfrbuPwp+5H6O44ySrgE/ X-Received: by 2002:a05:6808:140f:b0:3a7:3ea1:b596 with SMTP id w15-20020a056808140f00b003a73ea1b596mr5288749oiv.47.1690878507827; Tue, 01 Aug 2023 01:28:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690878507; cv=none; d=google.com; s=arc-20160816; b=dneZrcYPA5xnwiGD2xywe0tjp63gqJSpsUC3TraicY+7vQplOm0ydYulPgJZTaxi8Q dpanFYnN7ruSSPs1O5KJnJa4F013nO41i+FyhRJwCV4AgvWC1Ckk72GvWR0zjNs0JMMC XzVxSqetiRDAdtX+svM+/8KGUqfh1DiicScfozuVkzaS39GR1egJgFCaCne0sTK01kQq ZuvLqLK3H3FE/yeGnH4xOgPw2GhATWBWYPwvO4JZ6TTr8c5N2IqmC3e+TEGL5Z7kvr0X W6ShCNn7nXR9P4FpMWZntQsyvXWQSSkkdFWz9MFz9rXa5znpOZ2SiGsN4yvWuoTVxQXo mXdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=lD9QfzRcnMSRdyjh+eBVZt3kfBEpPx88XSfI8SM5XCI=; fh=XLgxCw6kQWJd90UaW0QKkMGE292sSoLJai8ThDu4OTQ=; b=JzTti6tAQnkEcHC1dszEmaiW63TjUbDKUJoi6mJ2g42bNGZOPD/2nKS4TE9ak3SS+u sdnyGl+5VNjOn63JKSFay24je/lzWJvWbSVQuQQHhzTGfE9I/GP4++Ry8oyyCAkUEf9Q hkKVapsKWfJ4sGAm9SLOh033230naJs+ldGJOnyRGcZQEsTSgvhovfUoojrdJwNUG8cE SuRWmquwwlMKYahsYB0F6gLqURpBoq95beoS+XOWBEaOHdBaAXIdReHfIgK2UIj9+Msr g03dVJ3NTPr3cYi2NaGR1aHz6TbSqs8YbfIxdOApYSJE/AsgVJiy8FhS8+tsSv6U57lP jydg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=bWUrDjWU; 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 kk2-20020a17090b4a0200b002684572d70csi10893363pjb.19.2023.08.01.01.28.16; Tue, 01 Aug 2023 01:28:27 -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=bWUrDjWU; 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 S231540AbjHAHtD (ORCPT + 99 others); Tue, 1 Aug 2023 03:49:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231426AbjHAHtD (ORCPT ); Tue, 1 Aug 2023 03:49:03 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38509E48; Tue, 1 Aug 2023 00:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1690876140; bh=qsbOXFqF2xX1Cj3m1X+7zylT1KmXCgIpush39Cl5JtQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bWUrDjWULaOlHXTqUu5UmVfeqHLaTpy2k1zUYBvlYy/zWGzYpIIMO0odL2xdqMWub 89KhGnUxOqLLpURKST/RRSaw1vdZ1EdRxudJofu/e9vi61Fh4nfRHnNV15aI6Hh4oV BRIeXzkTP9f9pOIBvRo8C0a7UzmQscMANtzRGTTc= Date: Tue, 1 Aug 2023 09:48:59 +0200 From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Willy Tarreau Cc: Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Yuan Tan , Zhangjin Wu Subject: Re: [PATCH v2 09/10] selftests/nolibc: test return value of read() in test_vfprintf Message-ID: References: <20230801-nolibc-warnings-v2-0-1ba5ca57bd9b@weissschuh.net> <20230801-nolibc-warnings-v2-9-1ba5ca57bd9b@weissschuh.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On 2023-08-01 08:59:17+0200, Willy Tarreau wrote: > On Tue, Aug 01, 2023 at 07:30:16AM +0200, Thomas Weißschuh wrote: > > If read() fails and returns -1 buf would be accessed out of bounds. > > > > Signed-off-by: Thomas Weißschuh > > --- > > tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++ > > 1 file changed, 6 insertions(+) > > > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > > index 82714051c72f..a334f8450a34 100644 > > --- a/tools/testing/selftests/nolibc/nolibc-test.c > > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > > @@ -1031,6 +1031,12 @@ 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); > > + if (r == -1) { > > + llen += printf(" read() = %s", errorname(errno)); > > + result(llen, FAIL); > > + return 1; > > + } > > + > > buf[r] = '\0'; > > In fact given the nature of this file (test if we properly implemented > our syscalls), I think that a more conservative approach is deserved > because if we messed up on read() we can have anything on return and we > don't want to trust that. As such I would suggest that we declare r as > ssize_t and verify that it's neither negative nor larger than > sizeof(buf)-1, which becomes: > > if ((size_t)r >= sizeof(buf)) { > ... fail ... > } As r == w is validated just below anyways we could move the assignment buf[r] = '\0' after that check and then we don't need a new block. > You'll also have to turn w to ssize_t then due to the test later BTW. Will do in any case.