• Stars
    star
    133
  • Rank 271,206 (Top 6 %)
  • Language
    C
  • License
    ISC License
  • Created over 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example of how to use the ptrace(2) system call to call a userspace method.

Using ptrace(2) To Call a Userspace Function

Unix systems define an incredibly powerful system call called ptrace(2). This system call is available on Linux, BSD, and OS X (but note that the interface is not exactly the same between the Linux ptrace and the BSD/OS X ptrace). Using ptrace you can arbitrarily inspect or modify the state of another process.

While working with ptrace I found a lot of examples online of using ptrace to make a Linux system call. However I was unable to find any examples of how to use ptrace to call a userspace method in the remote process, which is a lot more interesting (in my opinion) and also more difficult to do.

The purpose of this project is to demonstrate what the code looks like to do this. This code will do the equivalent of making this function call in the attached process:

fprintf(stderr, "instruction pointer = %p\n", rip);

where rip is the value of the instruction pointer when the process was attached. After the call to fprintf() completes, the program will resume execution where it was when it was attached, as if nothing has changed.

Note: this code is specific to the Linux implementation of ptrace.

How It Works

Just look at the source code. There are a lot of comments explaining exactly what is going on, what caveats there are, etc.

I also wrote some articles about this program here (part 1) and here (part 2).

Usage

You can compile the code with make. You should see that it builds an executable called call-fprintf. Invoke it like this:

call-fprintf -p <pid>

An easy way to test this is to open two terminals, run echo $$ in the first terminal to get the pid of the shell, and then in the other terminal run call-fprintf with the first shell's pid.

When you run the command, you will see output like this:

$ ./call-fprintf -p 21160
their %rip           0x7f229e153790
allocated memory at  0x7f229e669000
executing jump to mmap region
successfully jumped to mmap area
their libc           0x7f229e08b000
their fprintf        0x7f229e08b000
their stderr         0x7f229e447560
inserting code/data into the mmap area at 0x7f229e669000
setting the registers of the remote process
continuing execution
successfully caught TRAP signal
jumping back to original rip
successfully jumped back to original %rip at 0x7f229e153790
making call to mmap
munmap returned with status 0
restoring old text at 0x7f229e153790
restoring old registers
detaching

Issues With Yama ptrace_scope

If you get a failure like this:

$ ./call-fprintf -p 1
PTRACE_ATTACH: Operation not permitted

then you are trying to trace a process that you don't have permissions to trace, i.e. a process with a different user id than you. You can only ptrace a process whose effective user id is the same as yours (or if you are root).

If you instead get a failure like this:

$ ./call-fprintf -p 5603
PTRACE_ATTACH: Operation not permitted

The likely cause of this failure is that your system has kernel.yama.ptrace_scope = 1
If you would like to disable Yama, you can run: sudo sysctl kernel.yama.ptrace_scope=0

Then the issue is that you have Yama ptrace_scope configured to disallow ptrace. In particular, the default behavior of Ubuntu since Ubuntu 10.10 has been to set kernel.yama.ptrace_scope = 1. If this affects you, you can either run call-fprintf as root, or you can run the command listed in the error message to disable the Yama setting.

More Repositories

1

c.sh

Inline C/asm in Bash
Shell
249
star
2

clang-format-all

Run clang-format -i on all of the files in your C or C++ project
Shell
132
star
3

schemaless

Implementation of the FriendFeed Schema-less MySQL Pattern
Python
86
star
4

epollet

demonstration of how to use the EPOLLET flag
C
62
star
5

demangle

Tool to demangle C++ symbols.
C++
52
star
6

tornado-dns

Asynchronous DNS lookups for Tornado
Python
50
star
7

event-clock

A simple UDP clock service, using libevent
33
star
8

coin-ticker-mode

Emacs minor mode for showing the price of Bitcoin, Ethereum, and other cryptocurrencies.
Emacs Lisp
21
star
9

utxodump

dump bitcoin utxo data
Python
17
star
10

libevent-python

me hacking on libevent-python
C
13
star
11

spv

C++ Bitcoin SPV Client
C++
12
star
12

parse-elf

An example of how to parse an ELF symbol table.
C++
11
star
13

lua-bz2

a binding to libbzip2 for lua
C
7
star
14

autoresize.jquery.js

fork of james padolsey's autoresize jquery plugin
7
star
15

oakland-crime-scraper

get oakland crime data from oakland.crimespotting.org rss feed
Python
7
star
16

garfield

A simple, fast embedded HTTP server.
C++
7
star
17

symtab

C
6
star
18

e

A text editor.
Python
6
star
19

mehfil-scraper

trivial python script that scrapes the mehfil daily menu
Lua
6
star
20

spitfire

spitfire
Python
5
star
21

python.sh

Shell
5
star
22

libattr-hs

bindings to libattr for haskell
Haskell
5
star
23

buttninjas

Implementation of the buttninja string replacement technique.
Python
4
star
24

