From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A6AD18661A; Tue, 10 Sep 2024 10:45:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725965108; cv=none; b=LlxSL6ubw/pENNdtIJS0ktK4u5yv3S9OYh1zpCLBcyVmJTr3TZ5qhKZfqlo54/NGEnbjmd06vEgoBISnklTtED9gQhdxw93wJp626zOpdH/Ez+a81NqY8yd4Y16vnTR3xyW72u7XaQ267ot1yMbno4ipSDV/6Ze2yXaMaU89o0M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725965108; c=relaxed/simple; bh=Nbv8fLmA45Tdqrs3xCvYOgPlTrUqtwX4kkSCR2UuUoQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MAyRs4tekHqpnvxt4YtoK20iIluMgOvzUqtshD8WqX2gMMOWHiQuYuB97uY9dNbAWlE9jDgasvaDPrLawCUpwO3UwW/HKxpWHNwM5Xw/1BoJOw70y351OyAjs4pMz4tTdZ6dex7vV7PsSvXNmlgD5uu72ti1udZVq+Qifqaz10M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=t/RjMa17; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="t/RjMa17" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7341FC4CEC3; Tue, 10 Sep 2024 10:45:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1725965107; bh=Nbv8fLmA45Tdqrs3xCvYOgPlTrUqtwX4kkSCR2UuUoQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t/RjMa17Vjfwptiqs/Cdr/qAyX9JwO+e41yLoU89GDW/KkRnJB2Ve8mOA4a8KDu3o YFrYSQAQCZK7quVtYRJrSYBz3lXckTwL5OUn4za+t+hfHOwlF6ce8QaM8Lr75UrrA+ 5Pl6TO3gN+/9SdGWOzBe30MwY6KedD3Qu8YZTNhQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Andreas Ziegler , Daniel Borkmann , Sasha Levin Subject: [PATCH 5.10 139/186] libbpf: Add NULL checks to bpf_object__{prev_map,next_map} Date: Tue, 10 Sep 2024 11:33:54 +0200 Message-ID: <20240910092600.333600763@linuxfoundation.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240910092554.645718780@linuxfoundation.org> References: <20240910092554.645718780@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Andreas Ziegler [ Upstream commit cedc12c5b57f7efa6dbebfb2b140e8675f5a2616 ] In the current state, an erroneous call to bpf_object__find_map_by_name(NULL, ...) leads to a segmentation fault through the following call chain: bpf_object__find_map_by_name(obj = NULL, ...) -> bpf_object__for_each_map(pos, obj = NULL) -> bpf_object__next_map((obj = NULL), NULL) -> return (obj = NULL)->maps While calling bpf_object__find_map_by_name with obj = NULL is obviously incorrect, this should not lead to a segmentation fault but rather be handled gracefully. As __bpf_map__iter already handles this situation correctly, we can delegate the check for the regular case there and only add a check in case the prev or next parameter is NULL. Signed-off-by: Andreas Ziegler Signed-off-by: Daniel Borkmann Link: https://lore.kernel.org/bpf/20240703083436.505124-1-ziegler.andreas@siemens.com Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 015ed8253f73..33cdcfe10634 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9005,7 +9005,7 @@ __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i) struct bpf_map * bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj) { - if (prev == NULL) + if (prev == NULL && obj != NULL) return obj->maps; return __bpf_map__iter(prev, obj, 1); @@ -9014,7 +9014,7 @@ bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj) struct bpf_map * bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj) { - if (next == NULL) { + if (next == NULL && obj != NULL) { if (!obj->nr_maps) return NULL; return obj->maps + obj->nr_maps - 1; -- 2.43.0