[libc-commits] [libc] 57778ec - [libc] Fix scheduler test incorrectly guessing user privileges (#95562)
via libc-commits
libc-commits at lists.llvm.org
Wed Jun 19 18:16:05 PDT 2024
Author: OverMighty
Date: 2024-06-19T20:16:02-05:00
New Revision: 57778ec36c9c7e96b76a167f19dccbe00d49c9d4
URL: https://github.com/llvm/llvm-project/commit/57778ec36c9c7e96b76a167f19dccbe00d49c9d4
DIFF: https://github.com/llvm/llvm-project/commit/57778ec36c9c7e96b76a167f19dccbe00d49c9d4.diff
LOG: [libc] Fix scheduler test incorrectly guessing user privileges (#95562)
Non-root users may be able to set real-time scheduling policies. Don't
expect failure to set real-time scheduling policies based on UID.
Instead, check that if it fails, it is either due to missing privileges,
or unsupported parameters if the scheduling policy is not mandated by
POSIX.
Fixes #95564.
Added:
Modified:
libc/test/src/sched/param_and_scheduler_test.cpp
Removed:
################################################################################
diff --git a/libc/test/src/sched/param_and_scheduler_test.cpp b/libc/test/src/sched/param_and_scheduler_test.cpp
index 8e81f2ed1517f..747c7e3409e41 100644
--- a/libc/test/src/sched/param_and_scheduler_test.cpp
+++ b/libc/test/src/sched/param_and_scheduler_test.cpp
@@ -36,7 +36,7 @@
class SchedTest : public LIBC_NAMESPACE::testing::Test {
public:
- void testSched(int policy, bool can_set) {
+ void testSched(int policy, bool is_mandatory) {
LIBC_NAMESPACE::libc_errno = 0;
int init_policy = LIBC_NAMESPACE::sched_getscheduler(0);
@@ -74,24 +74,26 @@ class SchedTest : public LIBC_NAMESPACE::testing::Test {
param.sched_priority = max_priority + 1;
ASSERT_EQ(LIBC_NAMESPACE::sched_setscheduler(0, policy, ¶m), -1);
- // A bit hard to test as depending if we are root or not we can run into
+ // A bit hard to test as depending on user privileges we can run into
//
diff erent issues.
ASSERT_TRUE(LIBC_NAMESPACE::libc_errno == EINVAL ||
LIBC_NAMESPACE::libc_errno == EPERM);
LIBC_NAMESPACE::libc_errno = 0;
- // Some sched policies require permissions, so skip
param.sched_priority = min_priority;
- // Success / missing permissions.
- ASSERT_EQ(LIBC_NAMESPACE::sched_setscheduler(0, policy, ¶m),
- can_set ? 0 : -1);
- ASSERT_TRUE(can_set ? (LIBC_NAMESPACE::libc_errno == 0)
- : (LIBC_NAMESPACE::libc_errno == EINVAL ||
- LIBC_NAMESPACE::libc_errno == EPERM));
+ // Success/unsupported policy/missing permissions.
+ int setscheduler_result =
+ LIBC_NAMESPACE::sched_setscheduler(0, policy, ¶m);
+ ASSERT_TRUE(setscheduler_result == 0 || setscheduler_result == -1);
+ ASSERT_TRUE(
+ setscheduler_result != -1
+ ? (LIBC_NAMESPACE::libc_errno == 0)
+ : ((!is_mandatory && LIBC_NAMESPACE::libc_errno == EINVAL) ||
+ LIBC_NAMESPACE::libc_errno == EPERM));
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_EQ(LIBC_NAMESPACE::sched_getscheduler(0),
- can_set ? policy : init_policy);
+ setscheduler_result != -1 ? policy : init_policy);
ASSERT_ERRNO_SUCCESS();
// Out of bounds priority
@@ -121,17 +123,21 @@ class SchedTest : public LIBC_NAMESPACE::testing::Test {
ASSERT_ERRNO_EQ(EINVAL);
LIBC_NAMESPACE::libc_errno = 0;
- // Success / missing permissions
- ASSERT_EQ(LIBC_NAMESPACE::sched_setparam(0, ¶m), can_set ? 0 : -1);
- ASSERT_TRUE(can_set ? (LIBC_NAMESPACE::libc_errno == 0)
- : (LIBC_NAMESPACE::libc_errno == EINVAL ||
- LIBC_NAMESPACE::libc_errno == EPERM));
+ // Success/unsupported policy/missing permissions
+ int setparam_result = LIBC_NAMESPACE::sched_setparam(0, ¶m);
+ ASSERT_TRUE(setparam_result == 0 || setparam_result == -1);
+ ASSERT_TRUE(setparam_result != -1
+ ? (LIBC_NAMESPACE::libc_errno == 0)
+ : ((setscheduler_result == -1 &&
+ LIBC_NAMESPACE::libc_errno == EINVAL) ||
+ LIBC_NAMESPACE::libc_errno == EPERM));
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_EQ(LIBC_NAMESPACE::sched_getparam(0, ¶m), 0);
ASSERT_ERRNO_SUCCESS();
- ASSERT_EQ(param.sched_priority, can_set ? priority : init_priority);
+ ASSERT_EQ(param.sched_priority,
+ setparam_result != -1 ? priority : init_priority);
}
// Null test
@@ -145,12 +151,12 @@ class SchedTest : public LIBC_NAMESPACE::testing::Test {
using LlvmLibcSchedTest = SchedTest; \
TEST_F(LlvmLibcSchedTest, Sched_##policy) { testSched(policy, can_set); }
-// Root is required to set these policies.
-LIST_SCHED_TESTS(SCHED_FIFO, LIBC_NAMESPACE::getuid() == 0)
-LIST_SCHED_TESTS(SCHED_RR, LIBC_NAMESPACE::getuid() == 0)
-
-// No root is required to set these policies.
+// Mandated by POSIX.
LIST_SCHED_TESTS(SCHED_OTHER, true)
+LIST_SCHED_TESTS(SCHED_FIFO, true)
+LIST_SCHED_TESTS(SCHED_RR, true)
+
+// Linux extensions.
LIST_SCHED_TESTS(SCHED_BATCH, true)
LIST_SCHED_TESTS(SCHED_IDLE, true)
More information about the libc-commits
mailing list