xref: /linux/fs/backing-file.c (revision c3d355f427cea70fbe31b49e21f1915dbf37f2e6)
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 #define BACKING_IOCB_MASK \
119 	(IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND)
120 
121 static rwf_t iocb_to_rw_flags(int flags)
122 {
123 	return (__force rwf_t)(flags & BACKING_IOCB_MASK);
124 }
125 
126 static void backing_aio_put(struct backing_aio *aio)
127 {
128 	if (refcount_dec_and_test(&aio->ref)) {
129 		fput(aio->iocb.ki_filp);
130 		kmem_cache_free(backing_aio_cachep, aio);
131 	}
132 }
133 
134 static void backing_aio_cleanup(struct backing_aio *aio, long res)
135 {
136 	struct kiocb *iocb = &aio->iocb;
137 	struct kiocb *orig_iocb = aio->orig_iocb;
138 
139 	orig_iocb->ki_pos = iocb->ki_pos;
140 	if (aio->end_write)
141 		aio->end_write(orig_iocb, res);
142 
143 	backing_aio_put(aio);
144 }
145 
146 static void backing_aio_rw_complete(struct kiocb *iocb, long res)
147 {
148 	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
149 	struct kiocb *orig_iocb = aio->orig_iocb;
150 
151 	/* Pairs with kiocb_start_write() in vfs_iocb_iter_write() */
152 	if (aio->is_write)
153 		kiocb_end_write(iocb);
154 
155 	backing_aio_cleanup(aio, res);
156 	orig_iocb->ki_complete(orig_iocb, res);
157 }
158 
159 static void backing_aio_complete_work(struct work_struct *work)
160 {
161 	struct backing_aio *aio = container_of(work, struct backing_aio, work);
162 
163 	backing_aio_rw_complete(&aio->iocb, aio->res);
164 }
165 
166 static void backing_aio_queue_completion(struct kiocb *iocb, long res)
167 {
168 	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
169 
170 	/*
171 	 * Punt to a work queue to serialize updates of mtime/size.
172 	 */
173 	aio->res = res;
174 	INIT_WORK(&aio->work, backing_aio_complete_work);
175 	queue_work(file_inode(aio->orig_iocb->ki_filp)->i_sb->s_dio_done_wq,
176 		   &aio->work);
177 }
178 
179 static int backing_aio_init_wq(struct kiocb *iocb)
180 {
181 	struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
182 
183 	if (sb->s_dio_done_wq)
184 		return 0;
185 
186 	return sb_init_dio_done_wq(sb);
187 }
188 
189 static int do_backing_file_read_iter(struct file *file, struct iov_iter *iter,
190 				     struct kiocb *iocb, int flags)
191 {
192 	struct backing_aio *aio = NULL;
193 	int ret;
194 
195 	if (is_sync_kiocb(iocb)) {
196 		rwf_t rwf = iocb_to_rw_flags(flags);
197 
198 		return vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
199 	}
200 
201 	aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
202 	if (!aio)
203 		return -ENOMEM;
204 
205 	aio->orig_iocb = iocb;
206 	kiocb_clone(&aio->iocb, iocb, get_file(file));
207 	aio->iocb.ki_complete = backing_aio_rw_complete;
208 	refcount_set(&aio->ref, 2);
209 	ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
210 	backing_aio_put(aio);
211 	if (ret != -EIOCBQUEUED)
212 		backing_aio_cleanup(aio, ret);
213 	return ret;
214 }
215 
216 ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
217 			       struct kiocb *iocb, int flags,
218 			       struct backing_file_ctx *ctx)
219 {
220 	ssize_t ret;
221 
222 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
223 		return -EIO;
224 
225 	if (!iov_iter_count(iter))
226 		return 0;
227 
228 	if (iocb->ki_flags & IOCB_DIRECT &&
229 	    !(file->f_mode & FMODE_CAN_ODIRECT))
230 		return -EINVAL;
231 
232 	scoped_with_creds(ctx->cred)
233 		ret = do_backing_file_read_iter(file, iter, iocb, flags);
234 
235 	if (ctx->accessed)
236 		ctx->accessed(iocb->ki_filp);
237 
238 	return ret;
239 }
240 EXPORT_SYMBOL_GPL(backing_file_read_iter);
241 
242 static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
243 				      struct kiocb *iocb, int flags,
244 				      void (*end_write)(struct kiocb *, ssize_t))
245 {
246 	struct backing_aio *aio;
247 	int ret;
248 
249 	if (is_sync_kiocb(iocb)) {
250 		rwf_t rwf = iocb_to_rw_flags(flags);
251 
252 		ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
253 		if (end_write)
254 			end_write(iocb, ret);
255 		return ret;
256 	}
257 
258 	ret = backing_aio_init_wq(iocb);
259 	if (ret)
260 		return ret;
261 
262 	aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
263 	if (!aio)
264 		return -ENOMEM;
265 
266 	aio->orig_iocb = iocb;
267 	aio->is_write = true;
268 	aio->end_write = end_write;
269 	kiocb_clone(&aio->iocb, iocb, get_file(file));
270 	aio->iocb.ki_flags = flags;
271 	aio->iocb.ki_complete = backing_aio_queue_completion;
272 	refcount_set(&aio->ref, 2);
273 	ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
274 	backing_aio_put(aio);
275 	if (ret != -EIOCBQUEUED)
276 		backing_aio_cleanup(aio, ret);
277 	return ret;
278 }
279 
280 ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
281 				struct kiocb *iocb, int flags,
282 				struct backing_file_ctx *ctx)
283 {
284 	ssize_t ret;
285 
286 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
287 		return -EIO;
288 
289 	if (!iov_iter_count(iter))
290 		return 0;
291 
292 	/*
293 	 * F_SETFL and RWF_NOAPPEND can take append mode away from the user
294 	 * file. They checked its inode, not the backing one.
295 	 */
296 	if ((file->f_flags & O_APPEND) && !(flags & IOCB_APPEND) &&
297 	    IS_APPEND(file_inode(file)))
298 		return -EPERM;
299 
300 	ret = file_remove_privs(iocb->ki_filp);
301 	if (ret)
302 		return ret;
303 
304 	if (iocb->ki_flags & IOCB_DIRECT &&
305 	    !(file->f_mode & FMODE_CAN_ODIRECT))
306 		return -EINVAL;
307 
308 	scoped_with_creds(ctx->cred)
309 		return do_backing_file_write_iter(file, iter, iocb, flags, ctx->end_write);
310 }
311 EXPORT_SYMBOL_GPL(backing_file_write_iter);
312 
313 ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb,
314 				 struct pipe_inode_info *pipe, size_t len,
315 				 unsigned int flags,
316 				 struct backing_file_ctx *ctx)
317 {
318 	ssize_t ret;
319 
320 	if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
321 		return -EIO;
322 
323 	scoped_with_creds(ctx->cred)
324 		ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags);
325 
326 	if (ctx->accessed)
327 		ctx->accessed(iocb->ki_filp);
328 
329 	return ret;
330 }
331 EXPORT_SYMBOL_GPL(backing_file_splice_read);
332 
333 ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
334 				  struct file *out, struct kiocb *iocb,
335 				  size_t len, unsigned int flags,
336 				  struct backing_file_ctx *ctx)
337 {
338 	ssize_t ret;
339 
340 	if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
341 		return -EIO;
342 
343 	if (!out->f_op->splice_write)
344 		return -EINVAL;
345 
346 	/* splice refuses a user file in append mode, so F_SETFL cleared it */
347 	if ((out->f_flags & O_APPEND) && IS_APPEND(file_inode(out)))
348 		return -EPERM;
349 
350 	ret = file_remove_privs(iocb->ki_filp);
351 	if (ret)
352 		return ret;
353 
354 	scoped_with_creds(ctx->cred) {
355 		file_start_write(out);
356 		ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags);
357 		file_end_write(out);
358 	}
359 
360 	if (ctx->end_write)
361 		ctx->end_write(iocb, ret);
362 
363 	return ret;
364 }
365 EXPORT_SYMBOL_GPL(backing_file_splice_write);
366 
367 int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
368 		      struct backing_file_ctx *ctx)
369 {
370 	struct file *user_file = vma->vm_file;
371 	int ret;
372 
373 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
374 		return -EIO;
375 
376 	if (!can_mmap_file(file))
377 		return -ENODEV;
378 
379 	/* do_mmap() checked the inode of the user file */
380 	if ((vma->vm_flags & VM_SHARED) && IS_APPEND(file_inode(file)))
381 		return -EACCES;
382 
383 	vma_set_file(vma, file);
384 
385 	scoped_with_creds(ctx->cred) {
386 		ret = security_mmap_backing_file(vma, file, user_file);
387 		if (ret)
388 			return ret;
389 
390 		ret = vfs_mmap(vma->vm_file, vma);
391 	}
392 
393 	if (ctx->accessed)
394 		ctx->accessed(user_file);
395 
396 	return ret;
397 }
398 EXPORT_SYMBOL_GPL(backing_file_mmap);
399 
400 static int __init backing_aio_init(void)
401 {
402 	backing_aio_cachep = KMEM_CACHE(backing_aio, SLAB_HWCACHE_ALIGN);
403 	if (!backing_aio_cachep)
404 		return -ENOMEM;
405 
406 	return 0;
407 }
408 fs_initcall(backing_aio_init);
409