Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2307823imc; Tue, 12 Mar 2019 11:04:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6wF6zE5FRkVyDauLkaMv2GUHCqOw7QYlhAM7UiniZwOD2jt6W9x5z1fMnL3e+BzhlOtWt X-Received: by 2002:a65:6149:: with SMTP id o9mr36115348pgv.315.1552413855508; Tue, 12 Mar 2019 11:04:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552413855; cv=none; d=google.com; s=arc-20160816; b=N3/4k5b12yD5eUDzdzI6GutGF9KMgIGb/YCfLovEeSKbenqr8OelMthuSM9B8dzb4L dkIiT1DFaOTvjmE0co3aariMKNM4v5Lm4uR2HCBmbhUl0NRPbuDXf3SNnLLWZGZsqoKG GSwrGh/0+08SSEUTJWEuYSYgHL+Zu6iwLCh6oNw0i57yCjr1P/xAgYR/XYKcqSvsjpMm BB7yyXRVBTpEhS3OFqjuzIFxzj7U06adpjMLDaQdTeKmnvZkkoTBFnCRbu5wpFDZALnz zisF0msbEcaKGUMzkB40+yqtAjokNXBP4g3LPxJfYpQK9RujZrnbDtHgmNvaV7eOv+w8 8wpg== 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=efaL234eEee/LH8p3fSKYKqYCIGXqMCla8a6VZNim/Q=; b=k+/cltGS2rpFkjsErnpjTH0Ys3Oyt7EeZfmVdNMwND54nQkICVabFrjN30jf3pmtGA cLHutcGybX/JRl5wq16z6b8vzRe0RTr5w/NC8ebhzFV4OQqFLjbM10G0/3BMnO5NQs7k f0FERr+6kv12CxblSoXSyK+yBqS28IEkF0MnCdW7OGqKdYl/KTnn0jISAP5H5OPOTLEK Ir+QPOCgsItdl7Wq3Uq2bYFvHVp6LvTaHgrrpIz0Z1Jei3lCPAhdYKsaGJTlhmhgDJXY iIPufXWwULFvfjo+VICWroZBxaUXsvKhFOpKScUt6vP1LJtoFrNsziWVn1/q90w+NJH3 IUJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=g9r65nfa; 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 g1si8401880plt.318.2019.03.12.11.03.59; Tue, 12 Mar 2019 11:04:15 -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=g9r65nfa; 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 S1728370AbfCLSBt (ORCPT + 99 others); Tue, 12 Mar 2019 14:01:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:49560 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727810AbfCLRNK (ORCPT ); Tue, 12 Mar 2019 13:13:10 -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 0DFDF2087C; Tue, 12 Mar 2019 17:13:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552410789; bh=3l/lK/69KMYz3XILXGkh5EPwGQwtskJ/vIAMX5gEU18=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g9r65nfa3A8lTHPA8+HL9u8WFjLLQjz33c46bS8EuiOnUZHUFAyy5j2vviq2q0Cuf T4ZDdV/XfsFWMQmAQsE7ePQMoTVxCFeI6dxSQMg1b6KySWEuNzjEqsh0Yt46kce7ji 3Dcjqh48Y80MAZAWH8ErCz2WgryYK8k2/2RVyz8M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Alexei Starovoitov , Song Liu , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.20 111/171] bpf: run bpf programs with preemption disabled Date: Tue, 12 Mar 2019 10:08:11 -0700 Message-Id: <20190312170357.516315895@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 6cab5e90ab2bd323c9f3811b6c70a4687df51e27 ] Disabled preemption is necessary for proper access to per-cpu maps from BPF programs. But the sender side of socket filters didn't have preemption disabled: unix_dgram_sendmsg->sk_filter->sk_filter_trim_cap->bpf_prog_run_save_cb->BPF_PROG_RUN and a combination of af_packet with tun device didn't disable either: tpacket_snd->packet_direct_xmit->packet_pick_tx_queue->ndo_select_queue-> tun_select_queue->tun_ebpf_select_queue->bpf_prog_run_clear_cb->BPF_PROG_RUN Disable preemption before executing BPF programs (both classic and extended). Reported-by: Jann Horn Signed-off-by: Alexei Starovoitov Acked-by: Song Liu Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin --- include/linux/filter.h | 21 ++++++++++++++++++--- kernel/bpf/cgroup.c | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index b776626aeb84..958eddbc44d3 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -591,8 +591,8 @@ static inline u8 *bpf_skb_cb(struct sk_buff *skb) return qdisc_skb_cb(skb)->data; } -static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog, - struct sk_buff *skb) +static inline u32 __bpf_prog_run_save_cb(const struct bpf_prog *prog, + struct sk_buff *skb) { u8 *cb_data = bpf_skb_cb(skb); u8 cb_saved[BPF_SKB_CB_LEN]; @@ -611,15 +611,30 @@ static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog, return res; } +static inline u32 bpf_prog_run_save_cb(const struct bpf_prog *prog, + struct sk_buff *skb) +{ + u32 res; + + preempt_disable(); + res = __bpf_prog_run_save_cb(prog, skb); + preempt_enable(); + return res; +} + static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog, struct sk_buff *skb) { u8 *cb_data = bpf_skb_cb(skb); + u32 res; if (unlikely(prog->cb_access)) memset(cb_data, 0, BPF_SKB_CB_LEN); - return BPF_PROG_RUN(prog, skb); + preempt_disable(); + res = BPF_PROG_RUN(prog, skb); + preempt_enable(); + return res; } static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog, diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 9425c2fb872f..6c24b1fb2db8 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -572,7 +572,7 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk, bpf_compute_and_save_data_end(skb, &saved_data_end); ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb, - bpf_prog_run_save_cb); + __bpf_prog_run_save_cb); bpf_restore_data_end(skb, saved_data_end); __skb_pull(skb, offset); skb->sk = save_sk; -- 2.19.1