File: setuserns.c

package info (click to toggle)
bpfilter 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,076 kB
  • sloc: ansic: 30,397; sh: 1,383; cpp: 959; python: 495; yacc: 385; lex: 194; makefile: 9
file content (301 lines) | stat: -rw-r--r-- 7,872 bytes parent folder | download
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
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
 */

#include <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <asm/unistd.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <linux/mount.h>
#include <fcntl.h>
#include <sys/mount.h>

#include "core/helper.h"
#include "core/logger.h"

#define CMD_LEN 64
static char cmd[CMD_LEN];

struct st_opts
{
    const char *socket_path;
    const char *bpffs_mount_path;
};

static error_t st_opts_parser(int key, const char *arg, struct argp_state *state)
{
    struct st_opts *opts = state->input;

    switch (key) {
    case 's':
        opts->socket_path = arg;
        break;
    case 'b':
        opts->bpffs_mount_path = arg;
        break;
    case ARGP_KEY_END:
        if (!opts->socket_path)
            return bf_err_r(-EINVAL, "--socket argument required");
        if (!opts->bpffs_mount_path)
            opts->bpffs_mount_path = "/sys/fs/bpf";
        break;
    default:
        return ARGP_ERR_UNKNOWN;
    }

    return 0;
}

static inline void usage(void)
{
    bf_err("usage: setup_token_bin COMMAND [OPTIONS...]");
}

int send_fd(int sock_fd, int fd)
{
    struct msghdr msg = {};
    struct cmsghdr *cmsg;
    int fds[1] = { fd };
    char iobuf[1];
    struct iovec io = {
        .iov_base = iobuf,
        .iov_len = sizeof(iobuf),
    };
    union {
        char buf[CMSG_SPACE(sizeof(fds))];
        struct cmsghdr align;
    } u;
    int r;

    msg.msg_iov = &io;
    msg.msg_iovlen = 1;
    msg.msg_control = u.buf;
    msg.msg_controllen = sizeof(u.buf);
    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(fds));
    memcpy(CMSG_DATA(cmsg), fds, sizeof(fds));

    r = sendmsg(sock_fd, &msg, 0);
    if (r < 0)
        return bf_err_r(errno, "send_fd: failed to send message");
    if (r != 1)
        return bf_err_r(-EINVAL, "send_fd: unexpected amount of data sent (%d)", r);

    return 0;
}

int recv_fd(int sock_fd)
{
    struct msghdr msg = {};
    struct cmsghdr *cmsg;
    int fds[1];
    char iobuf[1];
    struct iovec io = {
        .iov_base = iobuf,
        .iov_len = sizeof(iobuf),
    };
    union {
        char buf[CMSG_SPACE(sizeof(fds))];
        struct cmsghdr align;
    } u;
    int r;

    msg.msg_iov = &io;
    msg.msg_iovlen = 1;
    msg.msg_control = u.buf;
    msg.msg_controllen = sizeof(u.buf);

    r = recvmsg(sock_fd, &msg, 0);
    if (r < 0)
        return bf_err_r(errno, "recv_fd: failed to receive message");
    if (r != 1)
        return bf_err_r(r, "recv_fd: unexpected amount of data received (%d)", r);

    cmsg = CMSG_FIRSTHDR(&msg);
    if (!cmsg)
        return bf_err_r(-EINVAL, "recv_fd: cmsg is NULL");
    if (cmsg->cmsg_len != CMSG_LEN(sizeof(fds)))
        return bf_err_r(-EINVAL, "recv_fd: cmsg has unexpected length");
    if (cmsg->cmsg_level != SOL_SOCKET)
        return bf_err_r(-EINVAL, "recv_fd: cmsg has unexpected level");
    if (cmsg->cmsg_type != SCM_RIGHTS)
        return bf_err_r(-EINVAL, "recv_fd: cmsg has unexpected type");

    memcpy(fds, CMSG_DATA(cmsg), sizeof(fds));

    return fds[0];
}

