Skip to content

Commit c21035a

Browse files
grandixximodwrobel
andcommitted
rtapi: make harden_rt() rootless-safe
setrlimit(RLIMIT_RTPRIO, RLIM_INFINITY) requires CAP_SYS_RESOURCE, which neither setuid root nor 'make setcap' (CAP_IPC_LOCK, CAP_NET_ADMIN, CAP_SYS_RAWIO, CAP_SYS_NICE) grants by default. Under rootless the call returns EPERM, which the previous code treated as fatal -- harden_rt() returned -errno, makeApp() fell back to SCHED_OTHER, and the SCHED_FIFO probe in rtapi_is_realtime() became a lie. Soften both setrlimit calls to best-effort: SCHED_FIFO scheduling itself only needs CAP_SYS_NICE, which the cap set does grant; the rlimit just bounds the achievable priority. Distros that want unlimited RT priority can ship a /etc/security/limits.d entry, or the operator can grant CAP_SYS_RESOURCE explicitly. Also update the iopl() error message: 'sudo make setuid' is no longer the only path, and the diagnostic should name the missing capability (CAP_SYS_RAWIO). Derived from Damian Wrobel's 2020 'Unify FIFO_SCHED between root and non-root user' commit, ported onto hdiethelm's rtapi cleanup v2 structure. Co-authored-by: Damian Wrobel <dwrobel@ertelnet.rybnik.pl>
1 parent 34863c9 commit c21035a

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/rtapi/uspace_rtapi_main.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ static int harden_rt() {
886886
RTAPI_MSG_ERR,
887887
"iopl() failed: %s\n"
888888
"cannot gain I/O privileges - "
889-
"forgot 'sudo make setuid' or using secure boot? -"
889+
"missing CAP_SYS_RAWIO or using secure boot? - "
890890
"parallel port access is not allowed\n",
891891
strerror(errno)
892892
);
@@ -895,19 +895,21 @@ static int harden_rt() {
895895

896896
struct sigaction sig_act = {};
897897
#ifdef __linux__
898-
// enable realtime
899-
if (setrlimit(RLIMIT_RTPRIO, &unlimited) < 0) {
900-
rtapi_print_msg(RTAPI_MSG_WARN, "setrlimit(RTLIMIT_RTPRIO): %s\n", strerror(errno));
901-
return -errno;
902-
}
898+
// Best-effort raise of RTPRIO/CORE soft caps. Setting these to
899+
// RLIM_INFINITY requires CAP_SYS_RESOURCE, which neither setuid root
900+
// nor file capabilities grant by default. Without it, threads still
901+
// get SCHED_FIFO via CAP_SYS_NICE; the rlimit just gates how high
902+
// they can go. Don't fail harden_rt() when it can't be raised.
903+
if (setrlimit(RLIMIT_RTPRIO, &unlimited) < 0)
904+
rtapi_print_msg(RTAPI_MSG_DBG,
905+
"setrlimit(RLIMIT_RTPRIO): %s\n", strerror(errno));
903906

904-
// enable core dumps
905907
if (setrlimit(RLIMIT_CORE, &unlimited) < 0)
906908
rtapi_print_msg(
907909
RTAPI_MSG_WARN, "setrlimit: %s - core dumps may be truncated or non-existent\n", strerror(errno)
908910
);
909911

910-
// even when setuid root
912+
// even when running with elevated capabilities
911913
if (prctl(PR_SET_DUMPABLE, 1) < 0)
912914
rtapi_print_msg(
913915
RTAPI_MSG_WARN,

0 commit comments

Comments
 (0)