From: Sergey Bugaev <bugaevc@gmail.com>
To: libc-alpha@sourceware.org
Cc: Joseph Myers <joseph@codesourcery.com>
Subject: [RFC PATCH] debug: Add tests for fortified fcntl ()
Date: Sat, 20 May 2023 21:21:25 +0300 [thread overview]
Message-ID: <20230520182125.3986459-1-bugaevc@gmail.com> (raw)
In-Reply-To: <CAN9u=HcmgmNkbp+7mo6GieGycDQ7s6LVS908f2o7WRv2_2y+Kg@mail.gmail.com>
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
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;
--
2.40.1
next prev parent reply other threads:[~2023-05-20 18:21 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-19 21:30 [RFC PATCH 0/1] Attempt to detect missing fcntl argument at compile time Sergey Bugaev
2023-05-19 21:30 ` [RFC PATCH 1/1] io: Add FORTIFY_SOURCE check for fcntl arguments Sergey Bugaev
2023-05-19 21:55 ` Joseph Myers
2023-05-20 11:46 ` Sergey Bugaev
2023-05-20 18:21 ` Sergey Bugaev [this message]
2023-05-23 18:40 ` [RFC PATCH] debug: Add tests for fortified fcntl () Adhemerval Zanella Netto
2023-05-23 19:19 ` Sergey Bugaev
2023-05-23 19:48 ` Adhemerval Zanella Netto
2023-05-24 7:15 ` Sergey Bugaev
2023-05-24 12:15 ` Adhemerval Zanella Netto
2023-05-23 19:09 ` [RFC PATCH 1/1] io: Add FORTIFY_SOURCE check for fcntl arguments Adhemerval Zanella Netto
2023-05-23 19:43 ` Sergey Bugaev
2023-05-23 19:56 ` Adhemerval Zanella Netto
2023-05-23 20:24 ` Sergey Bugaev
2023-05-23 20:44 ` Sergey Bugaev
2023-05-24 12:04 ` Adhemerval Zanella Netto
2023-05-23 19:15 ` Siddhesh Poyarekar
2023-05-23 20:01 ` Sergey Bugaev
2023-05-23 20:06 ` Sergey Bugaev
2023-05-23 21:46 ` Florian Weimer
2023-05-24 7:31 ` Sergey Bugaev
2023-05-24 8:29 ` Florian Weimer
2023-05-24 10:51 ` Sergey Bugaev
2023-05-24 11:18 ` Florian Weimer
2023-05-24 11:46 ` Siddhesh Poyarekar
2023-05-24 12:12 ` Andreas Schwab
2023-05-24 12:18 ` Florian Weimer
2023-05-24 12:37 ` Sergey Bugaev
2023-05-24 12:45 ` Florian Weimer
2023-05-24 13:02 ` Sergey Bugaev
2023-05-24 13:18 ` Florian Weimer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230520182125.3986459-1-bugaevc@gmail.com \
--to=bugaevc@gmail.com \
--cc=joseph@codesourcery.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).