Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2308128imc; Tue, 12 Mar 2019 11:04:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqyCc58A6ncxbn4M4oFw0iIZ/JwGDwTK7pcfiIG0BgrTuECXjBTnOXHVhILS/ZlnasR+qkGf X-Received: by 2002:a63:6f49:: with SMTP id k70mr5625258pgc.132.1552413872791; Tue, 12 Mar 2019 11:04:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552413872; cv=none; d=google.com; s=arc-20160816; b=mgSIkDVWuNxQFGpfXoK73e7prkBwvxCYDBMdl5cfWQFgKNuozPRVLvhNzqGsfv62B9 PRlDn9NPXAbsMLKQu3fJsJBzyDAIW8J8ZLgVJ7SlXyWk8Yg6uN2Z8fkXw3Jy51vQjMwT kZrbf1u+sU1/pMbqAhBtmkYhHWZepgqoQcenmADOOiTwfWbZlsEtkYumrU1aDFhSAEch H+nEy+vH8+dnIG+/InBuym97R9JYQzKbzRi9tsNJSWmDKazTsaxKL8tW5QJyMCVxEEis rRA/AZ+1JPxNWwPxvcyKjVfHeBk8ruVDWbW+yDKbJ+ydPWU3mjx+jRBhYzOhUF2+dWqa ocYQ== 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=+VbTv8RfDlE+k9fQ5tVIMevfCSH0Kx3GBiq3Dy0CW5Y=; b=qLzfGpObB7JbfMrc9sEwTUU6ICD0QXeLWga2TbFCbPTzl4sJgx2zSDDu+YWL9xeJyL jW1+6hAzk+AoHiaf7IPrixliginr/3vBB4tM8oozbHHEMAprHrnsLJIYUKTAmFmPz39k ULbvFze2tNqhqHtipQ5M6e4pZ3+rhLETL4RAL2atIiJmYCIQJ9aEQxtg11z2oAB1L8cZ np438IjuZNgxfP1vjAxXSbN2Etycjuyn1h4a+iOIEUVhln07/xnZLhcW/shMb2gTjJOV Wt7eAQC+wRw6sM7WY+WGPe+PV6TOgialPWfarM/A+ZjloG9bpr6lrYJgEUymUmvuOz9b xalw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ziPcqWAk; 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 u77si8961274pfj.139.2019.03.12.11.04.17; Tue, 12 Mar 2019 11:04:32 -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=ziPcqWAk; 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 S1728617AbfCLSDM (ORCPT + 99 others); Tue, 12 Mar 2019 14:03:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:48978 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727707AbfCLRM4 (ORCPT ); Tue, 12 Mar 2019 13:12:56 -0400 Received: from localhost (unknown [104.133.8.98]) (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 BCEFC217F4; Tue, 12 Mar 2019 17:12:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410775; bh=a0oytfARmBA8nk8UsHsNzcvQ3W99CX4ejXRxtke/j7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ziPcqWAkzHHdDsBM1Ce/w3CelkhoVZCH40OtXSJOgpn5ULtJ1goIt5xcWqy3Om6Uj Yw6AZfIeJoveWhXJ4m32SX3x3gDsYl4TWST+sQNfZonz/MQnyEVNNKM/kyZGR/plg4 j8rdKzVos1HhTKfvpYlaNCYDFGigKBqwALrGg99o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Alexei Starovoitov , Martin KaFai Lau , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.20 114/171] bpf: Fix syscalls stackmap lookup potential deadlock Date: Tue, 12 Mar 2019 10:08:14 -0700 Message-Id: <20190312170357.712585962@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312170347.868927101@linuxfoundation.org> References: <20190312170347.868927101@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 7c4cd051add3d00bbff008a133c936c515eaa8fe ] The map_lookup_elem used to not acquiring spinlock in order to optimize the reader. It was true until commit 557c0c6e7df8 ("bpf: convert stackmap to pre-allocation") The syscall's map_lookup_elem(stackmap) calls bpf_stackmap_copy(). bpf_stackmap_copy() may find the elem no longer needed after the copy is done. If that is the case, pcpu_freelist_push() saves this elem for reuse later. This push requires a spinlock. If a tracing bpf_prog got run in the middle of the syscall's map_lookup_elem(stackmap) and this tracing bpf_prog is calling bpf_get_stackid(stackmap) which also requires the same pcpu_freelist's spinlock, it may end up with a dead lock situation as reported by Eric Dumazet in https://patchwork.ozlabs.org/patch/1030266/ The situation is the same as the syscall's map_update_elem() which needs to acquire the pcpu_freelist's spinlock and could race with tracing bpf_prog. Hence, this patch fixes it by protecting bpf_stackmap_copy() with this_cpu_inc(bpf_prog_active) to prevent tracing bpf_prog from running. A later syscall's map_lookup_elem commit f1a2e44a3aec ("bpf: add queue and stack maps") also acquires a spinlock and races with tracing bpf_prog similarly. Hence, this patch is forward looking and protects the majority of the map lookups. bpf_map_offload_lookup_elem() is the exception since it is for network bpf_prog only (i.e. never called by tracing bpf_prog). Fixes: 557c0c6e7df8 ("bpf: convert stackmap to pre-allocation") Reported-by: Eric Dumazet Acked-by: Alexei Starovoitov Signed-off-by: Martin KaFai Lau Signed-off-by: Alexei Starovoitov Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- kernel/bpf/syscall.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index cf5040fd5434..5f03ddf7b615 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -712,8 +712,13 @@ static int map_lookup_elem(union bpf_attr *attr) if (bpf_map_is_dev_bound(map)) { err = bpf_map_offload_lookup_elem(map, key, value); - } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || - map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { + goto done; + } + + preempt_disable(); + this_cpu_inc(bpf_prog_active); + if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || + map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { err = bpf_percpu_hash_copy(map, key, value); } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { err = bpf_percpu_array_copy(map, key, value); @@ -743,7 +748,10 @@ static int map_lookup_elem(union bpf_attr *attr) } rcu_read_unlock(); } + this_cpu_dec(bpf_prog_active); + preempt_enable(); +done: if (err) goto free_value; -- 2.19.1