Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp6557086ybx; Mon, 11 Nov 2019 10:54:51 -0800 (PST) X-Google-Smtp-Source: APXvYqwihyb32rFFLeffYZbwJ35DZVO8WBbLOer6FTR/tOYAN1QZcIHcXeEtvEG+um8ktIbQPWIK X-Received: by 2002:a50:9269:: with SMTP id j38mr27879623eda.5.1573498491257; Mon, 11 Nov 2019 10:54:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573498491; cv=none; d=google.com; s=arc-20160816; b=0m3C6pgG1FzXfU2G5CJYNHmevIK9y7mouKDvzd+1n5oS/rDjAjM0FnF/d+U4nHrMN0 SfF28HXYnWWTXcsuwaUDxsY1lU3QD46hhYjKGWKD1xjBucBDRJmyjzuPreYXKDe2ek0g 9HEXvcaa85JjLC1vNe2WOVS4DTejC092lct3EXHIy5xAVU67dLV6R0X/TI1bDR9czzA7 jWlXDqcTKGedN43ev89Bcxre8WdQAwsrxYF9Bdh4NMVS7q8IgFFHx9cqV7Bi0MxI3T95 PAczX80k1O/VPX0Oa6iL8Wsw95g1Q4jSgDXjSJsAR9dUBxCITxjnHZ4+Ve7aXVOd8a82 Fpvg== 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=MugtNVqqVpK8NWiezYJ1WawxeodBA99DZciFDsBXxlI=; b=g8I9vTO0r6mRgafuQ5+DBTnzMuQQjD2wahsnb/JrNT/LD7muQsYUfDpaHu5jhWRREA 3GKK3WagNyBxeQsaf546F8VJ/KEm/za9LAA9cqa/G4MgEmQtRy6QhyaKAMxhK/XR4HON 8LZ7D+4R3/BvvPyvJdQRj+Yq7+uYyTHkkTsujif6C8VlcCvoXFnLhv2hnibB3sXqWsH+ PpReXaTnIq/KDw9XZz8vhSi5CA34O4DyDT8pzAaP8+tcuMPLaMYL7uNCx6Tay8SCftrj FcpyF6tAmDfLJZ+iPyjW+xy+MSsTpgNTzKyhxDbbiTKWtkNjUX/dvJHkAvaRcfpQT0kg S4Ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRZssEUf; 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 gu7si10119321ejb.405.2019.11.11.10.54.27; Mon, 11 Nov 2019 10:54:51 -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=@kernel.org header.s=default header.b=pRZssEUf; 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 S1730541AbfKKSx6 (ORCPT + 99 others); Mon, 11 Nov 2019 13:53:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:49222 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728456AbfKKSx5 (ORCPT ); Mon, 11 Nov 2019 13:53:57 -0500 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 179B02184C; Mon, 11 Nov 2019 18:53:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573498436; bh=0UXlZxhZLcvEzM7joCVK81AnM9ibdqsJS+MsEByHkqk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pRZssEUfvz6yWpbOw0fraG7V6FaeGgiwIGn4XTofGw9T40lvFGOTHcU4VLTA4NjCH x2cYPgIGAk6Ze1G+02ZDZDYLnH+IlSLrM6cHLKNEF+YvuU0qLLFrd/fEMdD9BqWYhS ggzzIlb15unIPLWynTESDLtfAstcAeGmySu+siWg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , Alexei Starovoitov , Yonghong Song , Martin KaFai Lau , Sasha Levin Subject: [PATCH 5.3 120/193] bpf: Fix use after free in bpf_get_prog_name Date: Mon, 11 Nov 2019 19:28:22 +0100 Message-Id: <20191111181510.026805214@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191111181459.850623879@linuxfoundation.org> References: <20191111181459.850623879@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 From: Daniel Borkmann [ Upstream commit 3b4d9eb2ee74dd5ea7fa36cffb0ca7f5bc4924da ] There is one more problematic case I noticed while recently fixing BPF kallsyms handling in cd7455f1013e ("bpf: Fix use after free in subprog's jited symbol removal") and that is bpf_get_prog_name(). If BTF has been attached to the prog, then we may be able to fetch the function signature type id in kallsyms through prog->aux->func_info[prog->aux->func_idx].type_id. However, while the BTF object itself is torn down via RCU callback, the prog's aux->func_info is immediately freed via kvfree(prog->aux->func_info) once the prog's refcount either hit zero or when subprograms were already exposed via kallsyms and we hit the error path added in 5482e9a93c83 ("bpf: Fix memleak in aux->func_info and aux->btf"). This violates RCU as well since kallsyms could be walked in parallel where we could access aux->func_info. Hence, defer kvfree() to after RCU grace period. Looking at ba64e7d85252 ("bpf: btf: support proper non-jit func info") there is no reason/dependency where we couldn't defer the kvfree(aux->func_info) into the RCU callback. Fixes: 5482e9a93c83 ("bpf: Fix memleak in aux->func_info and aux->btf") Fixes: ba64e7d85252 ("bpf: btf: support proper non-jit func info") Signed-off-by: Daniel Borkmann Signed-off-by: Alexei Starovoitov Acked-by: Yonghong Song Cc: Martin KaFai Lau Link: https://lore.kernel.org/bpf/875f2906a7c1a0691f2d567b4d8e4ea2739b1e88.1571779205.git.daniel@iogearbox.net Signed-off-by: Sasha Levin --- kernel/bpf/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index af5c60b07463e..aac966b32c42e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1316,6 +1316,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu) { struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); + kvfree(aux->func_info); free_used_maps(aux); bpf_prog_uncharge_memlock(aux->prog); security_bpf_prog_free(aux); @@ -1326,7 +1327,6 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) { bpf_prog_kallsyms_del_all(prog); btf_put(prog->aux->btf); - kvfree(prog->aux->func_info); bpf_prog_free_linfo(prog); if (deferred) -- 2.20.1