aboutsummaryrefslogtreecommitdiff
path: root/contrib/capso/capso.c
blob: 7ca3427505513de40a018610477d569b5a0ff6b3 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Worked example for a shared object with a file capability on it
 * leveraging itself for preprogrammed functionality.
 *
 * This example implements a shared library that can bind to
 * the privileged port. ":80".
 *
 * The shared library needs to be installed with
 * cap_net_bind_service=p. As a shared library, it provides the
 * function bind80().
 */

#define _GNU_SOURCE

#include <dlfcn.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/capability.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>

#include "capso.h"

extern char **environ;

/*
 * fake_exploit is some dedicated code to simulate a shell escape type
 * exploit. This is obviously not something serious to include in code
 * that has actually been audited for security, but we use it to
 * demonstrate an aspect of file capabilities vs. setuid root for
 * granting privilege.
 */
static void fake_exploit(void) {
#ifdef ALLOW_EXPLOIT
    const char *exploit = getenv("TRIGGER_EXPLOIT");
    if (exploit == NULL) {
	return;
    }

    switch (*exploit) {
    case '^':
    case '%':
	exploit++;
	cap_value_t caps = CAP_NET_BIND_SERVICE;
	cap_t c = cap_get_proc();
	cap_set_flag(c, CAP_INHERITABLE, 1, &caps, CAP_SET);
	if (cap_set_proc(c)) {
	    perror("Failed to raise inheritable capability");
	    exit(1);
	}
	if (*(exploit-1) == '%') {
	    break;
	}
	cap_free(c);
	if (cap_set_ambient(caps, CAP_SET) != 0) {
	    perror("Unable to raise ambient capability");
	    exit(1);
	}
	break;
    }

    char *ts = strdup(exploit);
    if (ts == NULL) {
	perror("Failed to duplicate exploit string");
	exit(1);
    }

    int i, j, n = 1;
    for (i = 0; ts[i]; i++) {
	switch (ts[i]) {
	case ' ':
	case '\t':
	    n++;
	    ts[i] = '\0';
	}
    }
    char **argv = calloc(n, sizeof(char *));
    for (i = 0, j = 0; j < n; j++) {
	char *s = ts+i;
	argv[j] = s;
	i += 1 + strlen(s);
	printf("execv argv[%d] = \"%s\"\n", j, s);
    }

    execv(argv[0], argv);
    perror("Execv failed");
    exit(1);
#endif /* def ALLOW_EXPLOIT */
}

/*
 * where_am_i determines the full path for the shared libary that
 * contains this function. It allocates the path in strdup()d memory
 * that should be free()d by the caller. If it can't find itself, it
 * returns NULL.
 */
static char *where_am_i(void)
{
    Dl_info info;
    if (dladdr(where_am_i, &info) == 0) {
	return NULL;
    }
    return strdup(info.dli_fname);
}

/*
 * try_bind80 attempts to reuseably bind to port 80 with the given
 * hostname. It returns a bound filedescriptor or -1 on error.
 */
