Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10423076ybi; Wed, 24 Jul 2019 22:57:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqwIe89r4F5t+gDubiQBewq4jMrIcuLGAMZaRno7Ex1aqzU45L4R68T6/HREOGYXAelihyz2 X-Received: by 2002:a17:902:7782:: with SMTP id o2mr5593064pll.12.1564034279787; Wed, 24 Jul 2019 22:57:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564034279; cv=none; d=google.com; s=arc-20160816; b=ZfF7sSGS4qjuUs9yWnGhhD7re1AklniFEkwzkdstQ0XwP2M40qGufxIxraDKelradQ Jgr1CyIfPQdhdXa2umCN/BrGXTb3I/3Wg8vdqeJUZmwz1B0S23VMMmdKxWSwVxv3Nmw0 R/3CSGcSC9joU3Zantt/ZN1szwKIcHJbWBHB9xgm5LAgVqe6LH//ha3bhRIewAxyO19I SKxwXyUi5f2H9CmQzg8laIMv+wt57jHf+J0d919cMDzMvpIRMcYlr8041bDr/+ici4NY xj0ZUZlmRzdVpO8D2dGJT9t0gwjN0KU3FvszMlzKp0zqusfdce+4KjtdaQw4p6hHNT+a QaaQ== 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=nZIXR5vRy5bisVIcECzgLqSJdwdSRlovBy26avowfus=; b=yzyH1GsuCwZGaxbrzNC71OzPZN7vK7DWKlJh8be07knMbodlBBWpW1zBcEwzNB6dS6 nLrXEdy/yWsKcQvStALA/Y/WieDln6XnrMEcr3loV158MRGLf3tuB8honwEgCns5ixpy PYtnOT7YOZzjC6l3lEDjccmoOdPL4igV0HKvGCutrVAPY2DI8fpJKmKwta7D8Bzq7VVC 29DGL6dRGlRdAdoMQ0YJYopkmgre2oF1eknY8v7CX9TyODzUuEpw4EG45bYaBejXTjGq tSS7JnpViUPbNlNI+84nWh/261Di9YJy+Ka1ctGeowd2dXewJkVU9VVm1CYVT733OHDQ ETpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HDSL81ZE; 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 j7si21812884pfi.10.2019.07.24.22.57.45; Wed, 24 Jul 2019 22:57:59 -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=HDSL81ZE; 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 S2404464AbfGYFkn (ORCPT + 99 others); Thu, 25 Jul 2019 01:40:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:55340 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404449AbfGYFkm (ORCPT ); Thu, 25 Jul 2019 01:40:42 -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 E97DD22C97; Thu, 25 Jul 2019 05:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564033241; bh=WA/QmvzwWY1QBno/1ZpHvMcMxGHz5yTMYShtb5BOoZg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HDSL81ZEcpMZuWnE63+vaZ66diGqafUpoqBY52dtPyjH7yvZM5zn5nnSgUmucvw1M ifsiY30Zv8A8hQSfGNRrm5VxVp+hWdtpjnIPX1cr4snmoiGjOv5iolGJEM5USFM8Ks Rts3koEnS2W5Gu+r/9EvF86FB2LIrmr9gMjriYj8= 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 4.19 141/271] bpf, libbpf, smatch: Fix potential NULL pointer dereference Date: Wed, 24 Jul 2019 21:20:10 +0200 Message-Id: <20190724191707.287649150@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191655.268628197@linuxfoundation.org> References: <20190724191655.268628197@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 bdb94939fd60..a350f97e3a1a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2293,10 +2293,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; @@ -2309,6 +2306,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