From patchwork Sun Jun 18 13:14:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13283815 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAD35184C for ; Sun, 18 Jun 2023 13:14:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2925C433C8; Sun, 18 Jun 2023 13:14:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687094059; bh=gqU+K+qy0q1kkyOxvRYqnKpRmiiRP6kD7h7NhH+7b5w=; h=From:To:Cc:Subject:Date:From; b=az0QjdOdzmCwsUiXofzzO1NBwRgBHfVoJH+wCcbMQFuTCTDQHUojO5FH/Rp66/gEo R4Y78mClV0kPyLovXwdcsPAJ0/MHAThyahSLoqGb4GBp7GPgEff7B5kuN4bigT9R1N /wEwBiL9/vOYGYY4QE9TG62GNp1QHhwe9jqDF84U7ywdn57axUagCfxp8UsqXSpGpF aXzPHqyy/E15aYWj5vgIf+3qAQ+fNIs5imdxQfcWa70CLn2ZJf9AKtccQ+i52GPT5v FBuCw3X5htnMMWGugaAJmtNuD8NnHAqV+EqVmJCEfxRQGf1dSqU2Aa55z+dPfLaTXo bU5FVJksToerQ== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo Subject: [PATCHv2 bpf] bpf: Force kprobe multi expected_attach_type for kprobe_multi link Date: Sun, 18 Jun 2023 15:14:14 +0200 Message-ID: <20230618131414.75649-1-jolsa@kernel.org> X-Mailer: git-send-email 2.41.0 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net We currently allow to create perf link for program with expected_attach_type == BPF_TRACE_KPROBE_MULTI. This will cause crash when we call helpers like get_attach_cookie or get_func_ip in such program, because it will call the kprobe_multi's version (current->bpf_ctx context setup) of those helpers while it expects perf_link's current->bpf_ctx context setup. Making sure that we use BPF_TRACE_KPROBE_MULTI expected_attach_type only for programs attaching through kprobe_multi link. Fixes: ca74823c6e16 ("bpf: Add cookie support to programs attached with kprobe multi link") Signed-off-by: Jiri Olsa --- kernel/bpf/syscall.c | 5 +++++ 1 file changed, 5 insertions(+) v2 changes: - moved the check to bpf_prog_attach_check_attach_type [Andrii] diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0c21d0d8efe4..129cc5c276c0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3440,6 +3440,11 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, return prog->enforce_expected_attach_type && prog->expected_attach_type != attach_type ? -EINVAL : 0; + case BPF_PROG_TYPE_KPROBE: + if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI && + attach_type != BPF_TRACE_KPROBE_MULTI) + return -EINVAL; + fallthrough; default: return 0; }