2
* lxc: linux Container library
4
* (C) Copyright IBM Corp. 2007, 2008
7
* Daniel Lezcano <dlezcano at fr.ibm.com>
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include <sys/prctl.h>
27
#include <sys/capability.h>
31
lxc_log_define(lxc_caps, lxc);
33
int lxc_caps_reset(void)
35
cap_t cap = cap_init();
39
ERROR("cap_init() failed : %m");
43
if (cap_set_proc(cap)) {
44
ERROR("cap_set_proc() failed : %m");
52
int lxc_caps_down(void)
57
caps = cap_get_proc();
59
ERROR("failed to cap_get_proc: %m");
63
ret = cap_clear_flag(caps, CAP_EFFECTIVE);
65
ERROR("failed to cap_clear_flag: %m");
69
ret = cap_set_proc(caps);
71
ERROR("failed to cap_set_proc: %m");
86
caps = cap_get_proc();
88
ERROR("failed to cap_get_proc: %m");
92
for (cap = 0; cap <= CAP_LAST_CAP; cap++) {
94
cap_flag_value_t flag;
96
ret = cap_get_flag(caps, cap, CAP_PERMITTED, &flag);
98
ERROR("failed to cap_get_flag: %m");
102
ret = cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap, flag);
104
ERROR("failed to cap_set_flag: %m");
109
ret = cap_set_proc(caps);
111
ERROR("failed to cap_set_proc: %m");
120
int lxc_caps_init(void)
122
uid_t uid = getuid();
123
gid_t gid = getgid();
124
uid_t euid = geteuid();
127
INFO("command is run as 'root'");
132
INFO("command is run as setuid root (uid : %d)", uid);
134
if (prctl(PR_SET_KEEPCAPS, 1)) {
135
ERROR("failed to 'PR_SET_KEEPCAPS': %m");
139
if (setresgid(gid, gid, gid)) {
140
ERROR("failed to change gid to '%d': %m", gid);
144
if (setresuid(uid, uid, uid)) {
145
ERROR("failed to change uid to '%d': %m", uid);
150
ERROR("failed to restore capabilities: %m");
156
INFO("command is run as user '%d'", uid);