Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp3620247rdb; Sun, 10 Dec 2023 13:18:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IHibhAL0pqOzLd0ajNEb8TvRi/MJEUy/KgWldmtDlOC3PYiyLSCUhW4RkivQWZALmPJ9vQQ X-Received: by 2002:a17:902:d2d1:b0:1d0:8e08:6a with SMTP id n17-20020a170902d2d100b001d08e08006amr4732855plc.6.1702243097794; Sun, 10 Dec 2023 13:18:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702243097; cv=none; d=google.com; s=arc-20160816; b=F2IYzLwwGrRASb1+DaX8kzaJtQZMUQqsa0eJXnpblKCK6KAGkZt5XodngFj0H2S2xe xmX6+0rgt/PpdcfOIkSZBmyyy5HPrLk3ZwtpJ2p5PIgfLPlMvQB7g9pizwu6jxPg1L4I ssGXnWNejA8bpwoxOOZlZ2Oj+rYabmTihSUymUoQsDT6sOESVZ6P+NgRajfrHjb+60tm o0XYdtzVm1kwBoJnRVgjCNcqvCq63N+bbhreio7gpePOB2I3Xyj5ktZVEuk0jUgJD476 mGSk7TWe74TBmO/zpjgNxRKU2h8S+SGeg9zD1ew6qbpxfkl3mv+QhoUId3xDuVdiZToH vyLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:organization :from:references:cc:to:subject; bh=jxu4gESS+2XK9muUBRONfzRgwBDcOIpV/SbR8/jNC00=; fh=yD6EJI542iIm0CRmShleoffvFZEHuLGrCg/N+Xh7gd8=; b=dkOHh27bRauAfVgS8iHnfQRDlbkTnkOEccHmRsYHf9Nr4rCA88EjWD0XCctP0JcY3g 8mDrxjlglpnd065TyVJh/8j0mhMFs56dZtOYqj3QUKcnal9QYPeLjh+6MZhkRr+plvY6 gPU/nDWGQfEL+MeaxfQEf2PR69ll4lU2jQVRv6bzIVniyN/qLh9ErzhWHwKg17VCumRo KRDowViBeHUwGQ1PvqAdwFySiMLlyCghGgwlmA6z8qvMQSoKhXp9QaumiL4Kcqa3EOuT tozOrtzJkOjc7wz2ymH++SJ6kk3FkKfLOeSCHm0SQu4ytxZ0EYpzeds8jeBM2KBJCUP3 k+8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id z16-20020a170903019000b001cc2853bfe6si5146556plg.192.2023.12.10.13.18.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 13:18:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id AFD508060C8A; Sun, 10 Dec 2023 13:18:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232120AbjLJVOW (ORCPT + 99 others); Sun, 10 Dec 2023 16:14:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231837AbjLJVOU (ORCPT ); Sun, 10 Dec 2023 16:14:20 -0500 Received: from mx01.omp.ru (mx01.omp.ru [90.154.21.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DCEFDB for ; Sun, 10 Dec 2023 13:14:26 -0800 (PST) Received: from [192.168.1.104] (178.176.74.216) by msexch01.omp.ru (10.188.4.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1258.12; Mon, 11 Dec 2023 00:14:16 +0300 Subject: Re: [PATCH] f2fs: data: fix possible overflow in check_swap_activate() To: Chao Yu , Jaegeuk Kim , CC: References: <20231025202019.5228-1-s.shtylyov@omp.ru> From: Sergey Shtylyov Organization: Open Mobile Platform Message-ID: <5769c4fa-3112-3582-00a0-2b17757d8cab@omp.ru> Date: Mon, 11 Dec 2023 00:14:16 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [178.176.74.216] X-ClientProxiedBy: msexch01.omp.ru (10.188.4.12) To msexch01.omp.ru (10.188.4.12) X-KSE-ServerInfo: msexch01.omp.ru, 9 X-KSE-AntiSpam-Interceptor-Info: scan successful X-KSE-AntiSpam-Version: 6.0.0, Database issued on: 12/10/2023 20:52:29 X-KSE-AntiSpam-Status: KAS_STATUS_NOT_DETECTED X-KSE-AntiSpam-Method: none X-KSE-AntiSpam-Rate: 59 X-KSE-AntiSpam-Info: Lua profiles 181999 [Dec 10 2023] X-KSE-AntiSpam-Info: Version: 6.0.0.2 X-KSE-AntiSpam-Info: Envelope from: s.shtylyov@omp.ru X-KSE-AntiSpam-Info: LuaCore: 6 0.3.6 62f5a4619c57459c9a142aa1486ed27913162963 X-KSE-AntiSpam-Info: {rep_avail} X-KSE-AntiSpam-Info: {Tracking_from_domain_doesnt_match_to} X-KSE-AntiSpam-Info: {relay has no DNS name} X-KSE-AntiSpam-Info: {SMTP from is not routable} X-KSE-AntiSpam-Info: {Found in DNSBL: 178.176.74.216 in (user) b.barracudacentral.org} X-KSE-AntiSpam-Info: {Found in DNSBL: 178.176.74.216 in (user) dbl.spamhaus.org} X-KSE-AntiSpam-Info: d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;omp.ru:7.1.1;127.0.0.199:7.1.2 X-KSE-AntiSpam-Info: ApMailHostAddress: 178.176.74.216 X-KSE-AntiSpam-Info: {DNS response errors} X-KSE-AntiSpam-Info: Rate: 59 X-KSE-AntiSpam-Info: Status: not_detected X-KSE-AntiSpam-Info: Method: none X-KSE-AntiSpam-Info: Auth:dmarc=temperror header.from=omp.ru;spf=temperror smtp.mailfrom=omp.ru;dkim=none X-KSE-Antiphishing-Info: Clean X-KSE-Antiphishing-ScanningType: Heuristic X-KSE-Antiphishing-Method: None X-KSE-Antiphishing-Bases: 12/10/2023 20:58:00 X-KSE-Antivirus-Interceptor-Info: scan successful X-KSE-Antivirus-Info: Clean, bases: 12/10/2023 7:07:00 PM X-KSE-Attachment-Filter-Triggered-Rules: Clean X-KSE-Attachment-Filter-Triggered-Filters: Clean X-KSE-BulkMessagesFiltering-Scan-Result: InTheLimit X-Spam-Status: No, score=-0.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 10 Dec 2023 13:18:15 -0800 (PST) Hello! Sorry for replying a month later than I should -- I got distracted by the other Svace reports... It took a significant part of the weekend to swap this stuff back in... :-/ On 11/7/23 6:29 PM, Chao Yu wrote: [...] >> In check_swap_activate(), if the *while* loop exits early (0- or 1-page >> long swap file), an overflow happens while calculating the value of the >> span parameter as the lowest_pblock variable ends up being greater than >> the highest_pblock variable. Let's set *span to 0 in this case... > > What do you think of returning -EINVAL for such case? I assume this is a > corner case. I don't know the code well enough but I got the impression that iff we have a file containing a single page, we'd have one successful call of add_swap_extent(). Am I missing something? >> Found by Linux Verification Center (linuxtesting.org) with the SVACE static >> analysis tool. >> >> Signed-off-by: Sergey Shtylyov >> --- >> This patch is against the 'master' branch of Jaegeuk Kim's F2FS repo... >> >>   fs/f2fs/data.c | 5 ++++- >>   1 file changed, 4 insertions(+), 1 deletion(-) >> >> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >> index 916e317ac925..342cb0d5056d 100644 >> --- a/fs/f2fs/data.c >> +++ b/fs/f2fs/data.c >> @@ -4047,7 +4047,10 @@ static int check_swap_activate(struct swap_info_struct *sis, >>           cur_lblock += nr_pblocks; >>       } >>       ret = nr_extents; >> -    *span = 1 + highest_pblock - lowest_pblock; >> +    if (lowest_pblock <= highest_pblock) > > if (unlikely(higest_pblock < lowest_pblock)) Well, Greg KH says we shouldn't use unlikely() unless we can prove that it indeed improves things... >     return -EINVAL; > > *span = 1 + highest_pblock - lowest_pblock; > > Thanks, > >> +        *span = 1 + highest_pblock - lowest_pblock; >> +    else >> +        *span = 0; >>       if (cur_lblock == 0) >>           cur_lblock = 1;    /* force Empty message */ >>       sis->max = cur_lblock; MBR, Sergey