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 args->pid = pid_nr_ns(task_pid(current), fc->pid_ns); 15 16 if (args->force) { 17 if (args->nocreds) 18 return 0; 19 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 WARN_ON(args->nocreds); 31 /* 32 * Keep the old behavior when idmappings support was not 33 * declared by a FUSE server. 34 * 35 * For those FUSE servers who support idmapped mounts, we send UID/GID 36 * only along with "inode creation" fuse requests, otherwise idmap == 37 * &invalid_mnt_idmap and req->in.h.{u,g}id will be equal to 38 * FUSE_INVALID_UIDGID. 39 */ 40 if (no_idmap) { 41 fsuid = current_fsuid(); 42 fsgid = current_fsgid(); 43 } 44 args->uid = from_kuid(fc->user_ns, fsuid); 45 args->gid = from_kgid(fc->user_ns, fsgid); 46 47 if (no_idmap && unlikely(args->uid == ((uid_t)-1) || args->gid == ((gid_t)-1))) 48 return -EOVERFLOW; 49 50 return 0; 51 } 52 53 static int fuse_req_prep(struct fuse_mount *fm, struct fuse_args *args, 54 const struct mnt_idmap *idmap) 55 { 56 if (!args->force && fm->fc->conn_error) 57 return -ECONNREFUSED; 58 59 return fuse_fill_creds(fm, args, idmap); 60 } 61 62 ssize_t __fuse_simple_request(const struct mnt_idmap *idmap, struct fuse_mount *fm, 63 struct fuse_args *args) 64 { 65 struct fuse_conn *fc = fm->fc; 66 int err = fuse_req_prep(fm, args, idmap); 67 68 if (err) 69 return err; 70 71 return fuse_chan_send(fc->chan, args); 72 } 73 74 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args, gfp_t gfp_flags) 75 { 76 struct fuse_conn *fc = fm->fc; 77 int err; 78 79 WARN_ON(args->force && !args->nocreds); 80 81 err = fuse_req_prep(fm, args, &invalid_mnt_idmap); 82 if (err) 83 return err; 84 85 return fuse_chan_send_bg(fc->chan, args, gfp_flags); 86 } 87 EXPORT_SYMBOL_GPL(fuse_simple_background); 88 89 int fuse_simple_notify_reply(struct fuse_mount *fm, struct fuse_args *args, u64 unique) 90 { 91 struct fuse_conn *fc = fm->fc; 92 int err; 93 94 WARN_ON(args->force && !args->nocreds); 95 96 err = fuse_req_prep(fm, args, &invalid_mnt_idmap); 97 if (err) 98 return err; 99 100 return fuse_chan_send_notify_reply(fc->chan, args, unique); 101 } 102