From patchwork Thu Dec 1 10:15:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 9455737 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 7D35360515 for ; Thu, 1 Dec 2016 10:16:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6DA5D284C6 for ; Thu, 1 Dec 2016 10:16:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 62840284CF; Thu, 1 Dec 2016 10:16:11 +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 15261284C6 for ; Thu, 1 Dec 2016 10:16:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756824AbcLAKPz (ORCPT ); Thu, 1 Dec 2016 05:15:55 -0500 Received: from mail-wj0-f179.google.com ([209.85.210.179]:33524 "EHLO mail-wj0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756322AbcLAKPP (ORCPT ); Thu, 1 Dec 2016 05:15:15 -0500 Received: by mail-wj0-f179.google.com with SMTP id xy5so199960722wjc.0 for ; Thu, 01 Dec 2016 02:15:14 -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=kcdW1OvhdtKvFA06Qezgw7CrcZ2zbTwwcIuFU6TM9GM=; b=TeKJDZmL19rkN8/C+k8VH1FFXx3oTN3/Kup5T8Ce7KrJ7a3sOiTB09Zi3O1gJEvxYm ym/bI5FeCh22yk0Ke01lVlRCJucYKDJpgeH6pTMFqAgTGtTkHGerlVm5IY2ld3tZsw2A F+4XL6jEqDIlLmr+6XQ/peVDfij8vUm9rEIec7QWe2qwqEfZpWutpLLUd7Ysi9dF9AVi Wi5rGLDA8pZLcfYBbAvGcW4XrLrI+H3gE0X4AU0Q73skgR+4QXC+RNoe0LS7mJaeFpL/ y+ufoV1mNRtM5oxxi5vi1uo+3iiviugh5X6KNE/HVtxbbhUgYitvwbU3lu6nQFQ7CZg8 v6OA== X-Gm-Message-State: AKaTC005iLcSZ1JfNvC8dSdUlkAgIoF21qlP78KrRJhJtvgXiKSY+C2FFEM8pMNJqHwNG4sx X-Received: by 10.194.71.17 with SMTP id q17mr3823134wju.180.1480587314080; Thu, 01 Dec 2016 02:15:14 -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 ab10sm77218141wjc.45.2016.12.01.02.15.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Dec 2016 02:15:13 -0800 (PST) From: Miklos Szeredi To: Al Viro Cc: linux-unionfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 6/8] ovl: intercept mmap Date: Thu, 1 Dec 2016 11:15:01 +0100 Message-Id: <1480587303-25088-7-git-send-email-mszeredi@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1480587303-25088-1-git-send-email-mszeredi@redhat.com> References: <1480587303-25088-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 a2d5b3d03e14..210838f97777 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; @@ -433,9 +453,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; /*