2 * attach.c: Support for attaching to the runtime from other processes.
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2007-2008 Novell, Inc.
14 #define DISABLE_ATTACH
16 #ifndef DISABLE_ATTACH
21 #include <sys/types.h>
22 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <sys/types.h>
35 #include <mono/metadata/assembly.h>
36 #include <mono/metadata/metadata.h>
37 #include <mono/metadata/class-internals.h>
38 #include <mono/metadata/object-internals.h>
39 #include <mono/metadata/threads-types.h>
40 #include <mono/metadata/gc-internal.h>
44 * This module enables other processes to attach to a running mono process and
45 * load agent assemblies.
46 * Communication is done through a UNIX Domain Socket located at
47 * /tmp/mono-<USER>/.mono-<PID>.
48 * We use a simplified version of the .net remoting protocol.
49 * To increase security, and to avoid spinning up a listener thread on startup,
50 * we follow the java implementation, and only start up the attach mechanism
51 * when we receive a QUIT signal and there is a file named
52 * '.mono_attach_pid<PID>' in /tmp.
55 * - This module allows loading of arbitrary code into a running mono runtime, so
56 * it is security critical.
57 * - Security is based on controlling access to the unix file to which the unix
58 * domain socket is bound. Permissions/ownership are set such that only the owner
59 * of the process can access the socket.
60 * - As an additional measure, the socket is only created when the process receives
61 * a SIGQUIT signal, which only its owner/root can send.
62 * - The socket is kept in a directory whose ownership is checked before creating
63 * the socket. This could allow an attacker a kind of DOS attack by creating the
64 * directory with the wrong permissions/ownership. However, the only thing such
65 * an attacker could prevent is the attaching of agents to the mono runtime.
76 /*******************************************************************/
77 /* Remoting Protocol type definitions from [MS-NRBF] and [MS-NRTP] */
78 /*******************************************************************/
87 static AgentConfig config;
89 static int listen_fd, conn_fd;
91 static char *ipc_filename;
93 static char *server_uri;
95 static HANDLE receiver_thread_handle;
97 static gboolean stop_receiver_thread;
99 static gboolean needs_to_start, started;
101 #define agent_lock() EnterCriticalSection (&agent_mutex)
102 #define agent_unlock() LeaveCriticalSection (&agent_mutex)
103 static CRITICAL_SECTION agent_mutex;
105 static void transport_connect (void);
107 static guint32 WINAPI receiver_thread (void *arg);
109 static void transport_start_receive (void);
112 * Functions to decode protocol data
115 decode_byte (guint8 *buf, guint8 **endbuf, guint8 *limit)
118 g_assert (*endbuf <= limit);
123 decode_int (guint8 *buf, guint8 **endbuf, guint8 *limit)
126 g_assert (*endbuf <= limit);
128 return (((int)buf [0]) << 0) | (((int)buf [1]) << 8) | (((int)buf [2]) << 16) | (((int)buf [3]) << 24);
132 decode_short (guint8 *buf, guint8 **endbuf, guint8 *limit)
135 g_assert (*endbuf <= limit);
137 return (((int)buf [0]) << 0) | (((int)buf [1]) << 8);
141 decode_string_value (guint8 *buf, guint8 **endbuf, guint8 *limit)
148 type = decode_byte (p, &p, limit);
149 if (type == PRIM_TYPE_NULL) {
153 g_assert (type == PRIM_TYPE_STRING);
157 guint8 b = decode_byte (p, &p, limit);
165 g_assert (length < (1 << 16));
167 s = g_malloc (length + 1);
169 g_assert (p + length <= limit);
170 memcpy (s, p, length);
179 /********************************/
180 /* AGENT IMPLEMENTATION */
181 /********************************/
184 mono_attach_parse_options (char *options)
186 config.enabled = TRUE;
190 if (!strcmp (options, "disable"))
191 config.enabled = FALSE;
195 mono_attach_init (void)
197 InitializeCriticalSection (&agent_mutex);
203 * Start the attach mechanism if needed. This is called from a signal handler so it must be signal safe.
205 * Returns: whenever it was started.
208 mono_attach_start (void)
216 /* Check for the existence of the trigger file */
219 * We don't do anything with this file, and the only thing an attacker can do
220 * by creating it is to enable the attach mechanism if the process receives a
221 * SIGQUIT signal, which can only be sent by the owner/root.
223 snprintf (path, sizeof (path), "/tmp/.mono_attach_pid%d", getpid ());
224 fd = open (path, O_RDONLY);
230 /* Act like we started */
237 * Our startup includes non signal-safe code, so ask the finalizer thread to
238 * do the actual startup.
240 needs_to_start = TRUE;
241 mono_gc_finalize_notify ();
246 /* Called by the finalizer thread when it is woken up */
248 mono_attach_maybe_start (void)
253 needs_to_start = FALSE;
255 transport_start_receive ();
262 mono_attach_cleanup (void)
267 unlink (ipc_filename);
269 stop_receiver_thread = TRUE;
271 /* This will cause receiver_thread () to break out of the read () call */
274 /* Wait for the receiver thread to exit */
275 if (receiver_thread_handle)
276 WaitForSingleObjectEx (receiver_thread_handle, 0, FALSE);
280 mono_attach_load_agent (MonoDomain *domain, char *agent, char *args, MonoObject **exc)
282 MonoAssembly *agent_assembly;
286 MonoArray *main_args;
288 MonoImageOpenStatus open_status;
290 agent_assembly = mono_assembly_open (agent, &open_status);
291 if (!agent_assembly) {
292 fprintf (stderr, "Cannot open agent assembly '%s': %s.\n", agent, mono_image_strerror (open_status));
298 * Can't use mono_jit_exec (), as it sets things which might confuse the
301 image = mono_assembly_get_image (agent_assembly);
302 entry = mono_image_get_entry_point (image);
304 g_print ("Assembly '%s' doesn't have an entry point.\n", mono_image_get_filename (image));
309 method = mono_get_method (image, entry, NULL);
311 g_print ("The entry point method of assembly '%s' could not be loaded\n", agent);
317 main_args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, 1);
318 mono_array_set (main_args, MonoString*, 0, mono_string_new (domain, args));
320 main_args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, 0);
326 mono_runtime_invoke (method, NULL, pa, exc);
334 * Create a UNIX domain socket and bind it to a file in /tmp.
336 * SECURITY: This routine is _very_ security critical since we depend on the UNIX
337 * permissions system to prevent attackers from connecting to the socket.
342 struct sockaddr_un name;
345 char *filename, *directory;
351 if (getuid () != geteuid ()) {
352 fprintf (stderr, "attach: disabled listening on an IPC socket when running in setuid mode.\n");
356 /* Create the socket. */
357 sock = socket (PF_UNIX, SOCK_STREAM, 0);
359 perror ("attach: failed to create IPC socket");
364 * For security reasons, create a directory to hold the listening socket,
365 * since there is a race between bind () and chmod () below.
367 /* FIXME: Use TMP ? */
369 res = getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw);
371 fprintf (stderr, "attach: getpwuid_r () failed.\n");
375 directory = g_strdup_printf ("/tmp/mono-%s", pw->pw_name);
376 res = mkdir (directory, S_IRUSR | S_IWUSR | S_IXUSR);
378 if (errno == EEXIST) {
379 /* Check type and permissions */
380 res = lstat (directory, &stat);
382 perror ("attach: lstat () failed");
385 if (!S_ISDIR (stat.st_mode)) {
386 fprintf (stderr, "attach: path '%s' is not a directory.\n", directory);
389 if (stat.st_uid != getuid ()) {
390 fprintf (stderr, "attach: directory '%s' is not owned by the current user.\n", directory);
393 if ((stat.st_mode & S_IRWXG) != 0 || (stat.st_mode & S_IRWXO) || ((stat.st_mode & S_IRWXU) != (S_IRUSR | S_IWUSR | S_IXUSR))) {
394 fprintf (stderr, "attach: directory '%s' should have protection 0700.\n", directory);
398 perror ("attach: mkdir () failed");
403 filename = g_strdup_printf ("%s/.mono-%d", directory, getpid ());
406 /* Bind a name to the socket. */
407 name.sun_family = AF_UNIX;
408 strcpy (name.sun_path, filename);
410 size = (offsetof (struct sockaddr_un, sun_path)
411 + strlen (name.sun_path) + 1);
413 if (bind (sock, (struct sockaddr *) &name, size) < 0) {
414 fprintf (stderr, "attach: failed to bind IPC socket '%s': %s\n", filename, strerror (errno));
419 /* Set permissions */
420 res = chmod (filename, S_IRUSR | S_IWUSR);
422 perror ("attach: failed to set permissions on IPC socket");
428 res = listen (sock, 16);
430 fprintf (stderr, "attach: listen () failed: %s\n", strerror (errno));
436 ipc_filename = g_strdup (filename);
438 server_uri = g_strdup_printf ("unix://%s/.mono-%d?/vm", directory, getpid ());
445 transport_connect (void)
453 transport_send (int fd, guint8 *data, int len)
457 stats.bytes_sent += len;
458 //printf ("X: %d\n", stats.bytes_sent);
460 res = write (fd, data, len);
462 /* FIXME: What to do here ? */
469 transport_start_receive (void)
473 transport_connect ();
478 receiver_thread_handle = CreateThread (NULL, 0, receiver_thread, NULL, 0, &tid);
479 g_assert (receiver_thread_handle);
482 static guint32 WINAPI
483 receiver_thread (void *arg)
485 int res, content_len;
490 printf ("attach: Listening on '%s'...\n", server_uri);
493 conn_fd = accept (listen_fd, NULL, NULL);
495 /* Probably closed by mono_attach_cleanup () */
498 printf ("attach: Connected.\n");
500 mono_thread_attach (mono_get_root_domain ());
501 /* Ask the runtime to not abort this thread */
502 //mono_thread_current ()->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
503 /* Ask the runtime to not wait for this thread */
504 mono_thread_current ()->state |= ThreadState_Background;
507 char *cmd, *agent_name, *agent_args;
511 res = read (conn_fd, buffer, 6);
513 if (res == -1 && errno == EINTR)
516 if (res == -1 || stop_receiver_thread)
522 if ((strncmp ((char*)buffer, "MONO", 4) != 0) || buffer [4] != 1 || buffer [5] != 0) {
523 fprintf (stderr, "attach: message from server has unknown header.\n");
527 /* Read content length */
528 res = read (conn_fd, buffer, 4);
535 content_len = decode_int (p, &p, p_end);
537 /* Read message body */
538 body = g_malloc (content_len);
539 res = read (conn_fd, body, content_len);
542 p_end = body + content_len;
544 cmd = decode_string_value (p, &p, p_end);
547 g_assert (!strcmp (cmd, "attach"));
549 agent_name = decode_string_value (p, &p, p_end);
550 agent_args = decode_string_value (p, &p, p_end);
552 printf ("attach: Loading agent '%s'.\n", agent_name);
553 mono_attach_load_agent (mono_domain_get (), agent_name, agent_args, &exc);
557 // FIXME: Send back a result
563 printf ("attach: Disconnected.\n");
565 if (stop_receiver_thread)
572 #else /* DISABLE_ATTACH */
575 mono_attach_parse_options (char *options)
580 mono_attach_init (void)
585 mono_attach_start (void)
591 mono_attach_maybe_start (void)
596 mono_attach_cleanup (void)
600 #endif /* DISABLE_ATTACH */