Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1019790rdb; Wed, 6 Dec 2023 06:39:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEnnl1b034yx5MFJPHDs6ut4/8I24WuNaJQGe9fTO3Ru6JMgWEx2kLsP4ejHb5foNtiUB+T X-Received: by 2002:a05:6a00:2999:b0:6cb:8995:9028 with SMTP id cj25-20020a056a00299900b006cb89959028mr785398pfb.19.1701873587038; Wed, 06 Dec 2023 06:39:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701873587; cv=none; d=google.com; s=arc-20160816; b=J7Ps0rPWUBvJwKBffALwZpGvuNyooP21Qf3RP3s2IPYvnZNbFDwGsacUnFHxCaaWQM 925NX8WIvnFDisaCS1EWwhQ/syoh9vD0PPxwQmrtteHg4nPGdSVwMtE1GlGCN865nCKt YyNctj74HKeMBIo4z9NVVbRCYLI9qrQaLYREcqypa0FDJQIOoo1HuoFCKSDeNzVHT+BG F+6SQEg7ArtvkTZR1q/tgb+BalPsc5CRVpPtNZmk4xjtFD+Rd/m/IRMxp643wdVF9iOd WT42z6Em8cPAyJos+aPMRwESoT8nzHWyI+XGi6EXPV78fIQWSgh5417o8DzRZQGgn8W9 i2Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=tW4rpGHQhN9YNVTV5oiqqcaigarX5iDMhMQO+9Q6sBI=; fh=OrccZmYyfjkZcQeKVItpBjeo/RJKLOjtnZh75sJNuRg=; b=RCZA+kSqTZGEtQHWUayKkwUXTtsNmLhNG5en5JmyKwYTO6k7FHcYunklXSS4oIVwdm eDPdiaGV9k9p7aABVFMuAhxNUmq7FuvJedAO4tf3JZo0TmjVmarpfiFYXH4qoT+gsHSZ WAdDQMM/LicZuvHhEVstUdDthgw4Y2eN4loYTUe4bnAvSXlMKFLm5jATCpW/fZz9Df/R omdhdgX2jt83zloGT2IJC6/viBlR+7nf2HBEyetLe/EJuVPgN674lbgXJ+FsGr0gLtGd 6/JW5O5PIx51MFBCJ2KMZNCjZwRJnhG1KbqJ36QTbWs6O58phyGlxme7aHC2Geh+PJid ajxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0kvVHlQk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u20-20020a056a00159400b006ce789c5378si34475pfk.373.2023.12.06.06.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 06:39:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=0kvVHlQk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 6B60A80C3A9B; Wed, 6 Dec 2023 06:39:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378974AbjLFOjZ (ORCPT + 99 others); Wed, 6 Dec 2023 09:39:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378970AbjLFOjY (ORCPT ); Wed, 6 Dec 2023 09:39:24 -0500 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A844DD5B for ; Wed, 6 Dec 2023 06:39:30 -0800 (PST) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-54c77d011acso9450a12.1 for ; Wed, 06 Dec 2023 06:39:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701873569; x=1702478369; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=tW4rpGHQhN9YNVTV5oiqqcaigarX5iDMhMQO+9Q6sBI=; b=0kvVHlQkWbFbLJ/N6mDI4+2NwPNiFc1a1bqcH8YyRBUQQnRM33ptdGgPouOmKzj+vM 31mSR5wENDfbI9HdVmVFXn10nAvx7T9AE8e0dl5QPz4CrYm1zQ80eoKbeir1dM6feypY RMuX71mOq0zXtYG7YtLaFvqKaAiwza/AYqltw8gL2Pi4bvaBbslUEQdMHDHjTuKs6R73 XpixfnL86MnfuWBppF9Whr77kuDbZnn37OTaWF9QE8B+gQs+YMCJVnaDOqfZLaukZ/RJ FgxefBeVtvx4Vrr/bKufL1uayeOXrVHP5IUvuKhoqk3W79hoThx3IDTJl0zPa8QZTcJw zlDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701873569; x=1702478369; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tW4rpGHQhN9YNVTV5oiqqcaigarX5iDMhMQO+9Q6sBI=; b=raGxpx0tQLw8GP+t/2qN8P/xiPJXLfjUAK42No8VTZnoYCjOLk42x21XU/vA6IpanV NiCMrGkBxPnfsHuN7xtJE2av+vcsLaUv8vymKdZaZl16zt4+nIQFHYs6SKRtNPgwpmC/ I15Iy3gpEh1hJ3haI1KO4tY5A7RkItojfsIZAJXR0g5jjnivV/GYpkPc3v0I/UlaRDBl Aehqt4WubgXF+UvPLaKDakjcx3gvUgcYd7YlDOgej/aaHftx7bZMyad1ja/Eb0oAaxs5 5u8rDxyMIQXE0L7jbq96RbOLJoT0eeCDDDpjpQc6hebMapEHtL5k04h1QDN/ydDV8u0H Eu0Q== X-Gm-Message-State: AOJu0Yz41x5yaiAdmauXolPd1yo1OepbCrSk4xxala9KVx5kEdiwj6pp kGcaOIe+zjiLvV5+El6hWQCJhRJFaj5LBq/XSoQqxA== X-Received: by 2002:a50:c35d:0:b0:54c:79ed:a018 with SMTP id q29-20020a50c35d000000b0054c79eda018mr96470edb.2.1701873568956; Wed, 06 Dec 2023 06:39:28 -0800 (PST) MIME-Version: 1.0 References: <20231206-refinanzieren-werkhalle-22db5334f256@brauner> In-Reply-To: <20231206-refinanzieren-werkhalle-22db5334f256@brauner> From: Jann Horn Date: Wed, 6 Dec 2023 15:38:50 +0100 Message-ID: Subject: Re: Is xt_owner's owner_mt() racy with sock_orphan()? [worse with new TYPESAFE_BY_RCU file lifetime?] To: Christian Brauner Cc: Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , netfilter-devel , coreteam@netfilter.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Network Development , kernel list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 06 Dec 2023 06:39:44 -0800 (PST) On Wed, Dec 6, 2023 at 2:58=E2=80=AFPM Christian Brauner wrote: > > On Tue, Dec 05, 2023 at 06:08:29PM +0100, Jann Horn wrote: > > On Tue, Dec 5, 2023 at 5:40=E2=80=AFPM Jann Horn wro= te: > > > > > > Hi! > > > > > > I think this code is racy, but testing that seems like a pain... > > > > > > owner_mt() in xt_owner runs in context of a NF_INET_LOCAL_OUT or > > > NF_INET_POST_ROUTING hook. It first checks that sk->sk_socket is > > > non-NULL, then checks that sk->sk_socket->file is non-NULL, then > > > accesses the ->f_cred of that file. > > > > > > I don't see anything that protects this against a concurrent > > > sock_orphan(), which NULLs out the sk->sk_socket pointer, if we're in > > > > Ah, and all the other users of ->sk_socket in net/netfilter/ do it > > under the sk_callback_lock... so I guess the fix would be to add the > > same in owner_mt? > > In your other mail you wrote: > > > I also think we have no guarantee here that the socket's ->file won't > > go away due to a concurrent __sock_release(), which could cause us to > > continue reading file credentials out of a file whose refcount has > > already dropped to zero? > > Is this an independent worry or can the concurrent __sock_release() > issue only happen due to a sock_orphan() having happened first? I think > that it requires a sock_orphan() having happend, presumably because the > socket gets marked SOCK_DEAD and can thus be released via > __sock_release() asynchronously? > > If so then taking sk_callback_lock() in owner_mt() should fix this. > (Otherwise we might need an additional get_active_file() on > sk->sk_socker->file in owner_mt() in addition to the other fix.) My understanding is that it could only happen due to a sock_orphan() having happened first, and so just sk_callback_lock() should probably be a sufficient fix. (I'm not an expert on net subsystem locking rules though.)