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, struct mnt_idmap *idmap) 7 { 8 struct fuse_conn *fc = fm->fc; 9 bool no_idmap = !fm->sb || (fm->sb->s_iflags & SB_I_NOIDMAP); 10 kuid_t fsuid = mapped_fsuid(idmap, fc->user_ns); 11 kgid_t fsgid = mapped_fsgid(idmap, fc->user_ns); 12 13 args->pid = pid_nr_ns(task_pid(current), fc->pid_ns); 14 15 if (args->force) { 16 if (args->nocreds) 17 return 0; 18 19 if (no_idmap) { 20 args->uid = from_kuid_munged(fc->user_ns, current_fsuid()); 21 args->gid = from_kgid_munged(fc->user_ns, current_fsgid()); 22 } else { 23 args->uid = FUSE_INVALID_UIDGID; 24 args->gid = FUSE_INVALID_UIDGID; 25 } 26 return 0; 27 } 28 29 WARN_ON(args->nocreds); 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 static int fuse_req_prep(struct fuse_mount *fm, struct fuse_args *args, struct mnt_idmap *idmap) 53 { 54 if (!args->force && fm->fc->conn_error) 55 return -ECONNREFUSED; 56 57 return fuse_fill_creds(fm, args, idmap); 58 } 59 60 ssize_t __fuse_simple_request(struct mnt_idmap *idmap, struct fuse_mount *fm, 61 struct fuse_args *args) 62 { 63 struct fuse_conn *fc = fm->fc; 64 int err = fuse_req_prep(fm, args, idmap); 65 66 if (err) 67 return err; 68 69 return fuse_chan_send(fc->chan, args); 70 } 71 72 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args, gfp_t gfp_flags) 73 { 74 struct fuse_conn *fc = fm->fc; 75 int err; 76 77 WARN_ON(args->force && !args->nocreds); 78 79 err = fuse_req_prep(fm, args, &invalid_mnt_idmap); 80 if (err) 81 return err; 82 83 return fuse_chan_send_bg(fc->chan, args, gfp_flags); 84 } 85 EXPORT_SYMBOL_GPL(fuse_simple_background); 86 87 int fuse_simple_notify_reply(struct fuse_mount *fm, struct fuse_args *args, u64 unique) 88 { 89 struct fuse_conn *fc = fm->fc; 90 int err; 91 92 WARN_ON(args->force && !args->nocreds); 93 94 err = fuse_req_prep(fm, args, &invalid_mnt_idmap); 95 if (err) 96 return err; 97 98 return fuse_chan_send_notify_reply(fc->chan, args, unique); 99 } 100