From nobody Fri Jun 19 17:12:32 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 163BDC433EF for ; Thu, 31 Mar 2022 22:34:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242510AbiCaWgd (ORCPT ); Thu, 31 Mar 2022 18:36:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233852AbiCaWga (ORCPT ); Thu, 31 Mar 2022 18:36:30 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F6281C60F2 for ; Thu, 31 Mar 2022 15:34:42 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id r13so2220640ejd.5 for ; Thu, 31 Mar 2022 15:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=acES2We7h+b61Mo8+oMpTpwxU8V43KYhLkYyDhUPIro=; b=LaYaFxb4308Ra+8BjjJVsy1kSa2az3SVuCIrWBRxAdwHMtb/+VOVrzEaIBCha1ZCeo nJqROMozxayEBaxR7ZlTP5R9Y69YpLpIPao0v5fo+Bxo94EAkVGMSNrwxa8Pi6dGVM+y Eq7Xjbx7dHimCkNtsWvDyU0HHWO78PgW2flQULDz7Rbb8eweGlg6kdjzIsmDZ4/9r0V2 UgDTxGdAbG7e4qOymXlueQxgdSazgglB5ivhKn/hzFxoikhFD0966apCjt2TOF7UI8XW FiWPMetAnbbhliZI+PxvIfzyZN2ZCcYtxEGL2Dr2JMUqDzxS4XeLBXH6DmivLNbJ6b3c tQ0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=acES2We7h+b61Mo8+oMpTpwxU8V43KYhLkYyDhUPIro=; b=BXGmX8+L6WW3B9htZMerKLAvSr7wlBX4IbeH1fwkb4xpgZNBAE81WEuyfCt/tEv4NR BwhAaHzQPLtcC1UfH1wQ/2iJ0kPWqLbr5y+/sCZ/vKjNod7derkv9DQpCSavzSM+Wemy MghXUIJbE0iHuH5myhpoB/daW03IOXwwmtStJtlRZpM+J5u3sBOu0+BM6P+kpxk8WE3r E3N7ASh3+dc+KAQTlA3eEVAwnU2AEOWQiDCsh9SaynmGyJIyq4cRfk6CmW+hiZSbXAdE ANK2wwmbtpzpSrWTU6RU+jZDW24ID8KaIQ+W2XK63kY0THSnfqUba5r1CId/QZHhVT8e ueQg== X-Gm-Message-State: AOAM530Q4LFbT2vQ+rNKyKQL2HiGSPf+sWdNOhJiOmFPCDAv8Ml3oK6O lY4xDEQA2AwSeP8lw9yComY= X-Google-Smtp-Source: ABdhPJzu84nlagno7/B/iCTh45NIAtes3ADlYNLucnAQtD/vsIqDQRUwSN2TCW8BrJEBpEFPCzOewA== X-Received: by 2002:a17:906:c04d:b0:6b9:252:c51c with SMTP id bm13-20020a170906c04d00b006b90252c51cmr6916234ejb.470.1648766081039; Thu, 31 Mar 2022 15:34:41 -0700 (PDT) Received: from localhost.localdomain (i130160.upc-i.chello.nl. [62.195.130.160]) by smtp.googlemail.com with ESMTPSA id bm23-20020a170906c05700b006d597fd51c6sm279739ejb.145.2022.03.31.15.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Mar 2022 15:34:40 -0700 (PDT) From: Jakob Koschel To: Jaegeuk Kim Cc: Chao Yu , Yunlei He , linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Mike Rapoport , "Brian Johannesmeyer" , Cristiano Giuffrida , "Bos, H.J." , Jakob Koschel Subject: [PATCH] f2fs: fix dereference of stale list iterator after loop body Date: Fri, 1 Apr 2022 00:34:14 +0200 Message-Id: <20220331223414.901960-1-jakobkoschel@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The list iterator variable will be a bogus pointer if no break was hit. Dereferencing it (cur->page in this case) could load an out-of-bounds/undef= ined value making it unsafe to use that in the comparision to determine if the specific element was found. Since 'cur->page' *can* be out-ouf-bounds it cannot be guaranteed that by chance (or intention of an attacker) it matches the value of 'page' even though the correct element was not found. This is fixed by using a separate list iterator variable for the loop and only setting the original variable if a suitable element was found. Then determing if the element was found is simply checking if the variable is set. Fixes: 8c242db9b8c0 ("f2fs: fix stale ATOMIC_WRITTEN_PAGE private pointer") Signed-off-by: Jakob Koschel Reviewed-by: Chao Yu --- fs/f2fs/segment.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 22dfeb991529..58695997d42f 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -355,16 +355,19 @@ void f2fs_drop_inmem_page(struct inode *inode, struct= page *page) struct f2fs_sb_info *sbi =3D F2FS_I_SB(inode); struct list_head *head =3D &fi->inmem_pages; struct inmem_pages *cur =3D NULL; + struct inmem_pages *tmp; f2fs_bug_on(sbi, !page_private_atomic(page)); mutex_lock(&fi->inmem_lock); - list_for_each_entry(cur, head, list) { - if (cur->page =3D=3D page) + list_for_each_entry(tmp, head, list) { + if (tmp->page =3D=3D page) { + cur =3D tmp; break; + } } - f2fs_bug_on(sbi, list_empty(head) || cur->page !=3D page); + f2fs_bug_on(sbi, !cur); list_del(&cur->list); mutex_unlock(&fi->inmem_lock); base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275 -- 2.25.1