101-mconsole-exec.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #
  2. # Minimalist mconsole exec patch
  3. #
  4. # 3.10 version (with bit more synchronous behavior) by fingon at iki dot fi
  5. # Adaptation to kernel 3.3.8 made by David Fernández (david at dit.upm.es) for
  6. # Starting point: mconsole-exec-2.6.30.patch for kernel 2.6.30
  7. # Author of original patch: Paolo Giarrusso, aka Blaisorblade
  8. # (http://www.user-mode-linux.org/~blaisorblade)
  9. #
  10. # Known misfeatures:
  11. #
  12. # - If output is too long, blocks (and breaks horribly)
  13. # (this misfeature from 3.10 patches, when minimalizing the patch;
  14. # workaround: redirect to a shared filesystem if long output is expected)
  15. #
  16. # - Nothing useful is done with stdin
  17. #
  18. --- a/arch/um/drivers/mconsole.h
  19. +++ b/arch/um/drivers/mconsole.h
  20. @@ -85,6 +85,7 @@ extern void mconsole_cad(struct mc_reque
  21. extern void mconsole_stop(struct mc_request *req);
  22. extern void mconsole_go(struct mc_request *req);
  23. extern void mconsole_log(struct mc_request *req);
  24. +extern void mconsole_exec(struct mc_request *req);
  25. extern void mconsole_proc(struct mc_request *req);
  26. extern void mconsole_stack(struct mc_request *req);
  27. --- a/arch/um/drivers/mconsole_kern.c
  28. +++ b/arch/um/drivers/mconsole_kern.c
  29. @@ -4,6 +4,7 @@
  30. * Licensed under the GPL
  31. */
  32. +#include <linux/kmod.h>
  33. #include <linux/console.h>
  34. #include <linux/ctype.h>
  35. #include <linux/string.h>
  36. @@ -26,6 +27,7 @@
  37. #include <linux/mount.h>
  38. #include <linux/file.h>
  39. #include <linux/uaccess.h>
  40. +#include <linux/completion.h>
  41. #include <asm/switch_to.h>
  42. #include <init.h>
  43. @@ -122,6 +124,59 @@ void mconsole_log(struct mc_request *req
  44. mconsole_reply(req, "", 0, 0);
  45. }
  46. +void mconsole_exec(struct mc_request *req)
  47. +{
  48. + struct subprocess_info *sub_info;
  49. + int res, len;
  50. + struct file *out;
  51. + char buf[MCONSOLE_MAX_DATA];
  52. +
  53. + char *envp[] = {
  54. + "HOME=/", "TERM=linux",
  55. + "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
  56. + NULL
  57. + };
  58. + char *argv[] = {
  59. + "/bin/sh", "-c",
  60. + req->request.data + strlen("exec "),
  61. + NULL
  62. + };
  63. +
  64. + sub_info = call_usermodehelper_setup("/bin/sh", argv, envp, GFP_ATOMIC, NULL, NULL, NULL);
  65. + if (sub_info == NULL) {
  66. + mconsole_reply(req, "call_usermodehelper_setup failed", 1, 0);
  67. + return;
  68. + }
  69. + res = call_usermodehelper_stdoutpipe(sub_info, &out);
  70. + if (res < 0) {
  71. + kfree(sub_info);
  72. + mconsole_reply(req, "call_usermodehelper_stdoutpipe failed", 1, 0);
  73. + return;
  74. + }
  75. +
  76. + res = call_usermodehelper_exec(sub_info, UMH_WAIT_PROC);
  77. + if (res < 0) {
  78. + kfree(sub_info);
  79. + mconsole_reply(req, "call_usermodehelper_exec failed", 1, 0);
  80. + return;
  81. + }
  82. +
  83. + for (;;) {
  84. + len = out->f_op->read(out, buf, sizeof(buf), &out->f_pos);
  85. + if (len < 0) {
  86. + mconsole_reply(req, "reading output failed", 1, 0);
  87. + break;
  88. + }
  89. + if (len == 0)
  90. + break;
  91. + mconsole_reply_len(req, buf, len, 0, 1);
  92. + }
  93. + fput(out);
  94. +
  95. + mconsole_reply_len(req, NULL, 0, 0, 0);
  96. +}
  97. +
  98. +
  99. void mconsole_proc(struct mc_request *req)
  100. {
  101. struct vfsmount *mnt = task_active_pid_ns(current)->proc_mnt;
  102. @@ -183,6 +238,7 @@ void mconsole_proc(struct mc_request *re
  103. stop - pause the UML; it will do nothing until it receives a 'go' \n\
  104. go - continue the UML after a 'stop' \n\
  105. log <string> - make UML enter <string> into the kernel log\n\
  106. + exec <string> - pass <string> to /bin/sh -c synchronously\n\
  107. proc <file> - returns the contents of the UML's /proc/<file>\n\
  108. stack <pid> - returns the stack of the specified pid\n\
  109. "
  110. --- a/arch/um/drivers/mconsole_user.c
  111. +++ b/arch/um/drivers/mconsole_user.c
  112. @@ -30,6 +30,7 @@ static struct mconsole_command commands[
  113. { "stop", mconsole_stop, MCONSOLE_PROC },
  114. { "go", mconsole_go, MCONSOLE_INTR },
  115. { "log", mconsole_log, MCONSOLE_INTR },
  116. + { "exec", mconsole_exec, MCONSOLE_PROC },
  117. { "proc", mconsole_proc, MCONSOLE_PROC },
  118. { "stack", mconsole_stack, MCONSOLE_INTR },
  119. };
  120. --- a/arch/um/os-Linux/file.c
  121. +++ b/arch/um/os-Linux/file.c
  122. @@ -555,6 +555,8 @@ int os_create_unix_socket(const char *fi
  123. addr.sun_family = AF_UNIX;
  124. + if (len > sizeof(addr.sun_path))
  125. + len = sizeof(addr.sun_path);
  126. snprintf(addr.sun_path, len, "%s", file);
  127. err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
  128. --- a/include/linux/kmod.h
  129. +++ b/include/linux/kmod.h
  130. @@ -45,4 +45,6 @@ static inline int request_module_nowait(
  131. #define try_then_request_module(x, mod...) (x)
  132. #endif
  133. +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info, struct file **filp);
  134. +
  135. #endif /* __LINUX_KMOD_H__ */
  136. --- a/include/linux/umh.h
  137. +++ b/include/linux/umh.h
  138. @@ -22,6 +22,7 @@ struct subprocess_info {
  139. const char *path;
  140. char **argv;
  141. char **envp;
  142. + struct file *stdout;
  143. int wait;
  144. int retval;
  145. int (*init)(struct subprocess_info *info, struct cred *new);
  146. --- a/kernel/umh.c
  147. +++ b/kernel/umh.c
  148. @@ -25,6 +25,7 @@
  149. #include <linux/ptrace.h>
  150. #include <linux/async.h>
  151. #include <linux/uaccess.h>
  152. +#include <linux/pipe_fs_i.h>
  153. #include <trace/events/module.h>
  154. @@ -70,6 +71,28 @@ static int call_usermodehelper_exec_asyn
  155. flush_signal_handlers(current, 1);
  156. spin_unlock_irq(&current->sighand->siglock);
  157. + /* Install output when needed */
  158. + if (sub_info->stdout) {
  159. + struct files_struct *f = current->files;
  160. + struct fdtable *fdt;
  161. +
  162. + sys_close(1);
  163. + sys_close(2);
  164. + get_file(sub_info->stdout);
  165. + fd_install(1, sub_info->stdout);
  166. + fd_install(2, sub_info->stdout);
  167. + spin_lock(&f->file_lock);
  168. + fdt = files_fdtable(f);
  169. + __set_bit(1, fdt->open_fds);
  170. + __clear_bit(1, fdt->close_on_exec);
  171. + __set_bit(2, fdt->open_fds);
  172. + __clear_bit(2, fdt->close_on_exec);
  173. + spin_unlock(&f->file_lock);
  174. +
  175. + /* disallow core files */
  176. + current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
  177. + }
  178. +
  179. /*
  180. * Our parent (unbound workqueue) runs with elevated scheduling
  181. * priority. Avoid propagating that into the userspace child.
  182. @@ -393,6 +416,20 @@ struct subprocess_info *call_usermodehel
  183. }
  184. EXPORT_SYMBOL(call_usermodehelper_setup);
  185. +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
  186. + struct file **filp)
  187. +{
  188. + struct file *f[2];
  189. +
  190. + if (create_pipe_files(f, 0) < 0)
  191. + return PTR_ERR(f);
  192. +
  193. + sub_info->stdout = f[1];
  194. + *filp = f[0];
  195. + return 0;
  196. +}
  197. +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
  198. +
  199. /**
  200. * call_usermodehelper_exec - start a usermode application
  201. * @sub_info: information about the subprocessa