Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Detach after exec() if we don't have permission to debug the child (i…
Browse files Browse the repository at this point in the history
…t's setuid/setgid).

2009-10-19  Martin Baulig  <martin@ximian.com>

	Detach after exec() if we don't have permission to debug the
	child; this happens if the exec()d binary is setuid/setgid.

	* backend/Inferior.cs
	(Inferior.ProcessEvent): Removed child_execd().
	(Inferior.InitializeAfterExec): Call GetApplication() and
	start.SetupApplication() here.

	* backend/ProcessServant.cs
	(ProcessServant.ChildExecd): Catch `TargetError.PermissionDenied'
	and detach from the process; this happens when exec()ing
	setuid/setgid applications.

	* classes/TargetException.cs
	(TargetError): Add `PermissionDenied'.

	* backend/server/x86-linux-ptrace.c
	(_server_ptrace_setup_inferior): Check `EACCES' and return
	`COMMAND_ERROR_PERMISSION_DENIED'.

svn path=/trunk/debugger/; revision=144386
  • Loading branch information
Martin Baulig committed Oct 19, 2009
1 parent 425cbed commit 4cd2602
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
22 changes: 22 additions & 0 deletions ChangeLog
@@ -1,3 +1,25 @@
2009-10-19 Martin Baulig <martin@ximian.com>

Detach after exec() if we don't have permission to debug the
child; this happens if the exec()d binary is setuid/setgid.

* backend/Inferior.cs
(Inferior.ProcessEvent): Removed child_execd().
(Inferior.InitializeAfterExec): Call GetApplication() and
start.SetupApplication() here.

* backend/ProcessServant.cs
(ProcessServant.ChildExecd): Catch `TargetError.PermissionDenied'
and detach from the process; this happens when exec()ing
setuid/setgid applications.

* classes/TargetException.cs
(TargetError): Add `PermissionDenied'.

* backend/server/x86-linux-ptrace.c
(_server_ptrace_setup_inferior): Check `EACCES' and return
`COMMAND_ERROR_PERMISSION_DENIED'.

2009-10-14 Martin Baulig <martin@ximian.com>

* backend/SingleSteppingEngine.cs
Expand Down
20 changes: 7 additions & 13 deletions backend/Inferior.cs
Expand Up @@ -698,6 +698,12 @@ public void InitializeAfterExec (int pid)
check_error (mono_debugger_server_initialize_thread (server_handle, pid, false));
this.child_pid = pid;

string exe_file, cwd;
string[] cmdline_args;
exe_file = GetApplication (out cwd, out cmdline_args);

start.SetupApplication (exe_file, cwd, cmdline_args);

SetupInferior ();

change_target_state (TargetState.Stopped, 0);
Expand Down Expand Up @@ -726,17 +732,6 @@ public void Attach (int pid)
change_target_state (TargetState.Stopped, 0);
}

void child_execd ()
{
string exe_file, cwd;
string[] cmdline_args;
exe_file = GetApplication (out cwd, out cmdline_args);

start.SetupApplication (exe_file, cwd, cmdline_args);

SetupInferior ();
}

public ChildEvent ProcessEvent (int status)
{
long arg, data1, data2;
Expand Down Expand Up @@ -766,7 +761,6 @@ public ChildEvent ProcessEvent (int status)
break;

case ChildEventType.CHILD_EXECD:
child_execd ();
break;
}

Expand Down Expand Up @@ -1376,7 +1370,7 @@ protected string GetApplication (out string cwd, out string[] cmdline_args)
int count;
string exe_file;
check_error (mono_debugger_server_get_application (
server_handle, out p_exe, out p_cwd,
server_handle, out p_exe, out p_cwd,
out count, out data));

cmdline_args = new string [count];
Expand Down
13 changes: 12 additions & 1 deletion backend/ProcessServant.cs
Expand Up @@ -281,7 +281,18 @@ internal void ChildExecd (SingleSteppingEngine engine, Inferior inferior)
native_language = new NativeLanguage (this, os, target_info);

