Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1586534rwd; Sun, 14 May 2023 23:24:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6qI+L/MgRN4+SsDHhBOpUcoIzaUkDhQvEZojkgmIN34W3OmvFEEDcBJ++o/aQyWTDNaQu4 X-Received: by 2002:a05:6a20:918a:b0:104:edde:67be with SMTP id v10-20020a056a20918a00b00104edde67bemr8746339pzd.17.1684131883645; Sun, 14 May 2023 23:24:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684131883; cv=none; d=google.com; s=arc-20160816; b=KZBLkikGedlvktAYUlzwNt1oCpNbhh5MAryQZwPKcnNr6fJhn3F5N3GJuYFnT7c1oU HVV99UbdjqYYWTOebWw3wYUx/FTeI2GUgnKAJDJYdSHJJSrryKVYQKus0fmJb31hCMpP BuhoYSboPDNFJr6HRhyZfudMdjajLBjJQqWck8O5DWBkAHoS0phuICf8X/82Skvtiuqa MvHEM5Y0l7pKN6p2oXYB30W7CbLnw+RzM/HreY6h1Ku+zEs3z/KeLb80/35pDNOOPwew y0vSm+1wcoIUpfVzjKT9s4HX1LaPJzoq8CbZzvxMgCDYEMulo8mNKPf7hBtCSpoGsCKp clJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=GWJZN0AxnMLj8Z4KaxYfGt0FY36qubWQkKlCweV20LE=; b=jsawf7YJACF5c37EMxTnoTsFA6dgQW3bbOX9Of/eguPeBbGUR7nihJA62MZ7911dTF Tf+T2PEkE/q/9VCX7hq/1bH7rFPG5pE1e5WKiWy5/FfKtayBaesAxUMOedBZ5DGbozaV HNGwviIVqiG1oltL4pn9/eioP4/u4UG4jnaEqwLpWU7qffOwFxRY3l3KqcW6QsFNh5OP 5DT2dgRj9BYGLj7Xowsq8Du3wuNPRf8/qaBOK3jjjRgWyFfuLSC42+P1+InA94QJBuEd ku1AMqEFO2TUU+KdnI/X+nLni1sMxsGIdJHVNEz7ikMkxBHv+Lj5Yf6C542SXL+4y3FD 2o0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=iKjTvLOa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y192-20020a638ac9000000b00527d158ec73si16012377pgd.3.2023.05.14.23.24.29; Sun, 14 May 2023 23:24:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=iKjTvLOa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239361AbjEOGHE (ORCPT + 99 others); Mon, 15 May 2023 02:07:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238926AbjEOGGl (ORCPT ); Mon, 15 May 2023 02:06:41 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61E412D41; Sun, 14 May 2023 23:00:12 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-50bc075d6b2so22492455a12.0; Sun, 14 May 2023 23:00:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684130411; x=1686722411; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GWJZN0AxnMLj8Z4KaxYfGt0FY36qubWQkKlCweV20LE=; b=iKjTvLOaLIAQInv4Zg3BQl2vPIyVFhi3CpcLEmlrMGBMoqotkbBGotUeAdfq21fElc cy/xMPodX7Gzzg1tBArbJcBknuUJ1HSw/3aqtfOO8R7DPQ8RJTLgnpxXyYhfxWd+zscU w0bmdI7FYW8O+e6RwW3bylQil94PcQ6lDb0Xc7KCrXCXraOs08V313MQmwhDBdnw7b0D t2jeED38yEr3U0W2Jfy6fSbtsCWW1YhGM6jxcOjAqQOMWw8KJpGFSxbPqdP4NrJN0OCv yyRiVX/WJFFmUChE/F6w4x8K6n3qxFApsZxtSAYsjc+mF8WoZ9Ap+0GJru6DYRta7Syk zhdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684130411; x=1686722411; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GWJZN0AxnMLj8Z4KaxYfGt0FY36qubWQkKlCweV20LE=; b=UjBwIhlwUi5DhBtXeWZCUUZ+UEkYJIpT4FgKhcGJgSPxDUDvTg6VobATjDTW5O59ap lT2NxBf+fINzpt7qgan6EJupJot/EaFwTQv4CCMrhf5z/KfeZHK5Lc5UBE0WuJUNED33 S+TWZnLyEA6P1z4J5YvHuAtG4F54swIA1euYqcuj+O+a7JodIKBc9SqKkvzKdgz9MsfF fmyGGM/8X3HOgjDklGyjGczdGqkhggLgr9cfZLXE0uLUF5m9sAUY+I3imvhLM0dOLDmy RTkCcL2IKj6JqTb5c2u4xXvH4fxFi0YHxgSTjENlBK6OhBUXQE2aEpJ1a4JGHXSw3Dxz 0UnA== X-Gm-Message-State: AC+VfDyxb/JOdOgsr927K8/p59r7pg4/OKFh8DPedXuIOiXgw21QSti2 kZfHNGZ2ynD76h3UjyeDVLJfvKRq2CwuqBqj2I0= X-Received: by 2002:aa7:c747:0:b0:50b:fb49:c4a8 with SMTP id c7-20020aa7c747000000b0050bfb49c4a8mr25276264eds.27.1684130410646; Sun, 14 May 2023 23:00:10 -0700 (PDT) MIME-Version: 1.0 References: <20230510122045.2259-1-zegao@tencent.com> <6308b8e0-8a54-e574-a312-0a97cfbf810c@meta.com> In-Reply-To: From: Ze Gao Date: Mon, 15 May 2023 13:59:59 +0800 Message-ID: Subject: Re: [PATCH] bpf: reject blacklisted symbols in kprobe_multi to avoid recursive trap To: Yonghong Song Cc: Song Liu , Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Ze Gao , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Dear all, On Thu, May 11, 2023 at 9:06=E2=80=AFAM Ze Gao wrote: > > I'm afraid filtering in user space tools is not enough, cause it's a kern= el BUG. > > it would 100% trigger a kernel crash if you run cmd like > > retsnoop -e 'pick_next_task_fair' -a ':kernel/sched/*.c' -vvv > > which is caused by that BPF_LINK_TYPE_KPROBE_MULTI accidentally > attaches bpf progs > to preempt_count_{add, sub}, which in turn triggers stackoverflow > because the handler itself > calls those functions. I managed to see the big picture of this problem by digging into the code. here is what it goes: rethook_trampoline_handler{ preempt_disable() { preempt_count_add() { <-- trace fprobe_handler() { ftrace_test_recursion_trylock ... ftrace_test_recursion_unlock <- it fails to detect the recursion caused by rethook (rethook_trampoline_handler precisely for this case) routines. } ... rethook_trampoline_handler { [ wash, rinse, repeat, CRASH!!! ] There are some pitfalls here: 1. fprobe exit callback should be guarded by ftrace recursion check as well since user might call any traceable functions just like kprobe_multi_link_prog_run calls migrate_{disable, enable}. In this case, detection in fprobe_handler only is not enough. 2. rethook_trampoline_handler should use preempt_{disable, enable}_notrace instead because it's beyond recursion-free region guarded like 1 3. many rethook helper functions are also used outside the recursion-free regions and therefore they should be marked notrace I've post a new series of patches to resolve cases mentioned above: [Link]: https://lkml.org/lkml/2023/5/14/452 In theory, bpf_kprobe as the user of fprobe+ rethook, is spared from suffering recursion again by applying these. And [Link]: https://lore.kernel.org/all/20230513090548.376522-1-zegao@tencent= .com/T/#u becomes optional. That leaves one final question, whether we need probe blacklist or bpf_kprobe blacklist depends on how we deal with user requests if one of its expected hook points fails because of recursion detected. Do we need to reject them in the first place by blacklist or let it fail to execute the callback silently, which needs your gentle advice. Regards, Ze