xref: /linux/fs/fuse/req.c (revision 2eb2329f9ce7f29f227656cc2686fe6f1879d11c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "dev.h"
4 #include "fuse_i.h"
5 
6 static int fuse_fill_creds(struct fuse_mount *fm, struct fuse_args *args,
7 			   const struct mnt_idmap *idmap)
8 {
9 	struct fuse_conn *fc = fm->fc;
10 	bool no_idmap = !fm->sb || (fm->sb->s_iflags & SB_I_NOIDMAP);
11 	kuid_t fsuid = mapped_fsuid(idmap, fc->user_ns);
12 	kgid_t fsgid = mapped_fsgid(idmap, fc->user_ns);
13 
14 	if (args->nocreds)
15 		return 0;
16 
17 	args->pid = pid_nr_ns(task_pid(current), fc->pid_ns);
18 
19 	if (args->force) {
20 		if (no_idmap) {
21 			args->uid = from_kuid_munged(fc->user_ns, current_fsuid());
22 			args->gid = from_kgid_munged(fc->user_ns, current_fsgid());
23 		} else {
24 			args->uid = FUSE_INVALID_UIDGID;
25 			args->gid = FUSE_INVALID_UIDGID;
26 		}
27 		return 0;
28 	}
29 
30 	/*
31 	 * Keep the old behavior when idmappings support was not
32 	 * declared by a FUSE server.
33 	 *
34 	 * For those FUSE servers who support idmapped mounts, we send UID/GID
35 	 * only along with "inode creation" fuse requests, otherwise idmap ==
36 	 * &invalid_mnt_idmap and req->in.h.{u,g}id will be equal to
37 	 * FUSE_INVALID_UIDGID.
38 	 */
39 	if (no_idmap) {
40 		fsuid = current_fsuid();
41 		fsgid = current_fsgid();
42 	}
43 	args->uid = from_kuid(fc->user_ns, fsuid);
44 	args->gid = from_kgid(fc->user_ns, fsgid);
45 
46 	if (no_idmap && unlikely(args->uid == ((uid_t)-1) || args->gid == ((gid_t)-1)))
47 		return -EOVERFLOW;
48 
49 	return 0;
50 }
51 
52 ssize_t __fuse_simple_request(const struct mnt_idmap *idmap, struct fuse_mount *fm,
53 			      struct fuse_args *args)
54 {
55 	struct fuse_conn *fc = fm->fc;
56 	int err = fuse_fill_creds(fm, args, idmap);
57 
58 	if (err)
59 		return err;
60 
61 	return fuse_chan_send(fc->chan, args);
62 }
63 
64 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args, gfp_t gfp_flags)
65 {
66 	struct fuse_conn *fc = fm->fc;
67 	int err;
68 
69 	WARN_ON(args->force && !args->nocreds);
70 
71 	err = fuse_fill_creds(fm, args, &invalid_mnt_idmap);
72 	if (err)
73 		return err;
74 
75 	return fuse_chan_send_bg(fc->chan, args, gfp_flags);
76 }
77 EXPORT_SYMBOL_GPL(fuse_simple_background);
78 
79 int fuse_simple_notify_reply(struct fuse_mount *fm, struct fuse_args *args, u64 unique)
80 {
81 	struct fuse_conn *fc = fm->fc;
82 	int err;
83 
84 	WARN_ON(args->force && !args->nocreds);
85 
86 	err = fuse_fill_creds(fm, args, &invalid_mnt_idmap);
87 	if (err)
88 		return err;
89 
90 	return fuse_chan_send_notify_reply(fc->chan, args, unique);
91 }
92