Skip to content

Commit

Permalink
Merge pull request dotnet#2079 from monojenkins/sync-pr-18537-from-mono
Browse files Browse the repository at this point in the history
[debugger] Access invalid memory address using PointerValue Command.
  • Loading branch information
thaystg authored Jan 26, 2020
2 parents 15f5465 + f3945c2 commit 63aae23
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/mono/mono/mini/debugger-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <process.h>
#endif
#include <ws2tcpip.h>
#include <windows.h>
#endif

#ifdef HOST_ANDROID
Expand Down Expand Up @@ -104,6 +105,13 @@

#include <mono/utils/mono-os-mutex.h>

#include <fcntl.h>
#include <sys/stat.h>

#ifndef S_IWUSR
#define S_IWUSR S_IWRITE
#endif

#define THREAD_TO_INTERNAL(thread) (thread)->internal_thread

typedef struct {
Expand Down Expand Up @@ -603,6 +611,10 @@ static MonoThreadHandle *debugger_thread_handle;

static int log_level;

static int file_check_valid_memory = -1;

static char* filename_check_valid_memory;

static gboolean embedding;

static FILE *log_file;
Expand Down Expand Up @@ -1104,6 +1116,12 @@ mono_debugger_agent_cleanup (void)
ids_cleanup ();

mono_de_cleanup ();

if (file_check_valid_memory != -1) {
remove (filename_check_valid_memory);
g_free (filename_check_valid_memory);
close (file_check_valid_memory);
}
}

/*
Expand Down Expand Up @@ -9513,13 +9531,54 @@ string_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
return ERR_NONE;
}

static void
create_file_to_check_memory_address (void)
{
if (file_check_valid_memory != -1)
return;
char *file_name = g_strdup_printf ("debugger_check_valid_memory.%d", getpid());
filename_check_valid_memory = g_build_filename (g_get_tmp_dir (), file_name, (const char*)NULL);
file_check_valid_memory = open(filename_check_valid_memory, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR);
g_free (file_name);
}

static gboolean
valid_memory_address (gpointer addr, gint size)
{
#ifndef _MSC_VER
gboolean ret = TRUE;
create_file_to_check_memory_address ();
if(file_check_valid_memory < 0) {
return TRUE;
}
write (file_check_valid_memory, (gpointer)addr, 1);
if (errno == EFAULT) {
ret = FALSE;
}
#else
int i = 0;
gboolean ret = FALSE;
__try {
for (i = 0; i < size; i++)
*((volatile char*)addr+i);
ret = TRUE;
} __except(1) {
return ret;
}
#endif
return ret;
}

static ErrorCode
pointer_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
{
ErrorCode err;
gint64 addr;
MonoClass* klass;
MonoDomain* domain = NULL;
MonoType *type = NULL;
int align;
int size = 0;

switch (command) {
case CMD_POINTER_GET_VALUE:
Expand All @@ -9531,7 +9590,13 @@ pointer_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
if (m_class_get_byval_arg (klass)->type != MONO_TYPE_PTR)
return ERR_INVALID_ARGUMENT;

buffer_add_value (buf, m_class_get_byval_arg (m_class_get_element_class (klass)), (gpointer)addr, domain);
type = m_class_get_byval_arg (m_class_get_element_class (klass));
size = mono_type_size (type, &align);

if (!valid_memory_address((gpointer)addr, size))
return ERR_INVALID_ARGUMENT;

buffer_add_value (buf, type, (gpointer)addr, domain);

break;
default:
Expand Down

0 comments on commit 63aae23

Please sign in to comment.