From: Toshiaki Makita Date: Mon, 31 Jul 2017 11:20:55 +0000 (+0900) Subject: Fix wrong netlink port id check X-Git-Tag: submit/tizen_4.0/20171018.110122~55 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bb9b92ab1ade08f12956004d19016f2b4aeb5bd6;p=platform%2Fupstream%2Fbcc.git Fix wrong netlink port id check As per man netlink, nlmsg_pid is not process id and in fact a value different from process id can be used. bpf: Wrong pid -1615084642, expected 24407 This problem can be triggered by using pyroute2 with bcc. Signed-off-by: Toshiaki Makita --- diff --git a/src/cc/libbpf.c b/src/cc/libbpf.c index 906e44f6..8b887956 100644 --- a/src/cc/libbpf.c +++ b/src/cc/libbpf.c @@ -725,6 +725,7 @@ int bpf_attach_xdp(const char *dev_name, int progfd, uint32_t flags) { } req; struct nlmsghdr *nh; struct nlmsgerr *err; + socklen_t addrlen; memset(&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; @@ -740,6 +741,17 @@ int bpf_attach_xdp(const char *dev_name, int progfd, uint32_t flags) { goto cleanup; } + addrlen = sizeof(sa); + if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) { + fprintf(stderr, "bpf: get sock name of netlink: %s\n", strerror(errno)); + goto cleanup; + } + + if (addrlen != sizeof(sa)) { + fprintf(stderr, "bpf: wrong netlink address length: %d\n", addrlen); + goto cleanup; + } + memset(&req, 0, sizeof(req)); req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; @@ -790,9 +802,9 @@ int bpf_attach_xdp(const char *dev_name, int progfd, uint32_t flags) { for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) { - if (nh->nlmsg_pid != getpid()) { + if (nh->nlmsg_pid != sa.nl_pid) { fprintf(stderr, "bpf: Wrong pid %d, expected %d\n", - nh->nlmsg_pid, getpid()); + nh->nlmsg_pid, sa.nl_pid); errno = EBADMSG; goto cleanup; }