1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
Fakeroot Next Generation - run command with fake root privileges
This program is copyrighted. Copyright information is available at the
AUTHORS file at the root of the source tree for the fakeroot-ng project
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <sys/ptrace.h>
#include <errno.h>
#include <string.h>
#include "syscalls.h"
#include "arch/platform.h"
// Retruns true of the specified pid has permission to perform a ptrace operation
static bool verify_permission( pid_t pid, pid_state *state )
{
pid_t traced=(pid_t)state->context_state[1];
// First, find out whether the pid we work on even exists
pid_state *child_state=lookup_state( traced );
if( child_state==NULL || child_state->debugger!=pid )
{
dlog("ptrace verify_permission: %d failed permission - not the debugger for "PID_F"\n", pid, traced);
errno=ESRCH;
return false;
}
if( child_state->trace_mode!=TRACE_STOPPED1 && child_state->trace_mode!=TRACE_STOPPED2 )
{
dlog("ptrace verify_permission: %d failed permission - "PID_F" is not stopped\n", pid, traced);
errno=ESRCH;
return false;
}
return true;
}
static bool begin_trace( pid_t debugger, pid_t child )
{
pid_state *child_state=lookup_state( child );
pid_state *parent_state=lookup_state( debugger );
if( child_state==NULL || parent_state==NULL || child_state->debugger!=0 ) {
dlog("begin_trace: %d Failed to start trace for "PID_F": child_state=%p, parent_state=%p", debugger, child, child_state,
parent_state );
if( child_state!=NULL ) {
dlog("child_state debugger="PID_F, child_state->debugger);
}
dlog("\n");
errno=EPERM;
return false;
}
child_state->debugger=debugger;
child_state->trace_mode=PTRACE_CONT;
parent_state->num_debugees++;
return true;
}
static void handle_cont_syscall( pid_t pid, pid_state *state )
{
if( verify_permission( pid, state ) ) {
pid_t child=(pid_t)state->context_state[1];
pid_state *child_state=lookup_state( child );
__ptrace_request req=(__ptrace_request)state->context_state[0];
if( req==PTRACE_CONT ) {
child_state->trace_mode|=TRACE_CONT;
dlog("ptrace: %d PTRACE_CONT("PID_F")\n", pid, child );
} else {
child_state->trace_mode|=TRACE_SYSCALL;
dlog("ptrace: %d PTRACE_SYSCALL("PID_F")\n", pid, child );
}
long rc=0;
if( (child_state->trace_mode&TRACE_MASK2)==TRACE_STOPPED1 ) {
dlog("handle_cont_syscall: "PID_F" process "PID_F" was in pre-syscall hook\n", pid, child );
// Need to restart the syscall
int status=child_state->context_state[1];
PTLIB_WAIT_RET wait_state=(PTLIB_WAIT_RET)child_state->context_state[0];
long ret=ptlib_get_syscall( child );
int sig=process_sigchld( child, wait_state, status, ret );
// If our processing requested no special handling, use the signal requested by the debugger
if( sig==0 ) {
sig=(int)state->context_state[3];
}
if( sig>=0 ) {
rc=ptlib_continue(PTRACE_SYSCALL, child, sig);
}
} else if( (child_state->trace_mode&TRACE_MASK2)==TRACE_STOPPED2 ) {
dlog("handle_cont_syscall: "PID_F" process "PID_F" was in post-syscall hook\n", pid, child );
child_state->trace_mode&=TRACE_MASK1;
rc=ptlib_continue( PTRACE_SYSCALL, child, (int)state->context_state[3] );
} else {
// Our child was not stopped (at least, by us)
// This is an internal inconsistency
dlog("handle_cont_syscall: "PID_F" process "PID_F" was started with no specific state (%x)\n", pid, child,
child_state->trace_mode );
dlog(NULL);
rc=-1;
}
if( rc!=-1 ) {
dlog("ptrace: %d request successful\n", pid );
ptlib_set_retval( pid, rc );
} else {
ptlib_set_error( pid, state->orig_sc, errno );
dlog("ptrace: %d request failed: %s\n", pid, strerror(errno) );
}
}
}
static bool handle_detach( pid_t pid, pid_state *state )
{
if( verify_permission( pid, state ) ) {
dlog("ptrace: %d PTRACE_DETACH("PID_F")\n", pid, (pid_t)state->context_state[1]);
pid_state *child_state=lookup_state((pid_t)state->context_state[1]);
child_state->debugger=0;
state->num_debugees--;
child_state->trace_mode=TRACE_DETACHED;
ptlib_set_retval( pid, 0 );
return true;
} else {
ptlib_set_error( pid, state->orig_sc, errno );
return false;
}
}
static void handle_kill( pid_t pid, pid_state *state )
{
pid_t child=(pid_t)state->context_state[1];
if( verify_permission( pid, state ) ) {
dlog("handle_kill: %d is sending a kill to "PID_F"\n", pid, child );
ptlib_continue(PTRACE_KILL, child, 0);
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, errno );
dlog("handle_kill: %d tried to kill "PID_F": %s\n", pid, child, strerror(errno));
}
}
static void handle_peek_data( pid_t pid, pid_state *state )
{
pid_t child=(pid_t)state->context_state[1];
if( verify_permission( pid, state ) ) {
errno=0;
long data=ptrace( (__ptrace_request)state->context_state[0], child, state->context_state[2], 0 );
if( data!=-1 || errno==0 ) {
dlog("handle_peek_data: %d is peeking data from "PID_F" at address %p\n", pid, child, state->context_state[2] );
// Write the result where applicable
// XXX This may be a Linux only semantics - pass addres to write result to as "data" argument
data=ptlib_set_mem( pid, &data, (void *)state->context_state[3], sizeof(data));
if( data!=-1 ) {
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, errno );
dlog("handle_peek_data: Our own poke failed: %s\n", strerror(errno) );
}
}
} else {
ptlib_set_error( pid, state->orig_sc, errno );
dlog("handle_peek_data: %d tried get data from "PID_F": %s\n", pid, child, strerror(errno));
}
}
static void handle_poke_data( pid_t pid, pid_state *state )
{
pid_t child=(pid_t)state->context_state[1];
if( verify_permission( pid, state ) &&
ptrace( (__ptrace_request)state->context_state[0], child, state->context_state[2], state->context_state[3] )==0 )
{
dlog("handle_poke_data: %d is pokeing data in "PID_F" at address %p\n", pid, child, state->context_state[2] );
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, errno );
dlog("handle_poke_data: %d tried push data to "PID_F": %s\n", pid, child, strerror(errno));
}
}
bool sys_ptrace( int sc_num, pid_t pid, pid_state *state )
{
bool ret=true;
if( state->state==pid_state::NONE ) {
state->context_state[0]=ptlib_get_argument( pid, 1 ); // request
state->context_state[1]=ptlib_get_argument( pid, 2 ); // pid
state->context_state[2]=ptlib_get_argument( pid, 3 ); // addr
state->context_state[3]=ptlib_get_argument( pid, 4 ); // data
dlog("ptrace: %d ptrace( %d, "PID_F", %p, %p )\n", pid, state->context_state[0], state->context_state[1], state->context_state[2],
state->context_state[3] );
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
state->state=pid_state::NONE;
// Let's see what whether we need to succeed
switch( state->context_state[0] ) {
case PTRACE_TRACEME:
if( begin_trace( state->parent, pid ) ) {
dlog("ptrace: %d PTRACE_TRACEME parent "PID_F"\n", pid, state->parent );
ptlib_set_retval( pid, 0 );
} else {
dlog("ptrace: %d PTRACE_TRACEME failed %s\n", pid, strerror(errno) );
ptlib_set_error( pid, state->orig_sc, errno );
}
break;
case PTRACE_ATTACH:
if( begin_trace( pid, (pid_t)state->context_state[1] ) ) {
dlog("ptrace: "PID_F" PTRACE_ATTACH("PID_F") succeeded\n", pid, (pid_t)state->context_state[1] );
ptlib_set_retval( pid, 0 );
} else {
dlog("ptrace: "PID_F" PTRACE_ATTACH("PID_F") failed %s\n", pid, (pid_t)state->context_state[1], strerror(errno) );
ptlib_set_error( pid, state->orig_sc, errno );
}
break;
case PTRACE_PEEKTEXT:
case PTRACE_PEEKDATA:
case PTRACE_PEEKUSER:
handle_peek_data( pid, state );
break;
case PTRACE_POKETEXT:
case PTRACE_POKEDATA:
case PTRACE_POKEUSER:
handle_poke_data( pid, state );
break;
#if 0
case PTRACE_GETREGS:
case PTRACE_GETFPREGS:
dlog("ptrace: %d GETREGS not yet implemented\n", pid);
ptlib_set_error( pid, state->orig_sc, EINVAL );
break;
case PTRACE_SETREGS:
case PTRACE_SETFPREGS:
dlog("ptrace: %d SETREGS not yet implemented\n", pid);
ptlib_set_error( pid, state->orig_sc, EINVAL );
break;
case PTRACE_GETSIGINFO:
dlog("ptrace: %d GETSIGINFO not yet implemented\n", pid);
ptlib_set_error( pid, state->orig_sc, EINVAL );
break;
case PTRACE_SETSIGINFO:
dlog("ptrace: %d SETSIGINFO not yet implemented\n", pid);
ptlib_set_error( pid, state->orig_sc, EINVAL );
break;
#endif
case PTRACE_SINGLESTEP:
// We do not support single step right now
ptlib_set_error( pid, state->orig_sc, EINVAL );
dlog("ptrace: "PID_F" tried to call SINGLESTEP on "PID_F"\n", pid, (pid_t)state->context_state[1]);
break;
case PTRACE_CONT:
case PTRACE_SYSCALL:
handle_cont_syscall( pid, state );
break;
case PTRACE_KILL:
handle_kill( pid, state );
break;
case PTRACE_DETACH:
handle_detach( pid, state );
break;
default:
dlog("ptrace: "PID_F" Unsupported option %lx\n", pid, state->context_state[0] );
ptlib_set_error(pid, state->orig_sc, EINVAL);
break;
}
ptlib_set_syscall( pid, state->orig_sc );
}
return ret;
}
|