Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2511046imm; Mon, 28 May 2018 09:27:17 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLcH6Oj8wXdxplYUHMrkhuM8013YCCju8jmw+5J8H83NoPrlzupKPm40x3B+873h7MlBPHw X-Received: by 2002:a17:902:7d89:: with SMTP id a9-v6mr10203313plm.238.1527524837357; Mon, 28 May 2018 09:27:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527524837; cv=none; d=google.com; s=arc-20160816; b=bDgfsbkBBp1NiuRzfyEy4kG4kgh+wZ9zNM5yRDUnBzoE9+ZSyxtLgfuA4ef2UhA6uo rQX3jB5AeZZkM9UqpqGaW/GmsV6VlTqcq61sujIl8hKvE4bvfnqPapf7Ez88NLPkrzNQ 6sbzpgSGKBV3V5cPIQ9GLwy3cA/e0X7ZtTLpcirrALcODhg6FESG5nDbi2en3AEpW6gk +lNiDb+EB4MMj29eZhRE9syR4mTNo50Aw9fnZYJ7IbpG9n4/w9G+D/Jt8IJiu0BuRlxi VuToNrpeAKBiSMDTlGZlWBeHnY6XrEHy21byYEMEKXUvPBY42//idO9icyoqtlUwIvYm CuHA== 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=E1l1QgF6zfOZzbE+eSbBQmnI+ytPMzfr7aN2N6xhlJk=; b=cgs2hlAp6VK55/aEW7aQe0YuAA9WmcrDRx6a+wU1m6S3ETPztnKZOjj/fnRW219DW3 CIRDcTyKvYSYVmeHjfXntwktj10poWGpcjp+o8e8xR7zPg4RLLRHDIlBgLI2wM/zbgu7 +qAkVhAnXsLGeuOKl4a8AyOe7i24NOwyE4sb87AqRHtcEUw2gBUGnWz65+SvJtbw+oau qVlQFWuSjk0GAGV8v5CaQCwgHQshkN63TccF9AYw5nAwOii/9R1IPDZ5C74Tf0r+9uhK whA/yawyg3zTKC9YKMrQ95LXoFOMUD8gAvDuRiHIffZecM/E/J1ost3Ortqp//8xVtBh MdTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=rYLVMPBG; 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 142-v6si6827808pga.694.2018.05.28.09.27.02; Mon, 28 May 2018 09:27:17 -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=rYLVMPBG; 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 S940031AbeE1Q0c (ORCPT + 99 others); Mon, 28 May 2018 12:26:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:58734 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935073AbeE1KKd (ORCPT ); Mon, 28 May 2018 06:10:33 -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 0F008208B4; Mon, 28 May 2018 10:10:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502232; bh=bZLQy/AQu7azcfATS/h8S/rEuZPT3jAiLylwq/oMUPY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rYLVMPBGUT2JcRkKhtNXeGoAOhiKy4IzuvmQAh3+NfwIHDFHftdngocvjtTGYhbta CeqysLVuWSLI/syakjNO3ksdOfOcjQ0pjU+J9PRFeseDpx6BXK5q9BTMS6+pZ+fUM1 OHLRq6dNl9vEJ8H6K/Q7yrC9Ftof3Kp8lIAXpb4s= 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 3.18 111/185] swap: divide-by-zero when zero length swap file on ssd Date: Mon, 28 May 2018 12:02:32 +0200 Message-Id: <20180528100101.226333546@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100050.700971285@linuxfoundation.org> References: <20180528100050.700971285@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 3.18-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 @@ -2217,6 +2217,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),