Skip to contentSkip to conversation

selftests/seccomp: Actually sleep for 1/10th second

PATCH In review Kees Cook 3 participants 6 messages Jan 27, 2019 – Jan 28, 2019

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 selftests/seccomp: Actually sleep for 1/10th second

Clang noticed that some none-zero sleep()s were actually using zero anyway. This switches to nanosleep() to gain sub-second granularity.

seccomp_bpf.c:2625:9: warning: implicit conversion from 'double' to

      'unsigned int' changes value from 0.1 to 0 [-Wliteral-conversion]
                sleep(0.1);
                ~~~~~ ^~~
Signed-off-by Kees Cook <keescook at chromium.org>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/seccomp/seccomp_bpf.c +3 -2
index 067cb4607d6c..a9f278c13f13 100644
@@ -2569,6 +2569,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
 {
 	long ret, sib;
 	void *status;
+	struct timespec delay = { .tv_nsec = 100000000 };
 
 	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
 		TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
@@ -2622,7 +2623,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
 	EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
 	/* Poll for actual task death. pthread_join doesn't guarantee it. */
 	while (!kill(self->sibling[sib].system_tid, 0))
-		sleep(0.1);
+		nanosleep(&delay, NULL);
 	/* Switch to the remaining sibling */
 	sib = !sib;
 
@@ -2647,7 +2648,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
 	EXPECT_EQ(0, (long)status);
 	/* Poll for actual task death. pthread_join doesn't guarantee it. */
 	while (!kill(self->sibling[sib].system_tid, 0))
-		sleep(0.1);
+		nanosleep(&delay, NULL);
 
 	ret = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_TSYNC,
 		      &self->apply_prog);
Signature · 5 lines
2.17.1


-- 
Kees Cook
#1 lore raw
Quoted 19 lines · Kees Cook

Clang noticed that some none-zero sleep()s were actually using zero anyway. This switches to nanosleep() to gain sub-second granularity.

seccomp_bpf.c:2625:9: warning: implicit conversion from 'double' to

      'unsigned int' changes value from 0.1 to 0 [-Wliteral-conversion]
                sleep(0.1);
                ~~~~~ ^~~
Signed-off-by Kees Cook <keescook at chromium.org>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/seccomp/seccomp_bpf.c +0 -0
index 067cb4607d6c..a9f278c13f13 100644

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2572

Quoted 4 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -0
@@ -2569,6 +2569,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
 {
        long ret, sib;
        void *status;
+       struct timespec delay = { .tv_nsec = 100000000 };

"Omitted fields are implicitly initialized the same as for objects that have static storage duration."
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
https://godbolt.org/z/cuGqxM
(So this wont sleep an arbitrary amount of seconds, phew)

Quoted 13 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
        ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
                TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
@@ -2622,7 +2623,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
        EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
        /* Poll for actual task death. pthread_join doesn't guarantee it. */
        while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);
        /* Switch to the remaining sibling */
        sib = !sib;
 

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2651

Quoted 5 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
@@ -2647,7 +2648,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
        EXPECT_EQ(0, (long)status);
        /* Poll for actual task death. pthread_join doesn't guarantee it. */
        while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);

Interesting bug. If the sleeps weren't doing anything, are they even needed? Does adding the tests cause them to continue to pass, or start to fail? If they weren't doing anything, and the tests were passing, maybe it's just better to remove them?

Signature · 2 lines
Thanks,
~Nick Desaulniers
author
#2 lore raw
Quoted 16 lines · Nick Desaulniers
Quoted 14 lines · Kees Cook

Clang noticed that some none-zero sleep()s were actually using zero anyway. This switches to nanosleep() to gain sub-second granularity.

seccomp_bpf.c:2625:9: warning: implicit conversion from 'double' to

      'unsigned int' changes value from 0.1 to 0 [-Wliteral-conversion]
                sleep(0.1);
                ~~~~~ ^~~
Signed-off-by Kees Cook <keescook at chromium.org>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 15 lines

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2572

Quoted 9 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -0
index 067cb4607d6c..a9f278c13f13 100644
@@ -2569,6 +2569,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
 {
        long ret, sib;
        void *status;
+       struct timespec delay = { .tv_nsec = 100000000 };

"Omitted fields are implicitly initialized the same as for objects that have static storage duration."
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
https://godbolt.org/z/cuGqxM
(So this wont sleep an arbitrary amount of seconds, phew)

Yup. :) Even an empty initializer works ... = { }; (Except that padding bytes aren't always included in the zeroing...)

Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
        ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
                TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
@@ -2622,7 +2623,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
        EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
        /* Poll for actual task death. pthread_join doesn't guarantee it. */
        while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);
        /* Switch to the remaining sibling */
        sib = !sib;
 
Quoted 11 lines

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2651

Quoted 6 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
@@ -2647,7 +2648,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
        EXPECT_EQ(0, (long)status);
        /* Poll for actual task death. pthread_join doesn't guarantee it. */
        while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);

Interesting bug. If the sleeps weren't doing anything, are they even needed? Does adding the tests cause them to continue to pass, or start to fail? If they weren't doing anything, and the tests were passing, maybe it's just better to remove them?

It was just spinning. This restores the intention of not being so aggressive in the wait loop. While the sleep could be removed, that wasn't the intention.

Signature · 1 line
Kees Cook
Reviewed-by
#3 lore raw
Quoted 63 lines · Kees Cook
Quoted 16 lines · Nick Desaulniers
Quoted 14 lines · Kees Cook

Clang noticed that some none-zero sleep()s were actually using zero anyway. This switches to nanosleep() to gain sub-second granularity.

