From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 76D73443AB7 for ; Mon, 20 Jul 2026 17:33:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784568846; cv=none; b=PiZykVH8TlIvOt56fG5jPAGN2WMHgrglviz1bdfsknYqJpNauYJkmeznInavuxWVx5FtjjJyBpyOIJcOwakr+wg+ttOqf2vtZ92ZW8Z0e0DrBiQiTaFEHBKjd302uTPp+LdYWA+g2I2Wvbqi3uEKFwKJz4r2Bwz29/t3v34aB1I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784568846; c=relaxed/simple; bh=5+1HIFGffDicEK+jtbv1grqiIBz6DgOALeu2G1thgSA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=sksYx1hEJve09k8GTuvRhjkPWtfzOFgldQfFjbD+PuOzINNfKDe4cpdmT8ZC8GwpGEt/9vSwIjy25YPnkTakiauUFeXbnftgCXaVpFkRQ4gnZe3jhyzpuRsXvJOOZ6hNjflqVk0Mej3XUped6pqibDqYzBRtSWJJofporaKOiBE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=d5BAe+fz; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d5BAe+fz" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-3811f512167so10233227a91.3 for ; Mon, 20 Jul 2026 10:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784568832; x=1785173632; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=9aCLrLf53nr1V0yxy7c+zTasVcZkTH5A7c7mDrM/Tqg=; b=d5BAe+fzs0Xm5dkiS+XFJ5RZm8Q4oc9Idcxp3OegzxpEoiBnLyVOoADwn5tMEf2eC6 TK0Bv/zOVtLPjIdv8jwBxBOCO5K8duD5dikFNEToU1Mbq8Kxxqz5dvfbFMocsxHJyjTE 57qL8+Ap3X/rslOFSZFiuCTcVZZKHpzZONj0asHKwk+lcAE4ra6sxaAmp5BZApK2rBGQ 6kD686ou0oUU3ldD3y42yDlfx5InRU3/eOUisHCDQs29e/k9J6c38nP+mgM+vhDvI+NU oeN8uODOSZxNbUl8Pc5q9brTbgxjm9PUtDS0luUq52PCLgRhPbCP9yeDOoYeNbyHtrTO glQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784568832; x=1785173632; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9aCLrLf53nr1V0yxy7c+zTasVcZkTH5A7c7mDrM/Tqg=; b=MszcuD+5kbYPLj7tsw6kn3hYFQUox3OQ6Zmq4j9KAqMWK+jYgWBNXHgpxeqQbPSnow BGV8c44WqoY7DZQVAmDyzWYrJ1gUR8LPFSAYRsB0sgEXdo58Q0CeWQj2+1HB4llsjeAE PDsDT/rIPW4j4iFP5gNjSaDbjT/ravelXnPGfyMhoyGB0U7ivnEjv1hfcWCv29TdYire O4vi03VgljM5JEP6NdtobjUY2etUOcPf2VzRBkvCLjPwzb+2x4zlIcbfBnCM1EwPyg7X pzdVxI4ZlnwTpxeWgw4JhFjEytHwCf/3Lty5h2ojMHbyp54skLuraDbhO/V+h/fhA1X4 Hv7Q== X-Forwarded-Encrypted: i=1; AHgh+RonM/LtsvDNAiy7qZvFNGmawT0ttzt8WPY2bFmywFhldEOkF5ghbtBNApYHixJJidWD8is6HTWYQ5NRgTA=@vger.kernel.org X-Gm-Message-State: AOJu0Yxob0dT4FR60Db6aleemPbrKkbc4Mgp+xwP2gUKjuoatA/eCLut aMHqHkS4YK//gnl0OoCElky2r6l1StAVOMWtfFoatNYc4VJz6a1ekyL/ X-Gm-Gg: AR+sD12ry/ZBfeVH7aWNbmWgWLzrSFmFowHKur5ZENp7s+FkoJ00W+w9KgzYT1ySYB2 tT24e5mkm1V2/aVZim//kmAPzzpm4UVcHRR2uHx5MSugpq2Oj5ht98IAxqLjifFnbc/3elRNIft yACM79ibLBfqAWqsR0BLRgBu2423k/Cn21CbjB/uFSrP3LSNEvJK1qs8W1ttnYqjQyKoQ04cnI3 Nbt0Cg55R1bX0jyY3b7A28DLDqn1sU021xKsK46YYbz0dxEkpSdq1w/o7KNvZx5jel6qRvUlGmK T9IrPjiXEEu6IT7XPdD4ZKCd5eRAEPYL6cn3jv3JaHEgscnk1hHBF0NvjtOXAfZIcg/qTgTSjAG gYH29NBzLTWzLWEa1I2q6WbNGqV8yAP91tin/jFWoZz7pcb7Tj5AjwmQN8gcUpfeUWdWCTujSOw JmDLRKpbZ7obm6Ga+9+H0= X-Received: by 2002:a17:90b:3b4d:b0:38e:7cb6:5c35 with SMTP id 98e67ed59e1d1-38e7cb65d63mr5164704a91.36.1784568831911; Mon, 20 Jul 2026 10:33:51 -0700 (PDT) Received: from [192.168.1.77] ([50.47.108.115]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38e46b2a4d4sm1675799a91.0.2026.07.20.10.33.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Jul 2026 10:33:51 -0700 (PDT) Message-ID: <4f264a16-bf79-4033-9196-603c168406e1@gmail.com> Date: Mon, 20 Jul 2026 10:33:50 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf v4 1/4] bpf: Fix potential UAF in bpf_netns_link_update_prog To: Pu Lehui , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Yonghong Song , Song Liu , Jiri Olsa , Emil Tsalapatis , Pu Lehui References: <20260720134547.1289964-1-pulehui@huaweicloud.com> <20260720134547.1289964-2-pulehui@huaweicloud.com> Content-Language: en-US From: Amery Hung In-Reply-To: <20260720134547.1289964-2-pulehui@huaweicloud.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 7/20/26 6:45 AM, Pu Lehui wrote: > From: Pu Lehui > > In bpf_netns_link_update_prog, the checks for old_prog and prog type > are currently performed locklessly before acquiring netns_bpf_mutex. > This creates a race condition that can lead to a UAF issue. > > If two threads concurrently execute BPF_LINK_UPDATE on the same netns > link, the following execution path can trigger a UAF: > > CPU0 CPU1 > bpf_netns_link_update_prog > if (old_prog && old_prog != link->prog) > return -EPERM; > bpf_netns_link_update_prog > if (old_prog && old_prog != link->prog) > ... > old_prog = xchg(&link->prog, new_prog); > bpf_prog_put(old_prog); > if (new_prog->type != link->prog->type) <-- trigger UAF > > Fix this by moving the old_prog and prog->type checks inside the > netns_bpf_mutex critical section. > > Fixes: 7f045a49fee0 ("bpf: Add link-based BPF program attachment to network namespace") > Reported-by: Sashiko > Signed-off-by: Pu Lehui Reviewed-by: Amery Hung > --- > kernel/bpf/net_namespace.c | 14 +++++++++----- > 1 file changed, 9 insertions(+), 5 deletions(-) > > diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c > index 25f30f9edaef..9fc62db1441c 100644 > --- a/kernel/bpf/net_namespace.c > +++ b/kernel/bpf/net_namespace.c > @@ -171,13 +171,17 @@ static int bpf_netns_link_update_prog(struct bpf_link *link, > struct net *net; > int idx, ret; > > - if (old_prog && old_prog != link->prog) > - return -EPERM; > - if (new_prog->type != link->prog->type) > - return -EINVAL; > - > mutex_lock(&netns_bpf_mutex); > > + if (old_prog && old_prog != link->prog) { > + ret = -EPERM; > + goto out_unlock; > + } > + if (new_prog->type != link->prog->type) { > + ret = -EINVAL; > + goto out_unlock; > + } > + > net = net_link->net; > if (!net || !check_net(net)) { > /* Link auto-detached or netns dying */