static int try_bind80(const char *hostname)
{
    struct addrinfo *conf, *detail = NULL;
    int err, ret = -1, one = 1;

    conf = calloc(1, sizeof(*conf));
    if (conf == NULL) {
      return -1;
    }

    conf->ai_family = PF_UNSPEC;
    conf->ai_socktype = SOCK_STREAM;
    conf->ai_protocol = 0;
    conf->ai_flags = AI_PASSIVE | AI_ADDRCONFIG;

    err = getaddrinfo(hostname, "80", conf, &detail);
    if (err != 0) {
	goto done;
    }

    ret = socket(detail->ai_family, detail->ai_socktype, detail->ai_protocol);
    if (ret == -1) {
	goto done;
    }

    if (setsockopt(ret, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
	close(ret);
	ret = -1;
	goto done;
    }

    if (bind(ret, detail->ai_addr, detail->ai_addrlen)) {
	close(ret);
	ret = -1;
	goto done;
    }

 done:
    if (detail != NULL) {
	freeaddrinfo(detail);
    }
    free(conf);

    return ret;
}

/*
 * set_fd3 forces file descriptor 3 to be associated with a unix
 * socket that can be used to send a file descriptor back to the
 * parent program.
 */
static int set_fd3(void *detail)
{
    int *sp = detail;

    close(sp[0]);
    if (dup2(sp[1], 3) != 3) {
	return -1;
    }
    close(sp[1]);

    return 0;
}

/*
 * bind80 returns a socket filedescriptor that is bound to port 80 of
 * the provided service address.
 *
 * Example:
 *
 *   int fd = bind80("localhost");
 *
 * fd < 0 in the case of error.
 */
int bind80(const char *hostname)
{
    cap_launch_t helper;
    pid_t child;
    char const *args[3];
    char *path;
    int fd, ignored;
    int sp[2];
    char junk[1];
    const int rec_buf_len = CMSG_SPACE(sizeof(int));
    char *rec_buf[CMSG_SPACE(sizeof(int))];
    struct iovec *iov;
    struct msghdr *msg;

    fd = try_bind80(hostname);
    if (fd >= 0) {
	return fd;
    }

#ifdef CAPSO_DEBUG
    printf("application bind80(%s) attempt failed\n", hostname);
    sleep(30);
#endif

    iov = calloc(1, sizeof(struct iovec));
    if (iov == NULL) {
      return -1;
    }
    msg = calloc(1, sizeof(struct msghdr));
    if (msg == NULL) {
      free(iov);
      return -1;
    }

    /*
     * Initial attempt didn't work, so try launching the shared
     * library as an executable and getting it to yield a bound
     * filedescriptor for us via a unix socket pair.
     */
    path = where_am_i();
    if (path == NULL) {
	perror("Unable to find self");
	goto drop_alloc;
    }

    args[0] = "bind80-helper";
    args[1] = hostname;
    args[2] = NULL;

    helper = cap_new_launcher(path, args, (void *) environ);
    if (helper == NULL) {
	goto drop_path;
    }

    if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sp)) {
	goto drop_helper;
    }

    cap_launcher_callback(helper, set_fd3);
    child = cap_launch(helper, sp);
    close(sp[1]);

    if (child <= 0) {
	goto drop_sp;
    }

    iov[0].iov_base = junk;
    iov[0].iov_len = 1;

    msg->msg_name = NULL;
    msg->msg_namelen = 0;
    msg->msg_iov = iov;
    msg->msg_iovlen = 1;
    msg->msg_control = rec_buf;
    msg->msg_controllen = rec_buf_len;

    if (recvmsg(sp[0], msg, 0) != -1) {
	fd = * (int *) CMSG_DATA(CMSG_FIRSTHDR(msg));
    }
    waitpid(child, &ignored, 0);

 drop_sp:
    close(sp[0]);

 drop_helper:
    cap_free(helper);

 drop_path:
    free(path);

 drop_alloc:
    free(msg);
    free(iov);

    return fd;
}

#include "../../libcap/execable.h"
//#define SO_MAIN int main

SO_MAIN(int argc, char **argv)
{
    const char *cmd = "<capso.so>";
    const cap_value_t cap_net_bind_service = CAP_NET_BIND_SERVICE;
    cap_t working;
    int fd;
    struct msghdr msg;
    struct cmsghdr *ctrl;
    struct iovec payload;
    char data[CMSG_SPACE(sizeof(fd))];
    char junk[1];

#ifdef CAPSO_DEBUG
    printf("invoking %s standalone\n", argv[0]);
    sleep(30);
#endif

    if (argv != NULL) {
	cmd = argv[0];
    }

    if (argc != 2 || argv[1] == NULL || !strcmp(argv[1], "--help")) {
	fprintf(stderr, "usage: %s <hostname>\n", cmd);
	exit(1);
    }

    working = cap_get_proc();
    if (working == NULL) {
	perror("Unable to read capabilities");
	exit(1);
    }

    if (cap_set_flag(working, CAP_EFFECTIVE, 1,
		     &cap_net_bind_service, CAP_SET) != 0) {
	perror("Unable to raise CAP_NET_BIND_SERVICE");
	exit(1);
    }

    if (cap_set_proc(working) != 0) {
	perror("Problem with cap_set_proc");
	fprintf(stderr, "Try: sudo setcap cap_net_bind_service=p %s\n",
		argv[0]);
	exit(1);
    }

    fd = try_bind80(argv[1]);

    memset(data, 0, sizeof(data));
    memset(&payload, 0, sizeof(payload));

    payload.iov_base = junk;
    payload.iov_len = 1;

    msg.msg_name = NULL;
    msg.msg_namelen = 0;
    msg.msg_iov = &payload;
    msg.msg_iovlen = 1;
    msg.msg_control = data;
    msg.msg_controllen = sizeof(data);

    ctrl = CMSG_FIRSTHDR(&msg);
    ctrl->cmsg_level = SOL_SOCKET;
    ctrl->cmsg_type = SCM_RIGHTS;
    ctrl->cmsg_len = CMSG_LEN(sizeof(fd));

    *((int *) CMSG_DATA(ctrl)) = fd;

    if (sendmsg(3, &msg, 0) < 0) {
	perror("Failed to write fd");
    }

    fake_exploit();

#ifdef CAPSO_DEBUG
    printf("exiting standalone %s\n", argv[0]);
    sleep(30);
#endif

    exit(0);
}