Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1890889rwb; Fri, 28 Jul 2023 17:27:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlEXZ9+dY0lqySZakMQoWNxRPkxqHoXxH2SQFr1BkXHZPPtdyPjmrEu229pmZPUS56ELM6hn X-Received: by 2002:a17:90b:3885:b0:268:41a:4b23 with SMTP id mu5-20020a17090b388500b00268041a4b23mr2439567pjb.18.1690590472770; Fri, 28 Jul 2023 17:27:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690590472; cv=none; d=google.com; s=arc-20160816; b=e916os7g6+MvIj6LHDCvC12Rtz7iKoHfYFr/V6CtH3XmlYafwPo1bvQ3stVav+viI6 hvf6yQ/hb5FQlSg1fMFMvsjuvnqeWYRBxpmpmXjg7PdpIQw1y2AKLE9POzjNwR/Vn8mM VUsgT01+I5DjE8C5XD2k/sCUxIgpLI3O3QLuU+kNDjItXFmRUmX588ptINW56HrZy+H0 q7oAagJwAgxM4sWa9fi6exfGs6nc4tsEU3W9DhOm5mMIAHrQViRA3xUyboCO3uCBGXd2 7i5HV+IhjC84uowm8ZGA3pEPS/Ea5wYsc6Fe1NO2MrQuBG5v+PqH6MYbLatBLNIUjeEX uHYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:mime-version:date :dkim-signature:message-id; bh=8hWvaYfIn7U7DdQhxoUICMLRNRgSRaJIOm4Ba5/IrXM=; fh=Y9oS8IEtZ89Fm0x8bg3UBmK/eL30cPuOlM1f8vIZueo=; b=xJTWNKONv5CmKb6n11iLhkAExXGPcsPplK0NKAbMkE8uJWiEPo2uaXV3bwlQCFMmJJ sWRRdyZgt5GmOY7RcZCvSz/vNTQX9QtXT/mFvS2H2JWOVDTn0oxbTmqh5GcqbNKh7tOp dva12tr92EQJNNViyCxbXpqJgq5lofMlNuAlT6AHeqQx4phFNPI7KBuj5PQtfvlSxk/N WxfM2rmSFT8/+mHQ/YthksfeCvRaA5EvEExU6dFpOiOMJXywC5ilSjr4eyIqiGicK8J+ Ej9fRiZrR26jANf9Zt1Tnxb3blQzWISup8iVNRAiN4GCaZcpMegjrkkResQ1vf84BNaT zsbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=WdVTItCT; 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=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z16-20020a170903019000b001b8b2cb7359si3717211plg.627.2023.07.28.17.27.40; Fri, 28 Jul 2023 17:27:52 -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=@linux.dev header.s=key1 header.b=WdVTItCT; 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=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235240AbjG1WCs (ORCPT + 99 others); Fri, 28 Jul 2023 18:02:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231146AbjG1WCo (ORCPT ); Fri, 28 Jul 2023 18:02:44 -0400 Received: from out-127.mta1.migadu.com (out-127.mta1.migadu.com [IPv6:2001:41d0:203:375::7f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 182D92D5F for ; Fri, 28 Jul 2023 15:02:29 -0700 (PDT) Message-ID: <266ab56e-ae83-7ddc-618e-3af228df81bd@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690581747; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8hWvaYfIn7U7DdQhxoUICMLRNRgSRaJIOm4Ba5/IrXM=; b=WdVTItCTD9ygpob4ihOR+/GOrZVv2IJlkxrw21VRkyQgnRjcam5YMd5HHNBToM2Fj52wGy d63P3UDnhS7hVxUvDfwgGmkgLcg78W1sDnP8lyY4VTL8ADsM6fRrb8FOnFs2aa7Gu7ZlEn qwjI3sZklIivyKGi+AVTXfYJpoIZiJs= Date: Fri, 28 Jul 2023 15:02:19 -0700 MIME-Version: 1.0 Subject: Re: [PATCH v4 bpf 1/2] bpf: fix skb_do_redirect return values Content-Language: en-US To: Yan Zhai Cc: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mykola Lysenko , Shuah Khan , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@cloudflare.com, Jordan Griege , Markus Elfring , Jakub Sitnicki References: X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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 On 7/25/23 6:08 PM, Yan Zhai wrote: > skb_do_redirect returns various of values: error code (negative), > 0 (success), and some positive status code, e.g. NET_XMIT_CN, > NET_RX_DROP. Commit 3a0af8fd61f9 ("bpf: BPF for lightweight tunnel > infrastructure") didn't check the return code correctly, so positive > values are propagated back along call chain: > > ip_finish_output2 > -> bpf_xmit > -> run_lwt_bpf > -> skb_do_redirect From looking at skb_do_redirect, the skb_do_redirect should have consumed the skb except for the -EAGAIN return value. afaik, -EAGAIN could only happen by using the bpf_redirect_peer helper. lwt does not have the bpf_redirect_peer helper available, so there is no -EAGAIN case in lwt. iow, skb_do_redirect should have always consumed the skb in lwt. or did I miss something? If that is the case, it feels like the fix should be in run_lwt_bpf() and the "if (ret == 0)" test in run_lwt_bpf() is unnecessary? ret = skb_do_redirect(skb); if (ret == 0) ret = BPF_REDIRECT; > > Inside ip_finish_output2, redirected skb will continue to neighbor > subsystem as if LWTUNNEL_XMIT_CONTINUE is returned, despite that this > skb could have been freed. The bug can trigger use-after-free warning > and crashes kernel afterwards: > > https://gist.github.com/zhaiyan920/8fbac245b261fe316a7ef04c9b1eba48