| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-netlink.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bpf-restrict-ifaces.h" |
| 7 |
#include "cgroup.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "fdset.h" |
| 10 |
#include "netlink-util.h" |
| 11 |
#include "set.h" |
| 12 |
#include "unit.h" |
| 13 |
|
| 14 |
#if BPF_FRAMEWORK |
| 15 |
/* libbpf, clang and llc compile time dependencies are satisfied */ |
| 16 |
|
| 17 |
#include "bpf-util.h" |
| 18 |
#include "bpf-link.h" |
| 19 |
#include "restrict-ifaces-skel.h" |
| 20 |
|
| 21 |
static struct restrict_ifaces_bpf *restrict_ifaces_bpf_free(struct restrict_ifaces_bpf *obj) { |
| 22 |
restrict_ifaces_bpf__destroy(obj); |
| 23 |
return NULL; |
| 24 |
} |
| 25 |
|
| 26 |
DEFINE_TRIVIAL_CLEANUP_FUNC(struct restrict_ifaces_bpf *, restrict_ifaces_bpf_free); |
| 27 |
|
| 28 |
static int prepare_restrict_ifaces_bpf( |
| 29 |
Unit* u, |
| 30 |
bool is_allow_list, |
| 31 |
const Set *restrict_network_interfaces, |
| 32 |
struct restrict_ifaces_bpf **ret_object) { |
| 33 |
|
| 34 |
_cleanup_(restrict_ifaces_bpf_freep) struct restrict_ifaces_bpf *obj = NULL; |
| 35 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 36 |
char *iface; |
| 37 |
int r, map_fd; |
| 38 |
|
| 39 |
assert(ret_object); |
| 40 |
|
| 41 |
obj = restrict_ifaces_bpf__open(); |
| 42 |
if (!obj) |
| 43 |
return log_unit_full_errno(u, u ? LOG_ERR : LOG_DEBUG, errno, "restrict-interfaces: Failed to open BPF object: %m"); |
| 44 |
|
| 45 |
r = sym_bpf_map__set_max_entries(obj->maps.sd_restrictif, MAX(set_size(restrict_network_interfaces), 1u)); |
| 46 |
if (r != 0) |
| 47 |
return log_unit_full_errno(u, u ? LOG_ERR : LOG_WARNING, r, |
| 48 |
"restrict-interfaces: Failed to resize BPF map '%s': %m", |
| 49 |
sym_bpf_map__name(obj->maps.sd_restrictif)); |
| 50 |
|
| 51 |
obj->rodata->is_allow_list = is_allow_list; |
| 52 |
|
| 53 |
r = restrict_ifaces_bpf__load(obj); |
| 54 |
if (r != 0) |
| 55 |
return log_unit_full_errno(u, u ? LOG_ERR : LOG_DEBUG, r, "restrict-interfaces: Failed to load BPF object: %m"); |
| 56 |
|
| 57 |
map_fd = sym_bpf_map__fd(obj->maps.sd_restrictif); |
| 58 |
|
| 59 |
SET_FOREACH(iface, restrict_network_interfaces) { |
| 60 |
uint8_t dummy = 0; |
| 61 |
int ifindex; |
| 62 |
|
| 63 |
ifindex = rtnl_resolve_interface(&rtnl, iface); |
| 64 |
if (ifindex < 0) { |
| 65 |
log_unit_warning_errno(u, ifindex, |
| 66 |
"restrict-interfaces: Couldn't find index of network interface '%s', ignoring: %m", |
| 67 |
iface); |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
if (sym_bpf_map_update_elem(map_fd, &ifindex, &dummy, BPF_ANY)) |
| 72 |
return log_unit_full_errno(u, u ? LOG_ERR : LOG_WARNING, errno, |
| 73 |
"restrict-interfaces: Failed to update BPF map '%s' fd: %m", |
| 74 |
sym_bpf_map__name(obj->maps.sd_restrictif)); |
| 75 |
} |
| 76 |
|
| 77 |
*ret_object = TAKE_PTR(obj); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
int bpf_restrict_ifaces_supported(void) { |
| 82 |
_cleanup_(restrict_ifaces_bpf_freep) struct restrict_ifaces_bpf *obj = NULL; |
| 83 |
static int supported = -1; |
| 84 |
int r; |
| 85 |
|
| 86 |
if (supported >= 0) |
| 87 |
return supported; |
| 88 |
|
| 89 |
if (DLOPEN_BPF(LOG_WARNING, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED) < 0) |
| 90 |
return (supported = false); |
| 91 |
|
| 92 |
r = prepare_restrict_ifaces_bpf(NULL, true, NULL, &obj); |
| 93 |
if (r < 0) { |
| 94 |
log_debug_errno(r, "restrict-interfaces: Failed to load BPF object: %m"); |
| 95 |
return (supported = false); |
| 96 |
} |
| 97 |
|
| 98 |
return (supported = bpf_can_link_program(obj->progs.sd_restrictif_i)); |
| 99 |
} |
| 100 |
|
| 101 |
static int restrict_ifaces_install_impl(Unit *u, CGroupRuntime *crt) { |
| 102 |
_cleanup_(bpf_link_freep) struct bpf_link *egress_link = NULL, *ingress_link = NULL; |
| 103 |
_cleanup_(restrict_ifaces_bpf_freep) struct restrict_ifaces_bpf *obj = NULL; |
| 104 |
_cleanup_free_ char *cgroup_path = NULL; |
| 105 |
_cleanup_close_ int cgroup_fd = -EBADF; |
| 106 |
int r; |
| 107 |
|
| 108 |
assert(u); |
| 109 |
assert(crt); |
| 110 |
|
| 111 |
CGroupContext *cc = ASSERT_PTR(unit_get_cgroup_context(u)); |
| 112 |
|
| 113 |
r = cg_get_path(crt->cgroup_path, /* suffix= */ NULL, &cgroup_path); |
| 114 |
if (r < 0) |
| 115 |
return log_unit_error_errno(u, r, "restrict-interfaces: Failed to get cgroup path: %m"); |
| 116 |
|
| 117 |
if (!cc->restrict_network_interfaces) |
| 118 |
return 0; |
| 119 |
|
| 120 |
r = prepare_restrict_ifaces_bpf(u, |
| 121 |
cc->restrict_network_interfaces_is_allow_list, |
| 122 |
cc->restrict_network_interfaces, |
| 123 |
&obj); |
| 124 |
if (r < 0) |
| 125 |
return r; |
| 126 |
|
| 127 |
cgroup_fd = open(cgroup_path, O_PATH | O_CLOEXEC | O_DIRECTORY, 0); |
| 128 |
if (cgroup_fd < 0) |
| 129 |
return -errno; |
| 130 |
|
| 131 |
ingress_link = sym_bpf_program__attach_cgroup(obj->progs.sd_restrictif_i, cgroup_fd); |
| 132 |
r = bpf_get_error_translated(ingress_link); |
| 133 |
if (r != 0) |
| 134 |
return log_unit_error_errno(u, r, "restrict-interfaces: Failed to create ingress cgroup link: %m"); |
| 135 |
|
| 136 |
egress_link = sym_bpf_program__attach_cgroup(obj->progs.sd_restrictif_e, cgroup_fd); |
| 137 |
r = bpf_get_error_translated(egress_link); |
| 138 |
if (r != 0) |
| 139 |
return log_unit_error_errno(u, r, "restrict-interfaces: Failed to create egress cgroup link: %m"); |
| 140 |
|
| 141 |
crt->restrict_ifaces_ingress_bpf_link = TAKE_PTR(ingress_link); |
| 142 |
crt->restrict_ifaces_egress_bpf_link = TAKE_PTR(egress_link); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
int bpf_restrict_ifaces_install(Unit *u) { |
| 148 |
CGroupRuntime *crt; |
| 149 |
int r; |
| 150 |
|
| 151 |
assert(u); |
| 152 |
|
| 153 |
crt = unit_get_cgroup_runtime(u); |
| 154 |
if (!crt) |
| 155 |
return 0; |
| 156 |
|
| 157 |
r = restrict_ifaces_install_impl(u, crt); |
| 158 |
fdset_close(crt->initial_restrict_ifaces_link_fds, /* async= */ false); |
| 159 |
return r; |
| 160 |
} |
| 161 |
|
| 162 |
int bpf_restrict_ifaces_serialize(Unit *u, FILE *f, FDSet *fds) { |
| 163 |
CGroupRuntime *crt; |
| 164 |
int r; |
| 165 |
|
| 166 |
assert(u); |
| 167 |
|
| 168 |
crt = unit_get_cgroup_runtime(u); |
| 169 |
if (!crt) |
| 170 |
return 0; |
| 171 |
|
| 172 |
r = bpf_serialize_link(f, fds, "restrict-ifaces-bpf-fd", crt->restrict_ifaces_ingress_bpf_link); |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
|
| 176 |
return bpf_serialize_link(f, fds, "restrict-ifaces-bpf-fd", crt->restrict_ifaces_egress_bpf_link); |
| 177 |
} |
| 178 |
|
| 179 |
int bpf_restrict_ifaces_add_initial_link_fd(Unit *u, int fd) { |
| 180 |
int r; |
| 181 |
|
| 182 |
assert(u); |
| 183 |
|
| 184 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 185 |
if (!crt) |
| 186 |
return -EINVAL; |
| 187 |
|
| 188 |
if (!crt->initial_restrict_ifaces_link_fds) { |
| 189 |
crt->initial_restrict_ifaces_link_fds = fdset_new(); |
| 190 |
if (!crt->initial_restrict_ifaces_link_fds) |
| 191 |
return log_oom(); |
| 192 |
} |
| 193 |
|
| 194 |
r = fdset_put(crt->initial_restrict_ifaces_link_fds, fd); |
| 195 |
if (r < 0) |
| 196 |
return log_unit_error_errno(u, r, |
| 197 |
"restrict-interfaces: Failed to put restrict-ifaces-bpf-fd %d to restored fdset: %m", fd); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
#else /* ! BPF_FRAMEWORK */ |
| 203 |
int bpf_restrict_ifaces_supported(void) { |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
int bpf_restrict_ifaces_install(Unit *u) { |
| 208 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 209 |
"restrict-interfaces: Failed to install; BPF programs built from source code are not supported."); |
| 210 |
} |
| 211 |
|
| 212 |
int bpf_restrict_ifaces_serialize(Unit *u, FILE *f, FDSet *fds) { |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
int bpf_restrict_ifaces_add_initial_link_fd(Unit *u, int fd) { |
| 217 |
return 0; |
| 218 |
} |
| 219 |
#endif |