Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3104310pxb; Mon, 4 Apr 2022 07:55:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHzPDJ3UDiQeP7EJDALfGDl6G4m/gpgLS4mKq2897bKTDf8w4B0fLPXxyRSqi6sQHbc5N0 X-Received: by 2002:a17:907:3d93:b0:6e7:4d22:75c8 with SMTP id he19-20020a1709073d9300b006e74d2275c8mr473723ejc.330.1649084119419; Mon, 04 Apr 2022 07:55:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649084119; cv=none; d=google.com; s=arc-20160816; b=zEfxwYtfT5n0Lt5B8kxAX8lSnU6og1IkfRoiNX3zuWzWcZAiZP/SfldosEUtdW3ZIL +u78NVqZkdrNMMJw7Lit/TRZKeN7BBfEN73iK4dMlxkwDb8HI8dLkqgLafInWzPF/n8O IZrGsFYiU5iOcrjg+MZsXjiySzzg2Dpaj+keL2ZgR/9kfBPg2b2EQo2MNtRTx8ZMCHHt TBHTASAlFKwqXdk0fUwdFdgZu50Ef08aDhTC03OLe6QpGQEExY1fDgluuqvNuYvlnj0H wqqttqzs3u1tKxKo8MabVPU02kFvye4tydRspp7JrdTKI/PUlhItnpLBHlZzG5wj4FMJ i6ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=acES2We7h+b61Mo8+oMpTpwxU8V43KYhLkYyDhUPIro=; b=xbRuXJsXKE5QhtJxv94v5CTSKTjgIycPUkjdmF07E+iHD5Xymj7/RFvEbg0TmaN/3O 7yDmQc+82z6PA7H6DUgDfaV2qyQnuL0Gj8WZZPuU3N8pUKdrk0PNJSyT4+nU+nAIt4AT yptT3CqgHy6368Os+n1YvryPjEeggwG35tDtoDEmce0ELrPTtzsXDJGyxbuExAac8IT9 J6KJMai71vziaJnzMQ2+/beVOxSS9kGe9qhht0MpCkX7GRjAHulva6xIBiPRJ+NYyfVY ARTK96BApCOzvIW9Wu/3/CCTx9FMu9viSLr1KpM6gvHwl0FCMico/QhulNoY00IhPunK N1Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=LaYaFxb4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x17-20020a05640226d100b00419a4ca714csi7992861edd.164.2022.04.04.07.54.53; Mon, 04 Apr 2022 07:55:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=LaYaFxb4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242506AbiCaWgd (ORCPT + 99 others); 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-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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/undefined 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 --- 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 = F2FS_I_SB(inode); struct list_head *head = &fi->inmem_pages; struct inmem_pages *cur = 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 == page) + list_for_each_entry(tmp, head, list) { + if (tmp->page == page) { + cur = tmp; break; + } } - f2fs_bug_on(sbi, list_empty(head) || cur->page != page); + f2fs_bug_on(sbi, !cur); list_del(&cur->list); mutex_unlock(&fi->inmem_lock); base-commit: f82da161ea75dc4db21b2499e4b1facd36dab275 -- 2.25.1