From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (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 382383446CA for ; Wed, 8 Apr 2026 19:22:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775676148; cv=none; b=LtN5qGLQKG8szFGmb3jeEHs4LYw3VatwcIY0scZLKWo9jTowynnf8DdA0ydVPM6mqE/WVtV9nRL8BKleQm0uhCrmGwvelrciKzagYjCgnEth8ApxsIgqrCv/P0L/XMQs5Fw51eKONaPGcqTL2Pzz67aQ9OcjtjXm9aCe2rWeJm4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775676148; c=relaxed/simple; bh=xeU+CpDbjuqTRy7NHM9S1q/5feA7r1UJohNuP6FPUFY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ZQ2JX5MOT7PZXeOS6Zak4+ky0+WsPmkIsa4lc9JMDSp8Wr7xX2PeyyhLUTlN+xXlx7A4kqG0PTK+PhNsr7FDhqgNe7k1/xKPqJ45B0Inh8kyWfAE5waARe1xHCt2ErHIWWuIgiT+iwSyZhl3l90rnBF/Z45zMwSew2CNI26DLH8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=j/N/2nXJ; arc=none smtp.client-ip=91.218.175.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="j/N/2nXJ" Date: Wed, 8 Apr 2026 12:22:06 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1775676135; 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=r06+65cH0yTHLeV6B/4VEG9UgecJFocJ5Fv7CVecJZ8=; b=j/N/2nXJ6G/XzxA/pZ7oHUMlkEaXecPiuV/MvK4oy24x+6smZSOqY/2JVzR45w8g6cpV/C 7WWB3gIXOZd4TOUeNgt724+fOogHzDpb2nVnNOZNEG4/+hblxfXW+bRP51bVkM+DgmhLlh u7EH+taxjWUhaz0Ec7A0i5B1NssR0fw= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau To: Jiayuan Chen , Kuniyuki Iwashima Cc: Eric Dumazet , bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , John Fastabend , Stanislav Fomichev , "David S. Miller" , Jakub Kicinski , Paolo Abeni , Simon Horman , Shuah Khan , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH bpf v6 1/2] bpf: tcp: Reject non-TCP skb in bpf_sk_assign_tcp_reqsk() Message-ID: <202648182730.i4ki.martin.lau@linux.dev> References: <20260403015851.148209-1-jiayuan.chen@linux.dev> <20260403015851.148209-2-jiayuan.chen@linux.dev> <20264619471.he8Q.martin.lau@linux.dev> <4820d99b-f2c4-4b24-b90f-87e6444284c4@linux.dev> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Migadu-Flow: FLOW_OUT On Tue, Apr 07, 2026 at 09:25:06PM -0700, Kuniyuki Iwashima wrote: > > > sashiko has flagged a similar issue with larger scope. > > > Please take a look. Thanks. > > > > > > https://sashiko.dev/#/patchset/20260403015851.148209-1-jiayuan.chen%40linux.dev > > > > > > Thanks a lot Martin, sashiko actually dug into a deeper issue here. > > > > Eric and Kuniyuki, > > > > I think the AI review has a point. Since BPF can modify skb fields, the > > following sequence still bypasses the protocol check in > > bpf_sk_assign_tcp_reqsk(): > > > > // for a UDP skb > > iph->protocol = TCP > > bpf_sk_assign_tcp_reqsk() > > iph->protocol = UDP > > > > On top of that, bpf_sk_assign() already has the same problem — it doesn't > > validate L4 protocol at all. > > Sigh... honestly it does not make sense to me to add changes > in the common fast path to protect someone with bpf capability > shooting oneself in the foot. > > On top of L4 validation in bpf_sk_assign() and bpf_sk_assign_tcp_reqsk(), > can't we mark such an skb immutable after the helpers and catch > subsequent writes to skb->data on the verifier ? Clearing the skb->sk in a helper like bpf_skb_store_bytes or rejecting direct writes to skb->data could break existing bpf program. I suspect adding a simple iph->protocol/ip6h->nexthdr check to the helper (e.g. bpf_sk_assign) could also break some tunneling use cases (e.g. ipip) also. > > > > > > So I think we should add a check matching skb against sk in > > skb_steal_sock() instead of adding check in bpf helper. Maybe limit the check to the '*prefetched' case in skb_steal_sock(). FWIW, in the early days of bpf_sk_assign, a tc bpf program could only get hold of a tcp_sock. Later, bpf_map_lookup_elem(&sock_map) was allowed in tc, and then udp/unix sock support was also added to sock_map. There have been discussions on tc bpf programs being able to do bpf_map_lookup_elem(&sock_map) to get a unix_sock. AFAIK, this looked-up unix_sock can be used in bpf_sk_assign. It probably makes sense for bpf_sk_assign to reject all non-tcp/non-udp sk.