From patchwork Tue Jul 2 16:04:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 13719984 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 C11A116131A for ; Tue, 2 Jul 2024 16:06:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719936368; cv=none; b=Vdln1gf0cZr177VEoI8aMN91mkbIG0iyFF1S/9DmXSHD0HefbHPOt+2JK4tGj+DHu1eIO6MyHFd04+ZwboRtx7IuWMtuUcPlTR7VxrUOnP90hQ/MSpVWVaZCoTaoCOrKLPprKCitHlIA8uEBgqlvGijT77YeXLseb5mBi3AJSxw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719936368; c=relaxed/simple; bh=4dzETbi9Y/b084FicibKmVea7ZM5e5Sg03XoJbguRug=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ms+Brxno6VA/SCvC7W3ltO4jQaAO1aYHvJZdw3q2WH05peUgGcYvIkNYYRWDoa7S5ncoaJXc1m7+sTicK121YlQ+8lqZRb2STdElSYcTolqDaZ3BKGIVDUofMhqGrw4U5sKopFrhTv31hDqZysZa3Xx7GfFszlFhjyYh1gDhaTU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=SqK076EE; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SqK076EE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719936365; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Mjj83sWED4E39rMUhKU3CbXImDWJ5adzZW0P/Iuo6cg=; b=SqK076EEOEsKc9nBgKIsYY/7//1Qd5tJ/P1pdusfvKw1RyakKzTdeKuA7DWOv+OjJ4bUEe FIw+XZ4kDw9BdUpMrp2RsX1Pp8AUHx4QkrM7ymY77pcmyi5VaeAb3Ever3YAa6jtkj/ee6 DPMi8GZbqXX9xxuwZi/+SEq4XVT09qU= Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-318-nYBUGwRyPxa3M9xpWsOu8Q-1; Tue, 02 Jul 2024 12:06:04 -0400 X-MC-Unique: nYBUGwRyPxa3M9xpWsOu8Q-1 Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-704d99cb97aso2843258a12.0 for ; Tue, 02 Jul 2024 09:06:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719936363; x=1720541163; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Mjj83sWED4E39rMUhKU3CbXImDWJ5adzZW0P/Iuo6cg=; b=g27W9EIpXITJIx9w64UGg9eECSCPD+ie6yLyNvn7HqIjcmy4yCNYvL82UY5EuAWI37 1zOb5fUvggiG3A5XOer5hdMQUmVf0CqoVrQosgsuWY/ZmxBngSC8qTF4jboDxuYAlXHq 0SI8S7e7cvssS7FEtTwWDedDFVjFBvuZK60utGmLqUMSlaHnaVvMDuZGyMtVmuxvu/yv EU8TLMXtZR6miyXmU9NDNiY4z4GhP0MCCP8SysihWGGPfG2Dqr4unyYQaIKingwirBMJ CkrUyD5JCrJ4Gfln8rFwiVXFxOJl1GaxvJc/i76BMEeNZitJNWyqrvboCaxziljzscJk n9VQ== X-Forwarded-Encrypted: i=1; AJvYcCUfSFz9FscqYpCEKnC/2C3kPlJJ/c+JWtDGythzrKbTBFu7y7FXnWbptraDL6llQNszqO8ac2fOxfaxizPW8wS5c5Ul2nPb X-Gm-Message-State: AOJu0YxYGgsYovlvfKGnrQSRLUe8nWhL58mF6FYtna7ZF9Ixv+Gw7ng5 21Ts+BODnYMgASMzbgTAR4rlHjJrkwLB4KdGRbUxVgsH09I79pKj1tkih760rOoh6FFykA5wPNX 4ErKOO3jXMS1xzTxWMClqsRJRXUTakq3DXfhJXqxnCiKpL71xo6RlAA== X-Received: by 2002:a17:902:f545:b0:1f7:11c8:bdd3 with SMTP id d9443c01a7336-1fadbc95462mr61872635ad.29.1719936363107; Tue, 02 Jul 2024 09:06:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFdwWP/5Xu3XpRWOahLnmgTsWWlEmtfBhnw9Oooenlhz1CzdwiplFFIvm2iazLql9A9mmRvUw== X-Received: by 2002:a17:902:f545:b0:1f7:11c8:bdd3 with SMTP id d9443c01a7336-1fadbc95462mr61872425ad.29.1719936362728; Tue, 02 Jul 2024 09:06:02 -0700 (PDT) Received: from ryzen.. ([240d:1a:c0d:9f00:ca7f:54ff:fe01:979d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac156903bsm85565195ad.208.2024.07.02.09.06.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jul 2024 09:06:02 -0700 (PDT) From: Shigeru Yoshida To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: kuniyu@amazon.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Shigeru Yoshida , syzkaller Subject: [PATCH net 1/2] af_unix: Fix uninit-value in __unix_walk_scc() Date: Wed, 3 Jul 2024 01:04:27 +0900 Message-ID: <20240702160428.10153-1-syoshida@redhat.com> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org KMSAN reported uninit-value access in __unix_walk_scc() [1]. In the list_for_each_entry_reverse() loop, when the vertex's index equals it's scc_index, the loop uses the variable vertex as a temporary variable that points to a vertex in scc. And when the loop is finished, the variable vertex points to the list head, in this case scc, which is a local variable on the stack (more precisely, it's not even scc and might underflow the call stack of __unix_walk_scc(): container_of(&scc, struct unix_vertex, scc_entry)). However, the variable vertex is used under the label prev_vertex. So if the edge_stack is not empty and the function jumps to the prev_vertex label, the function will access invalid data on the stack. This causes the uninit-value access issue. Fix this by introducing a new temporary variable for the loop. [1] BUG: KMSAN: uninit-value in __unix_walk_scc net/unix/garbage.c:478 [inline] BUG: KMSAN: uninit-value in unix_walk_scc net/unix/garbage.c:526 [inline] BUG: KMSAN: uninit-value in __unix_gc+0x2589/0x3c20 net/unix/garbage.c:584 __unix_walk_scc net/unix/garbage.c:478 [inline] unix_walk_scc net/unix/garbage.c:526 [inline] __unix_gc+0x2589/0x3c20 net/unix/garbage.c:584 process_one_work kernel/workqueue.c:3231 [inline] process_scheduled_works+0xade/0x1bf0 kernel/workqueue.c:3312 worker_thread+0xeb6/0x15b0 kernel/workqueue.c:3393 kthread+0x3c4/0x530 kernel/kthread.c:389 ret_from_fork+0x6e/0x90 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 Uninit was stored to memory at: unix_walk_scc net/unix/garbage.c:526 [inline] __unix_gc+0x2adf/0x3c20 net/unix/garbage.c:584 process_one_work kernel/workqueue.c:3231 [inline] process_scheduled_works+0xade/0x1bf0 kernel/workqueue.c:3312 worker_thread+0xeb6/0x15b0 kernel/workqueue.c:3393 kthread+0x3c4/0x530 kernel/kthread.c:389 ret_from_fork+0x6e/0x90 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 Local variable entries created at: ref_tracker_free+0x48/0xf30 lib/ref_tracker.c:222 netdev_tracker_free include/linux/netdevice.h:4058 [inline] netdev_put include/linux/netdevice.h:4075 [inline] dev_put include/linux/netdevice.h:4101 [inline] update_gid_event_work_handler+0xaa/0x1b0 drivers/infiniband/core/roce_gid_mgmt.c:813 CPU: 1 PID: 12763 Comm: kworker/u8:31 Not tainted 6.10.0-rc4-00217-g35bb670d65fc #32 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-2.fc40 04/01/2014 Workqueue: events_unbound __unix_gc Fixes: 3484f063172d ("af_unix: Detect Strongly Connected Components.") Reported-by: syzkaller Signed-off-by: Shigeru Yoshida Reviewed-by: Kuniyuki Iwashima --- v1->v2: https://lore.kernel.org/all/20240625152713.1147650-1-syoshida@redhat.com/ - A bit of elaboration on the commit message, as suggested by Iwashima-san. - Bundle a selftest written by Iwashima-san. --- net/unix/garbage.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/net/unix/garbage.c b/net/unix/garbage.c index dfe94a90ece4..23efb78fe9ef 100644 --- a/net/unix/garbage.c +++ b/net/unix/garbage.c @@ -476,6 +476,7 @@ static void __unix_walk_scc(struct unix_vertex *vertex, unsigned long *last_inde } if (vertex->index == vertex->scc_index) { + struct unix_vertex *v; struct list_head scc; bool scc_dead = true; @@ -486,15 +487,15 @@ static void __unix_walk_scc(struct unix_vertex *vertex, unsigned long *last_inde */ __list_cut_position(&scc, &vertex_stack, &vertex->scc_entry); - list_for_each_entry_reverse(vertex, &scc, scc_entry) { + list_for_each_entry_reverse(v, &scc, scc_entry) { /* Don't restart DFS from this vertex in unix_walk_scc(). */ - list_move_tail(&vertex->entry, &unix_visited_vertices); + list_move_tail(&v->entry, &unix_visited_vertices); /* Mark vertex as off-stack. */ - vertex->index = unix_vertex_grouped_index; + v->index = unix_vertex_grouped_index; if (scc_dead) - scc_dead = unix_vertex_dead(vertex); + scc_dead = unix_vertex_dead(v); } if (scc_dead) From patchwork Tue Jul 2 16:04:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 13719985 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 5BDEF14D42C for ; Tue, 2 Jul 2024 16:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719936376; cv=none; b=ckiBmsW+5iFsjE+ZRqrAcxdGNxeeyBza+PMFI4LXnxmDgV0L1jJvibBw5r8Y4Wp26ct8xF+cSSuXnZkSN0SLaCfeO0Qpztzclp63RKh0IDeX+8mWSwvi8oVFFzu4PtNNX45Kk1oyFfGHznFFjKSzheI7MOB4FwX25HZ1U7Tz8kk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719936376; c=relaxed/simple; bh=6bfthXh+z3FlLnjiFbGZ9DImdUQg6HnLJ/s7QzPwJlM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gDlKFYFNwVfuXuIXU/NPQxqOxZLpea1t0Auj9xtyXFzBBQr17fEI1+OI+Tbci+vsfE+zEjnEbo7+q5lJxN4He83b3WGKY1nB4AVw+sXsVvnNAV9BceU4Cro51I4Fp9txRT1DxEVmzHkGns+dxhLccvH0eCphHgRBcMS2+9VQz88= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Um5YQS4H; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Um5YQS4H" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719936372; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fhomTckPXydN9WWs1SvlxsuDB1WwN1ndD9L0d+zK7Ww=; b=Um5YQS4HWhYrMZ5ouy+mHg2XX6Bz5eXfXF3bA7Sz87zh1EYMdTT3pyLkYY35sx5Qt5aLJt J96iIwsdIWTSX38ulnVdcCLZZ3O+Q+s4Ypi/qFes776km1JuHYboEwfBbfI1PI9hTJnhWt iGrFE4Rwksf7JmeLZUWoaiZUDBz4Cu0= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-_5QD0MF9M3WS45qh75iWkw-1; Tue, 02 Jul 2024 12:06:10 -0400 X-MC-Unique: _5QD0MF9M3WS45qh75iWkw-1 Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-1f6174d0421so17636235ad.2 for ; Tue, 02 Jul 2024 09:06:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719936370; x=1720541170; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fhomTckPXydN9WWs1SvlxsuDB1WwN1ndD9L0d+zK7Ww=; b=KePKZ1ou2ZPSO1Ze3qDH5YY9bzdwCFVjwL47oqUAf92R9qTLF5zSvHYAo2jr75HSpJ QdgGhQ0l8zGnxY0EqLDr73Zy5QI1WJ3Pf7df74xcxgvMUFk+5gN2VDG22p2XC5XvG3O2 7tXCZv22sfctwWuPONRk25yetXbip2iGBK5Zwd8ycMqZY3CHiTlwF+degbEoIdnqZ0Kc JB/IDsvktbV6B6hV3WOPa1+R5qy/42v3qapXl23Ya7LKTqmkx/3GPhFTSHwFhowHFnuO xrSp8dPWrC+hGMKadBZAxLFTumKiwowWCbKzreTbu3yuh+0GaT061DJ2w4+7f0GR/ivU esSw== X-Forwarded-Encrypted: i=1; AJvYcCUc3KAouZJ7Bga9y7rTf9E+qxHHin+7MydpUp0DAY6id8n6pFzrAUMbTIElnGwphYabOk+bERJpAwIz7Ak/FPh2hXdL+zlJ X-Gm-Message-State: AOJu0Yyk6Ec1sET9EYqhUH/1Mpt1May06Huaxlv7mtNqupHgWoViPx8E qlLqrcw01WyNMPMOYg2kjwaVfhsqMkbcyZeKsvAu2c0nDoLcmvMtwPrCQ1YFykvcP3Q9ehgyqhr D6Kswc8Drcgx+hAbnOkWb0hLXSM4TZE4tGEzp5qkoU7bWOYocOBno7w== X-Received: by 2002:a17:902:cec2:b0:1fa:ab4a:faea with SMTP id d9443c01a7336-1fadbce9d6fmr65165375ad.43.1719936369828; Tue, 02 Jul 2024 09:06:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEotUuIOqN777VHgCHNLN6WhtC1Qu3pqhNm81uCxVDmVMCvhU4/9Nmd7+85UBA5eY+UeYtrPg== X-Received: by 2002:a17:902:cec2:b0:1fa:ab4a:faea with SMTP id d9443c01a7336-1fadbce9d6fmr65165115ad.43.1719936369422; Tue, 02 Jul 2024 09:06:09 -0700 (PDT) Received: from ryzen.. ([240d:1a:c0d:9f00:ca7f:54ff:fe01:979d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac156903bsm85565195ad.208.2024.07.02.09.06.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jul 2024 09:06:09 -0700 (PDT) From: Shigeru Yoshida To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: kuniyu@amazon.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Shigeru Yoshida Subject: [PATCH net 2/2] selftest: af_unix: Add test case for backtrack after finalising SCC. Date: Wed, 3 Jul 2024 01:04:28 +0900 Message-ID: <20240702160428.10153-2-syoshida@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240702160428.10153-1-syoshida@redhat.com> References: <20240702160428.10153-1-syoshida@redhat.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Kuniyuki Iwashima syzkaller reported a KMSAN splat in __unix_walk_scc() while backtracking edge_stack after finalising SCC. Let's add a test case exercising the path. Signed-off-by: Kuniyuki Iwashima Signed-off-by: Shigeru Yoshida diff --git a/tools/testing/selftests/net/af_unix/scm_rights.c b/tools/testing/selftests/net/af_unix/scm_rights.c index 2bfed46e0b19..d66336256580 100644 --- a/tools/testing/selftests/net/af_unix/scm_rights.c +++ b/tools/testing/selftests/net/af_unix/scm_rights.c @@ -14,12 +14,12 @@ FIXTURE(scm_rights) { - int fd[16]; + int fd[32]; }; FIXTURE_VARIANT(scm_rights) { - char name[16]; + char name[32]; int type; int flags; bool test_listener; @@ -172,6 +172,8 @@ static void __create_sockets(struct __test_metadata *_metadata, const FIXTURE_VARIANT(scm_rights) *variant, int n) { + ASSERT_LE(n * 2, sizeof(self->fd) / sizeof(self->fd[0])); + if (variant->test_listener) create_listeners(_metadata, self, n); else @@ -283,4 +285,23 @@ TEST_F(scm_rights, cross_edge) close_sockets(8); } +TEST_F(scm_rights, backtrack_from_scc) +{ + create_sockets(10); + + send_fd(0, 1); + send_fd(0, 4); + send_fd(1, 2); + send_fd(2, 3); + send_fd(3, 1); + + send_fd(5, 6); + send_fd(5, 9); + send_fd(6, 7); + send_fd(7, 8); + send_fd(8, 6); + + close_sockets(10); +} + TEST_HARNESS_MAIN