From patchwork Fri Jun 28 18:35:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11023177 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 52A49112C for ; Fri, 28 Jun 2019 18:35:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1C59E28174 for ; Fri, 28 Jun 2019 18:35:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 105922837D; Fri, 28 Jun 2019 18:35:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D2AFC28346 for ; Fri, 28 Jun 2019 18:35:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 535F58E0009; Fri, 28 Jun 2019 14:35:28 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4BB738E0002; Fri, 28 Jun 2019 14:35:28 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E54F8E0009; Fri, 28 Jun 2019 14:35:28 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-io1-f78.google.com (mail-io1-f78.google.com [209.85.166.78]) by kanga.kvack.org (Postfix) with ESMTP id 0FF5B8E0002 for ; Fri, 28 Jun 2019 14:35:28 -0400 (EDT) Received: by mail-io1-f78.google.com with SMTP id y5so7564348ioj.10 for ; Fri, 28 Jun 2019 11:35:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=o7DgJUcGo9xVHV7BN6dTCgC24ypKZRenCunIj09I43o=; b=rBT7o02XSsg/al2tsP53Cz6ge+ep9N+zgtJggKctiEPV180dtgtee1klLnSXMgo8p2 R841ukE463aHzloeu76vTjpcC8ZbYLFoBcjAzhE1ITXO7+vLVT3Dv/Z2zNztVf97AaBQ eCeWCPXvZ1rlQv3C7a31YQkUl2D80MXaVGYtlZx2SifpCD1mpbuxYkAZQzKzWNKNZ5Bs GdQfaOzm6hV8A3hOsFwRhhFUAfohxRhwHhKzSXo4eBRQEJmav32o/zbiuUvi97RLazPQ cZydjauNI91GAptOtgMvveRNcAa7C2McY0m/IrL4BluuDIlIKUIJOjYjgTEEoYI9BNvn Yo/g== X-Gm-Message-State: APjAAAWs/An29OxvsiGbXBLMCcLxFaPDbD2qGhcYtsMMjBqeIDl3ePj3 E7MZnzlhCFNaUzy/tZpKDdfudOZZqeaPPKNHxc6d589MWgNPwQPCrAkpq9EnWAc4ovHDG8Lsrmw j3VAjCrPEG38ckJcPjXPOV5wvPih6XRXha3HvfPSxssrw4r3Sqa1B0wlzg1eDpe+p/A== X-Received: by 2002:a6b:3c0a:: with SMTP id k10mr8192701iob.271.1561746927844; Fri, 28 Jun 2019 11:35:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0mVNGMa4zTan4vcEAjeD1od1I3S649G2N1G5JpkJboU6XVnQL7AF3U2eW7Z5Cu069/I63 X-Received: by 2002:a6b:3c0a:: with SMTP id k10mr8192650iob.271.1561746927190; Fri, 28 Jun 2019 11:35:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561746927; cv=none; d=google.com; s=arc-20160816; b=P3v7stI6QuFYNtRZJxKjAQ4UkHFOqTCcuCqOpPQucmUyDOHHa2gJL7K1bVDGywviH6 m3a57KIGgOgSVOgX2/q1GTy0bx3E2JnDOgcsFiEXKt44EBr9cUI9aqayJJkQGu95r+AG /yNF4Qu4pQuWT2IlCR6YO+dtS1RE+EuaPTwm6RuJgqBcUxAZ//kbo6T/Gt1HWaNPXOEt AjGIgQwZY++fNuWRnL8yPyJ7RLx5rDFRUMKYpFjawnYkk363J+vogcpYzozT+U+0Y06K jlyZsyZ1/3tuYwjBbpI+KaU02Wa1iH7tTxe/xUCkct2LBkPR7lnPZH8mdkUKKUusBdAw h0Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=o7DgJUcGo9xVHV7BN6dTCgC24ypKZRenCunIj09I43o=; b=sGQwcPl8pKIGYiWh4eUsQQrO43PwT0aq3nrtkLP6FclCTDhSIyneKTSI2mY2IPUBD0 hcayg6trj5E9Lgmkb3YLFG/puAq+sP3ro1MsGOs94z7QPKlbNw8x+yB5BUqgNQRWHMEm mLhIZJZIlze11NTQhxeLp9QbuqYaYI3J5JDTWEhB68Kj/Vz0DeY0QZkYKMSt8tB216rD DVhk32xcy9GkpkKRy8ScOs9X1Lb1n4sWwLOlHXlj1yfZW0zP/sYGmMroAXKuQh46YMJ8 Oq+Rl3BdLyJzvXKfdL0zHatZ+FUVg82qPp0YgGuvOc70gFrI60dKElB6nlXbpGtvCuNB Tyeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=W7BT12TU; spf=pass (google.com: domain of darrick.wong@oracle.com designates 156.151.31.85 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from userp2120.oracle.com (userp2120.oracle.com. [156.151.31.85]) by mx.google.com with ESMTPS id m18si4635273jaa.95.2019.06.28.11.35.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2019 11:35:27 -0700 (PDT) Received-SPF: pass (google.com: domain of darrick.wong@oracle.com designates 156.151.31.85 as permitted sender) client-ip=156.151.31.85; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=W7BT12TU; spf=pass (google.com: domain of darrick.wong@oracle.com designates 156.151.31.85 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x5SIYGGJ114587; Fri, 28 Jun 2019 18:35:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=o7DgJUcGo9xVHV7BN6dTCgC24ypKZRenCunIj09I43o=; b=W7BT12TUQkRJc9zRIeLhgn2TNRvJwTMiDY2FRc1JZiV7LY8SgJhVvHyo87i11W9DBPa0 pdCDemTe0pkBz32X7/AXbMRQmMr+omDeTw82InHU3ID+hot/1bDViavs0AMQpu2xalrc /Mf0xks9kzi7JYAlYEHg/Hqdu98lhi5JaWG/YgM7H71rO7WVKKkOxQZ/7blOdeZgl/pr 9FUAQNbeoiYEsIiEjutUGPEdZXAyInYOFdWCkcqe2teTVjDL3kR2iF0Xyo34VLK9uzF6 2IvnN5yBkZH/gKUyJsWEo+UmyOPG6dvxFFdCOvc2rndw6PvnRWiSKpHisobdXsZiNLnJ MA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 2t9cyqxym1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jun 2019 18:35:20 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x5SIYqFH152196; Fri, 28 Jun 2019 18:35:19 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3020.oracle.com with ESMTP id 2t9p6w238c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jun 2019 18:35:19 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x5SIZJNC027332; Fri, 28 Jun 2019 18:35:19 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 28 Jun 2019 11:35:19 -0700 Subject: [PATCH 1/2] mm: set S_SWAPFILE on blockdev swap devices From: "Darrick J. Wong" To: hch@infradead.org, akpm@linux-foundation.org, tytso@mit.edu, viro@zeniv.linux.org.uk, darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Date: Fri, 28 Jun 2019 11:35:17 -0700 Message-ID: <156174691783.1557844.3238867236650883424.stgit@magnolia> In-Reply-To: <156174691124.1557844.14293659081769020256.stgit@magnolia> References: <156174691124.1557844.14293659081769020256.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9302 signatures=668688 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1906280209 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9302 signatures=668688 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1906280210 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong Set S_SWAPFILE on block device inodes so that they have the same protections as a swap flie. Signed-off-by: Darrick J. Wong --- mm/swapfile.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 596ac98051c5..fa4edd0cca3a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2284,9 +2284,8 @@ EXPORT_SYMBOL_GPL(add_swap_extent); * requirements, they are simply tossed out - we will never use those blocks * for swapping. * - * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This - * prevents root from shooting her foot off by ftruncating an in-use swapfile, - * which will scribble on the fs. + * For all swap devices we set S_SWAPFILE across the life of the swapon. This + * prevents users from writing to the swap device, which will corrupt memory. * * The amount of disk space which a single swap extent represents varies. * Typically it is in the 1-4 megabyte range. So we can have hundreds of @@ -2551,13 +2550,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) inode = mapping->host; if (S_ISBLK(inode->i_mode)) { struct block_device *bdev = I_BDEV(inode); + set_blocksize(bdev, old_block_size); blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); - } else { - inode_lock(inode); - inode->i_flags &= ~S_SWAPFILE; - inode_unlock(inode); } + + inode_lock(inode); + inode->i_flags &= ~S_SWAPFILE; + inode_unlock(inode); filp_close(swap_file, NULL); /* @@ -2780,11 +2780,11 @@ static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) p->flags |= SWP_BLKDEV; } else if (S_ISREG(inode->i_mode)) { p->bdev = inode->i_sb->s_bdev; - inode_lock(inode); - if (IS_SWAPFILE(inode)) - return -EBUSY; - } else - return -EINVAL; + } + + inode_lock(inode); + if (IS_SWAPFILE(inode)) + return -EBUSY; return 0; } @@ -3185,8 +3185,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) atomic_inc(&proc_poll_event); wake_up_interruptible(&proc_poll_wait); - if (S_ISREG(inode->i_mode)) - inode->i_flags |= S_SWAPFILE; + inode->i_flags |= S_SWAPFILE; error = 0; goto out; bad_swap: @@ -3208,7 +3207,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); if (swap_file) { - if (inode && S_ISREG(inode->i_mode)) { + if (inode) { inode_unlock(inode); inode = NULL; } @@ -3221,7 +3220,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } if (name) putname(name); - if (inode && S_ISREG(inode->i_mode)) + if (inode) inode_unlock(inode); if (!error) enable_swap_slots_cache(); From patchwork Fri Jun 28 18:35:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11023199 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A454F1609 for ; Fri, 28 Jun 2019 18:35:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 701D028174 for ; Fri, 28 Jun 2019 18:35:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6121428514; Fri, 28 Jun 2019 18:35:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3FDF328174 for ; Fri, 28 Jun 2019 18:35:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 074A78E000A; Fri, 28 Jun 2019 14:35:35 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 023F98E0002; Fri, 28 Jun 2019 14:35:34 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E30078E000A; Fri, 28 Jun 2019 14:35:34 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yw1-f78.google.com (mail-yw1-f78.google.com [209.85.161.78]) by kanga.kvack.org (Postfix) with ESMTP id C0BC28E0002 for ; Fri, 28 Jun 2019 14:35:34 -0400 (EDT) Received: by mail-yw1-f78.google.com with SMTP id j144so10034533ywa.15 for ; Fri, 28 Jun 2019 11:35:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=MiY2VYpSFpzY6WkWYo65+t0yY/S+2bgdM8Tfcy+RPHs=; b=MIdsp7CblbgcpxsqvAle89nSQvs+Tf9pc777pwYkAcsCzIbrz5+EuYjNCe+PxPyj9p zoh6jT8LFfS7kilgWokwKRqwWItR78if1KvMv2HD2EkxxBNN4xg2Z2Rhz2ud+qLZ32NT wfTNoGbJPbHysROS/v938vPMEZ2ENhA92ecmgOxl+b0SYkYnLgZsKQi65+02bWAeKudU REVSYvBMioRgfQXVi2YH52roR/MnUevgV7XJrl7ITNrhTO86bY+5FB/zI+deNzUs4SD9 ofWq37C4LZobhJ0IHPgxzHSJzYD4HYpHlCu5PxYC5eDqth+jXSgYZDcXhuWkSopdXhXi TOUw== X-Gm-Message-State: APjAAAXfEJDGx2G9OxTO47AKJvq+t+sobIy1IpfOtPqffdSmTLUiRHjL 7IPwrhUIXBcfJbLTCArVBqu479zxHwCsZsOseqx52oVqeb280eTm16848NpP8sbkIo1r0mnX+lm HWIGigmGPoeoDg/lJcdQdo0ggnvDYzHqvPvXE2/8Dgavt4mAPIjAkOW5MDMFHaidxnw== X-Received: by 2002:a25:748a:: with SMTP id p132mr2745169ybc.196.1561746934508; Fri, 28 Jun 2019 11:35:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqzwxLQulCx7UYDQTsJ0judh+piH5VkbiiEOSBlozwi3uRFfaZYahzAL0ZpZ8Y6bYXyspvfA X-Received: by 2002:a25:748a:: with SMTP id p132mr2745139ybc.196.1561746933897; Fri, 28 Jun 2019 11:35:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561746933; cv=none; d=google.com; s=arc-20160816; b=ECpJ/suOcq7+jiKdKtvmV6CzTL7Me4JDYh3K+DQkMm8a1dNKqQZwqnKFTzNqv8n1XU NjOjWV7FTqbELYCiOk6K2I+PdyXUbFfxFH3Qc9xIw4+iXhfTtlctVAd80L9BDzBnTRsY tGPFh5aIaNa8mDuJ9KXAjUH8Dqa4W9SdNBinjnAOrn6ISqZp7GrSae7d3z/jV7aJAAVJ tCO3SAcYzu4qXr+c57lh1qr3Tewhm0hAdZyWDUv101e9juRROJ5QHOEtSzAmE254+5gl 9g7fOUGwz5VieR4yiGXAmT+3j1PKbaz6TX74oOZ24s4IQueEo2EmOkvDQI702+FcexB8 IzIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=MiY2VYpSFpzY6WkWYo65+t0yY/S+2bgdM8Tfcy+RPHs=; b=QSAlsR/C3w1fn7u7Nd2lExLD5hSmnyiwm5EDALn6gZ6UTgwctplOUEGUFwR+FN216i TGdXKIUTDlWjuLrJ+lrIjJmmFUCxXfVtdMAvU19v9xR2WJb/YJfJHKdA/BPid6YN5JCp +xyFGZOwW651vuuR17QZcraaDGFHDP0VTX9kpvEqnDfKq6TCHB/ctQSprqkFbN18sEG3 wCYLOr1DcjFkqH6uhxvIPWZxP8lQWa0LNxNkV2gSmBiS0reuOCHsA6fKa33wtfo0ma76 NU2H/gQZU41s32baTsGfZGDPypnzlJreRW4fndhpyrjgf+wEbXNSGxJwB8QHMP3RFBcp rnsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=EsEaGQJI; spf=pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.78 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from aserp2120.oracle.com (aserp2120.oracle.com. [141.146.126.78]) by mx.google.com with ESMTPS id i10si1201890ybk.212.2019.06.28.11.35.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2019 11:35:33 -0700 (PDT) Received-SPF: pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.78 as permitted sender) client-ip=141.146.126.78; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=EsEaGQJI; spf=pass (google.com: domain of darrick.wong@oracle.com designates 141.146.126.78 as permitted sender) smtp.mailfrom=darrick.wong@oracle.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x5SIYILG027632; Fri, 28 Jun 2019 18:35:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=MiY2VYpSFpzY6WkWYo65+t0yY/S+2bgdM8Tfcy+RPHs=; b=EsEaGQJIsCrbPmH9OznS4Gvkxc1orFTlcEwxQExpwlUwRPJJ9/2dGYPxRHFWSzZmu3PI oz2WYxk4CJsXjWlDQqTaUTzi4QBK2xaXdZRS0x/XQ0n46azFi8QfHloIyya1nw/juBCv /GcNjv/IJGQrLp4Oz3RwgLvBPC4WljMtRCvxbicMqns6wNmSrHmhKHNqH77k4EAGUxdc /W1fzi3PzDN1nIDiHkcbLDcLd+ZiOZYxKdRPC5KmuODEGdubVjskPrZl2cKTFxlVG+N0 uztVyNgEzIwjOCyFmMrxhWXH+XJo1bOazdUdyCKnzZrU7GMwZOJbkKkRTvt6ps9SySM6 Vg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 2t9c9q72tx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jun 2019 18:35:27 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x5SIXjZ1001279; Fri, 28 Jun 2019 18:35:26 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userp3020.oracle.com with ESMTP id 2tat7e3gfe-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jun 2019 18:35:26 +0000 Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x5SIZP9g002352; Fri, 28 Jun 2019 18:35:25 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 28 Jun 2019 11:35:25 -0700 Subject: [PATCH 2/2] vfs: don't allow writes to swap files From: "Darrick J. Wong" To: hch@infradead.org, akpm@linux-foundation.org, tytso@mit.edu, viro@zeniv.linux.org.uk, darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Date: Fri, 28 Jun 2019 11:35:24 -0700 Message-ID: <156174692434.1557844.13804911834937629088.stgit@magnolia> In-Reply-To: <156174691124.1557844.14293659081769020256.stgit@magnolia> References: <156174691124.1557844.14293659081769020256.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9302 signatures=668688 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1906280209 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9302 signatures=668688 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1906280210 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong Don't let userspace write to an active swap file because the kernel effectively has a long term lease on the storage and things could get seriously corrupted if we let this happen. Signed-off-by: Darrick J. Wong --- fs/attr.c | 16 ++++++++-------- fs/block_dev.c | 3 +++ mm/filemap.c | 3 +++ mm/memory.c | 3 ++- mm/mmap.c | 2 ++ mm/swapfile.c | 12 +++++++++++- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/fs/attr.c b/fs/attr.c index 1fcfdcc5b367..7480d5dd22c0 100644 --- a/fs/attr.c +++ b/fs/attr.c @@ -134,6 +134,14 @@ EXPORT_SYMBOL(setattr_prepare); */ int inode_newsize_ok(const struct inode *inode, loff_t offset) { + /* + * Truncation of in-use swapfiles is disallowed - the kernel owns the + * disk space now. We must prevent subsequent swapout to scribble on + * the now-freed blocks. + */ + if (IS_SWAPFILE(inode) && inode->i_size != offset) + return -ETXTBSY; + if (inode->i_size < offset) { unsigned long limit; @@ -142,14 +150,6 @@ int inode_newsize_ok(const struct inode *inode, loff_t offset) goto out_sig; if (offset > inode->i_sb->s_maxbytes) goto out_big; - } else { - /* - * truncation of in-use swapfiles is disallowed - it would - * cause subsequent swapout to scribble on the now-freed - * blocks. - */ - if (IS_SWAPFILE(inode)) - return -ETXTBSY; } return 0; diff --git a/fs/block_dev.c b/fs/block_dev.c index 749f5984425d..f57d15e5338b 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1948,6 +1948,9 @@ ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) if (bdev_read_only(I_BDEV(bd_inode))) return -EPERM; + if (IS_SWAPFILE(bd_inode)) + return -ETXTBSY; + if (!iov_iter_count(from)) return 0; diff --git a/mm/filemap.c b/mm/filemap.c index dad85e10f5f8..fd80bc20e30a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2938,6 +2938,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) if (IS_IMMUTABLE(inode)) return -EPERM; + if (IS_SWAPFILE(inode)) + return -ETXTBSY; + if (!iov_iter_count(from)) return 0; diff --git a/mm/memory.c b/mm/memory.c index abf795277f36..5acb5bb04e21 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2236,7 +2236,8 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE; if (vmf->vma->vm_file && - IS_IMMUTABLE(vmf->vma->vm_file->f_mapping->host)) + (IS_IMMUTABLE(vmf->vma->vm_file->f_mapping->host) || + IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))) return VM_FAULT_SIGBUS; ret = vmf->vma->vm_ops->page_mkwrite(vmf); diff --git a/mm/mmap.c b/mm/mmap.c index b3ebca2702bf..1abe55822324 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1488,6 +1488,8 @@ unsigned long do_mmap(struct file *file, unsigned long addr, return -EACCES; if (IS_IMMUTABLE(file->f_mapping->host)) return -EPERM; + if (IS_SWAPFILE(file->f_mapping->host)) + return -ETXTBSY; } /* diff --git a/mm/swapfile.c b/mm/swapfile.c index fa4edd0cca3a..1fc820c71baf 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3165,6 +3165,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) if (error) goto bad_swap; + /* + * Flush any pending IO and dirty mappings before we start using this + * swap device. + */ + inode->i_flags |= S_SWAPFILE; + error = inode_drain_writes(inode); + if (error) { + inode->i_flags &= ~S_SWAPFILE; + goto bad_swap; + } + mutex_lock(&swapon_mutex); prio = -1; if (swap_flags & SWAP_FLAG_PREFER) @@ -3185,7 +3196,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) atomic_inc(&proc_poll_event); wake_up_interruptible(&proc_poll_wait); - inode->i_flags |= S_SWAPFILE; error = 0; goto out; bad_swap: