From patchwork Tue Jun 4 23:43:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13685983 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (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 8D51E14D2BF for ; Tue, 4 Jun 2024 23:44:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544653; cv=none; b=dXj5cbjhCR7X0I0XhrhFM+CZOdsqENe/gbD0FLtNZR/rB6Ul0EG4ejmZByH+QXOPgYQtuep1+i2OGKYZZXN/WfT0+7F20yYhpNad+bXWCR20yJwhf2p9NXIjwlMmn6vq5tWgVdl1NoNKvDEe9Ygv6S0OjhUkaLVxb12YL08S0lk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544653; c=relaxed/simple; bh=lby9aJvZO2Maf4mTJKsdbfcgNIMFEVRB5P9sCbNI7QE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f9qyatR1rmT+sZsBpZ6SL/Jjp20eOWiwnQuegy83MW/4avRW+SeXW8Puyd1AYfS4KPM8ETrjyWwfUoOH+x0orjgxFlH/QjTN8dW7HPPB8ISUNE43+Q8IYWEdqfhiYfadujRWPac8scGw+J5eY9Re6ciIIiVI+d3foGdd2ONWmw8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=EmzaZhpC; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=EmzaZhpC; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="EmzaZhpC"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="EmzaZhpC" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 7657021964; Tue, 4 Jun 2024 23:44:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544649; h=from:from:reply-to: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=sUbPt4i9aiBseOCv1tcZRBoWFGpBswL0oJhqfeMjZSw=; b=EmzaZhpCDY3TBAXT98+hBIAIA/sgGOvR2q/3/HjtiVf+A1LA3QT7UmEQfBit3wJ9HKyN2T LNteMDGWWj3Az3P345L5EyizY3GRUDSEoSPcziujdW7hSb0/BdbUVafaR9gorCG7J2JMt1 EgbLK8VMelmpicz+5kAOpDGY7yj6OZA= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=EmzaZhpC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544649; h=from:from:reply-to: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=sUbPt4i9aiBseOCv1tcZRBoWFGpBswL0oJhqfeMjZSw=; b=EmzaZhpCDY3TBAXT98+hBIAIA/sgGOvR2q/3/HjtiVf+A1LA3QT7UmEQfBit3wJ9HKyN2T LNteMDGWWj3Az3P345L5EyizY3GRUDSEoSPcziujdW7hSb0/BdbUVafaR9gorCG7J2JMt1 EgbLK8VMelmpicz+5kAOpDGY7yj6OZA= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 3858F13A93; Tue, 4 Jun 2024 23:44:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id oOwdN8emX2bcJwAAD6G6ig (envelope-from ); Tue, 04 Jun 2024 23:44:07 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: Josef Bacik Subject: [PATCH v2 1/4] btrfs-progs: corrupt-block: fix memory leak in debug_corrupt_sector() Date: Wed, 5 Jun 2024 09:13:41 +0930 Message-ID: <3d9a9ecd46165c18f4ccd15f4e7aad489343dabe.1717544015.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Level: X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DWL_DNSWL_BLOCKED(0.00)[suse.com:dkim]; RCPT_COUNT_TWO(0.00)[2]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.com:+] X-Rspamd-Action: no action X-Rspamd-Queue-Id: 7657021964 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Flag: NO X-Spam-Score: -3.01 ASAN build (make D=asan) would cause memory leak for btrfs-corrupt-block inside debug_corrupt_sector(). This can be reproduced by fsck/013 test case. The cause is pretty simple, we just malloc a sector and forgot to free it. Issue: #806 Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo --- btrfs-corrupt-block.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 124597333771..e88319891910 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -70,7 +70,7 @@ static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror if (ret < 0) { errno = -ret; error("cannot read bytenr %llu: %m", logical); - return ret; + goto out; } printf("corrupting %llu copy %d\n", logical, mirror_num); memset(buf, 0, sectorsize); @@ -78,7 +78,7 @@ static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror if (ret < 0) { errno = -ret; error("cannot write bytenr %llu: %m", logical); - return ret; + goto out; } } @@ -90,7 +90,8 @@ static int debug_corrupt_sector(struct btrfs_root *root, u64 logical, int mirror if (mirror_num > num_copies) break; } - +out: + free(buf); return 0; } From patchwork Tue Jun 4 23:43:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13685985 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (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 CF8C514D703 for ; Tue, 4 Jun 2024 23:44:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544658; cv=none; b=miEsjVoZxd2m1EFMeH0PZP4RI50VXP38kESychVZSDTZ6doeTviiKBmWA9WgdNY7CuvtKuRskfj2nxOef+rJgi2lFhzkNFSo+jd4BbApRoelvgh07SfO3YUL1bK0Yp+BX+SGd3byASUSffmUcu0nLUprhiQwY+F6X52ynfeIttc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544658; c=relaxed/simple; bh=PZ2HDVSZ4VyYwCx3VZhoCHK401SnUK5LxvjLJMs73M4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VUsGjrPf7Ik2jUhJSKbw+Rp3NlVekTs8i2pkWcWR7JTjanP/T3xZXuSxOh5aHUZopIGppTsx0enbcaVzWrvAUcKGxZqtv7I61SduCct+opsncbZsQfYgheOd8RmqAD4u+HGGasCDEq+o+b9sl3onSNIVQi1rd3lcHiy9n/PZBnY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=NVdi/42f; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=NVdi/42f; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="NVdi/42f"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="NVdi/42f" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0493D219B4 for ; Tue, 4 Jun 2024 23:44:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544651; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6kvOLK3RdeCOYkMxQh3BqwinpGm3EViPpx7E5ZPvm9U=; b=NVdi/42f4SgnJ37kQWSkXw3t8u8c/Oi+lklJs14fZwXPKQW1BuB3hHXoNIp7Xmis97jvRW Os3LkrcaECh0H2lxmswoG8V1+Wp7hDvO6OWPCdm6JrPPes2nyxLf+8FIUph/cayTLR24QQ 1rC24GuSuBMKdWIlGfEunvL3YrctKZo= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b="NVdi/42f" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544651; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6kvOLK3RdeCOYkMxQh3BqwinpGm3EViPpx7E5ZPvm9U=; b=NVdi/42f4SgnJ37kQWSkXw3t8u8c/Oi+lklJs14fZwXPKQW1BuB3hHXoNIp7Xmis97jvRW Os3LkrcaECh0H2lxmswoG8V1+Wp7hDvO6OWPCdm6JrPPes2nyxLf+8FIUph/cayTLR24QQ 1rC24GuSuBMKdWIlGfEunvL3YrctKZo= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 119BC13A93 for ; Tue, 4 Jun 2024 23:44:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id iIepLcmmX2bcJwAAD6G6ig (envelope-from ) for ; Tue, 04 Jun 2024 23:44:09 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 2/4] btrfs-progs: print-tree: do sanity checks for dir items Date: Wed, 5 Jun 2024 09:13:42 +0930 Message-ID: <817cdc0c65b00491a78d7f47efddffa1f76ab087.1717544015.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Flag: NO X-Spam-Score: -3.01 X-Rspamd-Action: no action X-Rspamd-Queue-Id: 0493D219B4 X-Spam-Level: X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; RCVD_TLS_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_DN_NONE(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; DKIM_TRACE(0.00)[suse.com:+] There is a bug report that with UBSAN enabled, fuzz/006 test case would crash. It turns out that the image bko-154021-invalid-drop-level.raw has invalid dir items, that the name/data len is beyond the item. And if we try to read beyond the eb boundary, UBSAN got triggered. Normally in kernel tree-checker would reject such metadata in the first place, but in btrfs-progs we can not go that strict or we can not do a lot of repair. So here just enhance print_dir_item() to do extra sanity checks for data/name len before reading the contents. Issue: #805 Signed-off-by: Qu Wenruo --- kernel-shared/print-tree.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel-shared/print-tree.c b/kernel-shared/print-tree.c index 1b9386d87a0a..9a72ba39b426 100644 --- a/kernel-shared/print-tree.c +++ b/kernel-shared/print-tree.c @@ -78,6 +78,11 @@ static void print_dir_item(struct extent_buffer *eb, u32 size, printf("\n"); name_len = btrfs_dir_name_len(eb, di); data_len = btrfs_dir_data_len(eb, di); + if (data_len + name_len + cur > size) { + error("invalid length, cur=%u name_len=%u data_len=%u size=%u\n", + cur, name_len, data_len, size); + break; + } len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf); printf("\t\ttransid %llu data_len %u name_len %u\n", btrfs_dir_transid(eb, di), From patchwork Tue Jun 4 23:43:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13685984 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 9364114D2BF for ; Tue, 4 Jun 2024 23:44:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544657; cv=none; b=R5JtSc7DckzSqvYradGvZ5TwiiSjUOS2LaNZTtLX1a6lUsQwGnquri35yvxARuviJ2vftfrgEh7BjSwMkUPqSwFm/ci05WTb1c551yCgsyjW/KQ5oNijqB/NIQkJb6LzUf+QV7ydK5UL4bCQlstLE/j1dV9duGdGqhxwPKSxp1o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544657; c=relaxed/simple; bh=B2JZhXVX82KqO2IC4D3ehd5lWfbo5WqzaTS53eSfTO0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nkWnpiNo5eMskWuupnf7cJi7LCfAjBYvK9f/T2Q0UGCrkhnNkBOobFGZ9hi7FTZt4JF5M5OTZvvIv/j1N3D3/PvxMBXyfXp6HghP+Ly+lwvwGWOv106wXcU4zs8h2Hb6bq0/DcrmmaMA7wp2qeOYCeGbYvzlO9gMSmc3Yhcdu9U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=SrSq+97f; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=SrSq+97f; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="SrSq+97f"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="SrSq+97f" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C9A661F45A; Tue, 4 Jun 2024 23:44:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544652; h=from:from:reply-to: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=MCgerXgamrnjEOuVzLLCP2y9jzWZfqiJhZiQOjxjWWw=; b=SrSq+97f4Li6LHIDSaAV0PgooGzfwo7ZyYtqOP+r1FrCg5Q4l6ffvk3GJLgaG0gXLtfLm4 FQilsuBOOY0qGf0R7tU3LCOs+9mn8igdoW9Z0/SFIqnuM16lgViHANd38AvHM7qAX82zjj LzmQQfycbbm0oRUQC0RYDsyrsGTxbx4= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544652; h=from:from:reply-to: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=MCgerXgamrnjEOuVzLLCP2y9jzWZfqiJhZiQOjxjWWw=; b=SrSq+97f4Li6LHIDSaAV0PgooGzfwo7ZyYtqOP+r1FrCg5Q4l6ffvk3GJLgaG0gXLtfLm4 FQilsuBOOY0qGf0R7tU3LCOs+9mn8igdoW9Z0/SFIqnuM16lgViHANd38AvHM7qAX82zjj LzmQQfycbbm0oRUQC0RYDsyrsGTxbx4= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 8D22813A93; Tue, 4 Jun 2024 23:44:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id sPQbEsumX2bcJwAAD6G6ig (envelope-from ); Tue, 04 Jun 2024 23:44:11 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: Josef Bacik Subject: [PATCH v2 3/4] btrfs-progs: error out immediately if an unknown backref type is hit Date: Wed, 5 Jun 2024 09:13:43 +0930 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Flag: NO X-Spam-Score: -1.90 X-Spam-Level: X-Spamd-Result: default: False [-1.90 / 50.00]; BAYES_HAM(-2.10)[95.61%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_TWO(0.00)[2]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email]; RCVD_TLS_ALL(0.00)[] There is a bug report that for fuzzed image bko-155621-bad-block-group-offset.raw, "btrfs check --mode=lowmem --repair" would lead to a deadloop. Unlike original mode, lowmem mode relies on the backref walk to properly go through each root, but unfortunately inside __add_inline_refs() we doesn't handle unknown backref types correctly, causing it never moving forward thus deadloop. Fix it by erroring out to prevent deadloop. Issue: #788 Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo --- kernel-shared/backref.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel-shared/backref.c b/kernel-shared/backref.c index 89ccf073fca7..f46f3267e144 100644 --- a/kernel-shared/backref.c +++ b/kernel-shared/backref.c @@ -650,7 +650,8 @@ static int __add_inline_refs(struct btrfs_fs_info *fs_info, break; } default: - WARN_ON(1); + error("invalid backref type: %u", type); + ret = -EUCLEAN; } if (ret) return ret; From patchwork Tue Jun 4 23:43:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13685986 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 1A01014E2C0 for ; Tue, 4 Jun 2024 23:44:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544658; cv=none; b=ZHXgCYU28IFmqVZXWF/qIUTchzXR9ve48xc7OGpIkb/LNhMtqTyiyowNafrxBKOhM3lJUE4QiCHh708dJnVU5dAE7T+M7FArnHGUeSlUldkPgjc+Ys9QrLxk3TQZ6P1Oyu5HpmYNYtI2+612gV/Ekgc8Wg8RzanteZY3X4kwbt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717544658; c=relaxed/simple; bh=AU0U8uXax637DNYZf5JFxjnMc97P0Q24tS0JZg5uh3A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T6Pvtw0hxDEms+9tevnz5T3x6C+pk3HXSrnffdWzf466n0huPp2zzDzOu/w4k464cUHiyoUaPOoF/JCtVPs2uRrXhoIgRsLI+gQZimGM6xzY5YTzfihPbJkuOyf5hV+gYx0cBgwaC6PtnCrDHL2jN1rfWlBEt4xUCyjZ3R9xz+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=T1oT1Bfc; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=T1oT1Bfc; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="T1oT1Bfc"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="T1oT1Bfc" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 616411F45F for ; Tue, 4 Jun 2024 23:44:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544654; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G4FFxuYc8HLx9kOGXiZRjId/uKkVlbQY7wVxt9GD5gM=; b=T1oT1BfcYMIPikCh2ehVHTVw4nkaEIiMB5PRewYO4ywuPhHsufIcbO8cvQdUHX6QvXOZLg JW3EQbvwWfBqyYjtu+39bUmrJ1/T4b5t/4PsY7FOf+MYqDjgzPfHfHCB25DRbAZ3+4DYgj ODhKq7+6AXYLkQj60UJQ3hZ01ag3TUA= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1717544654; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G4FFxuYc8HLx9kOGXiZRjId/uKkVlbQY7wVxt9GD5gM=; b=T1oT1BfcYMIPikCh2ehVHTVw4nkaEIiMB5PRewYO4ywuPhHsufIcbO8cvQdUHX6QvXOZLg JW3EQbvwWfBqyYjtu+39bUmrJ1/T4b5t/4PsY7FOf+MYqDjgzPfHfHCB25DRbAZ3+4DYgj ODhKq7+6AXYLkQj60UJQ3hZ01ag3TUA= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 6E97E13A93 for ; Tue, 4 Jun 2024 23:44:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id SDa/CM2mX2bcJwAAD6G6ig (envelope-from ) for ; Tue, 04 Jun 2024 23:44:13 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 4/4] btrfs-progs: fix misc/038 test cases Date: Wed, 5 Jun 2024 09:13:44 +0930 Message-ID: <909e5e66fdbb40c9afe59175a0ed73741b8e22c8.1717544015.git.wqu@suse.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Flag: NO X-Spam-Score: -2.80 X-Spam-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; RCVD_TLS_ALL(0.00)[] The test case always fail in my VM, with the following error: $ sudo TEST=038\* make test-misc [TEST] misc-tests.sh [TEST/misc] 038-backup-root-corruption Backup 2 not overwritten test failed for case 038-backup-root-corruption After more debugging, the it turns out that there is nothing wrong except the final check: [ "$main_root_ptr" -ne "$backup_new_root_ptr" ] || _fail "Backup 2 not overwritten" The _fail() is only triggered if the previous check returns false, which is completely the opposite. Furthermore on the github CI, the kernel would commit 2 instead of 1 transaction, resulting the next slot never to match the current generation/tree root. The two bugs combined, resulting github CI always pass the test case, meanwhile for my VM which does the expected one transaction, it would always fail. Fix it by: - Use a proper "if [] then; fi" block to check the tree root bytenr - Use the generation diff to calculate the expected backup root slot - Log the full super block dump for debug usage Signed-off-by: Qu Wenruo --- .../038-backup-root-corruption/test.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/misc-tests/038-backup-root-corruption/test.sh b/tests/misc-tests/038-backup-root-corruption/test.sh index 9be0cee36239..28aa5baec91e 100755 --- a/tests/misc-tests/038-backup-root-corruption/test.sh +++ b/tests/misc-tests/038-backup-root-corruption/test.sh @@ -41,6 +41,9 @@ slot_num=$(echo $found | cut -f1 -d:) # To follow the dump-super output, where backup slot starts at 0. slot_num=$(($slot_num - 1)) +_log "Original superblock:" +_log "$(dump_super)" + # Save the backup slot info into the log _log "Backup slot $slot_num will be utilized" dump_super | run_check grep -A9 "backup $slot_num:" @@ -56,9 +59,14 @@ run_check_mount_test_dev -o usebackuproot run_check_umount_test_dev main_root_ptr=$(dump_super | awk '/^root\t/{print $2}') - -# The next slot should be overwritten -slot_num=$(( ($slot_num + 1) % 4 )) +cur_gen=$(dump_super | grep ^generation | awk '{print $2}') +# The slot to be used is based on how many transaction committed. +slot_num=$(( ($slot_num + $cur_gen - $backup_gen) % 4 )) backup_new_root_ptr=$(dump_super | grep -A1 "backup $slot_num" | grep backup_tree_root | awk '{print $2}') -[ "$main_root_ptr" -ne "$backup_new_root_ptr" ] || _fail "Backup 2 not overwritten" +_log "After the backup usage:" +_log "$(dump_super)" + +if [ "$main_root_ptr" -ne "$backup_new_root_ptr" ]; then + _fail "Backup ${slot_num} not overwritten" +fi