2004-09-23 15:47:11 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
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
|
2004-09-23 15:47:11 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
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 <assert.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2006-07-20 13:19:16 +00:00
|
|
|
#include <sys/types.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
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
2006-05-22 15:20:46 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
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
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_info("dropping root rights.");
|
2004-11-26 00:07:24 +00:00
|
|
|
|
|
|
|
|
#if defined(HAVE_SETRESUID)
|
|
|
|
|
setresuid(uid, uid, uid);
|
|
|
|
|
#elif defined(HAVE_SETREUID)
|
|
|
|
|
setreuid(uid, uid);
|
|
|
|
|
#else
|
|
|
|
|
setuid(uid);
|
|
|
|
|
seteuid(uid);
|
|
|
|
|
#endif
|
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 */
|
2004-09-23 15:47:11 +00:00
|
|
|
int pa_limit_caps(void) {
|
|
|
|
|
int r = -1;
|
|
|
|
|
cap_t caps;
|
|
|
|
|
cap_value_t nice_cap = CAP_SYS_NICE;
|
|
|
|
|
|
|
|
|
|
caps = cap_init();
|
|
|
|
|
assert(caps);
|
|
|
|
|
cap_clear(caps);
|
|
|
|
|
cap_set_flag(caps, CAP_PERMITTED, 1, &nice_cap, CAP_SET);
|
|
|
|
|
|
|
|
|
|
if (cap_set_proc(caps) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2007-05-25 20:35:30 +00:00
|
|
|
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
pa_log_info("dropped capabilities successfully.");
|
2007-05-25 20:35:30 +00:00
|
|
|
|
|
|
|
|
r = 1;
|
2004-09-23 15:47:11 +00:00
|
|
|
|
|
|
|
|
fail:
|
2007-05-25 20:35:30 +00:00
|
|
|
cap_free(caps);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-23 15:47:11 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
/* Drop all capabilities, effectively becoming a normal user */
|
2004-09-23 15:47:11 +00:00
|
|
|
int pa_drop_caps(void) {
|
|
|
|
|
cap_t caps;
|
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
|
|
caps = cap_init();
|
|
|
|
|
assert(caps);
|
|
|
|
|
|
|
|
|
|
cap_clear(caps);
|
|
|
|
|
|
2007-05-25 20:35:30 +00:00
|
|
|
prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0);
|
|
|
|
|
|
2004-09-23 22:42:49 +00:00
|
|
|
if (cap_set_proc(caps) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("failed to drop capabilities: %s", pa_cstrerror(errno));
|
2004-09-23 15:47:11 +00:00
|
|
|
goto fail;
|
2004-09-23 22:42:49 +00:00
|
|
|
}
|
2007-05-25 20:35:30 +00:00
|
|
|
|
2004-09-23 15:47:11 +00:00
|
|
|
r = 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
2007-05-25 20:35:30 +00:00
|
|
|
cap_free(caps);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-23 15:47:11 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
/* NOOPs in case capabilities are not available. */
|
2004-09-23 15:47:11 +00:00
|
|
|
int pa_limit_caps(void) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_drop_caps(void) {
|
|
|
|
|
pa_drop_root();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|