From badfc4d0051bc4fb7548e877859adfc22b60c896 Mon Sep 17 00:00:00 2001 From: Paul Lawrence Date: Tue, 4 Apr 2023 17:04:55 +0000 Subject: [PATCH] ANDROID: fuse: Support errors from fuse daemon in canonical path Previously errors from the daemon in FUSE_CANONICAL_PATH were simply ignored. In order to block inotifys, it is useful to be able to return errors from this opcode. [drosen: I've combined this patch with a few others that fix dealing with error codes (mostly ENOSYS) to make it harder to lose these again in the future: ANDROID: fsnotify: Do not notify lower fs of open when ENOSYS ANDROID: fuse: Skip canonical path logic if ENOSYS ANDROID: fuse: Fix the issue of fuse_dentry_canonical_path ] Bug: 238619640 Test: inotify no longer works on /storage/emulated/0/Android/media but does on child folders Bug: 326995824 Test: cts-tradefed run commandAndExit cts -m CtsOsTestCases -t android.os.cts.FileObserverTest Bug: 330136711 Test: ./cts-tradefed run commandAndExit cts -m CtsIncidentHostTestCases -t com.android.server.cts.ErrorsTest#testTombstone Signed-off-by: Paul Lawrence Change-Id: Icb15c090c6286c174338471a787712f8388de316 --- fs/fuse/dev.c | 2 +- fs/fuse/dir.c | 18 ++++++++++++++---- fs/fuse/fuse_i.h | 3 +++ fs/notify/inotify/inotify_user.c | 12 +++++++++--- include/linux/dcache.h | 2 +- include/linux/fsnotify.h | 18 ++++++++++++------ 6 files changed, 40 insertions(+), 15 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 2dcd57f16c64..aadf77671ede 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -2041,7 +2041,7 @@ static ssize_t fuse_dev_do_write(struct fuse_dev *fud, err = copy_out_args(cs, req->args, nbytes); fuse_copy_finish(cs); - if (!err && req->in.h.opcode == FUSE_CANONICAL_PATH) { + if (!err && req->in.h.opcode == FUSE_CANONICAL_PATH && !oh.error) { char *path = (char *)req->args->out_args[0].value; path[req->args->out_args[0].size - 1] = 0; diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index d455b5d0b2f3..d55ca21cd454 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -333,7 +333,7 @@ static struct vfsmount *fuse_dentry_automount(struct path *path) * look up paths on its own. Instead, we handle the lookup as a special case * inside of the write request. */ -static void fuse_dentry_canonical_path(const struct path *path, +static int fuse_dentry_canonical_path(const struct path *path, struct path *canonical_path) { struct inode *inode = d_inode(path->dentry); @@ -343,9 +343,12 @@ static void fuse_dentry_canonical_path(const struct path *path, char *path_name; int err; + if (fm->fc->no_dentry_canonical_path) + goto out; + path_name = (char *)get_zeroed_page(GFP_KERNEL); if (!path_name) - goto default_path; + return -ENOMEM; args.opcode = FUSE_CANONICAL_PATH; args.nodeid = get_node_id(inode); @@ -359,11 +362,18 @@ static void fuse_dentry_canonical_path(const struct path *path, err = fuse_simple_request(fm, &args); free_page((unsigned long)path_name); if (err > 0) - return; -default_path: + return 0; + if (err < 0 && err != -ENOSYS) + return err; + + if (err == -ENOSYS) + fm->fc->no_dentry_canonical_path = 1; + +out: canonical_path->dentry = path->dentry; canonical_path->mnt = path->mnt; path_get(canonical_path); + return 0; } const struct dentry_operations fuse_dentry_operations = { diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 2496b90c6910..173dc2008af2 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -773,6 +773,9 @@ struct fuse_conn { /** Is bmap not implemented by fs? */ unsigned no_bmap:1; + /** Is dentry_canonical_path not implemented by fs? */ + unsigned no_dentry_canonical_path:1; + /** Is poll not implemented by fs? */ unsigned no_poll:1; diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c index 35e80fd6edb7..2d17b3b7e21b 100644 --- a/fs/notify/inotify/inotify_user.c +++ b/fs/notify/inotify/inotify_user.c @@ -783,10 +783,15 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, /* support stacked filesystems */ if (path.dentry && path.dentry->d_op) { if (path.dentry->d_op->d_canonical_path) { - path.dentry->d_op->d_canonical_path(&path, + ret = path.dentry->d_op->d_canonical_path(&path, &alteredpath); - canonical_path = &alteredpath; - path_put(&path); + if (ret != -ENOSYS) { + if (ret) { + goto path_put_and_out; + } + canonical_path = &alteredpath; + path_put(&path); + } } } @@ -796,6 +801,7 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, /* create/update an inode mark */ ret = inotify_update_watch(group, inode, mask); +path_put_and_out: path_put(canonical_path); fput_and_out: fdput(f); diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 4a33d0b0b8cf..97bed2a15ec7 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -150,7 +150,7 @@ struct dentry_operations { struct vfsmount *(*d_automount)(struct path *); int (*d_manage)(const struct path *, bool); struct dentry *(*d_real)(struct dentry *, enum d_real_type type); - void (*d_canonical_path)(const struct path *, struct path *); + int (*d_canonical_path)(const struct path *, struct path *); } ____cacheline_aligned; /* diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index b57568d66fc4..9161264db36c 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -137,13 +137,19 @@ static inline int fsnotify_file(struct file *file, __u32 mask) struct path lower_path; int ret; - path->dentry->d_op->d_canonical_path(path, &lower_path); - ret = fsnotify_parent(lower_path.dentry, mask, - &lower_path, FSNOTIFY_EVENT_PATH); - path_put(&lower_path); + ret = path->dentry->d_op->d_canonical_path(path, + &lower_path); + if (ret != -ENOSYS) { + if (ret) + return ret; - if (ret) - return ret; + ret = fsnotify_parent(lower_path.dentry, mask, + &lower_path, FSNOTIFY_EVENT_PATH); + path_put(&lower_path); + + if (ret) + return ret; + } } }