From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 3F21B3858D35 for ; Tue, 23 May 2023 18:40:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F21B3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6af6f4a0e11so2256404a34.0 for ; Tue, 23 May 2023 11:40:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684867233; x=1687459233; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=kVNZlqh730OYWrvcz8d8SLb5w+q8W92IFT3ajYLfn6k=; b=p0X8CGah3INiR/z0IDAPUd0y/C1L6cKqKc4qd9JsebApRxEbLUU3r2dQt3jbsXzzp2 TuReQCvN/msnLzNajVSysuDwKqn+fE9ezEs2zPqKpLRB446ZoB+POPmllMAhXZGz9wPt 6GY3zi3XLgxwJbsj2a5v1X/tT7OzPJ43maCkENR5hMvMkefRQ68tXYHGCE8c7y8/RttO XQ5UKSlbq7jocsNO/h/JHX7HkYq/9nEwr/PMC9j6siWvk+/vDvT+/zGZMy4WXf37RGwU KLU6uUM3HonOv07RWdpmSDuZRGhmS9ybfBQKfLGlLTtTxwfPv2P+AdmQYPmTxahWDl3u tjJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684867233; x=1687459233; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kVNZlqh730OYWrvcz8d8SLb5w+q8W92IFT3ajYLfn6k=; b=ap8VZLxDllLSsLRU6jMfiPsST3rXAT28i7vUrmIC2wEDhSA1GqKxWkAiKsIJdszyn6 NmnUHpN34uO9PwEozOcIcTQ8FDmpW8Uy1ebLWwDIWDIZzvLEmGD2OyuPGDicL7nAmzNU rU/mZ3+oyu43SmRw6e/RP/TZ4znqsVJfTQxV/rdrvZt4tXML6feigx+/08X1J+ja/RqO aqk0ibuUcs3I1qpBOmugB97iSqvFAusU6Kv22DQQ1IDHaH4DMqiKr+ItePkFOJSX77bv 2tXahxhydMIWhXgDmMj2Kil/EIT3DfnANgLyLhuEmjpD46DVKeeqiRHrrc/oD16uwLtX G6/Q== X-Gm-Message-State: AC+VfDy3mWg/Is8fBLh833z7/U4zli55druzCbc34fwBZJ/xbeWmBh2r OhbX5BZxutE4wnPzUsu6aGD8nQ== X-Google-Smtp-Source: ACHHUZ4YztMhbx9ZLqE/ZxOYrsEtokz0lFrjuaHoqfrE5Rj44pTuVp7EvdzUADBS5jkSNxC4GVR22A== X-Received: by 2002:a9d:61ce:0:b0:6ab:35a6:de17 with SMTP id h14-20020a9d61ce000000b006ab35a6de17mr8639768otk.28.1684867233679; Tue, 23 May 2023 11:40:33 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c1:2e05:a4ad:a873:8177:a796? ([2804:1b3:a7c1:2e05:a4ad:a873:8177:a796]) by smtp.gmail.com with ESMTPSA id u21-20020a056830119500b006af800065f2sm2147906otq.59.2023.05.23.11.40.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 May 2023 11:40:33 -0700 (PDT) Message-ID: <2ee46c72-ea05-b45b-0848-30f31a2696ac@linaro.org> Date: Tue, 23 May 2023 15:40:30 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [RFC PATCH] debug: Add tests for fortified fcntl () Content-Language: en-US To: Sergey Bugaev , libc-alpha@sourceware.org Cc: Joseph Myers References: <20230520182125.3986459-1-bugaevc@gmail.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20230520182125.3986459-1-bugaevc@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,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 server2.sourceware.org List-Id: I think it would be better to include this test on the fcntl fortify patch itself, or at least on a patchset so the first one is set as prerequisite. On 20/05/23 15:21, Sergey Bugaev via Libc-alpha wrote: > Signed-off-by: Sergey Bugaev > --- > debug/tst-fortify.c | 158 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 158 insertions(+) > > diff --git a/debug/tst-fortify.c b/debug/tst-fortify.c > index 7850a4e5..17a15de7 100644 > --- a/debug/tst-fortify.c > +++ b/debug/tst-fortify.c > @@ -78,6 +78,15 @@ do_prepare (void) > } > } > > +/* Return VALUE, but do it in a way that the compiler cannot > + see that it's a compile-time constant. */ > +static int __attribute_noinline__ > +hide_constant (int value) > +{ > + volatile int v = value; > + return v; > +} > + > volatile int chk_fail_ok; > volatile int ret; > jmp_buf chk_fail_buf; > @@ -1763,6 +1772,155 @@ do_test (void) > ppoll (fds, l0 + 2, NULL, NULL); > CHK_FAIL_END > # endif > +#endif > + > + /* Check that we can do basic fcntl operations, both ones that require > + the third argument, and ones that do not. */ > + res = fcntl (STDIN_FILENO, F_GETFD); > + TEST_COMPARE (res, 0); > + res = fcntl (STDIN_FILENO, F_SETFD, 0); > + TEST_COMPARE (res, 0); > + > +#ifdef F_OFD_GETLK > + /* Check for confusion between 32- and 64-bit versions of the fcntl > + interface. But first, check that the kernel supports OFD locks at all, > + using a non-fortified function. */ > + int lockfd1 = xopen (temp_filename, O_RDWR, 0); > + int lockfd2 = xopen (temp_filename, O_RDWR, 0); It misses the inclusion of 'support/xunistd.h>' (the testcase fails for build it). > + > + struct flock flock; > + memset (&flock, 0, sizeof (flock)); > + flock.l_type = F_WRLCK; > + flock.l_whence = SEEK_SET; > + flock.l_start = 0; > + flock.l_len = INT32_MAX; > + flock.l_pid = 0; > + > + res = __fcntl (lockfd1, F_OFD_GETLK, &flock); This is an external case, so use the external default name. > + int ofd_locks_supported = (res != -1 || errno != EINVAL); > + > + if (ofd_locks_supported) > + { > + TEST_COMPARE (res, 0); > + TEST_COMPARE (flock.l_type, F_UNLCK); > + > + memset (&flock, 0, sizeof (flock)); > + flock.l_type = F_WRLCK; > + flock.l_whence = SEEK_SET; > + flock.l_start = 1234; > + flock.l_len = 5678; > + flock.l_pid = 0; > + > + res = fcntl (lockfd1, F_OFD_SETLK, &flock); > + TEST_COMPARE (res, 0); > + > + memset (&flock, 0, sizeof (flock)); > + flock.l_type = F_RDLCK; > + flock.l_whence = SEEK_SET; > + flock.l_start = 3542; > + flock.l_len = 411; > + flock.l_pid = 0; > + > + res = fcntl (lockfd2, F_OFD_GETLK, &flock); > + TEST_COMPARE (res, 0); > + /* Check that we get the expected values. */ > + TEST_COMPARE (flock.l_type, F_WRLCK); > + TEST_COMPARE (flock.l_whence, SEEK_SET); > + TEST_COMPARE (flock.l_start, 1234); > + TEST_COMPARE (flock.l_len, 5678); > + TEST_COMPARE (flock.l_pid, -1); > + } > +#endif > + > + /* Check that we can do fcntl operations with CMD that is not constant > + at compile time. */ > + res = fcntl (STDIN_FILENO, hide_constant (F_GETFD)); > + TEST_COMPARE (res, 0); > + res = fcntl (STDIN_FILENO, hide_constant (F_SETFD), 0); > + TEST_COMPARE (res, 0); > + > +#ifdef F_OFD_GETLK > + if (ofd_locks_supported) > + { > + memset (&flock, 0, sizeof (flock)); > + flock.l_type = F_RDLCK; > + flock.l_whence = SEEK_SET; > + flock.l_start = 3542; > + flock.l_len = 411; > + flock.l_pid = 0; > + > + res = fcntl (lockfd2, hide_constant (F_OFD_GETLK), &flock); > + TEST_COMPARE (res, 0); > + /* Check that we get the expected values. */ > + TEST_COMPARE (flock.l_type, F_WRLCK); > + TEST_COMPARE (flock.l_whence, SEEK_SET); > + TEST_COMPARE (flock.l_start, 1234); > + TEST_COMPARE (flock.l_len, 5678); > + TEST_COMPARE (flock.l_pid, -1); > + } > +#endif > + > +#if __USE_FORTIFY_LEVEL >= 1 > + CHK_FAIL_START > + fcntl (STDIN_FILENO, hide_constant (F_SETFD)); > + CHK_FAIL_END > +#endif > + > +#if defined (__USE_LARGEFILE64) || defined (__USE_TIME_BITS64) > + /* Also check fcntl64 (). */ > + res = fcntl64 (STDIN_FILENO, F_GETFD); > + TEST_COMPARE (res, 0); > + res = fcntl64 (STDIN_FILENO, F_SETFD, 0); > + TEST_COMPARE (res, 0); > + res = fcntl64 (STDIN_FILENO, hide_constant (F_GETFD)); > + TEST_COMPARE (res, 0); > + res = fcntl64 (STDIN_FILENO, hide_constant (F_SETFD), 0); > + TEST_COMPARE (res, 0); > + > +#ifdef F_OFD_GETLK > + if (ofd_locks_supported) > + { > + struct flock64 flock64; > + > + memset (&flock64, 0, sizeof (flock64)); > + flock64.l_type = F_RDLCK; > + flock64.l_whence = SEEK_SET; > + flock64.l_start = 3542; > + flock64.l_len = 411; > + flock64.l_pid = 0; > + > + res = fcntl64 (lockfd2, F_OFD_GETLK, &flock64); > + TEST_COMPARE (res, 0); > + /* Check that we get the expected values. */ > + TEST_COMPARE (flock64.l_type, F_WRLCK); > + TEST_COMPARE (flock64.l_whence, SEEK_SET); > + TEST_COMPARE (flock64.l_start, 1234); > + TEST_COMPARE (flock64.l_len, 5678); > + TEST_COMPARE (flock64.l_pid, -1); > + > + memset (&flock64, 0, sizeof (flock64)); > + flock64.l_type = F_RDLCK; > + flock64.l_whence = SEEK_SET; > + flock64.l_start = 3542; > + flock64.l_len = 411; > + flock64.l_pid = 0; > + > + res = fcntl64 (lockfd2, hide_constant (F_OFD_GETLK), &flock64); > + TEST_COMPARE (res, 0); > + /* Check that we get the expected values. */ > + TEST_COMPARE (flock64.l_type, F_WRLCK); > + TEST_COMPARE (flock64.l_whence, SEEK_SET); > + TEST_COMPARE (flock64.l_start, 1234); > + TEST_COMPARE (flock64.l_len, 5678); > + TEST_COMPARE (flock64.l_pid, -1); > + } > +#endif > + > +# if __USE_FORTIFY_LEVEL >= 1 > + CHK_FAIL_START > + fcntl64 (STDIN_FILENO, hide_constant (F_SETFD)); > + CHK_FAIL_END > +# endif > #endif > > return ret;