101-mconsole-exec.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. @@ -24,6 +25,7 @@
  37. #include <linux/fs.h>
  38. #include <linux/mount.h>
  39. #include <linux/file.h>
  40. +#include <linux/completion.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/switch_to.h>
  43. @@ -121,6 +123,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. @@ -187,6 +242,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. @@ -534,6 +534,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. @@ -62,6 +62,7 @@ struct subprocess_info {
  131. int wait;
  132. int retval;
  133. int (*init)(struct subprocess_info *info, struct cred *new);
  134. + struct file *stdout;
  135. void (*cleanup)(struct subprocess_info *info);
  136. void *data;
  137. };
  138. @@ -104,4 +105,6 @@ extern int usermodehelper_read_trylock(v
  139. extern long usermodehelper_read_lock_wait(long timeout);
  140. extern void usermodehelper_read_unlock(void);
  141. +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info, struct file **filp);
  142. +
  143. #endif /* __LINUX_KMOD_H__ */
  144. --- a/kernel/kmod.c
  145. +++ b/kernel/kmod.c
  146. @@ -39,6 +39,7 @@
  147. #include <linux/rwsem.h>
  148. #include <linux/ptrace.h>
  149. #include <linux/async.h>
  150. +#include <linux/pipe_fs_i.h>
  151. #include <asm/uaccess.h>
  152. #include <trace/events/module.h>
  153. @@ -231,6 +232,28 @@ static int ____call_usermodehelper(void
  154. flush_signal_handlers(current, 1);
  155. spin_unlock_irq(&current->sighand->siglock);
  156. + /* Install output when needed */
  157. + if (sub_info->stdout) {
  158. + struct files_struct *f = current->files;
  159. + struct fdtable *fdt;
  160. +
  161. + sys_close(1);
  162. + sys_close(2);
  163. + get_file(sub_info->stdout);
  164. + fd_install(1, sub_info->stdout);
  165. + fd_install(2, sub_info->stdout);
  166. + spin_lock(&f->file_lock);
  167. + fdt = files_fdtable(f);
  168. + __set_bit(1, fdt->open_fds);
  169. + __clear_bit(1, fdt->close_on_exec);
  170. + __set_bit(2, fdt->open_fds);
  171. + __clear_bit(2, fdt->close_on_exec);
  172. + spin_unlock(&f->file_lock);
  173. +
  174. + /* disallow core files */
  175. + current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
  176. + }
  177. +
  178. /* We can run anywhere, unlike our parent keventd(). */
  179. set_cpus_allowed_ptr(current, cpu_all_mask);
  180. @@ -544,6 +567,20 @@ struct subprocess_info *call_usermodehel
  181. }
  182. EXPORT_SYMBOL(call_usermodehelper_setup);
  183. +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
  184. + struct file **filp)
  185. +{
  186. + struct file *f[2];
  187. +
  188. + if (create_pipe_files(f, 0)<0)
  189. + return PTR_ERR(f);
  190. + sub_info->stdout = f[1];
  191. + *filp = f[0];
  192. + return 0;
  193. +}
  194. +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
  195. +
  196. +
  197. /**
  198. * call_usermodehelper_exec - start a usermode application
  199. * @sub_info: information about the subprocessa