From 8db4d3b33c31a005d9c23a64bd9da2f285ed8735 Mon Sep 17 00:00:00 2001 From: Jesper Dangaard Brouer Date: Fri, 12 Nov 2021 10:01:58 +0100 Subject: [PATCH] lib_xsk_extend: Avoid crashing in XSK_BTF_READ_xxx macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The macros XSK_BTF_READ_xxx doesn't handle or report on errors if the input or field were wrong, which is not uncommon for macros. The bad behavior is that the macro continue to dereference the pointer even-when xsk_btf__read_xxx() functions returns an error. This often leads to crashing the application. Change macro to only dereference when no errors were reported. Side-note: The compiler warnings will detect if API user didn't init the 'dest' value with a default value. As the effect of the change is that the 'dest' value will not be touch, and thus contain the value before the macro was used. Example: warning: ‘mark’ may be used uninitialized in this function Signed-off-by: Jesper Dangaard Brouer --- AF_XDP-interaction/af_xdp_user.c | 3 ++- AF_XDP-interaction/lib_xsk_extend.h | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/AF_XDP-interaction/af_xdp_user.c b/AF_XDP-interaction/af_xdp_user.c index 53c3ad6..d8de2f2 100644 --- a/AF_XDP-interaction/af_xdp_user.c +++ b/AF_XDP-interaction/af_xdp_user.c @@ -470,8 +470,9 @@ static int print_meta_info_time_api2(uint8_t *pkt) static void print_meta_info_mark(uint8_t *pkt, struct xdp_hints_mark *meta) { struct xsk_btf_info *xbi = meta->xbi; - __u32 mark; + __u32 mark = 0; + /* The 'mark' value is not updated in case of errors */ XSK_BTF_READ_INTO(mark, &meta->mark, xbi, pkt); if (debug_meta) printf("meta-mark mark:%u\n", mark); diff --git a/AF_XDP-interaction/lib_xsk_extend.h b/AF_XDP-interaction/lib_xsk_extend.h index aa6b1aa..904d684 100644 --- a/AF_XDP-interaction/lib_xsk_extend.h +++ b/AF_XDP-interaction/lib_xsk_extend.h @@ -43,12 +43,12 @@ LIBBPF_API bool xsk_btf__field_member(const char *field, struct xsk_btf_info *xb /* Notice: that field must NOT be a C-string as macro will stringify it */ #define XSK_BTF_READ_FIELD_INTO(dest, field, xbi, addr) ({ \ typeof(dest) *_d; \ - xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \ - dest = *_d; }) + int _err=xsk_btf__read_field((void **)&_d, sizeof(dest), #field, xbi, addr); \ + if (!_err) dest = *_d; }) #define XSK_BTF_READ_INTO(dest, member, xbi, addr) ({ \ typeof(dest) *_d; \ - xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \ - dest = *_d; }) + int _err=xsk_btf__read((void **)&_d, sizeof(dest), member, xbi, addr); \ + if (!_err) dest = *_d; }) #endif /* __LIB_XSK_EXTEND_H */