^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * umd - User mode driver support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/shmem_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/pipe_fs_i.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs_struct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/task_work.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/usermode_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static struct vfsmount *blob_to_mnt(const void *data, size_t len, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct file_system_type *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) ssize_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) loff_t pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) type = get_fs_type("tmpfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (!type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) mnt = kern_mount(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) put_filesystem(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (IS_ERR(mnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) file = file_open_root(mnt->mnt_root, mnt, name, O_CREAT | O_WRONLY, 0700);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) mntput(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return ERR_CAST(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) written = kernel_write(file, data, len, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (written != len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int err = written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) filp_close(file, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) mntput(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Flush delayed fput so exec can open the file read-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) flush_delayed_fput();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) task_work_run();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * umd_load_blob - Remember a blob of bytes for fork_usermode_driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @info: information about usermode driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * @data: a blob of bytes that can be executed as a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @len: The lentgh of the blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int umd_load_blob(struct umd_info *info, const void *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (WARN_ON_ONCE(info->wd.dentry || info->wd.mnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) mnt = blob_to_mnt(data, len, info->driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (IS_ERR(mnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return PTR_ERR(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) info->wd.mnt = mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) info->wd.dentry = mnt->mnt_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) EXPORT_SYMBOL_GPL(umd_load_blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * umd_unload_blob - Disassociate @info from a previously loaded blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @info: information about usermode driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int umd_unload_blob(struct umd_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (WARN_ON_ONCE(!info->wd.mnt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) !info->wd.dentry ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) info->wd.mnt->mnt_root != info->wd.dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) kern_unmount(info->wd.mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) info->wd.mnt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) info->wd.dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) EXPORT_SYMBOL_GPL(umd_unload_blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int umd_setup(struct subprocess_info *info, struct cred *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct umd_info *umd_info = info->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct file *from_umh[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct file *to_umh[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* create pipe to send data to umh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) err = create_pipe_files(to_umh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err = replace_fd(0, to_umh[0], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) fput(to_umh[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) fput(to_umh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* create pipe to receive data from umh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) err = create_pipe_files(from_umh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fput(to_umh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) replace_fd(0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) err = replace_fd(1, from_umh[1], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) fput(from_umh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) fput(to_umh[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) replace_fd(0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) fput(from_umh[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) set_fs_pwd(current->fs, &umd_info->wd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) umd_info->pipe_to_umh = to_umh[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) umd_info->pipe_from_umh = from_umh[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) umd_info->tgid = get_pid(task_tgid(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void umd_cleanup(struct subprocess_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct umd_info *umd_info = info->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* cleanup if umh_setup() was successful but exec failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (info->retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) umd_cleanup_helper(umd_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * umd_cleanup_helper - release the resources which were allocated in umd_setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * @info: information about usermode driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) void umd_cleanup_helper(struct umd_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) fput(info->pipe_to_umh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fput(info->pipe_from_umh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) put_pid(info->tgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) info->tgid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) EXPORT_SYMBOL_GPL(umd_cleanup_helper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * fork_usermode_driver - fork a usermode driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @info: information about usermode driver (shouldn't be NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Returns either negative error or zero which indicates success in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * executing a usermode driver. In such case 'struct umd_info *info'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * is populated with two pipes and a tgid of the process. The caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * responsible for health check of the user process, killing it via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * tgid, and closing the pipes when user process is no longer needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int fork_usermode_driver(struct umd_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct subprocess_info *sub_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) const char *argv[] = { info->driver_name, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (WARN_ON_ONCE(info->tgid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) sub_info = call_usermodehelper_setup(info->driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) (char **)argv, NULL, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) umd_setup, umd_cleanup, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!sub_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) EXPORT_SYMBOL_GPL(fork_usermode_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)