From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jakub Kicinski Subject: [PATCH bpf-next 1/4] tools: libbpf: handle NULL program gracefully in bpf_program__nth_fd() Date: Thu, 26 Jul 2018 14:32:18 -0700 Message-ID: <20180726213221.1295-2-jakub.kicinski@netronome.com> References: <20180726213221.1295-1-jakub.kicinski@netronome.com> Cc: oss-drivers@netronome.com, netdev@vger.kernel.org, Jakub Kicinski To: alexei.starovoitov@gmail.com, daniel@iogearbox.net Return-path: Received: from mail-qt0-f193.google.com ([209.85.216.193]:35042 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731007AbeGZWvK (ORCPT ); Thu, 26 Jul 2018 18:51:10 -0400 Received: by mail-qt0-f193.google.com with SMTP id a5-v6so3125454qtp.2 for ; Thu, 26 Jul 2018 14:32:30 -0700 (PDT) In-Reply-To: <20180726213221.1295-1-jakub.kicinski@netronome.com> Sender: netdev-owner@vger.kernel.org List-ID: bpf_map__fd() handles NULL map gracefully and returns -EINVAL. bpf_program__fd() and bpf_program__nth_fd() crash in this case. Make the behaviour more consistent by validating prog pointer as well. Signed-off-by: Jakub Kicinski Reviewed-by: Quentin Monnet --- tools/lib/bpf/libbpf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 955f8eafbf41..afa9860db755 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1991,6 +1991,9 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n) { int fd; + if (!prog) + return -EINVAL; + if (n >= prog->instances.nr || n < 0) { pr_warning("Can't get the %dth fd from program %s: only %d instances\n", n, prog->section_name, prog->instances.nr); -- 2.17.1