Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2297385imc; Tue, 12 Mar 2019 10:51:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6bTlLE8Xy1mmtKQVu0ljHG78z82ux6gJVWoNxclrv1mWePM5WLLhvpZ3aEUkKOcgS0ITA X-Received: by 2002:a63:3541:: with SMTP id c62mr35686271pga.157.1552413072670; Tue, 12 Mar 2019 10:51:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552413072; cv=none; d=google.com; s=arc-20160816; b=BBEhtFisVfmgTR5uOD/WLXh7JVDrn+AtHHfTMUyHLC+Ae/V0TsDOsD08Nmx/M52AOr e1VJltVicYaRN2CVChqf+qH2IbuOkT0zEa4DCxT+929Y54UIUKmNsbuyO+uxlw2ji60Y oNMwPvynY87cAqKemEoYUiZbOY6U/12l5wlzknkAycYmCL8j+V/tJhK2YBRBB3vn+7Sv /SJ8BR0uvjSzoo4+sb0W053paO/ywfr0y60haVGey1YZSpQ/qz5uS4uUPWtATQnhL3Kx U1rb8UjvHR2qWzTizQNEAy1qjfeHn0go9xNZiURZ4xMS+/owAB3OGVtyaY4vuqQJmCNf hqKg== 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=8B3z6q470j0tw2SIohbiu7Z/DpiwN62Id+3W10bIF+k=; b=uM0mkl0rLLrF62UFuTFwRkgQuGAR7CHrRKmZio6iHy81t0LGuUW02h9f9+As4HlG4v 4TFG+PkzLh0GNiX8ywykKqWch2oYdcr8owzLDwA5NNGwL4pWf6MTEYABhYgQgYVXzEIh F9us3M7Y/Lb6vKCNjV0pqsGzunuDY8p2G4r6XfggM8bYJtoDJxzykzy5OUFnAMKbSxE2 j5NSpYiF2mvaZ3490A9OWuOyl4c4k0EVO3xTpPkD4gevpI7JJ0TH33LBArXOoEptAK10 NbW7KLwZV3PHYsfxriz4XZmEKgxuBdvrDP94w9MLaBxyS7lJ1noZDX8fxdE8mW2ay1Vh hj9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BOCroV6Z; 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 b6si8418305pfj.211.2019.03.12.10.50.55; Tue, 12 Mar 2019 10:51:12 -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=BOCroV6Z; 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 S1728910AbfCLRtf (ORCPT + 99 others); Tue, 12 Mar 2019 13:49:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:54232 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728499AbfCLROu (ORCPT ); Tue, 12 Mar 2019 13:14:50 -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 8937C21852; Tue, 12 Mar 2019 17:14:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410888; bh=igaBtgDtcVnkLryR7cVeYhcSxpleez6O5p5c0wAfwXw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BOCroV6Ztrtp4DTh283tDnPtqNBjAyGdCYxuQkN6RqjywVRne71ltJ8AWwrLNLSZ+ szH5s0OeK6e+Blnbw5G8v+rax9R3yqJQX7meErRnkCJK6SZ0Hd03KW0YBUYV08WffW 2eHJhj1UovD18e2NjiNW7jk7e2Fu8cplSUv3stxM= 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.19 096/149] bpf: Fix syscalls stackmap lookup potential deadlock Date: Tue, 12 Mar 2019 10:08:34 -0700 Message-Id: <20190312170357.671001829@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190312170349.421581206@linuxfoundation.org> References: <20190312170349.421581206@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.19-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 382c09dddf93..cc40b8be1171 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -701,8 +701,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); @@ -722,7 +727,10 @@ static int map_lookup_elem(union bpf_attr *attr) rcu_read_unlock(); err = ptr ? 0 : -ENOENT; } + this_cpu_dec(bpf_prog_active); + preempt_enable(); +done: if (err) goto free_value; -- 2.19.1