Repository navigation
Expand file tree
/
Copy pathdropcaps.c
More file actions
229 lines (200 loc) · 6.23 KB
/
Copy pathdropcaps.c
File metadata and controls
229 lines (200 loc) · 6.23 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
VTun-embedded - Virtual Tunnel over TCP/IP network.
Copyright (C) 2025 Jan-Espen Oversand <sigsegv@radiotube.org>
VTun has been derived from VPPP package by Maxim Krasnyansky.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "vtun.h"
#include "lib.h"
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <syslog.h>
#include <errno.h>
#include "sys_dropcaps.h"
/* Helpers for pipe I/O (native-endian, same-arch child/parent) */
static int write_full(int fd, const void *buf, size_t len)
{
const char *p = (const char *)buf;
while (len > 0) {
ssize_t wr = write(fd, p, len);
if (wr < 0) return -1;
p += (size_t)wr;
len -= (size_t)wr;
}
return 0;
}
static int read_full(int fd, void *buf, size_t len)
{
char *p = (char *)buf;
while (len > 0) {
ssize_t rr = read(fd, p, len);
if (rr <= 0) return -1;
p += (size_t)rr;
len -= (size_t)rr;
}
return 0;
}
/* Serialize minimal vtun_host state to pipe. Native endianness by design. */
int serialize_host_to_pipe(int fd, const struct vtun_host *h)
{
const uint32_t magic = 0x7674756E; /* 'vtun' */
const uint16_t host_len = (uint16_t)(h->host ? strlen(h->host) : 0);
const char *hoststr = h->host ? h->host : "";
uint32_t fields[11];
fields[0] = (uint32_t) h->flags;
fields[1] = (uint32_t) h->timeout;
fields[2] = (uint32_t) h->spd_in;
fields[3] = (uint32_t) h->spd_out;
fields[4] = (uint32_t) h->zlevel;
fields[5] = (uint32_t) h->cipher;
fields[6] = (uint32_t) h->persist;
fields[7] = (uint32_t) h->multi;
fields[8] = (uint32_t) h->ka_interval;
fields[9] = (uint32_t) h->ka_maxfail;
fields[10] = 0;
if (write_full(fd, &magic, sizeof(magic)) < 0) {
return -1;
}
if (write_full(fd, &host_len, sizeof(host_len)) < 0) {
return -1;
}
if (host_len && write_full(fd, hoststr, host_len) < 0) {
return -1;
}
if (write_full(fd, fields, sizeof(fields)) < 0) {
return -1;
}
return 0;
}
/* Deserialize vtun_host state from pipe. Allocates and returns vtun_host. */
struct vtun_host *deserialize_host_from_pipe(int fd)
{
uint32_t magic = 0;
uint16_t host_len = 0, dev_len = 0;
if (read_full(fd, &magic, sizeof(magic)) < 0) {
return NULL;
}
if (read_full(fd, &host_len, sizeof(host_len)) < 0) {
return NULL;
}
if (magic != 0x7674756E || host_len > 1024) {
return NULL;
}
char *hoststr = (char *)malloc((size_t)host_len + 1);
if (!hoststr) {
return NULL;
}
if (host_len && read_full(fd, hoststr, host_len) < 0) {
free(hoststr);
return NULL;
}
hoststr[host_len] = '\0';
uint32_t fields[11];
if (read_full(fd, fields, sizeof(uint32_t) * 11) < 0) {
free(hoststr);
return NULL;
}
struct vtun_host *h = (struct vtun_host *)malloc(sizeof(struct vtun_host));
if (!h) {
free(hoststr);
return NULL;
}
memset(h, 0, sizeof(struct vtun_host));
h->host = hoststr;
h->flags = (int)fields[0];
h->timeout = (int)fields[1];
h->spd_in = (int)fields[2];
h->spd_out = (int)fields[3];
h->zlevel = (int)fields[4];
h->cipher = (int)fields[5];
h->persist = (int)fields[6];
h->multi = (int)fields[7];
h->ka_interval = (int)fields[8];
h->ka_maxfail = (int)fields[9];
return h;
}
void free_deserialized_host(struct vtun_host *h)
{
if (!h) return;
free(h->host);
free(h);
}
int dropcaps_needed() {
int needed = (geteuid() == 0);
if (!needed) {
vtun_syslog(LOG_INFO, "Not running as root, will not setuid or drop capabilities");
}
return needed;
}
int dropcaps_current_session()
{
gid_t target_gid = (gid_t)-1;
uid_t target_uid = (uid_t)-1;
if (vtun.dropcaps && proc_dropcaps(1, vtun.setuid || vtun.setgid) != 0) {
vtun_syslog(LOG_ERR, "Failed to drop capabilities");
return 0;
}
if (vtun.setgid) {
if (vtun.setgid_gid != (gid_t)-1) {
target_gid = vtun.setgid_gid;
} else {
struct group *gr = getgrnam("nobody");
if (!gr) {
vtun_syslog(LOG_ERR, "group 'nobody' not found; unable to set group id");
return 0;
}
target_gid = gr->gr_gid;
}
vtun_syslog(LOG_INFO, "Setting gid of pid %d to %d", getpid(), (int)target_gid);
setgroups(0, NULL);
if (setgid(target_gid) != 0) {
vtun_syslog(LOG_ERR, "setgid failed: %s", strerror(errno));
return 0;
}
if (setegid(target_gid) != 0) {
vtun_syslog(LOG_ERR, "setegid failed: %s", strerror(errno));
return 0;
}
}
if (vtun.setuid) {
if (vtun.setuid_uid != (uid_t)-1) {
target_uid = vtun.setuid_uid;
} else {
struct passwd *pw = getpwnam("nobody");
if (!pw) {
vtun_syslog(LOG_ERR, "user 'nobody' not found; unable to set user id");
return 0;
}
target_uid = pw->pw_uid;
}
vtun_syslog(LOG_INFO, "Setting uid of pid %d to %d", getpid(), (int)target_uid);
if (setuid(target_uid) != 0) {
vtun_syslog(LOG_ERR, "setuid failed: %s", strerror(errno));
return 0;
}
if (seteuid(target_uid) != 0) {
vtun_syslog(LOG_ERR, "setuid failed: %s", strerror(errno));
return 0;
}
}
if ((vtun.setuid || vtun.setgid) && vtun.dropcaps) {
if (proc_dropcaps(0, 0) != 0) {
vtun_syslog(LOG_ERR, "Failed to drop capabilities");
return 0;
}
}
return 1;
}