int do_in(const struct st_opts *opts)
{
    _cleanup_close_ int sock_fd = -1;
    _cleanup_close_ int bpffs_fd = -1;
    _cleanup_close_ int mount_fd = -1;
    struct sockaddr_un addr = {};
    int r;

    /**
     * Get socket
     */
    sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sock_fd < 0)
        return bf_err_r(errno, "do_in: can't create socket");

    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, opts->socket_path, sizeof(addr.sun_path) - 1);

    r = connect(sock_fd, (struct sockaddr *)&addr, sizeof(addr));
    if (r < 0)
        return bf_err_r(errno, "do_in: failed to connect to socket at %s", opts->socket_path);

    /**
     * Configure bpffs to allow for BPF tokens
     */
    bpffs_fd = fsopen("bpf", 0);
    if (bpffs_fd < 0)
        return bf_err_r(errno, "do_in: failed to open BPF filesystem");

    r = send_fd(sock_fd, bpffs_fd);
    if (r)
        return bf_err_r(r, "do_in: failed to send file descriptor");

    mount_fd = recv_fd(sock_fd);
    if (mount_fd < 0)
        return bf_err_r(mount_fd, "do_in: failed to receive mount fd");

    r = move_mount(mount_fd, "", AT_FDCWD, opts->bpffs_mount_path, MOVE_MOUNT_F_EMPTY_PATH);
    if (r)
        return bf_err_r(errno, "failed to move mount");

    return 0;
}

int do_out(const struct st_opts *opts)
{
    _cleanup_close_ int sock_fd = -1;
    _cleanup_close_ int client_fd = -1;
    _cleanup_close_ int bpffs_fd = -1;
    _cleanup_close_ int mnt_fd = -1;
    struct sockaddr_un addr = {};
    int r;

    /**
     * Configure a socket
     */
    sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sock_fd < 0)
        return bf_err_r(errno, "do_out: failed to create socket");

    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, opts->socket_path, sizeof(addr.sun_path) - 1);

    r = bind(sock_fd, (struct sockaddr *)&addr, sizeof(addr));
    if (r < 0)
        return bf_err_r(errno, "do_out: failed to bind socket to %s", opts->socket_path);

    r = listen(sock_fd, 1);
    if (r)
        return bf_err_r(errno, "do_out: failed to listen to connections");

    client_fd = accept(sock_fd, NULL, NULL);
    if (client_fd < 0)
        return bf_err_r(errno, "do_out: failed to accept connection");

    /**
     * Receive a bpffs FD
     */
    bpffs_fd = recv_fd(client_fd);
    if (bpffs_fd < 0)
        return bf_err_r(bpffs_fd, "do_out: failed to receive file descriptor");

    r = fsconfig(bpffs_fd, FSCONFIG_SET_STRING, "delegate_cmds", "any", 0);
    if (r)
        return bf_err_r(r, "do_out: failed to set 'delegate_cmds'");

    r = fsconfig(bpffs_fd, FSCONFIG_SET_STRING, "delegate_maps", "any", 0);
    if (r)
        return bf_err_r(r, "do_out: failed to set 'delegate_maps'");

    r = fsconfig(bpffs_fd, FSCONFIG_SET_STRING, "delegate_progs", "any", 0);
    if (r)
        return bf_err_r(r, "do_out: failed to set 'delegate_progs'");

    r = fsconfig(bpffs_fd, FSCONFIG_SET_STRING, "delegate_attachs", "any", 0);
    if (r)
        return bf_err_r(r, "do_out: failed to set 'delegate_attachs'");

    r = fsconfig(bpffs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
    if (r)
        return bf_err_r(r, "do_out: failed to create fsconfig");

    mnt_fd = fsmount(bpffs_fd, 0, 0);
    if (mnt_fd < 0)
        return bf_err_r(mnt_fd, "do_out: failed to fsmount bpffs");

    /**
     * Send the FD back
     */
    r = send_fd(client_fd, mnt_fd);
    if (r)
        return bf_err_r(r, "do_out: failed to send file descriptor");

    return 0;
}

int main(int argc, char *argv[])
{
    static struct argp_option options[] = {
        {"socket", 's', "SOCKET_PATH", 0, "Path to the socket to use to communicate", 0},
        {"bpffs-mount-path", 'b', "BPFFS_MOUNT_PATH", 0, "Path to mount the bpffs with delegated attributes. Defaults to /sys/fs/bpf.", 0},
        {0},
    };

    const char *command;
    struct st_opts opts = {};
    struct argp argp = {
        options, (argp_parser_t)st_opts_parser,
        NULL,    NULL,
        0,       NULL,
        NULL,
    };
    int r;

    if (argc < 2 || argv[1][0] == '-') {
        usage();
        return EXIT_FAILURE;
    }

    command = argv[1];

    snprintf(cmd, CMD_LEN, "%s %s", argv[0], argv[1]);
    argv[1] = cmd;
    argv++;
    argc--;

    r = argp_parse(&argp, argc, argv, 0, 0, &opts);
    if (r)
        return bf_err_r(r, "failed to parse arguments");

    if (bf_streq(command, "in")) {
        r = do_in(&opts);
    } else if (bf_streq(command, "out")) {
        r = do_out(&opts);
    } else {
        usage();
        return EXIT_FAILURE;
    }

    return r;

    return 0;
}