From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 582DD475347 for ; Wed, 23 Sep 2026 21:37:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.200 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790199446; cv=none; b=Bqk4o5RCt10VXjo/LSgijDDJKi7EorXOF3KcZzD58JUi48OgKGdFSxdFYPS4bRagIoFTRXP72FjXOaOwx8sjKNP87EeppKxg2MALTCMO//PjLIBlKA0SXrhJWBgbWJ0/1PXAUTdHcyjmnJb9eAqnJ6mfmoA/8U5nQ/W1K9Zuf84= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790199446; c=relaxed/simple; bh=3/+1+eMkWeI6H0UBlfnDyhLX+/TN0y96Z5pyrUF8ZP8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cWQl1/XqM+2ZisWw9HK9RcifBhwOUIlEP+YvGBkVt0LeUG0icFiAqcmmmdqaUiryBcNcsu60qsiJAxOVRJ8dMFsQ7NkGINrwQtEAqvTfoaYUQCYJ6pPhLFcBP76/iKgI34+hyHz6VgcwzBLyLJlVL+ojXt2q1rbEEKjJWA6vmgI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HAVZCX9E; arc=none smtp.client-ip=209.85.214.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HAVZCX9E" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2d9336581a2so18890245ad.3 for ; Wed, 23 Sep 2026 14:37:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790199443; x=1790804243; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ZagZdA+ul65iAb7C09F0ANSb8lxpTUREosL0NdV8zPw=; b=HAVZCX9ExhQv9R4Q6KJHf2v5TZX0v1a3yjtaIqDLFk7Vd0v3ZJK3+CR9gV6Tn8OOYI iZTXWBChyC7nFhcRPRhr9RaBn1Y4bAVimi3iKKR2UTShT0O4DyN2K0ZnSSmC0x0TueTn 39JcWI+ZKih2D/gDbP+//i4E7NDWrkgnKr018emjbpD30SAlwMqceY2FtCf0f+g8FfiR it60/6rQIEZdXUg/gwmoS6VMqrqyT0B+VkualhNavCyd81SeRm3sfUiof1bDSLcMcqmP gRTB+eXD1qf8LwUPs7b50I5piB372RT9QYKxH/eN4MeVnlPgMwz+hm7WCvmzW1KpsWDw kxbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790199443; x=1790804243; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ZagZdA+ul65iAb7C09F0ANSb8lxpTUREosL0NdV8zPw=; b=AOyJ0Ql33ck61hoHnPBnTyDFg99slYfbgZJHeKB3cwkgLnGu+mMXrHgfF9gJmPTc+9 yoTFR5QcDSDVzpIAP8jrLITdwq5meR0lFFH4z0a+g2frQF0tMoag8wA9sM2llj5sPNFc BBhDOJ3qoDk7t51gwguGG0LSp3NrmeS8iAy4E7VDJBJp679cLXop+mi3BU+d1VfUA5Wn ljDyBrlsh+YWQ28usDhJjjgcvlAz/ayBG4s9oK1adW9JJh2aX21vAO4dcweW+JE4/EBs 52nCRvYyTSk9eWNtsVZSg1DLV2IO8GzwcCpO6oeN5g6Zk/WCf9/CEAqbJNx5CSEcqP8H Yw+g== X-Forwarded-Encrypted: i=1; AKwUvBwE2mxBtIbqjA4UlLNTefKPPUgIspxONzgaLUeq1s7xFoUUUhgBPHMM9wx8whJH4KdPSz00JC4=@vger.kernel.org X-Gm-Message-State: AFuF++nMdhvX5cQu5SzNa0LTHngsnVRzMhZJHzjv37jcEyZqQYUJuOsJ 9sWPknFuKi8uyiEeFhgljpCLHgTg7QUITRtb+kjJww33MMUso1dUF17TQ65Rqqm0tlFIvCXsEu9 MqxzqBA== X-Received: from plbd1.prod.google.com ([2002:a17:902:f141:b0:2df:5bfd:2185]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c40c:b0:2dd:c053:9c6c with SMTP id d9443c01a7336-2df7e40706dmr3287995ad.34.1790199443113; Wed, 23 Sep 2026 14:37:23 -0700 (PDT) Date: Wed, 23 Sep 2026 21:35:31 +0000 In-Reply-To: <20260923213719.224838-1-kuniyu@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260923213719.224838-1-kuniyu@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20260923213719.224838-2-kuniyu@google.com> Subject: [PATCH v2 bpf-next 1/8] bpf: tcp: Convert deny-list for bpf_{get,set}sockopt() to allow-list. From: Kuniyuki Iwashima To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Yonghong Song , John Fastabend , Stanislav Fomichev , Eric Dumazet , Neal Cardwell , Willem de Bruijn , Tenzin Ukyab , "=?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?=" , Kuniyuki Iwashima , Kuniyuki Iwashima , bpf@vger.kernel.org, netdev@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Currently, four bpf_tcp_ops callbacks are not allowed to call bpf_setsockopt() and bpf_getsockopt(). However, the deny-list is fragile, and when a new callback is added, we might re-open a can of worms. [1][2] Let's convert it to allow-list. Note that it can be written as offsetof(,X) <= moff <= offsetof(,Y) but clang can optimise to similar code anyway. Link: https://lore.kernel.org/r/20220929070407.965581-5-martin.lau@linux.dev/ #[0] Link: https://lore.kernel.org/bpf/20260421155804.135786-1-kafai.wan@linux.dev/ #[1] Signed-off-by: Kuniyuki Iwashima --- net/ipv4/bpf_tcp_ops.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c index 681fed642999..1ada3b781bf1 100644 --- a/net/ipv4/bpf_tcp_ops.c +++ b/net/ipv4/bpf_tcp_ops.c @@ -210,6 +210,24 @@ const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = { .ret_type = RET_INTEGER, }; +static bool is_sockopt_supported(u32 moff) +{ + switch (moff) { + case offsetof(struct bpf_tcp_ops, active_established): + case offsetof(struct bpf_tcp_ops, passive_established): + case offsetof(struct bpf_tcp_ops, rto): + case offsetof(struct bpf_tcp_ops, rtt): + case offsetof(struct bpf_tcp_ops, set_state): + case offsetof(struct bpf_tcp_ops, retrans): + case offsetof(struct bpf_tcp_ops, connect): + case offsetof(struct bpf_tcp_ops, listen): + case offsetof(struct bpf_tcp_ops, parse_hdr): + return true; + } + + return false; +} + static const struct bpf_func_proto * get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -221,22 +239,13 @@ get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_sk_storage_delete: return &bpf_sk_storage_delete_proto; case BPF_FUNC_setsockopt: - /* The sk may be an unlocked listener (synack path) or NULL - * fullsock; disable for members that can run unlocked. - */ - if (moff == offsetof(struct bpf_tcp_ops, rwnd_init) || - moff == offsetof(struct bpf_tcp_ops, timeout_init) || - moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) || - moff == offsetof(struct bpf_tcp_ops, write_hdr_opt)) - return NULL; - return &bpf_sk_setsockopt_proto; + if (is_sockopt_supported(moff)) + return &bpf_sk_setsockopt_proto; + return NULL; case BPF_FUNC_getsockopt: - if (moff == offsetof(struct bpf_tcp_ops, rwnd_init) || - moff == offsetof(struct bpf_tcp_ops, timeout_init) || - moff == offsetof(struct bpf_tcp_ops, hdr_opt_len) || - moff == offsetof(struct bpf_tcp_ops, write_hdr_opt)) - return NULL; - return &bpf_sk_getsockopt_proto; + if (is_sockopt_supported(moff)) + return &bpf_sk_getsockopt_proto; + return NULL; case BPF_FUNC_get_retval: if (moff == offsetof(struct bpf_tcp_ops, timeout_init) || moff == offsetof(struct bpf_tcp_ops, rwnd_init)) -- 2.56.0.rc1.315.gc6ed9934b7-goog