From patchwork Tue Sep 24 15:29:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Massimiliano Pellizzer X-Patchwork-Id: 1988992 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XCkMG69Hjz1xsw for ; Wed, 25 Sep 2024 01:30:18 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1st7UL-0004Mu-OQ; Tue, 24 Sep 2024 15:30:05 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1st7UK-0004MS-8v for kernel-team@lists.ubuntu.com; Tue, 24 Sep 2024 15:30:04 +0000 Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id B05363F1F4 for ; Tue, 24 Sep 2024 15:30:03 +0000 (UTC) Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-5365b801741so5176959e87.2 for ; Tue, 24 Sep 2024 08:30:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727191801; x=1727796601; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LOAKsxlKLsdSqiMlly/IgsDuWGDJdWHp8BT37BNL56Y=; b=TOeR5IVuwVEuQ4QwaehnFz5LVgALnJQIf9yGeJirzyg304mcs2BFt+DlJZxs1GMfg4 kufVkPqBLgGzkktRkV58ydGhHa6oZxFHmI/UlfbY+8k/meatmRlEoYf3Lxwdb/KdID4z I8IJ92spDuCOD0VYgaqx5HjkaaJEwgfuzosaK7BfBU3ElGH9c3+1ZMHolos7EM/z0/r3 LxVXMiEErynHMelRNSBnVKC3o7+zoupxPiKEB2+P3fxplfHMG3G+OQFVHN6SL9ZP7DQ2 cB+W975ATbHZwCwWG/wxUkUaTVz6TMmxwav1E2G5TGDErkQHiFipPc7p6a1qBJt8btdP 6zWA== X-Gm-Message-State: AOJu0Yzbx1CxuaxW0n/7A7EyczvZowX3VqlqOEXjKvkGWw2gc5wYZ1nj zM3pHYDsKVcri2fuRJhjD13S+nZTxazQfh/MPl2ccyqSR6UXHM1euaxhzDUH2bldCR7kQEfwyo1 6jRQnA1uZB3rSoV0i+EIWow1kMGEy2mQxPNeusiA9fo+o0Hp0f7q4yAZZZKiFcFAg9HTnVAaEoK Kt9M4Pl28jfQ== X-Received: by 2002:a05:6512:3b29:b0:536:a7a4:c3d4 with SMTP id 2adb3069b0e04-536ac32e40fmr10810183e87.39.1727191801006; Tue, 24 Sep 2024 08:30:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpMLuMd46A5HRigjhX/YcTzvG0KjVluCTjSL0d7N9NNW8X3GbKOUDFPgAF1qlUWXOe/oAOXw== X-Received: by 2002:a05:6512:3b29:b0:536:a7a4:c3d4 with SMTP id 2adb3069b0e04-536ac32e40fmr10810140e87.39.1727191800495; Tue, 24 Sep 2024 08:30:00 -0700 (PDT) Received: from framework-canonical.station (net-93-71-67-9.cust.vodafonedsl.it. [93.71.67.9]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a9392f50078sm96304066b.59.2024.09.24.08.29.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2024 08:30:00 -0700 (PDT) From: Massimiliano Pellizzer To: kernel-team@lists.ubuntu.com Subject: [SRU][J][PATCH 1/1] bpf: Check rcu_read_lock_trace_held() before calling bpf map helpers Date: Tue, 24 Sep 2024 17:29:12 +0200 Message-ID: <20240924152936.233013-2-massimiliano.pellizzer@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240924152936.233013-1-massimiliano.pellizzer@canonical.com> References: <20240924152936.233013-1-massimiliano.pellizzer@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Hou Tao [ Upstream commit 169410eba271afc9f0fb476d996795aa26770c6d ] These three bpf_map_{lookup,update,delete}_elem() helpers are also available for sleepable bpf program, so add the corresponding lock assertion for sleepable bpf program, otherwise the following warning will be reported when a sleepable bpf program manipulates bpf map under interpreter mode (aka bpf_jit_enable=0): WARNING: CPU: 3 PID: 4985 at kernel/bpf/helpers.c:40 ...... CPU: 3 PID: 4985 Comm: test_progs Not tainted 6.6.0+ #2 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996) ...... RIP: 0010:bpf_map_lookup_elem+0x54/0x60 ...... Call Trace: ? __warn+0xa5/0x240 ? bpf_map_lookup_elem+0x54/0x60 ? report_bug+0x1ba/0x1f0 ? handle_bug+0x40/0x80 ? exc_invalid_op+0x18/0x50 ? asm_exc_invalid_op+0x1b/0x20 ? __pfx_bpf_map_lookup_elem+0x10/0x10 ? rcu_lockdep_current_cpu_online+0x65/0xb0 ? rcu_is_watching+0x23/0x50 ? bpf_map_lookup_elem+0x54/0x60 ? __pfx_bpf_map_lookup_elem+0x10/0x10 ___bpf_prog_run+0x513/0x3b70 __bpf_prog_run32+0x9d/0xd0 ? __bpf_prog_enter_sleepable_recur+0xad/0x120 ? __bpf_prog_enter_sleepable_recur+0x3e/0x120 bpf_trampoline_6442580665+0x4d/0x1000 __x64_sys_getpgid+0x5/0x30 ? do_syscall_64+0x36/0xb0 entry_SYSCALL_64_after_hwframe+0x6e/0x76 Signed-off-by: Hou Tao Link: https://lore.kernel.org/r/20231204140425.1480317-2-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin (backported from commit d6d6fe4bb105595118f12abeed4a7bdd450853f3 linux-6.1.y) [mpellizzer: added the missing header rcupdate_trace.h] CVE-2023-52621 Signed-off-by: Massimiliano Pellizzer Acked-by: Roxana Nicolescu --- kernel/bpf/helpers.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 273f2f0deb23..026f3a5e71a6 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "../../lib/kstrtox.h" @@ -24,12 +25,13 @@ * * Different map implementations will rely on rcu in map methods * lookup/update/delete, therefore eBPF programs must run under rcu lock - * if program is allowed to access maps, so check rcu_read_lock_held in - * all three functions. + * if program is allowed to access maps, so check rcu_read_lock_held() or + * rcu_read_lock_trace_held() in all three functions. */ BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key) { - WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); + WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && + !rcu_read_lock_bh_held()); return (unsigned long) map->ops->map_lookup_elem(map, key); } @@ -45,7 +47,8 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto = { BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key, void *, value, u64, flags) { - WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); + WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && + !rcu_read_lock_bh_held()); return map->ops->map_update_elem(map, key, value, flags); } @@ -62,7 +65,8 @@ const struct bpf_func_proto bpf_map_update_elem_proto = { BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) { - WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); + WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() && + !rcu_read_lock_bh_held()); return map->ops->map_delete_elem(map, key); }