From patchwork Sat May 20 18:21:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 69722 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5E843858421 for ; Sat, 20 May 2023 18:21:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A5E843858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684606912; bh=qm1rhIaW2sO3tvTcY6FYi06fwB/Dms8JVF4hilGDO4U=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=YezuCo2J9wzmFyGCggERED2VpDdwLeeBUBQL1esL2ZeVchepEkim4XsXuTf+QclJm IE9RurwhaRzYuarSs04oPLDrZbfHs07Sbg4cU9scZR9zjC2CqSgS0Sr1FuFcpv/c8R F6Lp13PvaHh6OP6bjDxRygWG8zeIdoqdJUsqotA8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id 1F1553858C52 for ; Sat, 20 May 2023 18:21:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1F1553858C52 Received: by mail-lj1-x235.google.com with SMTP id 38308e7fff4ca-2ac785015d6so50153011fa.0 for ; Sat, 20 May 2023 11:21:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684606888; x=1687198888; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qm1rhIaW2sO3tvTcY6FYi06fwB/Dms8JVF4hilGDO4U=; b=NjqB8O9OdJ5X6DczGp1FFWLQXR3sAvbufaHh00qxHAdEnOQVnEoTeATzYSSqvxE0/4 Y+1lAjCYsZVabnCIG3YlOuXjRal2yo50AlajZalPlp80Zi1vkjv7cmCxpfkhFKN/DyzB V29iHuay9ikDKiFsFqrvFYDOJsZgrnfd8llQREpzbOjPvii9NjiPDlVq+U+d+OxFQ94g uQ5XLIluSu87CtLGbHwD13ILHMt8DsOH1TJtFTf+5eynG+J7f7cpVKxkpoAl0EzqqWEO kFPqmKHDWFnsECZKD0P+y4GsK8v8JBG2DIpAjG0lmGm3v7Vyiker6pxH3YyL1gCmqgwM cO+Q== X-Gm-Message-State: AC+VfDx14HnBu6w66LiH/7rNG2L1UFo56StyR99JkWH0ub98VJ66aL+M uJYxEvjkBYGtxVah6q7JuZX8mkk2gRU= X-Google-Smtp-Source: ACHHUZ5Z1gPBRvF9F+wvxd9cQXpmvIzYgJggb2wmrvlu/ePesq3yqhCL117rf9tpFv/gZ2Olow4g9w== X-Received: by 2002:a2e:9b43:0:b0:2a9:ef05:8b73 with SMTP id o3-20020a2e9b43000000b002a9ef058b73mr2086270ljj.44.1684606887390; Sat, 20 May 2023 11:21:27 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:9ffc:86d8:a1a6:e298]) by smtp.gmail.com with ESMTPSA id n18-20020a2e9052000000b00295a3a64816sm390848ljg.2.2023.05.20.11.21.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 May 2023 11:21:26 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joseph Myers Subject: [RFC PATCH] debug: Add tests for fortified fcntl () Date: Sat, 20 May 2023 21:21:25 +0300 Message-Id: <20230520182125.3986459-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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); + + 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); + 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;