ptrace-syscall

issue with ptrace + syscall
C
4
star
25

watchman-rpm

RPM Packaging for facebook/watchman
4
star
26

mysql-minutiae

MySQL Minutiae Presentation
4
star
27

splogger

toy logger using spread
C
4
star
28

kohlrabi

mini webapp for viewing tabular reports
Python
4
star
29

color-theme-awesome

awesome colors
Emacs Lisp
3
star
30

geoquad

python module for geoquads
C
3
star
31

mu-rpm

my rpm packaging for mu + mu4e
3
star
32

scanasm

C++
3
star
33

jump

A shell autojump utility.
Go
3
star
34

tools

C++
3
star
35

WebCrawl

web crawler in haskell
Haskell
3
star
36

mixtape

Python
3
star
37

list-unsubscribe

Helper script to unsubscribe from marketing emails.
Python
3
star
38

strutil

fast string utilities for python
C
3
star
39

old-eklitzke.org

JavaScript
3
star
40

evan-haskell-euler

my project euler solutions
Haskell
3
star
41

protolog

Structured logging system built around Google Protocol Buffers
Python
3
star
42

tf-slice

Demonstration of generating mini-batches in Tensorlfow from GPU memory.
Python
3
star
43

scribl

thread-safe statistics and counters
C
3
star
44

wlinfo

Grab Wayland info
C
3
star
45

nairobi-crypto

Presentation on Blockchains/Cryptocurrencies given to Nairobi Garage
TeX
3
star
46

radio-player

A Haskell program to play Internet radio using the new GStreamer bindings
Haskell
3
star
47

fud

a python debugger
Python
3
star
48

danger

C++
3
star
49

tornado-timeout-example

Python
2
star
50

mysql-tips

A developer's guide to MySQL
2
star
51

udp-timer

demo stm app
Haskell
2
star
52

drakebot

C++
2
star
53

hspresent

terminal presentations
Haskell
2
star
54

drifter

a simple js/canvas game
JavaScript
2
star
55

hcalc

trivial calculator in haskell
Haskell
2
star
56

icfp12

icfp12
Python
2
star
57

hackbot

a trivial irc robot (using twisted)
Python
2
star
58

lark

tiny music server
2
star
59

dnn-fastai-project

Jupyter Notebook
2
star
60

graphomatic

JavaScript
2
star
61

bogus-tornado-bench

Python
2
star
62

pnpoly

C
2
star
63

index

implementation of codesear.ch
C++
2
star
64

gst-rip

my cd ripper
2
star
65

bitcoin-tools

dumping ground for my tools
Jupyter Notebook
2
star
66

cogent

simple slice cache
C
2
star
67

associative-benchmark

a benchmark of C++ map/unorderd_map vs. Python dict
C++
2
star
68

cmemcache-yelp

some patches to cmemcache 0.94
Python
2
star
69

hanoi

Towers of Hanoi, in x86 Assembly
Assembly
2
star
70

smiley

JavaScript
2
star
71

icfp08

purely imperative python (pip)
Python
2
star
72

evan-rip

my cd ripper
2
star
73

libfk

libfk
C
2
star
74

python-lru

lru implementation
C
2
star
75

python-tempmount

Handles doing temporary mounts from Python
Python
2
star
76

popcnt.sh

Population count in Bash
Shell
2
star
77

gaia

Python wrapper of getaddrinfo_a(3)
Python
1
star
78

backup-bitcoin

Bitcoin Core backup script for Google Cloud Storage.
Shell
1
star
79

graff

graff spotting
Python
1
star
80

prometheus-node-exporter-deb

Go
1
star
81

hellocl

hello world in opencl
C
1
star
82

bitcoin-dirlocals

.dirlocals.el file for working on bitcoin from emacs
Emacs Lisp
1
star
83

inotifyreload

Python
1
star
84

bitcoin-hd-seed

Tool for extracting a Bitcoin HD wallet seed
Python
1
star
85

yelp-el

emacs stuff for yelp
Emacs Lisp
1
star
86

gh-mirror

mirror github repos
Python
1
star
87

serial-hashes

proof of concept
JavaScript
1
star
88

percona-live-2016

Slides for my Percona Live 2016 talk
TeX
1
star
89

algorithms

Python
1
star
90

ptrace-idiosyncrasy

Demonstrate an interesting idiosyncrasy of the ptrace(2) system call.
C
1
star
91

toffee

dns
Python
1
star
92

thread-bench

Simple benchmark for std::thread in C++11
C++
1
star
93

Data.ConsistentHash

Consistent hashing module for Haskell
1
star
94

stm-presentation

STM Presentation
1
star
95

scponly-rpm

1
star
96

cxx_playground

C++
1
star
97

slow.py

slowness
Python
1
star
98

eklitzke.github.com

1
star
99

mapserv

prototype of an r-tree thrift service
Python
1
star
100

offline-gpg-master-key

Shell
1
star