Мой первый совет - проверить возвращаемое значение setpriority()
, чтобы узнать, не вернула ли она ошибку. Мое ожидание состоит в том, что он указывает на ошибку, возвращая -1
и что проверка errno
показала бы, что EACCES
была ошибкой, указывающей, что вызывающий абонент не обладал необходимой привилегией.
Есть три способа, чтобы исправить это:
1) Сделать исполняемый корень SUID или запустить его с Судом (не безопасно).
2) Предоставьте исполняемому файлу возможность CAP_SYS_NICE
(например, sudo setcap cap_sys_nice=ep <executable>
).
3) Отрегулируйте жесткие и мягкие пределы для корпуса (ulimit
) или пользователя (в зависимости от дистрибутива /etc/security/limits.conf
). Или просто отрегулируйте жесткий предел и позвольте программе отрегулировать мягкий предел.
Обратите внимание, что возможности SUID и файла не действуют на разделы, смонтированные с помощью nosuid
(часто используются зашифрованные домашние каталоги).
EDIT: @TrentP указал, что вы можете отказаться от привилегий. Ниже приведен код, который является ярким примером того, как сделать это, хотя он работает в режиме реального времени приоритет:
/*
* Set_policy_priority
*
* This is used to set the priority and policy for the real-time
* scheduler. This normally requires some form of privilege, as the
* default hard ulimit of 0 will prevent an unprivileged program from
* doing so.
*
* The most secure thing to do is grant the executable the potential
* to enable CAP_SYS_RESOURCE (with sudo setcap cap_sys_resource=p
* <executable>). If it is needed, this routine will enable the
* capability, raise the hard limit, and then irrevocably drop the
* privilege.
*
*/
int set_policy_priority(int policy, int priority, int nofiles) {
const cap_value_t cap_vector[1] = { CAP_SYS_RESOURCE };
cap_t privilege_dropped = cap_init();
cap_t privilege_off = cap_dup(privilege_dropped);
cap_set_flag(privilege_off, CAP_PERMITTED, 1, cap_vector, CAP_SET);
cap_t privilege_on = cap_dup(privilege_off);
cap_set_flag(privilege_on, CAP_EFFECTIVE, 1, cap_vector, CAP_SET);
struct sched_param param;
struct rlimit rl;
int e, min, max;
// See if priority we want is in the range offered by SCHED_FIFO
min = sched_get_priority_min(policy);
max = sched_get_priority_max(policy);
if (verbose) {
fprintf(stderr, "For policy SCHED_FIFO min priority is %d, max is %d.\n", min, max);
}
if ((min>priority)||(max<priority)) {
fprintf(stderr, "Desired priority of %d is out of range.\n", priority);
return 1;
}
// See if the RTPRIO limits allows the priority we want
if (getrlimit(RLIMIT_RTPRIO, &rl) != 0) {
e = errno;
fprintf(stderr, "Failed to getrlimit(): %s.\n", strerror(e));
return 1;
}
if (verbose) {
fprintf(stderr, "RTPRIO soft limit is %d, hard is %d.\n",
(int) rl.rlim_cur, (int) rl.rlim_max);
}
// Adjust hard limit if necessary
if (rl.rlim_max < priority) {
if (cap_set_proc(privilege_on) != 0) {
fprintf(stderr, "Need to raise RTPRIO hard limit, but can't enable CAP_SYS_RESOURCE.\n");
return 1;
}
rl.rlim_max = priority;
if (setrlimit(RLIMIT_RTPRIO, &rl) != 0) {
e = errno;
fprintf(stderr, "Failed to raise hard limit for RTPRIO to %d: %s.\n",
(int) rl.rlim_max, strerror(e));
return 1;
}
if (cap_set_proc(privilege_off)) {
fprintf(stderr, "Failed to turn off privileges.\n");
return 1;
}
if (verbose) {
printf("Raised hard limit for RTPRIO to %d.\n", (int) rl.rlim_max);
}
}
// Adjust soft limit if necessary
if (rl.rlim_cur < priority) {
rl.rlim_cur = priority;
if (setrlimit(RLIMIT_RTPRIO, &rl) != 0) {
e = errno;
fprintf(stderr, "Failed to raise soft limit for RTPRIO to %d: %s.\n",
(int) rl.rlim_cur, strerror(e));
return 1;
}
if (verbose) {
printf("Raised soft limit for RTPRIO to %d.\n", (int) rl.rlim_cur);
}
}
// Set desired priority with class SCHED_FIFO
param.sched_priority = priority;
if (sched_setscheduler(0, policy, ¶m) != 0) {
e = errno;
fprintf(stderr, "Setting policy failed: %s.\n", strerror(e));
return 1;
} else if (verbose) {
printf("Set policy SCHED_FIFO, priority %d.\n", param.sched_priority);
}
// See if the NOFILE limits allows the number of fds we want
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
e = errno;
fprintf(stderr, "Failed to getrlimit(): %s.\n", strerror(e));
return 1;
}
if (verbose) {
fprintf(stderr, "NOFILE soft limit is %d, hard is %d.\n",
(int) rl.rlim_cur, (int) rl.rlim_max);
}
// Adjust hard limit if necessary
if (rl.rlim_max < nofiles) {
if (cap_set_proc(privilege_on) != 0) {
fprintf(stderr, "Need to raise NOFILE hard limit, but can't enable CAP_SYS_RESOURCE.\n");
return 1;
}
rl.rlim_max = nofiles;
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
e = errno;
fprintf(stderr, "Failed to raise hard limit for NOFILE to %d: %s.\n",
(int) rl.rlim_max, strerror(e));
return 1;
}
if (cap_set_proc(privilege_off)) {
fprintf(stderr, "Failed to turn off privileges.\n");
return 1;
}
if (verbose) {
printf("Raised hard limit for NOFILE to %d.\n", (int) rl.rlim_max);
}
}
// Adjust soft limit if necessary
if (rl.rlim_cur < nofiles) {
rl.rlim_cur = nofiles;
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
e = errno;
fprintf(stderr, "Failed to raise soft limit for NOFILE to %d: %s.\n",
(int) rl.rlim_cur, strerror(e));
return 1;
}
if (verbose) {
printf("Raised soft limit for NOFILE to %d.\n", (int) rl.rlim_cur);
}
}
if (cap_set_proc(privilege_dropped)) {
fprintf(stderr, "Failed to turn irrevocably drop privileges.\n");
return 1;
}
return 0;
}
Как вариант 1 и 2, вы можете сделать исполняемый Setuid корень, то падение суперпользователя и seteuid для пользователя без полномочий root, но выполняйте его таким образом, чтобы он сохранял CAP_SYS_NICE (но не другие привилегии). – TrentP
Спасибо, я также обнаружил, что «sudo ./a.out» тоже будет работать. И ваш «sudo setcap cap_sys_nice ep = a.out» будет отлично, (навсегда). –
@HindForsum отредактирован для добавления sudo в setuid решение. –