forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetuid_linux.c
101 lines (85 loc) · 3.18 KB
/
Setuid_linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "util/Setuid.h"
#include "memory/Allocator.h"
#include "exception/Except.h"
//#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/capability.h>
#include <sys/prctl.h>
#ifndef CAP_TO_MASK
#define CAP_TO_MASK(X) (1 << ((X) & 31))
#endif
#ifndef _LINUX_CAPABILITY_VERSION_3
#define _LINUX_CAPABILITY_VERSION_3 0x20080522
#endif
#define PERMITTED_MASK CAP_TO_MASK(CAP_NET_ADMIN)
static inline int capSet(cap_user_header_t hdr, cap_user_data_t data)
{
int capset(cap_user_header_t hdr, cap_user_data_t data);
return capset(hdr, data);
}
static inline int capGet(cap_user_header_t hdr, cap_user_data_t data)
{
int capget(cap_user_header_t hdr, cap_user_data_t data);
return capget(hdr, data);
}
void Setuid_preSetuid(struct Allocator* alloc, struct Except* eh)
{
cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 2);
hdr->version = _LINUX_CAPABILITY_VERSION_3;
hdr->pid = 0;
if (capGet(hdr, data)) {
Except_throw(eh, "Error getting capabilities: [errno:%d (%s)]", errno, strerror(errno));
}
data->permitted &= PERMITTED_MASK | CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID);
data->effective = data->permitted;
data->inheritable = 0;
if (capSet(hdr, data)) {
Except_throw(eh, "Error setting capabilities: [errno:%d (%s)]", errno, strerror(errno));
}
if (prctl(PR_SET_KEEPCAPS, 1)) {
Except_throw(eh, "Error keeping capabilities: [errno:%d (%s)]", errno, strerror(errno));
}
}
void Setuid_postSetuid(struct Allocator* alloc, struct Except* eh)
{
cap_user_header_t hdr = Allocator_calloc(alloc, sizeof(*hdr), 1);
cap_user_data_t data = Allocator_calloc(alloc, sizeof(*data), 2);
hdr->version = _LINUX_CAPABILITY_VERSION_3;
hdr->pid = 0;
if (capGet(hdr, data)) {
Except_throw(eh, "Error getting capabilities (post-setuid): [errno:%d (%s)]",
errno, strerror(errno));
}
data->permitted &= PERMITTED_MASK;
data->effective = data->permitted;
data->inheritable = 0;
if (capSet(hdr, data)) {
Except_throw(eh, "Error setting capabilities (post-setuid): [errno:%d (%s)]",
errno, strerror(errno));
}
if (prctl(PR_SET_KEEPCAPS, 0)) {
Except_throw(eh, "Error un-keeping capabilities (post-setuid): [errno:%d (%s)]",
errno, strerror(errno));
}
}