Skip to content

Commit

Permalink
[libc] Fix scheduler test incorrectly guessing user privileges (llvm#…
Browse files Browse the repository at this point in the history
…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 llvm#95564.
  • Loading branch information
overmighty authored Jun 20, 2024
1 parent f8f4235 commit 57778ec
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions libc/test/src/sched/param_and_scheduler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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, &param), -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
// different 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, &param),
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, &param);
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
Expand Down Expand Up @@ -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, &param), 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, &param);
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, &param), 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
Expand All @@ -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)

Expand Down

0 comments on commit 57778ec

Please sign in to comment.