From: Masatake YAMATO Date: Sat, 25 Apr 2026 23:57:17 +0000 (+0900) Subject: lsfd: use memset explicitly to fill bpf_attr with zero X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=d0dca4880f2c0bd91d5cedc34d9ad9c66c8aff35;p=thirdparty%2Futil-linux.git lsfd: use memset explicitly to fill bpf_attr with zero The original code used initialize-lists for initializing variables (attr) type of union bpf_attr. However, the folloing syscalls failed with EINVAL: syscall(SYS_bpf, BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr)); syscall(SYS_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); CHECK_ATTR macro of Linux might cause EINVAL: /* helper macro to check that unused fields 'union bpf_attr' are zero */ #define CHECK_ATTR(CMD) \ memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ sizeof(attr->CMD##_LAST_FIELD), 0, \ sizeof(*attr) - \ offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ sizeof(attr->CMD##_LAST_FIELD)) != NULL I doubt initialize-lists worked well. Signed-off-by: Masatake YAMATO --- diff --git a/lsfd-cmd/unkn.c b/lsfd-cmd/unkn.c index efd6d402c..3bac2feb4 100644 --- a/lsfd-cmd/unkn.c +++ b/lsfd-cmd/unkn.c @@ -1070,21 +1070,20 @@ static void anon_bpf_prog_free(struct unkn *unkn) static void anon_bpf_prog_get_more_info(struct anon_bpf_prog_data *prog_data) { - union bpf_attr attr = { - .prog_id = (int32_t)prog_data->id, - .next_id = 0, - .open_flags = 0, - }; + int bpf_fd; + union bpf_attr attr; struct bpf_prog_info info = { 0 }; - union bpf_attr info_attr = { - .info.info_len = sizeof(info), - .info.info = (uint64_t)(uintptr_t)&info, - }; + union bpf_attr info_attr; - int bpf_fd = syscall(SYS_bpf, BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr)); + memset(&attr, 0, sizeof(attr)); + attr.prog_id = (int32_t)prog_data->id; + bpf_fd = syscall(SYS_bpf, BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr)); if (bpf_fd < 0) return; + memset(&info_attr, 0, sizeof(info_attr)); + info_attr.info.info_len = sizeof(info); + info_attr.info.info = (uint64_t)(uintptr_t)&info; info_attr.info.bpf_fd = bpf_fd; if (syscall(SYS_bpf, BPF_OBJ_GET_INFO_BY_FD, &info_attr, offsetofend(union bpf_attr, info)) == 0) { memcpy(prog_data->name, @@ -1261,21 +1260,20 @@ static void anon_bpf_map_free(struct unkn *unkn) static void anon_bpf_map_get_more_info(struct anon_bpf_map_data *map_data) { - union bpf_attr attr = { - .map_id = (int32_t)map_data->id, - .next_id = 0, - .open_flags = 0, - }; + int bpf_fd; + union bpf_attr attr; struct bpf_map_info info = { 0 }; - union bpf_attr info_attr = { - .info.info_len = sizeof(info), - .info.info = (uint64_t)(uintptr_t)&info, - }; + union bpf_attr info_attr; - int bpf_fd = syscall(SYS_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); + memset(&attr, 0, sizeof(attr)); + attr.map_id = (int32_t)map_data->id; + bpf_fd = syscall(SYS_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); if (bpf_fd < 0) return; + memset(&info_attr, 0, sizeof(info_attr)); + info_attr.info.info_len = sizeof(info); + info_attr.info.info = (uint64_t)(uintptr_t)&info; info_attr.info.bpf_fd = bpf_fd; if (syscall(SYS_bpf, BPF_OBJ_GET_INFO_BY_FD, &info_attr, offsetofend(union bpf_attr, info)) == 0) { memcpy(map_data->name,