Inferior new_inferior = Inferior.CreateInferior (manager, this, start);
new_inferior.InitializeAfterExec (inferior.PID);
try {
new_inferior.InitializeAfterExec (inferior.PID);
} catch (Exception ex) {
if ((ex is TargetException) && (((TargetException) ex).Type == TargetError.PermissionDenied)) {
Report.Error ("Permission denied when trying to debug exec()ed child {0}, detaching!",
inferior.PID);
} else {
Report.Error ("InitializeAfterExec() failed on pid {0}: {1}", inferior.PID, ex);
}
new_inferior.DetachAfterFork ();
return;
}

SingleSteppingEngine new_thread = new SingleSteppingEngine (
manager, this, new_inferior, inferior.PID);
Expand Down
1 change: 1 addition & 0 deletions backend/server/server.h
Expand Up @@ -36,6 +36,7 @@ typedef enum {
COMMAND_ERROR_NOT_IMPLEMENTED,
COMMAND_ERROR_IO_ERROR,
COMMAND_ERROR_NO_CALLBACK_FRAME,
COMMAND_ERROR_PERMISSION_DENIED
} ServerCommandError;

typedef enum {
Expand Down
15 changes: 12 additions & 3 deletions backend/server/x86-linux-ptrace.c
Expand Up @@ -491,6 +491,9 @@ _server_ptrace_setup_inferior (ServerHandle *handle)
handle->inferior->os.mem_fd = open64 (filename, O_RDONLY);

if (handle->inferior->os.mem_fd < 0) {
if (errno == EACCES)
return COMMAND_ERROR_PERMISSION_DENIED;

g_warning (G_STRLOC ": Can't open (%s): %s", filename, g_strerror (errno));
return COMMAND_ERROR_UNKNOWN_ERROR;
}
Expand Down Expand Up @@ -614,15 +617,20 @@ server_ptrace_get_application (ServerHandle *handle, gchar **exe_file, gchar **c
char buffer [BUFSIZ+1];
GPtrArray *array;
gchar *cmdline, **ptr;
gsize pos, len;
gssize pos, len;
int i;

len = readlink (exe_filename, buffer, BUFSIZ);
if (len < 0) {
g_free (cwd_filename);
g_free (exe_filename);
g_free (cmdline_filename);
g_warning (G_STRLOC ": Can't get exe file of %d", handle->inferior->pid);

if (errno == EACCES)
return COMMAND_ERROR_PERMISSION_DENIED;

g_warning (G_STRLOC ": Can't get exe file of %d: %s", handle->inferior->pid,
g_strerror (errno));
return COMMAND_ERROR_UNKNOWN_ERROR;
}

Expand All @@ -634,7 +642,8 @@ server_ptrace_get_application (ServerHandle *handle, gchar **exe_file, gchar **c
g_free (cwd_filename);
g_free (exe_filename);
g_free (cmdline_filename);
g_warning (G_STRLOC ": Can't get cwd of %d", handle->inferior->pid);
g_warning (G_STRLOC ": Can't get cwd of %d: %s", handle->inferior->pid,
g_strerror (errno));
return COMMAND_ERROR_UNKNOWN_ERROR;
}

Expand Down
3 changes: 3 additions & 0 deletions classes/TargetException.cs
Expand Up @@ -23,6 +23,7 @@ public enum TargetError {
NotImplemented,
IOError,
NoCallbackFrame,
PermissionDenied,

NoStack = 101,
NoMethod,
Expand Down Expand Up @@ -115,6 +116,8 @@ protected static string GetMessage (TargetError type)
return "No invocation found.";
case TargetError.InvalidContext:
return "Operation not permitted outside managed context.";
case TargetError.PermissionDenied:
return "Permission denied.";
default:
return "Unknown error";
}
Expand Down

0 comments on commit 4cd2602

Please sign in to comment.