usermode_driver.c 4.3 KB

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