From patchwork Wed May 29 22:01:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bethany Jamison X-Patchwork-Id: 1941488 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VqNdh6nzSz20fB for ; Thu, 30 May 2024 08:02:00 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sCRMn-0006NN-Qa; Wed, 29 May 2024 22:01:53 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sCRMj-0006LK-N3 for kernel-team@lists.ubuntu.com; Wed, 29 May 2024 22:01:49 +0000 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 96D513F213 for ; Wed, 29 May 2024 22:01:49 +0000 (UTC) Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-792ff352462so35026585a.1 for ; Wed, 29 May 2024 15:01:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717020108; x=1717624908; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+UnSXVxBQ/cHd92krxtiVZ6T7uIoRUgeg14kxLHgb+o=; b=SFA9aK6iSmK7L01crCsTeU7GcpKpPOgqScVdDQYaBsOZibgcnrNmTTBcdy+FGXmrB3 /yfNIKcf2boOypYPC4b1EUpQt/th8KPCsohpndIvVI4QiPXsKb+201kicYgHp2OeF5UP mZSkSaTBHu9fhwcQShQtuxNffTR74zq37jcSp7eMYCBypSgHzwjuopdliTD/oBxJQET4 qula+g5quyEupYk4LET1mFHQ2S8yLnk2uPsGV65Ng0c8pN9xNyeDfklZ2kGBv/hJtQdR lDF+7pRzoF2ZuZ7LNR/FZY419QjrliwmWkzgLo5mP+uSg4Bmxh7l/Jjyn/TkT5tbAh35 edJQ== X-Gm-Message-State: AOJu0Yz+SJty8BvHrnvWmb+5mlAo0UxG/sojpcQ11OU1NPlO4MhEv861 0oXyxTKk43jyw6PCkS/ftIE5heyrAPRj30rWo8SLocUO6CixhfdfPrVcqQsGe8JqSPaJyPY4/Kf dOqE3sWWFmvd2wECfM5ZwgpKMC6VXihqVAnMGNunTF1seRzd4N/nPWU6as9Cm6zs+mdaRP7CDLj st387OPdrsCw== X-Received: by 2002:a05:620a:3727:b0:794:b6fc:c493 with SMTP id af79cd13be357-794e9e3b480mr53518785a.74.1717020108402; Wed, 29 May 2024 15:01:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHok7kqu2wlqHL7CcKQVwGuLts3JJBZQqdB+umgizO0u5EkhhfnVxif9SnVIWqxha6u8aB6Bg== X-Received: by 2002:a05:620a:3727:b0:794:b6fc:c493 with SMTP id af79cd13be357-794e9e3b480mr53515485a.74.1717020107803; Wed, 29 May 2024 15:01:47 -0700 (PDT) Received: from smtp.gmail.com (syn-047-024-050-157.res.spectrum.com. [47.24.50.157]) by smtp.gmail.com with ESMTPSA id af79cd13be357-794abd085ddsm505233185a.76.2024.05.29.15.01.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 15:01:47 -0700 (PDT) From: Bethany Jamison To: kernel-team@lists.ubuntu.com Subject: [SRU][M/F][PATCH 2/2] af_unix: Fix garbage collector racing against connect() Date: Wed, 29 May 2024 17:01:44 -0500 Message-Id: <20240529220144.42274-3-bethany.jamison@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529220144.42274-1-bethany.jamison@canonical.com> References: <20240529220144.42274-1-bethany.jamison@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Michal Luczaj Garbage collector does not take into account the risk of embryo getting enqueued during the garbage collection. If such embryo has a peer that carries SCM_RIGHTS, two consecutive passes of scan_children() may see a different set of children. Leading to an incorrectly elevated inflight count, and then a dangling pointer within the gc_inflight_list. sockets are AF_UNIX/SOCK_STREAM S is an unconnected socket L is a listening in-flight socket bound to addr, not in fdtable V's fd will be passed via sendmsg(), gets inflight count bumped connect(S, addr) sendmsg(S, [V]); close(V) __unix_gc() ---------------- ------------------------- ----------- NS = unix_create1() skb1 = sock_wmalloc(NS) L = unix_find_other(addr) unix_state_lock(L) unix_peer(S) = NS // V count=1 inflight=0 NS = unix_peer(S) skb2 = sock_alloc() skb_queue_tail(NS, skb2[V]) // V became in-flight // V count=2 inflight=1 close(V) // V count=1 inflight=1 // GC candidate condition met for u in gc_inflight_list: if (total_refs == inflight_refs) add u to gc_candidates // gc_candidates={L, V} for u in gc_candidates: scan_children(u, dec_inflight) // embryo (skb1) was not // reachable from L yet, so V's // inflight remains unchanged __skb_queue_tail(L, skb1) unix_state_unlock(L) for u in gc_candidates: if (u.inflight) scan_children(u, inc_inflight_move_tail) // V count=1 inflight=2 (!) If there is a GC-candidate listening socket, lock/unlock its state. This makes GC wait until the end of any ongoing connect() to that socket. After flipping the lock, a possibly SCM-laden embryo is already enqueued. And if there is another embryo coming, it can not possibly carry SCM_RIGHTS. At this point, unix_inflight() can not happen because unix_gc_lock is already taken. Inflight graph remains unaffected. Fixes: 1fd05ba5a2f2 ("[AF_UNIX]: Rewrite garbage collector, fixes race.") Signed-off-by: Michal Luczaj Reviewed-by: Kuniyuki Iwashima Link: https://lore.kernel.org/r/20240409201047.1032217-1-mhal@rbox.co Signed-off-by: Paolo Abeni (cherry picked from commit 47d8ac011fe1c9251070e1bd64cb10b48193ec51) CVE-2024-26923 Signed-off-by: Bethany Jamison --- net/unix/garbage.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/net/unix/garbage.c b/net/unix/garbage.c index 7abee5e48088f..de2c37149f721 100644 --- a/net/unix/garbage.c +++ b/net/unix/garbage.c @@ -235,11 +235,22 @@ void unix_gc(void) * receive queues. Other, non candidate sockets _can_ be * added to queue, so we must make sure only to touch * candidates. + * + * Embryos, though never candidates themselves, affect which + * candidates are reachable by the garbage collector. Before + * being added to a listener's queue, an embryo may already + * receive data carrying SCM_RIGHTS, potentially making the + * passed socket a candidate that is not yet reachable by the + * collector. It becomes reachable once the embryo is + * enqueued. Therefore, we must ensure that no SCM-laden + * embryo appears in a (candidate) listener's queue between + * consecutive scan_children() calls. */ list_for_each_entry_safe(u, next, &gc_inflight_list, link) { + struct sock *sk = &u->sk; long total_refs; - total_refs = file_count(u->sk.sk_socket->file); + total_refs = file_count(sk->sk_socket->file); BUG_ON(!u->inflight); BUG_ON(total_refs < u->inflight); @@ -247,6 +258,11 @@ void unix_gc(void) list_move_tail(&u->link, &gc_candidates); __set_bit(UNIX_GC_CANDIDATE, &u->gc_flags); __set_bit(UNIX_GC_MAYBE_CYCLE, &u->gc_flags); + + if (sk->sk_state == TCP_LISTEN) { + unix_state_lock(sk); + unix_state_unlock(sk); + } } }