2004-09-23 15:47:11 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2004-09-23 15:47:11 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-09-23 15:47:11 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-09-23 15:47:11 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2006-07-20 13:19:16 +00:00
|
|
|
#include <sys/types.h>
|
2008-08-06 18:54:13 +02:00
|
|
|
|
|
|
|
|
#include <pulse/i18n.h>
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2008-08-06 18:54:13 +02:00
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2008-10-04 01:06:35 +02:00
|
|
|
#include <pulsecore/core-util.h>
|
2004-09-23 15:47:11 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_CAPABILITY_H
|
|
|
|
|
#include <sys/capability.h>
|
|
|
|
|
#endif
|
2007-05-25 20:35:30 +00:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
#endif
|
2004-09-23 15:47:11 +00:00
|
|
|
|
|
|
|
|
#include "caps.h"
|
|
|
|
|
|
2006-04-25 07:13:44 +00:00
|
|
|
/* Glibc <= 2.2 has broken unistd.h */
|
|
|
|
|
#if defined(linux) && (__GLIBC__ <= 2 && __GLIBC_MINOR__ <= 2)
|
|
|
|
|
int setresgid(gid_t r, gid_t e, gid_t s);
|
|
|
|
|
int setresuid(uid_t r, uid_t e, uid_t s);
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_GETUID
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
/* Drop root rights when called SUID root */
|
2004-09-23 15:47:11 +00:00
|
|
|
void pa_drop_root(void) {
|
2004-09-23 22:42:49 +00:00
|
|
|
uid_t uid = getuid();
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-23 22:42:49 +00:00
|
|
|
if (uid == 0 || geteuid() != 0)
|
|
|
|
|
return;
|
2004-11-21 21:31:28 +00:00
|
|
|
|
2008-10-19 22:25:58 +02:00
|
|
|
pa_log_info(_("Dropping root privileges."));
|
2004-11-26 00:07:24 +00:00
|
|
|
|
|
|
|
|
#if defined(HAVE_SETRESUID)
|
2008-01-24 09:35:50 +00:00
|
|
|
pa_assert_se(setresuid(uid, uid, uid) >= 0);
|
2004-11-26 00:07:24 +00:00
|
|
|
#elif defined(HAVE_SETREUID)
|
2008-01-24 09:35:50 +00:00
|
|
|
pa_assert_se(setreuid(uid, uid) >= 0);
|
2004-11-26 00:07:24 +00:00
|
|
|
#else
|
2008-01-24 09:35:50 +00:00
|
|
|
pa_assert_se(setuid(uid) >= 0);
|
|
|
|
|
pa_assert_se(seteuid(uid) >= 0);
|
2004-11-26 00:07:24 +00:00
|
|
|
#endif
|
2008-01-24 09:35:50 +00:00
|
|
|
|
|
|
|
|
pa_assert_se(getuid() == uid);
|
|
|
|
|
pa_assert_se(geteuid() == uid);
|
2004-09-23 15:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
void pa_drop_root(void) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-05-25 20:35:30 +00:00
|
|
|
#if defined(HAVE_SYS_CAPABILITY_H) && defined(HAVE_SYS_PRCTL_H)
|
2004-09-23 22:42:49 +00:00
|
|
|
|
2007-05-25 20:35:30 +00:00
|
|
|
/* Limit permitted capabilities set to CAPSYS_NICE */
|
2008-05-15 23:34:41 +00:00
|
|
|
void pa_limit_caps(void) {
|
2004-09-23 15:47:11 +00:00
|
|
|
cap_t caps;
|
|
|
|
|
cap_value_t nice_cap = CAP_SYS_NICE;
|
|
|
|
|
|
2008-03-31 22:12:32 +00:00
|
|
|
pa_assert_se(caps = cap_init());
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert_se(cap_clear(caps) == 0);
|
|
|
|
|
pa_assert_se(cap_set_flag(caps, CAP_EFFECTIVE, 1, &nice_cap, CAP_SET) == 0);
|
|
|
|
|
pa_assert_se(cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET) == 0);
|
2007-05-29 17:24:48 +00:00
|
|
|
|
2008-06-22 01:48:46 +02:00
|
|
|
if (cap_set_proc(caps) < 0)
|
|
|
|
|
/* Hmm, so we couldn't limit our caps, which probably means we
|
|
|
|
|
* hadn't any in the first place, so let's just make sure of
|
|
|
|
|
* that */
|
|
|
|
|
pa_drop_caps();
|
|
|
|
|
else
|
2008-08-06 18:54:13 +02:00
|
|
|
pa_log_info(_("Limited capabilities successfully to CAP_SYS_NICE."));
|
2007-05-29 17:24:48 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert_se(cap_free(caps) == 0);
|
2008-06-22 01:48:46 +02:00
|
|
|
|
|
|
|
|
pa_assert_se(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == 0);
|
2004-09-23 15:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
/* Drop all capabilities, effectively becoming a normal user */
|
2008-03-31 22:12:32 +00:00
|
|
|
void pa_drop_caps(void) {
|
2004-09-23 15:47:11 +00:00
|
|
|
cap_t caps;
|
|
|
|
|
|
2008-08-03 16:16:22 +02:00
|
|
|
#ifndef __OPTIMIZE__
|
|
|
|
|
/* Valgrind doesn't not know set_caps, so we bypass it here -- but
|
2008-10-04 00:13:05 +02:00
|
|
|
* only in development builds.*/
|
2008-08-03 16:16:22 +02:00
|
|
|
|
2008-10-04 00:13:05 +02:00
|
|
|
if (pa_in_valgrind() && !pa_have_caps())
|
2008-08-03 16:16:22 +02:00
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-03-31 22:12:32 +00:00
|
|
|
pa_assert_se(prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) == 0);
|
2004-09-23 15:47:11 +00:00
|
|
|
|
2008-03-31 22:12:32 +00:00
|
|
|
pa_assert_se(caps = cap_init());
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert_se(cap_clear(caps) == 0);
|
2008-03-31 22:12:32 +00:00
|
|
|
pa_assert_se(cap_set_proc(caps) == 0);
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert_se(cap_free(caps) == 0);
|
2008-06-22 01:48:46 +02:00
|
|
|
|
|
|
|
|
pa_assert_se(!pa_have_caps());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bool_t pa_have_caps(void) {
|
|
|
|
|
cap_t caps;
|
|
|
|
|
cap_flag_value_t flag = CAP_CLEAR;
|
|
|
|
|
|
2008-08-03 16:16:22 +02:00
|
|
|
#ifdef __OPTIMIZE__
|
2008-06-22 01:48:46 +02:00
|
|
|
pa_assert_se(caps = cap_get_proc());
|
2008-08-03 16:16:22 +02:00
|
|
|
#else
|
|
|
|
|
if (!(caps = cap_get_proc()))
|
|
|
|
|
return FALSE;
|
|
|
|
|
#endif
|
2008-06-22 01:48:46 +02:00
|
|
|
pa_assert_se(cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &flag) >= 0);
|
|
|
|
|
pa_assert_se(cap_free(caps) == 0);
|
|
|
|
|
|
|
|
|
|
return flag == CAP_SET;
|
2004-09-23 15:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
/* NOOPs in case capabilities are not available. */
|
2008-05-17 10:57:52 +00:00
|
|
|
void pa_limit_caps(void) {
|
2004-09-23 15:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-31 22:24:37 +00:00
|
|
|
void pa_drop_caps(void) {
|
2004-09-23 15:47:11 +00:00
|
|
|
pa_drop_root();
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-22 01:48:46 +02:00
|
|
|
pa_bool_t pa_have_caps(void) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-23 15:47:11 +00:00
|
|
|
#endif
|