|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <errno.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <sys/types.h> |
| 7 | +#include <sys/socket.h> |
| 8 | +#include <netinet/in.h> |
| 9 | + |
| 10 | +#include <linux/filter.h> |
| 11 | +#include <bpf/bpf.h> |
| 12 | +#include <bpf/libbpf.h> |
| 13 | + |
| 14 | +#include "bpf_rlimit.h" |
| 15 | +#include "bpf_util.h" |
| 16 | +#include "cgroup_helpers.h" |
| 17 | + |
| 18 | +#define CG_PATH "/sockopt" |
| 19 | + |
| 20 | +#define SOL_CUSTOM 0xdeadbeef |
| 21 | + |
| 22 | +static int getsetsockopt(void) |
| 23 | +{ |
| 24 | + int fd, err; |
| 25 | + char buf[4] = {}; |
| 26 | + socklen_t optlen; |
| 27 | + |
| 28 | + fd = socket(AF_INET, SOCK_STREAM, 0); |
| 29 | + if (fd < 0) { |
| 30 | + log_err("Failed to create socket"); |
| 31 | + return -1; |
| 32 | + } |
| 33 | + |
| 34 | + /* IP_TOS - BPF bypass */ |
| 35 | + |
| 36 | + buf[0] = 0x08; |
| 37 | + err = setsockopt(fd, SOL_IP, IP_TOS, buf, 1); |
| 38 | + if (err) { |
| 39 | + log_err("Failed to call setsockopt(IP_TOS)"); |
| 40 | + goto err; |
| 41 | + } |
| 42 | + |
| 43 | + buf[0] = 0x00; |
| 44 | + optlen = 1; |
| 45 | + err = getsockopt(fd, SOL_IP, IP_TOS, buf, &optlen); |
| 46 | + if (err) { |
| 47 | + log_err("Failed to call getsockopt(IP_TOS)"); |
| 48 | + goto err; |
| 49 | + } |
| 50 | + |
| 51 | + if (buf[0] != 0x08) { |
| 52 | + log_err("Unexpected getsockopt(IP_TOS) buf[0] 0x%02x != 0x08", |
| 53 | + buf[0]); |
| 54 | + goto err; |
| 55 | + } |
| 56 | + |
| 57 | + /* IP_TTL - EPERM */ |
| 58 | + |
| 59 | + buf[0] = 1; |
| 60 | + err = setsockopt(fd, SOL_IP, IP_TTL, buf, 1); |
| 61 | + if (!err || errno != EPERM) { |
| 62 | + log_err("Unexpected success from setsockopt(IP_TTL)"); |
| 63 | + goto err; |
| 64 | + } |
| 65 | + |
| 66 | + /* SOL_CUSTOM - handled by BPF */ |
| 67 | + |
| 68 | + buf[0] = 0x01; |
| 69 | + err = setsockopt(fd, SOL_CUSTOM, 0, buf, 1); |
| 70 | + if (err) { |
| 71 | + log_err("Failed to call setsockopt"); |
| 72 | + goto err; |
| 73 | + } |
| 74 | + |
| 75 | + buf[0] = 0x00; |
| 76 | + optlen = 4; |
| 77 | + err = getsockopt(fd, SOL_CUSTOM, 0, buf, &optlen); |
| 78 | + if (err) { |
| 79 | + log_err("Failed to call getsockopt"); |
| 80 | + goto err; |
| 81 | + } |
| 82 | + |
| 83 | + if (optlen != 1) { |
| 84 | + log_err("Unexpected optlen %d != 1", optlen); |
| 85 | + goto err; |
| 86 | + } |
| 87 | + if (buf[0] != 0x01) { |
| 88 | + log_err("Unexpected buf[0] 0x%02x != 0x01", buf[0]); |
| 89 | + goto err; |
| 90 | + } |
| 91 | + |
| 92 | + /* SO_SNDBUF is overwritten */ |
| 93 | + |
| 94 | + buf[0] = 0x01; |
| 95 | + buf[1] = 0x01; |
| 96 | + buf[2] = 0x01; |
| 97 | + buf[3] = 0x01; |
| 98 | + err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, buf, 4); |
| 99 | + if (err) { |
| 100 | + log_err("Failed to call setsockopt(SO_SNDBUF)"); |
| 101 | + goto err; |
| 102 | + } |
| 103 | + |
| 104 | + buf[0] = 0x00; |
| 105 | + buf[1] = 0x00; |
| 106 | + buf[2] = 0x00; |
| 107 | + buf[3] = 0x00; |
| 108 | + optlen = 4; |
| 109 | + err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, buf, &optlen); |
| 110 | + if (err) { |
| 111 | + log_err("Failed to call getsockopt(SO_SNDBUF)"); |
| 112 | + goto err; |
| 113 | + } |
| 114 | + |
| 115 | + if (*(__u32 *)buf != 0x55AA*2) { |
| 116 | + log_err("Unexpected getsockopt(SO_SNDBUF) 0x%x != 0x55AA*2", |
| 117 | + *(__u32 *)buf); |
| 118 | + goto err; |
| 119 | + } |
| 120 | + |
| 121 | + close(fd); |
| 122 | + return 0; |
| 123 | +err: |
| 124 | + close(fd); |
| 125 | + return -1; |
| 126 | +} |
| 127 | + |
| 128 | +static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title) |
| 129 | +{ |
| 130 | + enum bpf_attach_type attach_type; |
| 131 | + enum bpf_prog_type prog_type; |
| 132 | + struct bpf_program *prog; |
| 133 | + int err; |
| 134 | + |
| 135 | + err = libbpf_prog_type_by_name(title, &prog_type, &attach_type); |
| 136 | + if (err) { |
| 137 | + log_err("Failed to deduct types for %s BPF program", title); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + prog = bpf_object__find_program_by_title(obj, title); |
| 142 | + if (!prog) { |
| 143 | + log_err("Failed to find %s BPF program", title); |
| 144 | + return -1; |
| 145 | + } |
| 146 | + |
| 147 | + err = bpf_prog_attach(bpf_program__fd(prog), cgroup_fd, |
| 148 | + attach_type, 0); |
| 149 | + if (err) { |
| 150 | + log_err("Failed to attach %s BPF program", title); |
| 151 | + return -1; |
| 152 | + } |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +static int run_test(int cgroup_fd) |
| 158 | +{ |
| 159 | + struct bpf_prog_load_attr attr = { |
| 160 | + .file = "./sockopt_sk.o", |
| 161 | + }; |
| 162 | + struct bpf_object *obj; |
| 163 | + int ignored; |
| 164 | + int err; |
| 165 | + |
| 166 | + err = bpf_prog_load_xattr(&attr, &obj, &ignored); |
| 167 | + if (err) { |
| 168 | + log_err("Failed to load BPF object"); |
| 169 | + return -1; |
| 170 | + } |
| 171 | + |
| 172 | + err = prog_attach(obj, cgroup_fd, "cgroup/getsockopt"); |
| 173 | + if (err) |
| 174 | + goto close_bpf_object; |
| 175 | + |
| 176 | + err = prog_attach(obj, cgroup_fd, "cgroup/setsockopt"); |
| 177 | + if (err) |
| 178 | + goto close_bpf_object; |
| 179 | + |
| 180 | + err = getsetsockopt(); |
| 181 | + |
| 182 | +close_bpf_object: |
| 183 | + bpf_object__close(obj); |
| 184 | + return err; |
| 185 | +} |
| 186 | + |
| 187 | +int main(int args, char **argv) |
| 188 | +{ |
| 189 | + int cgroup_fd; |
| 190 | + int err = EXIT_SUCCESS; |
| 191 | + |
| 192 | + if (setup_cgroup_environment()) |
| 193 | + goto cleanup_obj; |
| 194 | + |
| 195 | + cgroup_fd = create_and_get_cgroup(CG_PATH); |
| 196 | + if (cgroup_fd < 0) |
| 197 | + goto cleanup_cgroup_env; |
| 198 | + |
| 199 | + if (join_cgroup(CG_PATH)) |
| 200 | + goto cleanup_cgroup; |
| 201 | + |
| 202 | + if (run_test(cgroup_fd)) |
| 203 | + err = EXIT_FAILURE; |
| 204 | + |
| 205 | + printf("test_sockopt_sk: %s\n", |
| 206 | + err == EXIT_SUCCESS ? "PASSED" : "FAILED"); |
| 207 | + |
| 208 | +cleanup_cgroup: |
| 209 | + close(cgroup_fd); |
| 210 | +cleanup_cgroup_env: |
| 211 | + cleanup_cgroup_environment(); |
| 212 | +cleanup_obj: |
| 213 | + return err; |
| 214 | +} |
0 commit comments