seccomp_bpf.c:2625:9: warning: implicit conversion from 'double' to

      'unsigned int' changes value from 0.1 to 0 [-Wliteral-conversion]
                sleep(0.1);
                ~~~~~ ^~~
Signed-off-by Kees Cook <keescook at chromium.org>
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 15 lines

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2572

Quoted 9 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -0
index 067cb4607d6c..a9f278c13f13 100644
@@ -2569,6 +2569,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
 {
        long ret, sib;
        void *status;
+       struct timespec delay = { .tv_nsec = 100000000 };

"Omitted fields are implicitly initialized the same as for objects that have static storage duration."
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
https://godbolt.org/z/cuGqxM
(So this wont sleep an arbitrary amount of seconds, phew)

Yup. :) Even an empty initializer works ... = { }; (Except that padding bytes aren't always included in the zeroing...)

Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
        ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
                TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
@@ -2622,7 +2623,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
        EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
        /* Poll for actual task death. pthread_join doesn't guarantee it. */
        while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);
        /* Switch to the remaining sibling */
        sib = !sib;
 
Quoted 11 lines

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2651

Quoted 6 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
@@ -2647,7 +2648,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
        EXPECT_EQ(0, (long)status);
        /* Poll for actual task death. pthread_join doesn't guarantee it. */
        while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);

Interesting bug. If the sleeps weren't doing anything, are they even needed? Does adding the tests cause them to continue to pass, or start to fail? If they weren't doing anything, and the tests were passing, maybe it's just better to remove them?

Quoted 1 line

It was just spinning.

So this test has been broken? If so, do you know for how long? Or who's monitoring them? Either way, thanks for noticing and fixing.

+Guenter; did you notice if this test was failing? Are your boot tests running kselftests?

Quoted 3 lines

This restores the intention of not being so
aggressive in the wait loop. While the sleep could be removed, that wasn't the intention.

Oh, yeah I guess the comment above it about pthread_join is relevant. I just get highly highly suspicious whenever I see sleeps added to any code.
Reviewed-by: Nick Desaulniers <ndesaulniers at google.com>

Signature · 2 lines
Thanks,
~Nick Desaulniers
#4 lore raw
Quoted 64 lines · Nick Desaulniers
Quoted 63 lines · Kees Cook
Quoted 16 lines · Nick Desaulniers
Quoted 14 lines · Kees Cook

Clang noticed that some none-zero sleep()s were actually using zero anyway. This switches to nanosleep() to gain sub-second granularity.

seccomp_bpf.c:2625:9: warning: implicit conversion from 'double' to

       'unsigned int' changes value from 0.1 to 0 [-Wliteral-conversion]
                 sleep(0.1);
                 ~~~~~ ^~~
Signed-off-by Kees Cook <keescook at chromium.org>
---
  tools/testing/selftests/seccomp/seccomp_bpf.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 15 lines

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2572

Quoted 9 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -0
index 067cb4607d6c..a9f278c13f13 100644
@@ -2569,6 +2569,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
  {
         long ret, sib;
         void *status;
+       struct timespec delay = { .tv_nsec = 100000000 };

"Omitted fields are implicitly initialized the same as for objects that have static storage duration."
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
https://godbolt.org/z/cuGqxM
(So this wont sleep an arbitrary amount of seconds, phew)

Yup. :) Even an empty initializer works ... = { }; (Except that padding bytes aren't always included in the zeroing...)

Quoted 24 lines
Quoted 14 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
         ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
                 TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
@@ -2622,7 +2623,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
         EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
         /* Poll for actual task death. pthread_join doesn't guarantee it. */
         while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);
         /* Switch to the remaining sibling */
         sib = !sib;
 

tools/testing/selftests/seccomp/seccomp_bpf.c TEST_F(TSYNC, two_siblings_not_under_filter) · line 2651

Quoted 5 lines
tools/testing/selftests/seccomp/seccomp_bpf.c +1 -1
@@ -2647,7 +2648,7 @@ TEST_F(TSYNC, two_siblings_not_under_filter)
         EXPECT_EQ(0, (long)status);
         /* Poll for actual task death. pthread_join doesn't guarantee it. */
         while (!kill(self->sibling[sib].system_tid, 0))
-               sleep(0.1);
+               nanosleep(&delay, NULL);

Interesting bug. If the sleeps weren't doing anything, are they even needed? Does adding the tests cause them to continue to pass, or start to fail? If they weren't doing anything, and the tests were passing, maybe it's just better to remove them?

Quoted 8 lines
Quoted 1 line

It was just spinning.

So this test has been broken? If so, do you know for how long? Or who's monitoring them? Either way, thanks for noticing and fixing.

+Guenter; did you notice if this test was failing? Are your boot tests running kselftests?

No, I don't run kselftests at this time.

Guenter

Quoted 9 lines
Quoted 3 lines

This restores the intention of not being so
aggressive in the wait loop. While the sleep could be removed, that wasn't the intention.

Oh, yeah I guess the comment above it about pthread_join is relevant. I just get highly highly suspicious whenever I see sleeps added to any code.
Reviewed-by: Nick Desaulniers <ndesaulniers at google.com>

author
#5 lore raw
In reply to Nick Desaulniers
Quoted 2 lines · Nick Desaulniers

So this test has been broken? If so, do you know for how long? Or who's monitoring them? Either way, thanks for noticing and fixing.

No, it's been working fine. It just consumes 100% cpu during the spin-wait. The intention was to sleep for a little bit to avoid a tight loop. It was just resource-inefficient.

Signature · 1 line
Kees Cook