xref: /linux/fs/backing-file.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Common helpers for stackable filesystems and backing files.
4  *
5  * Forked from fs/overlayfs/file.c.
6  *
7  * Copyright (C) 2017 Red Hat, Inc.
8  * Copyright (C) 2023 CTERA Networks.
9  */
10 
11 #include <linux/fs.h>
12 #include <linux/backing-file.h>
13 #include <linux/splice.h>
14 #include <linux/mm.h>
15 #include <linux/security.h>
16 
17 #include "internal.h"
18 
19 /**
20  * backing_file_open - open a backing file for kernel internal use
21  * @user_file:  file the user requested to open
22  * @flags:	open flags
23  * @real_path:	path of the backing file
24  * @cred:	credentials for open
25  *
26  * Open a backing file for a stackable filesystem (e.g., overlayfs).
27  * @user_file->f_path may be on the stackable filesystem and @real_path
28  * on the underlying filesystem. In this case, we want to be able to
29  * return the path of the stackable filesystem. This is done by
30  * embedding the returned file into a container structure that also
31  * stores the stacked file's path, which can be retrieved using
32  * backing_file_user_path().
33  */
34 struct file *backing_file_open(const struct file *user_file, int flags,
35 			       const struct path *real_path,
36 			       const struct cred *cred)
37 {
38 	const struct path *user_path = file_user_path(user_file);
39 	struct inode *real_inode = d_inode(real_path->dentry);
40 	struct file *f;
41 	int error;
42 
43 	/*
44 	 * may_open() checked the inode of the user file, which need not carry
45 	 * S_APPEND and S_IMMUTABLE of the backing inode.
46 	 */
47 	if ((flags & O_ACCMODE) != O_RDONLY) {
48 		if (IS_IMMUTABLE(real_inode))
49 			return ERR_PTR(-EPERM);
50 		if (IS_APPEND(real_inode) && !(flags & O_APPEND))
51 			return ERR_PTR(-EPERM);
52 	}
53 
54 	f = alloc_empty_backing_file(flags, cred, user_file);
55 	if (IS_ERR(f))
56 		return f;
57 
58 	/*
59 	 * A user file that must not generate fsnotify events (e.g. the fd
60 	 * that fanotify creates for a permission event with
61 	 * dentry_open_nonotify()) must not generate them through its backing
62 	 * file either. Otherwise a listener watching the backing filesystem
63 	 * gets a nested permission event from its own read(2), which only
64 	 * another reader of the same group can answer.
65 	 */
66 	if (FMODE_FSNOTIFY_NONE(user_file->f_mode))
67 		file_set_fsnotify_mode(f, FMODE_NONOTIFY);
68 
69 	path_get(user_path);
70 	backing_file_set_user_path(f, user_path);
71 	error = vfs_open(real_path, f);
72 	if (error) {
73 		fput(f);
74 		f = ERR_PTR(error);
75 	}
76 
77 	return f;
78 }
79 EXPORT_SYMBOL_GPL(backing_file_open);
80 
81 struct file *backing_tmpfile_open(const struct file *user_file, int flags,
82 				  const struct path *real_parentpath,
83 				  umode_t mode, const struct cred *cred)
84 {
85 	const struct mnt_idmap *real_idmap = mnt_idmap(real_parentpath->mnt);
86 	const struct path *user_path = &user_file->f_path;
87 	struct file *f;
88 	int error;
89 
90 	f = alloc_empty_backing_file(flags, cred, user_file);
91 	if (IS_ERR(f))
92 		return f;
93 
94 	path_get(user_path);
95 	backing_file_set_user_path(f, user_path);
96 	error = vfs_tmpfile(real_idmap, real_parentpath, f, mode);
97 	if (error) {
98 		fput(f);
99 		f = ERR_PTR(error);
100 	}
101 	return f;
102 }
103 EXPORT_SYMBOL(backing_tmpfile_open);
104 
105 struct backing_aio {
106 	struct kiocb iocb;
107 	refcount_t ref;
108 	struct kiocb *orig_iocb;
109 	/* used for aio completion */
110 	bool is_write;
111 	void (*end_write)(struct kiocb *iocb, ssize_t);
112 	struct work_struct work;
113 	long res;
114 };
115 
116 static struct kmem_cache *backing_aio_cachep;
117 
118 /*
119  * RWF_NOAPPEND stays set in ki_flags.  Pass it on, because the backing file is
120  * in append mode as well and a new kiocb for it starts with IOCB_APPEND.
121  */
122 #define BACKING_IOCB_MASK \
123 	(IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND | \
124 	 (__force int) RWF_NOAPPEND)
125 
126 static rwf_t iocb_to_rw_flags(int flags)
127 {
128 	return (__force rwf_t)(flags & BACKING_IOCB_MASK);
129 }
130 
131 static void backing_aio_put(struct backing_aio *aio)
132 {
133 	if (refcount_dec_and_test(&aio->ref)) {
134 		fput(aio->iocb.ki_filp);
135 		kmem_cache_free(backing_aio_cachep, aio);
136 	}
137 }
138 
139 static void backing_aio_cleanup(struct backing_aio *aio, long res)
140 {
141 	struct kiocb *iocb = &aio->iocb;
142 	struct kiocb *orig_iocb = aio->orig_iocb;
143 
144 	orig_iocb->ki_pos = iocb->ki_pos;
145 	if (aio->end_write)
146 		aio->end_write(orig_iocb, res);
147 
148 	backing_aio_put(aio);
149 }
150 
151 static void backing_aio_rw_complete(struct kiocb *iocb, long res)
152 {
153 	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
154 	struct kiocb *orig_iocb = aio->orig_iocb;
155 
156 	/* Pairs with kiocb_start_write() in vfs_iocb_iter_write() */
157 	if (aio->is_write)
158 		kiocb_end_write(iocb);
159 
160 	backing_aio_cleanup(aio, res);
161 	orig_iocb->ki_complete(orig_iocb, res);
162 }
163 
164 static void backing_aio_complete_work(struct work_struct *work)
165 {
166 	struct backing_aio *aio = container_of(work, struct backing_aio, work);
167 
168 	backing_aio_rw_complete(&aio->iocb, aio->res);
169 }
170 
171 static void backing_aio_queue_completion(struct kiocb *iocb, long res)
172 {
173 	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
174 
175 	/*
176 	 * Punt to a work queue to serialize updates of mtime/size.
177 	 */
178 	aio->res = res;
179 	INIT_WORK(&aio->work, backing_aio_complete_work);
180 	queue_work(file_inode(aio->orig_iocb->ki_filp)->i_sb->s_dio_done_wq,
181 		   &aio->work);
182 }
183 
184 static int backing_aio_init_wq(struct kiocb *iocb)
185 {
186 	struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
187 
188 	if (sb->s_dio_done_wq)
189 		return 0;
190 
191 	return sb_init_dio_done_wq(sb);
192 }
193 
194 static int do_backing_file_read_iter(struct file *file, struct iov_iter *iter,
195 				     struct kiocb *iocb, int flags)
196 {
197 	struct backing_aio *aio = NULL;
198 	int ret;
199 
200 	if (is_sync_kiocb(iocb)) {
201 		rwf_t rwf = iocb_to_rw_flags(flags);
202 
203 		return vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
204 	}
205 
206 	aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
207 	if (!aio)
208 		return -ENOMEM;
209 
210 	aio->orig_iocb = iocb;
211 	kiocb_clone(&aio->iocb, iocb, get_file(file));
212 	aio->iocb.ki_complete = backing_aio_rw_complete;
213 	refcount_set(&aio->ref, 2);
214 	ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
215 	backing_aio_put(aio);
216 	if (ret != -EIOCBQUEUED)
217 		backing_aio_cleanup(aio, ret);
218 	return ret;
219 }
220 
221 ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
222 			       struct kiocb *iocb, int flags,
223 			       struct backing_file_ctx *ctx)
224 {
225 	ssize_t ret;
226 
227 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
228 		return -EIO;
229 
230 	if (!iov_iter_count(iter))
231 		return 0;
232 
233 	if (iocb->ki_flags & IOCB_DIRECT &&
234 	    !(file->f_mode & FMODE_CAN_ODIRECT))
235 		return -EINVAL;
236 
237 	scoped_with_creds(ctx->cred)
238 		ret = do_backing_file_read_iter(file, iter, iocb, flags);
239 
240 	if (ctx->accessed)
241 		ctx->accessed(iocb->ki_filp);
242 
243 	return ret;
244 }
245 EXPORT_SYMBOL_GPL(backing_file_read_iter);
246 
247 static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
248 				      struct kiocb *iocb, int flags,
249 				      void (*end_write)(struct kiocb *, ssize_t))
250 {
251 	struct backing_aio *aio;
252 	int ret;
253 
254 	if (is_sync_kiocb(iocb)) {
255 		rwf_t rwf = iocb_to_rw_flags(flags);
256 
257 		ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
258 		if (end_write)
259 			end_write(iocb, ret);
260 		return ret;
261 	}
262 
263 	ret = backing_aio_init_wq(iocb);
264 	if (ret)
265 		return ret;
266 
267 	aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
268 	if (!aio)
269 		return -ENOMEM;
270 
271 	aio->orig_iocb = iocb;
272 	aio->is_write = true;
273 	aio->end_write = end_write;
274 	kiocb_clone(&aio->iocb, iocb, get_file(file));
275 	aio->iocb.ki_flags = flags;
276 	aio->iocb.ki_complete = backing_aio_queue_completion;
277 	refcount_set(&aio->ref, 2);
278 	ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
279 	backing_aio_put(aio);
280 	if (ret != -EIOCBQUEUED)
281 		backing_aio_cleanup(aio, ret);
282 	return ret;
283 }
284 
285 ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
286 				struct kiocb *iocb, int flags,
287 				struct backing_file_ctx *ctx)
288 {
289 	ssize_t ret;
290 
291 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
292 		return -EIO;
293 
294 	if (!iov_iter_count(iter))
295 		return 0;
296 
297 	/*
298 	 * F_SETFL and RWF_NOAPPEND can take append mode away from the user
299 	 * file. They checked its inode, not the backing one.
300 	 */
301 	if ((file->f_flags & O_APPEND) && !(flags & IOCB_APPEND) &&
302 	    IS_APPEND(file_inode(file)))
303 		return -EPERM;
304 
305 	ret = file_remove_privs(iocb->ki_filp);
306 	if (ret)
307 		return ret;
308 
309 	if (iocb->ki_flags & IOCB_DIRECT &&
310 	    !(file->f_mode & FMODE_CAN_ODIRECT))
311 		return -EINVAL;
312 
313 	scoped_with_creds(ctx->cred)
314 		return do_backing_file_write_iter(file, iter, iocb, flags, ctx->end_write);
315 }
316 EXPORT_SYMBOL_GPL(backing_file_write_iter);
317 
318 ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb,
319 				 struct pipe_inode_info *pipe, size_t len,
320 				 unsigned int flags,
321 				 struct backing_file_ctx *ctx)
322 {
323 	ssize_t ret;
324 
325 	if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
326 		return -EIO;
327 
328 	scoped_with_creds(ctx->cred)
329 		ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags);
330 
331 	if (ctx->accessed)
332 		ctx->accessed(iocb->ki_filp);
333 
334 	return ret;
335 }
336 EXPORT_SYMBOL_GPL(backing_file_splice_read);
337 
338 ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
339 				  struct file *out, struct kiocb *iocb,
340 				  size_t len, unsigned int flags,
341 				  struct backing_file_ctx *ctx)
342 {
343 	ssize_t ret;
344 
345 	if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
346 		return -EIO;
347 
348 	if (!out->f_op->splice_write)
349 		return -EINVAL;
350 
351 	/* splice refuses a user file in append mode, so F_SETFL cleared it */
352 	if ((out->f_flags & O_APPEND) && IS_APPEND(file_inode(out)))
353 		return -EPERM;
354 
355 	ret = file_remove_privs(iocb->ki_filp);
356 	if (ret)
357 		return ret;
358 
359 	scoped_with_creds(ctx->cred) {
360 		file_start_write(out);
361 		ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags);
362 		file_end_write(out);
363 	}
364 
365 	if (ctx->end_write)
366 		ctx->end_write(iocb, ret);
367 
368 	return ret;
369 }
370 EXPORT_SYMBOL_GPL(backing_file_splice_write);
371 
372 int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
373 		      struct backing_file_ctx *ctx)
374 {
375 	struct file *user_file = vma->vm_file;
376 	int ret;
377 
378 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
379 		return -EIO;
380 
381 	if (!can_mmap_file(file))
382 		return -ENODEV;
383 
384 	/* do_mmap() checked the inode of the user file */
385 	if ((vma->vm_flags & VM_SHARED) && IS_APPEND(file_inode(file)))
386 		return -EACCES;
387 
388 	vma_set_file(vma, file);
389 
390 	scoped_with_creds(ctx->cred) {
391 		ret = security_mmap_backing_file(vma, file, user_file);
392 		if (ret)
393 			return ret;
394 
395 		ret = vfs_mmap(vma->vm_file, vma);
396 	}
397 
398 	if (ctx->accessed)
399 		ctx->accessed(user_file);
400 
401 	return ret;
402 }
403 EXPORT_SYMBOL_GPL(backing_file_mmap);
404 
405 static int __init backing_aio_init(void)
406 {
407 	backing_aio_cachep = KMEM_CACHE(backing_aio, SLAB_HWCACHE_ALIGN);
408 	if (!backing_aio_cachep)
409 		return -ENOMEM;
410 
411 	return 0;
412 }
413 fs_initcall(backing_aio_init);
414