Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9982579ybi; Wed, 24 Jul 2019 13:30:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqyXUaY/5/A0bfQUiIuj2oGZp5rRBf4uglkp7pZr9wj9AywAkT2QhEHenxJ2x10QXr+Q84Vd X-Received: by 2002:a63:4846:: with SMTP id x6mr46848268pgk.332.1564000243912; Wed, 24 Jul 2019 13:30:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564000243; cv=none; d=google.com; s=arc-20160816; b=g3EUFZ84NZqvMGRQhmhmz4yxedMgjnB24MEpendijJHZvM36CIkddlEhdclPjj+/fz UeDyuOhzqwu9U9En1J6XlbTTO64s5Om9Kr+D64PfkuivGBPDTZXW4xLOijMCgVaalCYg mEilByfXvZcUJ4RA1rE79p4oSqb/xF1xFWj8SGl1ZEEKrwiBt/iHeGzXdvyz5/z7XXgO SJw+tdHlleRdtFCwz+d4LBvLjqFOnf/a8z28PvIiCON+g9KBSc4Av4Drcvk07y/hfwef LId2mZ9ldQ0ZZaxfRAz2NJBt3aSl7BbrlXWHP37dKMh5VafLRfoMUhhRp2lS+A1R2btk 6B6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KrmWVmvsCuOXQ1DGoVSUAxgvE1Otz5cLxUBHkTeK+ZQ=; b=FnOZbCQHwvmFITbr9T4yAf3ladKfI8UFcvdnJxEL/CMZRjr4JAc9QTBFmVkDOhfbLs G1EXX+Px/hXpOnJj+SYUeJAZJA26WYyt6iPQTZbXoR9Zj/aipkG94m5cgvsuqFbzLAlI 8OsjYzvl+cUTT3S/MgtrhvhtzACCXsW3gJmaFk6TeB4KfGcz0Vg+dk0Guh87+NYsZZTd Us+s5u8yLA8N1CXLDijNyKlgvsBhZDw013mGAxkZ5a0BbbYF9MlgmTK1Spa/Hr/qlfxp 6cRzDLfexrigE5iYu+8EaEDtii7nJDyn/jlJdk6pKVM4cHvcrzDI/TkyT1Sakq/bzCuf 0LQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=m4C+QgG5; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l14si16266763pga.567.2019.07.24.13.30.28; Wed, 24 Jul 2019 13:30:43 -0700 (PDT) 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=@kernel.org header.s=default header.b=m4C+QgG5; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388821AbfGXTdQ (ORCPT + 99 others); Wed, 24 Jul 2019 15:33:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:54922 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388808AbfGXTdO (ORCPT ); Wed, 24 Jul 2019 15:33:14 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B8A9722ADB; Wed, 24 Jul 2019 19:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563996793; bh=F+8EmJejqOpRpK8Viro3WnVsxOnaTLw0Ge4pF4WAdvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m4C+QgG5FUGcPqGIfpEECTZWOYOkpKs9Tvw8A3gEglmBkFw3RILsfCPxbJSSNtGys U2SNG0zubReAmDWoK4H6ZZjyCFV22H/ypZSuz5TvPdP+s42MboCllbgzXPagghr1HX 00oOSkRZWdd23YaPsT6WfDY+CIuiCyH6dUibgHKM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Leo Yan , Yonghong Song , Daniel Borkmann , Sasha Levin Subject: [PATCH 5.2 219/413] bpf, libbpf, smatch: Fix potential NULL pointer dereference Date: Wed, 24 Jul 2019 21:18:30 +0200 Message-Id: <20190724191750.367082697@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191735.096702571@linuxfoundation.org> References: <20190724191735.096702571@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 33bae185f74d49a0d7b1bfaafb8e959efce0f243 ] Based on the following report from Smatch, fix the potential NULL pointer dereference check: tools/lib/bpf/libbpf.c:3493 bpf_prog_load_xattr() warn: variable dereferenced before check 'attr' (see line 3483) 3479 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, 3480 struct bpf_object **pobj, int *prog_fd) 3481 { 3482 struct bpf_object_open_attr open_attr = { 3483 .file = attr->file, 3484 .prog_type = attr->prog_type, ^^^^^^ 3485 }; At the head of function, it directly access 'attr' without checking if it's NULL pointer. This patch moves the values assignment after validating 'attr' and 'attr->file'. Signed-off-by: Leo Yan Acked-by: Yonghong Song Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 151f7ac1882e..3865a5d27251 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3487,10 +3487,7 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type, int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, struct bpf_object **pobj, int *prog_fd) { - struct bpf_object_open_attr open_attr = { - .file = attr->file, - .prog_type = attr->prog_type, - }; + struct bpf_object_open_attr open_attr = {}; struct bpf_program *prog, *first_prog = NULL; enum bpf_attach_type expected_attach_type; enum bpf_prog_type prog_type; @@ -3503,6 +3500,9 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, if (!attr->file) return -EINVAL; + open_attr.file = attr->file; + open_attr.prog_type = attr->prog_type; + obj = bpf_object__open_xattr(&open_attr); if (IS_ERR_OR_NULL(obj)) return -ENOENT; -- 2.20.1