Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp450065iog; Mon, 13 Jun 2022 06:12:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyMw2BLDCBen5hUe+Xw3VrSt9QDOeFo+r6Hp0suOHuHkD0zE5reT+PyQEd5DKphD07XY6tQ X-Received: by 2002:a63:8349:0:b0:403:fee8:7b8f with SMTP id h70-20020a638349000000b00403fee87b8fmr14340676pge.434.1655125525659; Mon, 13 Jun 2022 06:05:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125525; cv=none; d=google.com; s=arc-20160816; b=yX06RNKTwQhIpWvA/bX0wOjP0G0iDKnr3s0Hid2rVKVezrh7WZivIHww96jvOdxkD+ 9jZsKrWgrZ/2wnk+2VPCIYo23PnxbR2iyP299VNJDx6D88zjhoe74QcJ+1sNn8knPPac drab7UIZEZJuizd8NE0RbT1P9k0UUyXBCUBTckjduqvm9FYJfNOuYaCzYIH/Gw1SLckk FuePrI9kCPt3qXR8LZDmAD2P2DlLcLbaMPbP/h/cpwa/32bEmuS+CQHAaZTQ8cYy0bFG bxOxa4AnTUjrpZ5O6+Fp+iweWOOhuSrYEUsivwiR6u/nkRvqTWJhh+GO3vraKsow2zYd J8dg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/02Zb6/AUPNVoBgQjn3LLgQr8WaZuBf0KGnM5IUdN1g=; b=WgL+EEtM1z1s/6idy01mX4SEKDlzEOJmRaaK29rGz3A+OdGoLlOF2OGF7iHt9rR9os dCkiqwZVmjNSHlkoVr3SAPtqoCtD0vAI+eg9rN/n8Cs+z8KybA6m59+cycWePh51UuNC uusDi4tf6BE0qEdToFKd2tFCd1giMwDAfRNlY0mNiKZXi493yQBdoq/fMkSty3f09KI5 WI7o6fZBRmOeHw20KxAvgHws0mAbmSLxgk4sjTgaPT0uAf043ujE3KphmuFgmNa/8V5L j9vAbrhnMlATKaHqg1/IiukEATswQ+wUUKnj4n0k7UEQupeaslpluOteTUlECd301CMX U58A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mJhfoL8+; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k21-20020aa788d5000000b0050dfc3c58fbsi9914715pff.5.2022.06.13.06.05.07; Mon, 13 Jun 2022 06:05:25 -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=@linuxfoundation.org header.s=korg header.b=mJhfoL8+; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352810AbiFMLZl (ORCPT + 99 others); Mon, 13 Jun 2022 07:25:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353029AbiFMLTG (ORCPT ); Mon, 13 Jun 2022 07:19:06 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AA2139BBA; Mon, 13 Jun 2022 03:40:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 94952CE1109; Mon, 13 Jun 2022 10:40:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67A37C34114; Mon, 13 Jun 2022 10:40:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116851; bh=xhk+jTQMkpS4G6E67FnKiOm6izJyEeX+mMpX/wlbihI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mJhfoL8+/P5z0HQ5FPtpgrXyQ99G1O8G942C0QdCBUF2qkhrSKS4OBb1YeJiEpLeB EZ4BF5AqQDS0VT9rp1KdKzDxPYNOcvsQLytdZOJn1aaNWAWirRcjlvtOIm8GxMvllL v+33v8IFQyp6XZvdZYXb12tqpH+RqZiBVq7dtnWw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jakob Koschel , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.4 196/411] f2fs: fix dereference of stale list iterator after loop body Date: Mon, 13 Jun 2022 12:07:49 +0200 Message-Id: <20220613094934.538790848@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Jakob Koschel [ Upstream commit 2aaf51dd39afb6d01d13f1e6fe20b684733b37d5 ] 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 Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- 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 78c54bb7898d..7759323bd775 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -352,16 +352,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, !IS_ATOMIC_WRITTEN_PAGE(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); -- 2.35.1