From patchwork Thu Jan 2 03:53:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Sun X-Patchwork-Id: 13924298 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 CFE6938F91 for ; Thu, 2 Jan 2025 03:53:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735790027; cv=none; b=UTKrrB9CBp6R/9gm6WGlVadSw+Tm3T32DL6E08UePadZIuIBOx0A7uYhNeugAnEPWoVe+1dDHwsBmmMWeM/B8sH6bcV8CTG2qUFF/d3ElYVm8GsqtrpF4ayqZprUBsRaCy24gek0/3r9X1RStK8HUEip5Qu+QtmHt2raiNtgIJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735790027; c=relaxed/simple; bh=+gLoUi/xji7yyskMfGOL0U61JGMXgKaElbAmlN3MjXs=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Tbuew29Qs+ESh8r2XOQL17kulPaabudkhh45YNaCMqo5oGCkREhhUpdYQXJAeOOJ4UjZGmwTOhkax1QH3gmiMTnoL88EVa5j8584Q0zuSnMNywGVkDNn0pLuX2BUEvzdaZofpo6TsodhWFrkL1mJBy6WWsIJiOddvRoBZws7GBU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=J4IWtJ7p; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J4IWtJ7p" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-216401de828so139756875ad.3 for ; Wed, 01 Jan 2025 19:53:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735790024; x=1736394824; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=IaONGo+R6aXL6TBZE4poKKiIN/NuJaEYQsxBIydnnhg=; b=J4IWtJ7px2djg3wOscfah6XZWjH/8XBQsG4ENdOO6L1Gm6cSmV+9y7cQ84K74U2WkP m7biYTksoZAja28q/y+ojLQFiEIrBGwBeMDjLWUVQ7TgCscXtuhGd5/PnwgYPwkH08uk JFIBMtjcFXN17BS4M8UMy21GIb/6S4gCjRWB+xuFNO8ybJjHN6dCRzBKiFwMtI+G36GS 9i7o0142LO90QDxcjF5XxRsH/T4E5op5qYPgHdw60kmEx5hLAYYWCVVfY1weKUV/X6w4 UNRtY65prkhRdLCcVC20pc/UDXRk2HOe2cqGMiUQCBFsBJLRx5qKMedzXz/zW4U4eCIA Df0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735790024; x=1736394824; 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=IaONGo+R6aXL6TBZE4poKKiIN/NuJaEYQsxBIydnnhg=; b=ti+fl5X+1DCLJpC3Ws9nvOsXnx7Lkg9flsve7LgifBy/AvpbGk/jmTz80qaTVbA+eJ gtDPxkgbUXGsiAMezjRRQg1witWKcJZthJo6KMq0SsjV/9ui2wDV0yuhvtZ/nmaBOVZi Jad3+qfUXC5WnbfoF1xEKM8ODxiDVFHraC8mIcmCcag6L+hil0zpiYawb118VtdkIVah n8yY0Rfha3C8wPUJIavUY3eerlKtw9sfkjl+7txDDd/KdtpAMv58tDA89BXeuba86pIo OdokG3EDVsdFhPwisQdjwyjnZ7JQUOHkVFy/5MpbMMX1lDcU1MV7N3hzFFC9g3ZBPulc 9A1w== X-Gm-Message-State: AOJu0YwqGOyjWY982fqZd0MI1WDDj5/puVsuXj3LahnwHb67DYanIQSF pf8FlEF+U0u2qZQPoeEQnOKe9OpQSKBbhV7kS1+prVuHgyS8Sv0JEreDiM6aDUA= X-Gm-Gg: ASbGncs3iLxRiYNx8P3tNOky3FjI6ktAsd07BWz3i+Q0qcCglUEc13PCb2k3OV2z0tS uK1/OXn71UrXy32OoUXLngkQxDMiO1SEuadQzpn4X+b7BiWAgmzZqsTBQjn6K1ET8nPOJLeYvXF IyIIpS39f/1Ro0l+/0HuF7mugumoRjlDyr3kBxwwMs+ZBopXV9arNL0cA6gnyX2v/mjWFaToo7T LkTdNT34SQi8tZANFE5eX32jPCFmFGOk2zBXkNRhxTC/0Fu8GFp4cRaP/rHcNpWIsMy/KxpEuLd xs0YoABfJSqyAw== X-Google-Smtp-Source: AGHT+IHvsFZ7POkwrTnv04ReK2uu8bQhSI0MA5O6CkXONiYUno9XYhr/zuTzgoD/diow/Ip4LxGNaw== X-Received: by 2002:a17:902:d481:b0:218:a4ea:a786 with SMTP id d9443c01a7336-219e6f28491mr631516115ad.53.1735790024138; Wed, 01 Jan 2025 19:53:44 -0800 (PST) Received: from localhost ([206.237.119.147]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc962cadsm217657075ad.15.2025.01.01.19.53.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 19:53:43 -0800 (PST) From: Julian Sun To: ocfs2-devel@lists.linux.dev Cc: mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, Julian Sun , syzbot+2313dda4dc4885c93578@syzkaller.appspotmail.com Subject: [PATCH 1/2] ocfs2: Add a sanity check for corrupted file system. Date: Thu, 2 Jan 2025 11:53:20 +0800 Message-Id: <20250102035321.391540-1-sunjunchao2870@gmail.com> X-Mailer: git-send-email 2.39.5 Precedence: bulk X-Mailing-List: ocfs2-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Hi, Recently syzbot reported a use-after-free issue[1]. The root cause of the problem is that the journal inode recorded in this file system image is corrupted. The value of "di->id2.i_list.l_next_free_rec" is 8193, which is greater than the value of "di->id2.i_list.l_count" (19). To solve this problem, an additional check should be added within ocfs2_get_clusters_nocache(). If the check fails, an error will be returned and the file system will be set to read-only. [1]: https://lore.kernel.org/all/67577778.050a0220.a30f1.01bc.GAE@google.com/T/ Reported-and-tested-by: syzbot+2313dda4dc4885c93578@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=2313dda4dc4885c93578 Signed-off-by: Julian Sun --- fs/ocfs2/extent_map.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c index f7672472fa82..7fdbecb9c031 100644 --- a/fs/ocfs2/extent_map.c +++ b/fs/ocfs2/extent_map.c @@ -414,6 +414,16 @@ static int ocfs2_get_clusters_nocache(struct inode *inode, el = &di->id2.i_list; tree_height = le16_to_cpu(el->l_tree_depth); + if (le16_to_cpu(di->id2.i_list.l_next_free_rec) > + le16_to_cpu(di->id2.i_list.l_count)) { + ret = ocfs2_error(inode->i_sb, "Invalid dinode #%llu: next_free_rec is %u, count is %u\n", + (unsigned long long)di_bh->b_blocknr, + le16_to_cpu(di->id2.i_list.l_next_free_rec), + le16_to_cpu(di->id2.i_list.l_count)); + + return ret; + } + if (tree_height > 0) { ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster, &eb_bh); From patchwork Thu Jan 2 03:53:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julian Sun X-Patchwork-Id: 13924299 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 AA916182BC for ; Thu, 2 Jan 2025 03:53:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735790033; cv=none; b=BS12DwfR7NUrcbijCmfm9SqvgdhlDWG3AMwSLJ9ekPptbA0o8+zvMcX20sQ/Z7ypEOOCzKL6QqlJiJwiuOOFPUu6Hyg51DBXqvoyFKudxAT5/sQad3NIjqtaeCToFG91gfqgSD8ymX4EZyuodX9knNhJFhFnQirvSQAbiXgeUCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735790033; c=relaxed/simple; bh=HNFxpv1mLEyTrxt/e0pR+ELgUU5F+Y8/dxxOxE2sq9I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nGz+vHOg3PQpp7QtVcC3cIi0ImFtx2BotXOt96Auz5GvtQqn7pHvTHmZJMalkoDc2Fm9oqf2kB1lQpB2vJAr8iKK44yJq40ZlxTEsR0iK6HcqA9L4HzYh07ULQTjNnXlUX7mlFhqECIBtFj5mcXBk9uQMPQuOsdsLH1pfb/hJ/c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=I4yOzVP6; arc=none smtp.client-ip=209.85.216.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I4yOzVP6" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2eec9b3a1bbso11743588a91.3 for ; Wed, 01 Jan 2025 19:53:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735790030; x=1736394830; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1KBRkcb48K8Wm8eXNHwyO281AhSO6O4DAGPjtvLu80A=; b=I4yOzVP6ZGX8t8Hz/dDNE3iEDjSVW85jw+VVVGtd0khjtpkFccS++Wa4RXdYJhpEtn K5J0qS5g9HVQ+wxLVSQvW9eE6WJssr4VN/A870kdfLcLH22A4kEsrNndf19NBzu9jlR+ jeP+C+SIxtSNrRHc/m+mgswyq8yi31s2gbxN2Cy27qJ9tyw4uZkpaIItn6cYgTWxf3JR syDmYtPyTFqvMSRXh9IWjGUdUXuziEDc16dv7z9nH4aS+w2q9WbUdqX73buOgUs7fWKT l2g66GwkJ4iuDVvpQwnZ8lsEbf1QJgCQ+D7AFN38Eepz80n8gfd97YqLVkZkpbcFI8oD piAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735790030; x=1736394830; 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=1KBRkcb48K8Wm8eXNHwyO281AhSO6O4DAGPjtvLu80A=; b=fKath09hdev3P6zSsnmqFnpo1hrJM33z5TWl2xdy3z2fqb1M5XYTHFC8VyEXsAjdSf jJszJfKIQyXacAuCIHgQTJWkwk1zHl3FhjfnHisZhHixxure1XM8Xb6lNhWAaQi1HTA3 5a8IPN0JdbOZkVdX3fG8ktjO63f7X/bdLHT73bT0KhpifCabHQCAOvxS1FzXkdlKa+Hr 2rN0ABbNB0qCrgdDevdCYt/2fFbCtTQpRvXdJ7PH9+CEIFZgrH4oWbP9hdzTigOLgT+i 5c2yP7bc6ya7Ye3hRvaTmZMbBDYbpBCgF70DOkppiChLXjQBHZeKVm34A3eiVmTuO1FU pooQ== X-Gm-Message-State: AOJu0Yzb0eSLya2fs1uiuWzJK7kaon/vu4sDciELf2cS3eMYPh3L1DlP gx6uHj2k1vdhPiVkmmWhSTzhe4ExLwg/+VU2FNrbYLf0eXT8McsCNpTE/hlDIXY= X-Gm-Gg: ASbGncummUE7qE9vF8D6RKTOJciKvbLt61HU52F14cwB6Ar4BKG7Uw/LOl5Nj289kX0 j2jBNaK3oNRmy8K5/RO/Tf9/fOIIXpCm8otnYsBMaEIeEJBylBa6xsYJ/Qa2dPbu7moWCAtZyDy dtkKtEDAeJktH+2t/mdanngC6/sgajfMeHf2SH4dIHzCjeFxo2AjkBYJiA69V+FGGoLLHajOozl 7LnLyxL6RmDqy0B2/oHGnvij1Fv2ue/7jsluuMGbIJqkguL880aYd+2cYNc4dus1NP2Ljfq4GsR WEcsk9xzVFDocw== X-Google-Smtp-Source: AGHT+IF9a8XAYlALKMZ2rh7R3DFgOUBIZfcymguYAj16BLov/pvDSbECOafZekIp0r+nG0gVNiGJSA== X-Received: by 2002:a17:90b:3cd0:b0:2ee:a76a:830 with SMTP id 98e67ed59e1d1-2f452eb3279mr68641091a91.24.1735790030099; Wed, 01 Jan 2025 19:53:50 -0800 (PST) Received: from localhost ([206.237.119.147]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f2ed52cf40sm29972392a91.1.2025.01.01.19.53.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 19:53:49 -0800 (PST) From: Julian Sun To: ocfs2-devel@lists.linux.dev Cc: mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com, Julian Sun Subject: [PATCH 2/2] ocfs2: Correct l_next_free_rec in online check Date: Thu, 2 Jan 2025 11:53:21 +0800 Message-Id: <20250102035321.391540-2-sunjunchao2870@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250102035321.391540-1-sunjunchao2870@gmail.com> References: <20250102035321.391540-1-sunjunchao2870@gmail.com> Precedence: bulk X-Mailing-List: ocfs2-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Correct the value of l_next_free_rec to l_count during the online check, as done in the check_el() function in ocfs2_tools. Signed-off-by: Julian Sun --- fs/ocfs2/inode.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c index 2cc5c99fe941..1fc9544dc1fa 100644 --- a/fs/ocfs2/inode.c +++ b/fs/ocfs2/inode.c @@ -200,6 +200,20 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, return inode; } +static int has_extents(struct ocfs2_dinode *di) +{ + /* inodes flagged with other stuff in id2 */ + if (di->i_flags & (OCFS2_SUPER_BLOCK_FL | OCFS2_LOCAL_ALLOC_FL | + OCFS2_CHAIN_FL | OCFS2_DEALLOC_FL)) + return 0; + /* i_flags doesn't indicate when id2 is a fast symlink */ + if (S_ISLNK(di->i_mode) && di->i_size && di->i_clusters == 0) + return 0; + if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) + return 0; + + return 1; +} /* * here's how inodes get read from disk: @@ -1528,6 +1542,16 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, return -OCFS2_FILECHECK_ERR_VALIDFLAG; } + if (has_extents(di) && le16_to_cpu(di->id2.i_list.l_next_free_rec) > + le16_to_cpu(di->id2.i_list.l_count)) { + di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count; + changed = 1; + mlog(ML_ERROR, + "Filecheck: reset dinode #%llu: l_next_free_rec to %u\n", + (unsigned long long)bh->b_blocknr, + le16_to_cpu(di->id2.i_list.l_next_free_rec)); + } + if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { di->i_blkno = cpu_to_le64(bh->b_blocknr); changed = 1;