Article View: linux.kernel.netdev
Article #6960Re: [PATCH net v7 1/2] tun: keep a kernel copy of the socket filter program
From: Willem de Bruijn
Date: Fri, 23 Oct 2026 22:21
Date: Fri, 23 Oct 2026 22:21
52 lines
13872 bytes
13872 bytes
Rongguang Wei wrote:
> From: Rongguang Wei <weirongguang@kylinos.cn>
>
> TUNATTACHFILTER copies only the sock_fprog header, so tun->fprog.filter
> stays a pointer into the address space of the process that issued the
> ioctl. tun_attach() reads it again whenever a queue is attached to the
> persistent device later on.
>
> Rebuilding the filter from that pointer is not reliable. With the inverted
> check below, a failure of the attach was not fatal: -EFAULT for an unmapped
> address, -EINVAL for bytes that are not a valid classic BPF program, -EPERM
> for a locked filter and -ENOMEM all ended up in "err = 0" for a new tfile,
> so the queue was attached without a filter. And when the read succeeded,
> the early return meant the queue was never published at all.
>
> Keep the program in the kernel instead: tun->fprog_kern holds the
> instructions, and each queue gets its own program built from it with the
> new sk_attach_filter_kern(), the kernel memory counterpart of
> sk_attach_filter(). The copy is freed when the filter is detached or
> replaced and with the device, and tun->fprog is left untouched so
> TUNGETFILTER keeps its uapi behaviour.
>
> Also fix the inverted check, which made tun_attach() return early when
> sk_attach_filter_kern() succeeded instead of when it failed. Neither change
> works on its own: with only the kernel copy, every re-attach returns 0
> without publishing the queue; with only the check fixed, a re-attach that
> used to succeed without installing a filter would start to fail.
>
> sk_attach_filter_kern() builds the program with bpf_prog_create(), which
> does not keep an original program, so SO_GET_FILTER returns -EACCES and
> sock_diag omits the filter for sockets that use it. tun sockets are not
> exposed as file descriptors, so this is not user visible.
>
> The header and the instructions are checked in tun_copy_filter() before
> anything is replaced: a TUNATTACHFILTER that no verifier would accept now
> fails early and leaves the previously attached filter in place. The
> instructions are verified with the new bpf_prog_validate_classic(), which
> runs the same checks as bpf_prog_create() on the copied program. Before
> this series the check only happened per queue in tun_attach_filter(),
> which never ran when no queue was attached, so an invalid program could be
> stored; with the corrected error check, every later attach of such a
> program would then fail with -EINVAL.
>
> The inverted check was discovered by manual code inspection first, and the
> review of v1 reported the other things.
>
> Fixes: 54f968d6efdb ("tuntap: move socket to tun_file")
> Link: https://lore.kernel.org/netdev/179027275318.2160803.4185895144088175048@kernel.org/
> Signed-off-by: Rongguang Wei <weirongguang@kylinos.cn>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Technical Details
Message-ID:
<willemdebruijn.kernel.195229eab691d@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-351262-linux-kernel-netdev=dewy.ruReferences:
<20261023092239.186665-1-clementwei90@163.com> <20261023092239.186665-2-clementwei90@163.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-351262-linux-kernel-netdev=dewy.ru
From: willemdebruijn.kernel@gmail.com (Willem de Bruijn)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v7 1/2] tun: keep a kernel copy of the socket filter
program
Date: Fri, 23 Oct 2026 18:21:43 -0400
Organization: Usenet Network
Message-ID: <willemdebruijn.kernel.195229eab691d@gmail.com>
References: <20261023092239.186665-1-clementwei90@163.com>
<20261023092239.186665-2-clementwei90@163.com>
MIME-Version: 1.0
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
logging-data="627822"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:fj/y8AJuSf71eIjhDIVmcZMDCY0= sha256:A4e/yzht1LZWWKdZks2XjMGyZzJT4OYwidi3w7Zik1A=
sha1:2PitucU/pKCKxtGryQOUEtpixBM= sha256:6OXyRM9nAy+4VAHqyLnbLRvL9VN0/AeEHqlJAR5ncqA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 526AF1115AE; Sat, 24 Oct 2026 01:22:12 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id X4DfDTCyGoSe;
Sat, 24 Oct 2026 01:22:04 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-351262-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 50E0F111588; Sat, 24 Oct 2026 01:21:55 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id 8EA1F3B7A4; Fri, 23 Oct 2026 22:21:47 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1792794107;
b=u45MsDrWoaXwif6YP3Uz3sju6OVN5G0bK65A8fU1oCR6t88uWG0OH/PAqdxlr3TRBCWL
y7ijusTcvPsVdWgYSzoueqPSM8bmt/4TSNoHhUtKXiT6YU2MFkMJWum1unIOQjknwO/hz
ZvtTB8lXzSx5/JjpAan1mi/he3A95Cu4h/6ht6mzKwHyIiwBl8PptiIx1/0nteJRKK8NA
bK8dKeKxyfUYV/17QLg8DoJ7zSjrFP6N250uf10LBbU4RGHne6nYMJwi7zXhaBwrn8O0B
hmlqRQ4gLcXNQWax0vWZDDxsbgJ/2MY0CoX38hzc1FuzXsLNpgWE3zmXvXcHYCdJVew==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1792794107;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
X-Gm-Gg:X-Received:Received:Date:From:To:Cc:Message-ID:In-Reply-To:
References:Subject:X-Mailing-List:List-Id:List-Post:List-Help:
List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
Content-Type:Content-Transfer-Encoding;
bh=IWBwMrXqNWCj4I6BbO1n05p76FpYZ/EMafoBUaFtezM=;
b=xnIiLYARfqBZUMlQeq8yW97BdJziaeyNrjFvKTOmH47uZpmAnAom0C2jxbM95sRJfAD9
HJL7Pio+zn8TgB9dYLxCDoxN/70oArgpiV6VFRAhPLG2qYi/5d2eqbSHza48pgDNXFJVG
cLHv5k7W/koRLqLJU6AyYVA5scvpBNZgGXkBfmJGQqRWJeqmmc1RwWgVbkcR4VhD1zLFH
tStXYOSFFXlKGefQEL9fp9euDI927vJe9n02JaoCE3jblylc2edAxzJ+qsGc99rtMSLnn
ck56gQoWlQ2ZWREQICsfTJeYIBTWDonH3MINmRgETeVnR5jKQ/RemEcVzL/3k0vjgIQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=PufYFUDN;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id EB98E606A321;
Fri, 23 Oct 2026 22:21:46 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=PufYFUDN
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.217.41
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1792794106;
b=ne2ddQuEvCnp7FxyuLB8eazFsvJEtH8Gr5VYp+LDi903T37KHDDE/IGxto2wnAkc1l64
JkMf2OaG766cgK0gn05M3WqlrqO9Np0Wba69h775dSmQeXRscvUTz+QyrjaIiwjiWi5sm
fvgneHueF6pEJivUFSARZDg5Z8iXYcgYOH+6q1Ae8csU5BM5mDUTD1GCSH9J8KLXO0S4E
fJSvUN+pYqm28coqBb4GNEwUsyC1r3Ws3No6DSB5B3SpSC0QMGbQRl3mGPTgVoqFoWYm6
DKAf4rtUsoydnZiLZDlU40tbVs1Y6ZjeHYajavbm4VyXH8g92zOWLHbiwii5e33w0cA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1792794106;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:
MIME-Version:Content-Type:Content-Transfer-Encoding;
bh=IWBwMrXqNWCj4I6BbO1n05p76FpYZ/EMafoBUaFtezM=;
b=CYzkvZyF3+l5wM89YIfpW2QKTBWtuS2MdUV+NnIwT/feWA45eV9CR25fgOaF9L0D84W1
tAb1S9hV5E3slTNifdGlG6S1p0eJ6tbxV/odh1hNemgTearjqrIKbSiJAOVnTBeaZso7p
gcRIvGrJSd48FOmHPYLq7k8xA0f7ekyGFr0QVmZb/UdHoCy544yuecfxbDQhiHEeHyPzn
jqo0RTXJDsP4iPXA2GrwRwbm2eZxKSa5PWnI51qWbjyrWfGLJJzjyiaoTY0cdGDUuNyV0
mkqroUzOrteXHxeq5x8zpf1CUUHSpH2CgJjar5qOCeE/Jp3n2wwmV//D+pByWFH31LA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=PufYFUDN;
arc=none smtp.remote-ip=209.85.217.41
X-Received: from mail-vs1-f41.google.com (mail-vs1-f41.google.com [209.85.217.41])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2E82606A31A
for <netdev@vger.kernel.org>; Fri, 23 Oct 2026 22:21:45 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2E82606A31A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-vs1-f41.google.com with SMTP id ada2fe7eead31-7cc81984fceso449038137.0
for <netdev@vger.kernel.org>; Fri, 23 Oct 2026 15:21:45 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1792794104; x=1793398904; darn=vger.kernel.org;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=IWBwMrXqNWCj4I6BbO1n05p76FpYZ/EMafoBUaFtezM=;
b=PufYFUDNeX4ALEFqpZT/VxPm6/CdzqV9uR2+nCymKyY0UZdBxieol2SPIIriXTfTOn
R0NEm/MqkmKEm/rC2j+Z4wsftgYjkzP1qMTwQ6eVXrRrEEdwEXZiYH2NBEDWkSCoUxav
47boeX2hLMwI/pKiZ5TKpvWsZDkA22dw5MrquBN6YE5rmdcpc1rPsAi9WmwvGF4JCnPx
JQd30VPqAWdfdS8vmkRhiwatYXg9ImsA9etrVmA0A4GawK+Sw+4nRS1jKpHcur9FqN/c
3p6T6Pl2A3Ub/UqaUA8PHXq9K6MekMCYzxaGAHiV8HGZLV7r563FN7Aj6Al8gVbr4t/g
UY3Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1792794104; x=1793398904;
h=content-transfer-encoding:content-type:mime-version:subject
:references:in-reply-to:message-id:cc:to:from:date:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=IWBwMrXqNWCj4I6BbO1n05p76FpYZ/EMafoBUaFtezM=;
b=O5djUeDIIQQ2NFhaX6aY/V/oNc6IzDVhHLn7WYacVUPO538rUQQ6G9+pspxTIXBubv
MO8Z/PiLEHdPi1oU73loTcVT8S8hpmcqUg8IwwO7JlEd+MS76osnNU1UcISdW6PHDSed
bEyaQtP18Ij0Y1bCuc0JKpHVh0Ka7Fstf4dzCgsgehsOnCjPOADxGvKLAQ5ogOjbyubq
869VlUo/rxJo5mCPTdHFfjsMx0LJmPiDF5D1TDn7HU16z00pYdo6ka6a501DL4E47VQl
hj0NNI0zIu5k2xwNr1zybYvjCN3v7jqWP4/ddxW51gsc6a+aoRpKWV060oaBKPISPw93
C/JA==
X-Forwarded-Encrypted: i=1; AKwUvBw4dz5S9taCaRx5t2coesNieS72uY6hPEoeUnpZV9Mm7dToHiF02VqGbelxZeDwv2V9MHgIsF4=@vger.kernel.org
X-Gm-Message-State: AFq9FYL/nLS2VdyDJRfg6rtmu0nhDnxZs2qYkIspFst9FwUpc6Ao6w62
Xr+ciXvo2WplOTkXJ4Dn7MRedNANTzAY6fURL8rn/BkXux4g/xj/Xc5i
X-Gm-Gg: AYBFou0A/O7pGf9HoXxnOTtkoA9IIQgJ/VSsJ/hR84DogYkl+wUAl7NJrQr4cTSGmoq
4mbxaTKBBwdgcgPaPC6rVzim5CxhbErDaHR57lzb78ALw3IriZ1MZQqzYqIx0sWUYnFJKk1hcTV
YCByHoRc5iz0k4FzYWiFHmxMcg/aPwAOF+sKl5ya2tP/b+Ug5wPQIMFWJc9cc93+r9zmLoU/vr3
sqo15A2g3xJS865f1b881k8THp7CjuWGMkl1VmJ+iwGiy8szMH1CzoAJbb2irT+vWOCihgdsvTX
bQ1IDLRMsJUhQ2CfNDb7ZSYw5WHfBFeY5KWE5DxJxEjjjA49fZKr21sDkiBaTfPeRsXvYlAOJNC
Tm4YlgqcnMIGgFoLdSqxbFITQEqgV7yTdqsHUQ/qYBEQZBWBbL0p+XSRFeeSjAPnNWSZVFzaHjP
QFbpbA1umwPGuVTrmLN0vMcZmubEcHM2weqwYwLpMqe7t6XP1UV2ZBLcog2KiAxDu057cTJW6Qj
21IXm11mOOHKB/J97iO5wUjX9UUAB4LiLHOS1dpjk8nUHGtoRxw7TOXVrtBKwLdlDa5K1DTr8Ys
Sj4cguon0GTd6mdU56q1z+brTVWtx9fIW89X8UV9cmtqH31kG66jA+GvYoJq9/DWDH/SSRNJnXk
auHuhgkh1rja9t6RRKTY=
X-Received: by 2002:a05:6102:f9e:b0:7bd:b263:14c8 with SMTP id ada2fe7eead31-7ce5ce55591mr182590137.30.1792794104502;
Fri, 23 Oct 2026 15:21:44 -0700 (PDT)
X-Received: from gmail.com (111.46.245.35.bc.googleusercontent.com. [35.245.46.111])
by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-9910e7d9dcesm791938241.6.2026.10.23.15.21.43
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 23 Oct 2026 15:21:43 -0700 (PDT)
X-Cc: willemdebruijn.kernel@gmail.com,
jasowangio@gmail.com,
andrew+netdev@lunn.ch,
davem@davemloft.net,
kuba@kernel.org,
Rongguang Wei <weirongguang@kylinos.cn>
X-In-Reply-To: <20261023092239.186665-2-clementwei90@163.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list