From 3d549546655db14105884a81c95a34b2dda779ea Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Fri, 18 Sep 2015 10:49:47 +0000 Subject: [PATCH] Protect against fd reuse. --- experimental/example/app/app.py | 1 + experimental/tracer/ptrace/thread.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/experimental/example/app/app.py b/experimental/example/app/app.py index f1fc4f00a..e8d3d6045 100644 --- a/experimental/example/app/app.py +++ b/experimental/example/app/app.py @@ -47,6 +47,7 @@ def ignore_error(f): @app.route('/') def hello(): qotd_msg = do_qotd() + qotd_msg += do_qotd() return qotd_msg if __name__ == "__main__": diff --git a/experimental/tracer/ptrace/thread.go b/experimental/tracer/ptrace/thread.go index 530c94928..f3cd468a6 100644 --- a/experimental/tracer/ptrace/thread.go +++ b/experimental/tracer/ptrace/thread.go @@ -52,6 +52,7 @@ type thread struct { currentIncoming map[int]*Fd currentOutgoing map[int]*Fd + closedOutgoing []*Fd } func newThread(pid int, process *process, tracer *PTracer) *thread { @@ -206,8 +207,8 @@ func (t *thread) handleConnect(call, result *syscall.PtraceRegs) { t.process.newFd(connection) t.logf("Made connection from %s:%d -> %s:%d on fd %d", - connection.ToAddr, connection.ToPort, connection.FromAddr, - connection.FromPort, fd) + connection.FromAddr, connection.FromPort, + connection.ToAddr, connection.ToPort, fd) } func (t *thread) handleClose(call, result *syscall.PtraceRegs) { @@ -231,6 +232,12 @@ func (t *thread) handleClose(call, result *syscall.PtraceRegs) { } t.currentOutgoing = map[int]*Fd{} + for _, outgoing := range t.closedOutgoing { + //t.logf("Fd %d caused %d", fdNum, outgoing.fd) + fd.Children = append(fd.Children, outgoing) + } + t.closedOutgoing = []*Fd{} + t.tracer.store.RecordConnection(t.process.pid, fd) } @@ -238,6 +245,11 @@ func (t *thread) handleClose(call, result *syscall.PtraceRegs) { delete(t.process.fds, fdNum) for _, thread := range t.process.threads { delete(thread.currentIncoming, fdNum) + + if _, ok := thread.currentOutgoing[fdNum]; ok { + thread.closedOutgoing = append(thread.closedOutgoing, fd) + } + delete(thread.currentOutgoing, fdNum) } }