Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3294108pxf; Mon, 5 Apr 2021 08:18:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPnxVypsij2u3REEzchWbuXXt1mInMf+ynx6P2NqyQNgy4esgnhiSI19mYL1mk+tP9KwLL X-Received: by 2002:a05:6638:d47:: with SMTP id d7mr24361168jak.2.1617635926633; Mon, 05 Apr 2021 08:18:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617635926; cv=none; d=google.com; s=arc-20160816; b=orzfbAtjdMlYV+VJM7lJz2Wb6rsCMPCXjAj1YOyLG7qqcCBi6pS8DlolDeKHeEjKn+ RXXNtX9FD/GUpodMKbDeGs6xlrVriQYgCbXADpo4zxDLqsdNCksMxavOT+ic0mQgSvtC tZ6F5keNRoyN9ZqgPGC4diVVq0uKG4VKYF0ksm+qeYDmYpQLjTz6+tregmn0IT61VQSO ivxAcNbYIjGNGbDHYtrbip5X8jh8HdMMPzknsb1kOOHZ+fodPRZzkD6II5SNpWTDzHce BZJsJm8/VCfWQ/B7RaZU01C302QnRtGUraS+FjmPO2LYcXuZz0ehQaqAR8gSeMdgrwaz 1o+w== 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=4xALyfrmwuWjgVjytT3k4e8o7dnXMzgiUsvGdnLFU3E=; b=WWv0UatRjyoUk9GioOjYIffblU5vrwVHaSbfh0rV9yeSY/MKPQ80DpTMwgWRch9vT0 Q9hkDWF2mSgUuQo2To/CUzDMm4j4D/ExHQML89dkCUl877Lee98ARUURZc3zaNSFNOYR Ly0KdCiHFAE4TJKBzXHZk7s2zfpjfYHSlGo5iH84Cc9SR7RF5jFlo8PIGd2Tit74vkzS D4uoNfPFHEDz+cJnwKb5KIgquyHx44ScYPL34u1ScIrDSLgKS8MPFfdzh+WtsCuaqZKG p55VTY8nDcbjo0NY/SZzm0iXDnjAI28M6k9weyDVA72+XoErtnzlzl7ebuRtC8MqkoSp Av2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UO5y6SC5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e21si16084121jaq.58.2021.04.05.08.18.33; Mon, 05 Apr 2021 08:18:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UO5y6SC5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S238106AbhDEJEZ (ORCPT + 99 others); Mon, 5 Apr 2021 05:04:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:46030 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237596AbhDEJDO (ORCPT ); Mon, 5 Apr 2021 05:03:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CC0B861002; Mon, 5 Apr 2021 09:03:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617613387; bh=QHdcJTezi9bvA14StpNy4bsRWJsJICWDl/4yxaAs0+Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UO5y6SC5ui5r6TxI2ntA+Bg9yfJ57D9rVZOgv1PtOPNcBkDoHEy0mLOhXpRunPH26 3gWL9G8DEf90B/BO/y9p01i3iNfY2JbiAe9d+Fi2ni63SP/AzuWoIKHLwMfriX952V /vaK1tqnUwiW9EgjkJbe3IDUJ+zPKwNdgMBT7RuY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ritesh Harjani , "Darrick J. Wong" , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.4 10/74] iomap: Fix negative assignment to unsigned sis->pages in iomap_swapfile_activate Date: Mon, 5 Apr 2021 10:53:34 +0200 Message-Id: <20210405085025.065661001@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210405085024.703004126@linuxfoundation.org> References: <20210405085024.703004126@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ritesh Harjani [ Upstream commit 5808fecc572391867fcd929662b29c12e6d08d81 ] In case if isi.nr_pages is 0, we are making sis->pages (which is unsigned int) a huge value in iomap_swapfile_activate() by assigning -1. This could cause a kernel crash in kernel v4.18 (with below signature). Or could lead to unknown issues on latest kernel if the fake big swap gets used. Fix this issue by returning -EINVAL in case of nr_pages is 0, since it is anyway a invalid swapfile. Looks like this issue will be hit when we have pagesize < blocksize type of configuration. I was able to hit the issue in case of a tiny swap file with below test script. https://raw.githubusercontent.com/riteshharjani/LinuxStudy/master/scripts/swap-issue.sh kernel crash analysis on v4.18 ============================== On v4.18 kernel, it causes a kernel panic, since sis->pages becomes a huge value and isi.nr_extents is 0. When 0 is returned it is considered as a swapfile over NFS and SWP_FILE is set (sis->flags |= SWP_FILE). Then when swapoff was getting called it was calling a_ops->swap_deactivate() if (sis->flags & SWP_FILE) is true. Since a_ops->swap_deactivate() is NULL in case of XFS, it causes below panic. Panic signature on v4.18 kernel: ======================================= root@qemu:/home/qemu# [ 8291.723351] XFS (loop2): Unmounting Filesystem [ 8292.123104] XFS (loop2): Mounting V5 Filesystem [ 8292.132451] XFS (loop2): Ending clean mount [ 8292.263362] Adding 4294967232k swap on /mnt1/test/swapfile. Priority:-2 extents:1 across:274877906880k [ 8292.277834] Unable to handle kernel paging request for instruction fetch [ 8292.278677] Faulting instruction address: 0x00000000 cpu 0x19: Vector: 400 (Instruction Access) at [c0000009dd5b7ad0] pc: 0000000000000000 lr: c0000000003eb9dc: destroy_swap_extents+0xfc/0x120 sp: c0000009dd5b7d50 msr: 8000000040009033 current = 0xc0000009b6710080 paca = 0xc00000003ffcb280 irqmask: 0x03 irq_happened: 0x01 pid = 5604, comm = swapoff Linux version 4.18.0 (riteshh@xxxxxxx) (gcc version 8.4.0 (Ubuntu 8.4.0-1ubuntu1~18.04)) #57 SMP Wed Mar 3 01:33:04 CST 2021 enter ? for help [link register ] c0000000003eb9dc destroy_swap_extents+0xfc/0x120 [c0000009dd5b7d50] c0000000025a7058 proc_poll_event+0x0/0x4 (unreliable) [c0000009dd5b7da0] c0000000003f0498 sys_swapoff+0x3f8/0x910 [c0000009dd5b7e30] c00000000000bbe4 system_call+0x5c/0x70 Exception: c01 (System Call) at 00007ffff7d208d8 Signed-off-by: Ritesh Harjani [djwong: rework the comment to provide more details] Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Signed-off-by: Sasha Levin --- fs/iomap/swapfile.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/iomap/swapfile.c b/fs/iomap/swapfile.c index 152a230f668d..bd0cc3dcc980 100644 --- a/fs/iomap/swapfile.c +++ b/fs/iomap/swapfile.c @@ -169,6 +169,16 @@ int iomap_swapfile_activate(struct swap_info_struct *sis, return ret; } + /* + * If this swapfile doesn't contain even a single page-aligned + * contiguous range of blocks, reject this useless swapfile to + * prevent confusion later on. + */ + if (isi.nr_pages == 0) { + pr_warn("swapon: Cannot find a single usable page in file.\n"); + return -EINVAL; + } + *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage; sis->max = isi.nr_pages; sis->pages = isi.nr_pages - 1; -- 2.30.1