Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2408466imu; Wed, 21 Nov 2018 11:09:09 -0800 (PST) X-Google-Smtp-Source: AFSGD/Wytx3+5o2BqfJFLSxit/65jJ3a7G8eU1qMuRGp8QczFnX2cBThWdyTmmnMDFOIVLI1Ixtp X-Received: by 2002:a63:d047:: with SMTP id s7mr7024539pgi.311.1542827349122; Wed, 21 Nov 2018 11:09:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542827349; cv=none; d=google.com; s=arc-20160816; b=dqaiQGCP5PlVoEvPcj2yx1BtYM9KoRl9ckqzd9ivchZ2/GKRNeTJy980YSff0V8pEZ UJE4crFihhdFJcXE3ZYiv1Vuzy1Z/nGBQ5+wCbeAQW6f73C3zZczKRqqaT4JBslqxsl2 r1rOilM3rJrD3p15I4b2pckJwPCF+qf6ufBL+B9FfRNLCeUum+n73bS2wIBZpB8bHUNx GzYR4vlM1oloTRKuoJHcgCcT6qRxUd9pSgd5HZRtwhpzVy82h2ACqByCpQcqCyLB1vic uGW8Zl3rmqHgM0aFcK5FuMmP3xNlXRtT7GXeyY8x9lJ4ReLT1twhBrPEbqJ8X1lxwIyS 2xjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=CqOYxN785h2fQNoNZ4FK6G9FNsxlTMO566BsH6YdgMg=; b=zdft9/9n0rRgGLzI+/ELTkbH0Ei5+bM+Vm1uZcmb0vCVgvjTimdRJcAaLmiOukVLVe 1iAPxb390sVId5IWcfJb93SW9pn7U2w109VgJ80UFoIFVsNEVapkdUWecYf0Acft/6jn 5YeDwzdTS+wLrX1coDGyClBV6izi/lj7BH4pPpBV3sOX4L84rBP90ND0Wvt3Rx6UHpE/ K9UK2W6raosGAAN9wMJ1XJdG6uirQWVgHgNm6tBJtkZUahM8oexwoAL4U9Hm1J9tD4+C NOedGcinIQiywTsnV7XMD1K9A+usxaRPrfnBvqDvbV73lwTHyQhkFxdue4GDnVAMoYom /HuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Km8wIjWm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3-v6si47321855plz.12.2018.11.21.11.08.53; Wed, 21 Nov 2018 11:09:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Km8wIjWm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732195AbeKVDyA (ORCPT + 99 others); Wed, 21 Nov 2018 22:54:00 -0500 Received: from mail-qk1-f194.google.com ([209.85.222.194]:36330 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727628AbeKVDyA (ORCPT ); Wed, 21 Nov 2018 22:54:00 -0500 Received: by mail-qk1-f194.google.com with SMTP id o125so5349588qkf.3; Wed, 21 Nov 2018 09:18:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=CqOYxN785h2fQNoNZ4FK6G9FNsxlTMO566BsH6YdgMg=; b=Km8wIjWmSh6RphcTKtwKXbgNnF7/FWwCkxN1IbFRxyhET1k5/u0u+/hVfCnltgA04t 8EamhmeH6XSPYsGZR90KWufNZCTJh9iw7UUf5RvPM0vAfRrpBErxuk9quVrWd1uDNa05 t+Ol/2l7+4fK3AKOPJFVEGCY96hjoVOfG8GOSN06O8E/BrCPINEj0qgtFPU7eZNMXVqd mJoHYfG1Yb+qATGDeYppaj6ej1JKklI9kHkxFvNzXhNi5ps3OCXP8XpPjKvIQtGi5YP8 j8yK6XhaGBh2IOb6wJFZ11vIiIdzmTvpHqDOEuzfC5jxq2GtwKEK1+ZMJ5IKrEUx6psl SmRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=CqOYxN785h2fQNoNZ4FK6G9FNsxlTMO566BsH6YdgMg=; b=XQNWbD3RzAkZzusagWi7f1pFlrM19HalPJ4/tesvoe2VRC+9PZ33D4LHasi2j+rZd7 7Cfu9miTB79oNo+WGY4+g6a9D1722qpdTC69d2wp5VsZnm/kaHBPwCmXxQRvDykrLrVu eyOkJfKRbk2/9PYPQSiYbF8Scwjzo8gdlFRT3QTohaS0rYNNKwj4XP3vJFCQ8ooFNqEB HXxHBSNhSaKyo8jpCjF+BMb7rQgyVBG3vAoUhjxFFJW3SwSDXPnF6bkmNCUfqmxh1qj0 2g/BDfYt6ewiPiDYwDXVdZqoVf3DUSnNnoLkqWtW9pxZPTXf/snBrIjNHTguhiwrfcXQ 3kTA== X-Gm-Message-State: AA+aEWbWunRzUUMOD1xjf6g7SiSanfqMNLstvb5EIOHFOIGS2Abiqn9o GXe/DaPoVLDXZX3QE2hPpgqUJPP7kLY1hY/Vr6k= X-Received: by 2002:a37:9201:: with SMTP id u1mr6671700qkd.258.1542820722858; Wed, 21 Nov 2018 09:18:42 -0800 (PST) MIME-Version: 1.0 References: <1542786192-19164-1-git-send-email-wen.yang99@zte.com.cn> In-Reply-To: <1542786192-19164-1-git-send-email-wen.yang99@zte.com.cn> From: Y Song Date: Wed, 21 Nov 2018 09:18:06 -0800 Message-ID: Subject: Re: [PATCH 3/4] tools: bpftool: fix potential NULL pointer dereference in do_load To: wen.yang99@zte.com.cn Cc: Alexei Starovoitov , Daniel Borkmann , Jakub Kicinski , Quentin Monnet , Jiong Wang , guro@fb.com, Sandipan Das , John Fastabend , netdev , LKML , zhong.weidong@zte.com.cn, wang.yi59@zte.com.cn, julia.lawall@lip6.fr Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 20, 2018 at 11:42 PM Wen Yang wrote: > > This patch fixes a possible null pointer dereference in > do_load, detected by the semantic patch > deref_null.cocci, with the following warning: > > ./tools/bpf/bpftool/prog.c:1021:23-25: ERROR: map_replace is NULL but dereferenced. > > The following code has potential null pointer references: > 881 map_replace = reallocarray(map_replace, old_map_fds + 1, > 882 sizeof(*map_replace)); > 883 if (!map_replace) { > 884 p_err("mem alloc failed"); > 885 goto err_free_reuse_maps; > 886 } > > ... > 1019 err_free_reuse_maps: > 1020 for (i = 0; i < old_map_fds; i++) > 1021 close(map_replace[i].fd); > 1022 free(map_replace); I did not see any issues here. We have code looks like: 867 struct map_replace *map_replace = NULL; 868 struct bpf_program *prog = NULL, *pos; 869 unsigned int old_map_fds = 0; ... 948 map_replace = reallocarray(map_replace, old_map_fds + 1, 949 sizeof(*map_replace)); 950 if (!map_replace) { 951 p_err("mem alloc failed"); 952 goto err_free_reuse_maps; 953 } 954 map_replace[old_map_fds].idx = idx; 955 map_replace[old_map_fds].name = name; 956 map_replace[old_map_fds].fd = fd; 957 old_map_fds++; ... old_map_fds becomes non zero if and only if map_replace is not NULL. > Signed-off-by: Wen Yang > Reviewed-by: Tan Hu > CC: Julia Lawall > --- > tools/bpf/bpftool/prog.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c > index 5302ee2..de42187 100644 > --- a/tools/bpf/bpftool/prog.c > +++ b/tools/bpf/bpftool/prog.c > @@ -1017,8 +1017,9 @@ static int do_load(int argc, char **argv) > err_close_obj: > bpf_object__close(obj); > err_free_reuse_maps: > - for (i = 0; i < old_map_fds; i++) > - close(map_replace[i].fd); > + if (map_replace) > + for (i = 0; i < old_map_fds; i++) > + close(map_replace[i].fd); > free(map_replace); > return -1; > } > -- > 2.9.5 >