From patchwork Thu Nov 24 10:55:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 9445207 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 293E86075F for ; Thu, 24 Nov 2016 11:04:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 281FD27CF5 for ; Thu, 24 Nov 2016 11:04:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1BE1927D45; Thu, 24 Nov 2016 11:04:39 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AF90627CF5 for ; Thu, 24 Nov 2016 11:04:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964905AbcKXLEW (ORCPT ); Thu, 24 Nov 2016 06:04:22 -0500 Received: from mail-wj0-f177.google.com ([209.85.210.177]:35968 "EHLO mail-wj0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964890AbcKXLEV (ORCPT ); Thu, 24 Nov 2016 06:04:21 -0500 Received: by mail-wj0-f177.google.com with SMTP id qp4so30619760wjc.3 for ; Thu, 24 Nov 2016 03:04:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SK21SWdNMTEeweLdS5g3Tlee4Q0NpxDeXDI+tkI25rQ=; b=dUhhVYDw1g+OGZ/Rqacsv6uUfYAaoSi5HSLlmkHp8aa7a2aPg6ww+8i8Ha76awCnD/ 3bwp1Fxwlps5H/oopqXxpiiCNmPeo57RcceqBKYvvXSG7kw/bVoKMWCNcG28LSGZ6DOU brmvE+oRS+XOlbxPpFZFiF+Hh7atcdPA8L1kQ2fMH5FJxBLkqNDmfCzO+a+aEnqkWkAH 6TZtS3XNNlpXAR2GZgmlK28wmUL6LFgwc9h1JOQMaM+TgVI0RW+zOOfJ4sD/uDFwh4Sl IjDId4HV5qk0dfx3CqjmzKFsfa9wqhiD060u2cnmVSmV6N0e0H+rT6bz+skEDUF7a8Z8 C3lw== X-Gm-Message-State: AKaTC014S/j/fPLU4e5hXCRi5h7mCEv/t4fzY7AP29QZ1ulYQziBIGMBcw0lDOjYbWxvLlt+ X-Received: by 10.194.87.230 with SMTP id bb6mr1890776wjb.163.1479984957551; Thu, 24 Nov 2016 02:55:57 -0800 (PST) Received: from veci.piliscsaba.szeredi.hu (pool-dsl-2c-0018.externet.hu. [217.173.44.24]) by smtp.gmail.com with ESMTPSA id g73sm7587416wme.16.2016.11.24.02.55.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Nov 2016 02:55:56 -0800 (PST) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/7] ovl: intercept mmap Date: Thu, 24 Nov 2016 11:55:43 +0100 Message-Id: <1479984944-1017-8-git-send-email-mszeredi@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1479984944-1017-1-git-send-email-mszeredi@redhat.com> References: <1479984944-1017-1-git-send-email-mszeredi@redhat.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP ... in order to handle the corner case when the file is copied up after being opened read-only and mapped shared. Can be verified with the following script: - 8< - - - - - 8< - - - - - 8< - - - - - 8< - - - - cd / rm -rf /tmp/ovl-rorw-test mkdir /tmp/ovl-rorw-test cd /tmp/ovl-rorw-test cat << EOF > rorw-map.c #include #include #include #include #include int main(int argc, char *argv[]) { int rofd, rwfd; int ret; char buf[4]; char *addr; rofd = open(argv[1], O_RDONLY); if (rofd == -1) err(1, "ro open"); addr = mmap(NULL, 4, PROT_READ, MAP_SHARED, rofd, 0); if (addr == MAP_FAILED) err(1, "ro mmap"); if (memcmp(addr, "bubu", 4) == 0) errx(1, "identical startup data"); rwfd = open(argv[1], O_WRONLY); if (rwfd == -1) err(1, "rw open"); ret = write(rwfd, "bubu", 4); if (ret == -1) err(1, "write"); if (ret < 4) errx(1, "short write"); if (memcmp(addr, "bubu", 4) != 0) errx(1, "bad mmap data"); ret = read(rofd, buf, 4); if (ret == -1) err(1, "read"); if (ret < 4) errx(1, "short read"); if (memcmp(buf, "bubu", 4) != 0) errx(1, "bad read data"); return 0; } EOF gcc -o rorw-map rorw-map.c mkdir -p mnt lower upper work echo baba > lower/foo mount -t overlay overlay -olowerdir=lower,upperdir=upper,workdir=work mnt ./rorw-map mnt/foo umount mnt - 8< - - - - - 8< - - - - - 8< - - - - - 8< - - - - No output means success, "rorw-map: bad mmap data" means failure. Signed-off-by: Miklos Szeredi --- fs/overlayfs/inode.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c index 09c6f99bd5db..25b31c6ebe9e 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -392,6 +393,25 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *to) return ret; } +static int ovl_mmap(struct file *file, struct vm_area_struct *vma) +{ + if (likely(ovl_file_is_lower(file))) + return OVL_CALL_REAL_FOP(file, mmap(file, vma)); + + file = filp_clone_open(file); + if (IS_ERR(file)) + return PTR_ERR(file); + + fput(vma->vm_file); + /* transfer ref: */ + vma->vm_file = file; + + if (!file->f_op->mmap) + return -ENODEV; + + return file->f_op->mmap(file, vma); +} + static struct ovl_fops *ovl_fops_find(const struct file_operations *orig) { struct ovl_fops *ofop; @@ -431,9 +451,10 @@ static struct ovl_fops *ovl_fops_get(struct file *file) /* Intercept these: */ if (orig->read_iter) ofop->fops.read_iter = ovl_read_iter; + if (orig->mmap) + ofop->fops.mmap = ovl_mmap; /* These will need to be intercepted: */ - ofop->fops.mmap = orig->mmap; ofop->fops.fsync = orig->fsync; /*