Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp357405rbe; Thu, 29 Feb 2024 01:21:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWclH7+iMKtkRYr1o6nE708ECmYMsmBymLgeftGov+h1R9z+Sy+QvztoTLvZ9vlYw4YhCPnwmM2cZVRv+nEACjoG8Shi0W4HnCQIqa2kQ== X-Google-Smtp-Source: AGHT+IHwmcEP01+X++5cJAP0mmKoxuDC1n0Fk+DChVkY/BuVJrUC8pKLlGOsFH1yCei9VpMaJeuX X-Received: by 2002:ac8:5852:0:b0:42e:703c:6ad1 with SMTP id h18-20020ac85852000000b0042e703c6ad1mr1732529qth.43.1709198478562; Thu, 29 Feb 2024 01:21:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709198478; cv=pass; d=google.com; s=arc-20160816; b=CH3F6wTiPVoy/darAaYPy6c5/ZwD9CbcvlK4htwgFfrxRVQRQo9kH0vLOx4zyQxRe7 BBvecF6+l8VLjaHKHuUHwqWpp5vLOkxnnP0M6+ztgMjF9Dyzx55IIBrHFm9YVLKo5HzU c3B2u5r2nqSEyU2GCXYlUoUqZ5bc0Qh+ksuRWV8BoSXxghuX+Em6EMj6I44vmCDRhjsr VVhwqSL9byCJ75tcMUAXz06McTf+3ciBTeOQOFL4ipkvSVJCtULZv88QWW/6ezUzK54+ ZSngwQerqrImjMp4n/VMD1QZxfkYOBF13QbNYDggdAhWuxobnmFNvxrX1oYJGeU9NxEq BwGA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=UEeLGEekuGzSf5uElXsWKObA17/FsH/JeyK75VLgXNY=; fh=D/2HUQBFX15pLPXuhaHXIWuCFWpu9IrScpB8F8TszIM=; b=GmNxMyokeWvoa+d3/XeDBN8RrjmcYH+Dty7wVde3tajC1O8zhVi9baJjXRj5xCnU8w PNmRM0CwjiycFlI7UTE+g+u15HPHN8OlTmBYnFsDwPdo1kODC9eRcFwk8uFPYVkhC1wm L436J0KU1i/6ITVLz3df3OcdsMf4SqKWlpbro3ukn41F6cUjF7jaHFieK1LDyigtNuPf rCp4IFE4ey6/MNvkdcN+gTY2cVRmXEVmvbps94YSW91Jb1zIbifxgUulnbSrfmfJtGgy rrQ3a1LjmaKeL1uI7xHENfHMWNNWF+TxSX6eBhSIYUNl9wl+IScojHbz8nYshjb1pshQ 5gtw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=altlinux.org); spf=pass (google.com: domain of linux-kernel+bounces-86397-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86397-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id u10-20020a05622a14ca00b0042ea8a596fasi1007033qtx.171.2024.02.29.01.21.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 01:21:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86397-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=altlinux.org); spf=pass (google.com: domain of linux-kernel+bounces-86397-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86397-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A4E051C22FD3 for ; Thu, 29 Feb 2024 09:21:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05EC05A7AB; Thu, 29 Feb 2024 09:20:26 +0000 (UTC) Received: from air.basealt.ru (air.basealt.ru [194.107.17.39]) (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 EAEAE5A4D4; Thu, 29 Feb 2024 09:20:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.107.17.39 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709198425; cv=none; b=fvz7QMCerVq6grc+X6NISrKnzjePQjh5i3fe/o51/50MEwqQ0EGuX6eTDcF2KXzVRTJ3ggDbfMW9x9cMrd0WUBVUpGOM3NsBC7MjsCM6eDR/qt7hwWEueh3KbY5XyF5FQqFyrjgkycnDCAielkgA6LjYbRyLf3ymPTT6/0BQtPQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709198425; c=relaxed/simple; bh=4F/X5Z6QsULNfoSYXx+VhyVs0V627xYO/1kLOHJm/zU=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=buCX5YUqJZCIEUy109cdNxk+vF5wFqHllAvu5DEmFGRBGCqM13Gxz0Smc8F2g34MlYS89m2sPtmsV4GaTeb27cvzlIK/XTPPTx28nITGD14VIbSotbDH0KyMrBy7xpZP05P5Goxae/Byjj8WwX6oJUvRwr0HU8vTLauBKF88mXQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org; spf=pass smtp.mailfrom=altlinux.org; arc=none smtp.client-ip=194.107.17.39 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=altlinux.org Received: by air.basealt.ru (Postfix, from userid 490) id AF09C2F2025F; Thu, 29 Feb 2024 09:20:20 +0000 (UTC) X-Spam-Level: Received: from [10.88.144.178] (obninsk.basealt.ru [217.15.195.17]) by air.basealt.ru (Postfix) with ESMTPSA id A8BA72F20251; Thu, 29 Feb 2024 09:20:18 +0000 (UTC) Message-ID: <077c8417-03bf-8d61-5d3a-0aef91b55659@basealt.ru> Date: Thu, 29 Feb 2024 12:20:18 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH net] gtp: fix use-after-free and null-ptr-deref in gtp_newlink() Content-Language: en-US To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20240228114703.465107-1-oficerovas@altlinux.org> From: Vasiliy Kovalev In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, 29.02.2024 02:51, Pablo Neira Ayuso wrote: > On Wed, Feb 28, 2024 at 02:47:03PM +0300, Alexander Ofitserov wrote: >> The gtp_link_ops operations structure for the subsystem must be >> registered after registering the gtp_net_ops pernet operations structure. > > A fix for this was already applied, see: > > commit 136cfaca22567a03bbb3bf53a43d8cb5748b80ec > Author: Vasiliy Kovalev > Date: Wed Feb 14 19:27:33 2024 +0300 > > gtp: fix use-after-free and null-ptr-deref in gtp_genl_dump_pdp() > >> diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c >> index 2129ae42c7030..0ddec4cc84093 100644 >> --- a/drivers/net/gtp.c >> +++ b/drivers/net/gtp.c >> @@ -1903,26 +1903,26 @@ static int __init gtp_init(void) >> >> get_random_bytes(>p_h_initval, sizeof(gtp_h_initval)); >> >> - err = rtnl_link_register(>p_link_ops); >> + err = register_pernet_subsys(>p_net_ops); >> if (err < 0) >> goto error_out; > > BTW, I like that this calls register_pernet_subsys() before > rtnl_link_register(), where a rtnetlink request could come before > pernet is set up. > >> - err = register_pernet_subsys(>p_net_ops); >> + err = rtnl_link_register(>p_link_ops); >> if (err < 0) >> - goto unreg_rtnl_link; >> + goto unreg_pernet_subsys; >> >> err = genl_register_family(>p_genl_family); >> if (err < 0) >> - goto unreg_pernet_subsys; >> + goto unreg_rtnl_link; >> >> pr_info("GTP module loaded (pdp ctx size %zd bytes)\n", >> sizeof(struct pdp_ctx)); >> return 0; >> >> -unreg_pernet_subsys: >> - unregister_pernet_subsys(>p_net_ops); >> unreg_rtnl_link: >> rtnl_link_unregister(>p_link_ops); >> +unreg_pernet_subsys: >> + unregister_pernet_subsys(>p_net_ops); >> error_out: >> pr_err("error loading GTP module loaded\n"); >> return err; >> -- >> 2.42.1 >> >> This patch fixes another problem, but a similar one, since the sequence is incorrect when registering subsystems. Initially, the registration sequence in the gtp module was as follows: 1) rtnl_link_register(); 2) genl_register_family(); 3) register_pernet_subsys(); During debugging of the module, when starting the syzkaller reproducer, it turned out that after genl_register_family() (2), without waiting for register_pernet_subsys()(3), the .dumpit event is triggered, in which the data of the unregistered pernet subsystem is accessed. That is, the bug was fixed by the commit 136cfaca2256 ("gtp: fix use-after-free and null-ptr-deref in gtp_genl_dump_pdp()") [1] and the registration sequence became as follows: 1) rtnl_link_register(); 2) register_pernet_subsys(); 3) genl_register_family(); However, syzkaller has discovered another problem: after registering rtnl_link_register, the .newlink event is triggered, in which the data of the unregistered pernet subsystem is accessed. This problem is reproducible on current stable kernels and the latest upstream kernel 6.8-rc6, in which the patch 136cfaca2256 [1] is applied. Therefore, the correct sequence should be as follows: 1) register_pernet_subsys(); 2) rtnl_link_register(); 3) genl_register_family(); The proposed patch is developed on top of the commit changes [1], does not conflict with it and fixes the described bug. [1] https://lore.kernel.org/lkml/20240220160434.29bcaf43@kernel.org/T/#mb1f72c2ad57b7ea6d47333e8616beccf8bce0e23 -- Regards, Vasiliy Kovalev