Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2454243imm; Mon, 28 May 2018 08:22:16 -0700 (PDT) X-Google-Smtp-Source: AB8JxZobTHf7FiiknWDkVra9m5pRbPdPfEJ24Kr8w5NNJMYTg0LVTOFuIntX925Yd5p6gaVriKg7 X-Received: by 2002:a65:5002:: with SMTP id f2-v6mr10698067pgo.38.1527520936337; Mon, 28 May 2018 08:22:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527520936; cv=none; d=google.com; s=arc-20160816; b=Xihhbk7eJxNnrfl2ZlsUKkGGxPrkFD8XDSqUCZlHLEJk22r7CC9PgzkR+pVGO4tREa 3i7y/DVKwzS1bqWhkQTwoRDNXYKO28M9d2D6iSgs7Do3yfWAxHOzNe1bAMbrrbeL472V +Z+c5srP/aa2Gy4xVS7ouM57dgmQp6LG9BXVkwEzDy2R5tdO3okaHx+CV1bFptxI+r0R DyNCsiFFxufB3u+WqA0EZVR6DpcMLx5sVzng4pEq8J48W2FzhH37MOs+xAVEsDTrgLx6 RrZcVu9PNx2nfdhslwwinMyy/KvEbIbGQdFfBvxT6JUBbd8Ozs8vTj0SLf4Fr+BZY6KK wzOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=N+56kci6yIyYPdBdAvERfn9YE7JM9z5iROpCZGUbDR8=; b=FSjdAZZdvY3RfO6slwwFI3Yg1espKHXaZWtJFFYIqyezygoyTibH2nrB/FEV9JMI9O WPGdrhUDBI4fh5q+JliTVGeF8vuqLpoS/h38jGC5XGHsbaifperFqWROfzpvW3poUWJH hGz8EEo90eNqEfeRGAhYjZMfTDYka2xTmEAo7MyWqWmxOYgvAMeIvEPnUfTRJiaIFeq7 +uUeCJ6DLb0Dj8WY2dA4jgW0W3r/Z7T3AhK2i24nEVROwZANM2sd0gQpp+H9WhXC4eTp OuF51QEZrJ4gyu9WzIJc/5WMNwTOYPUpv/oZ5glSWWVFkWiJwMiC3qHeDfJnafU16cTO 0vIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CM65qlI3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a8-v6si31082854plm.121.2018.05.28.08.22.01; Mon, 28 May 2018 08:22:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CM65qlI3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S968872AbeE1KVV (ORCPT + 99 others); Mon, 28 May 2018 06:21:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:40658 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966480AbeE1KVP (ORCPT ); Mon, 28 May 2018 06:21:15 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E211820899; Mon, 28 May 2018 10:21:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502875; bh=tTs/npTsIJxiVLorIRaeSysJ3lzcw4jg896YiG5OFNY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CM65qlI3EcsiQsFE8laJjXWvFn/Dy1ktKVkauI/NWlq2HQypPqdfa0FKkYAKzTt/f 9SZSWoimlmhvkY04Ujo9idREbmtw+8d+eGJN0ivrbrz8neW/ZxkVx38jHSn2wGjE/k WDM+qdFxK6Lfi6vJP04KUiXidVgRXtN+OipjgnLc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Abraham , Mark.Landis@Teradata.com, Andrew Morton , Randy Dunlap , Linus Torvalds , Sasha Levin Subject: [PATCH 4.4 161/268] swap: divide-by-zero when zero length swap file on ssd Date: Mon, 28 May 2018 12:02:15 +0200 Message-Id: <20180528100220.653221328@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100202.045206534@linuxfoundation.org> References: <20180528100202.045206534@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tom Abraham [ Upstream commit a06ad633a37c64a0cd4c229fc605cee8725d376e ] Calling swapon() on a zero length swap file on SSD can lead to a divide-by-zero. Although creating such files isn't possible with mkswap and they woud be considered invalid, it would be better for the swapon code to be more robust and handle this condition gracefully (return -EINVAL). Especially since the fix is small and straightforward. To help with wear leveling on SSD, the swapon syscall calculates a random position in the swap file using modulo p->highest_bit, which is set to maxpages - 1 in read_swap_header. If the swap file is zero length, read_swap_header sets maxpages=1 and last_page=0, resulting in p->highest_bit=0 and we divide-by-zero when we modulo p->highest_bit in swapon syscall. This can be prevented by having read_swap_header return zero if last_page is zero. Link: http://lkml.kernel.org/r/5AC747C1020000A7001FA82C@prv-mh.provo.novell.com Signed-off-by: Thomas Abraham Reported-by: Reviewed-by: Andrew Morton Cc: Randy Dunlap Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- mm/swapfile.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2258,6 +2258,10 @@ static unsigned long read_swap_header(st maxpages = swp_offset(pte_to_swp_entry( swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; last_page = swap_header->info.last_page; + if (!last_page) { + pr_warn("Empty swap-file\n"); + return 0; + } if (last_page > maxpages) { pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", maxpages << (PAGE_SHIFT - 10),