Permalink
Browse files

Detach after exec() if we don't have permission to debug the child (i…

…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...
1 parent 425cbed commit 4cd2602722671e925a7d748aa1d3cea2fa840e4d Martin Baulig committed Oct 19, 2009
Showing with 57 additions and 17 deletions.
  1. +22 −0 ChangeLog
  2. +7 −13 backend/Inferior.cs
  3. +12 −1 backend/ProcessServant.cs
  4. +1 −0 backend/server/server.h
  5. +12 −3 backend/server/x86-linux-ptrace.c
  6. +3 −0 classes/TargetException.cs
View
@@ -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
View
@@ -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);
@@ -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;
@@ -766,7 +761,6 @@ public ChildEvent ProcessEvent (int status)
break;
case ChildEventType.CHILD_EXECD:
- child_execd ();
break;
}
@@ -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];
View
@@ -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);
View
@@ -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 {
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -23,6 +23,7 @@ public enum TargetError {
NotImplemented,
IOError,
NoCallbackFrame,
+ PermissionDenied,
NoStack = 101,
NoMethod,
@@ -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";
}

0 comments on commit 4cd2602

Please sign in to comment.