Commit Graph

18 Commits (master)

Author SHA1 Message Date
rofl0r 1e00b9ac1e get rid of ip_type.c 2020-09-23 17:00:16 +01:00
rofl0r ed8f8444ab allocator_thread: rework message sending structures
simplify code by adding a new at_msg struct that contains both
the message header and the message body.
this allow e.g. atomic writes of the whole message instead of doing
it in 2 steps. unfortunately reads still have to be done in at least
2 steps, since there are no atomicity guarantees[0].
additionally the message header shrunk from 8 to 4 bytes.

[0]: https://stackoverflow.com/questions/14661708/under-what-conditions-are-pipe-reads-atomic
2020-09-20 18:17:51 +01:00
rofl0r 9f17774b99 allocator_thread.c: whitespace cleanup 2019-02-28 13:32:57 +00:00
Tom Li 11988579f5 allocator_thread.c: set O_CLOEXEC/FD_CLOEXEC for pipes, fix #273.
In proxychains, we create pipes and use them internally.
If exec() is called by the program we run, the pipes opened
previously are never closed, causing a file descriptor leak
may eventually crash the program.

This commit calls fcntl() to set FD_CLOEXEC flags on pipes.
AFAIK there's no race condition on pipe creation, but we still
prefer to call the newer pipe2() with O_CLOEXEC if it's supported
by the system, due to its advantage of atomic operation, which
prevents potential race conditions in the future.

Signed-off-by: Tom Li <tomli@tomli.me>
2018-12-25 18:03:39 +00:00
rofl0r 3366dc3131 allocator_thread.c: fix build on FreeBSD due to MAP_ANON
thx to @Low-power for testing
2018-01-10 16:09:09 +00:00
rofl0r d28f4df8e2 allocator_thread.c: define _DARWIN_C_SOURCE so we get MAP_ANON
https://github.com/nneonneo/osx-10.9-opensource/blob/master/xnu-2422.1.72/bsd/sys/mman.h#L142
https://opensource.apple.com/source/xnu/xnu-344/bsd/sys/mman.h
2017-12-21 16:30:17 +00:00
rofl0r 03880ce695 allocator_thread: add assertion that we get the right response
in order to prevent future bugs like the one fixed in cc7bc891ff
we need to assure that the response is of the same type as the request -
if not, some unexpected race condition happened.
2017-12-19 00:35:01 +00:00
rofl0r cc7bc891ff allocator_thread: fix segfault with weechat 2.0
it was reported that weechat 2.0 on ubuntu 16.04 LTS x86_64 segfaulted like this:

4 0x00007f6bf0e7e0c0 in __stack_chk_fail () at stack_chk_fail.c:28
5 0x00007f6bf2536bce in at_get_ip_for_host (host=0x339c4d0 "abcdefghijklmnop.onion", len=22) at src/allocator_thread.c:290
 readbuf = {octet = "irc.", as_int = 778269289} msg = {msgtype = ATM_GETNAME, datalen = 13}

what happened was that weechat forked, thus got its own private copy of the VM
and thus a private copy of the mutex which should prevent parallel use of
at_get_ip_for_host() & friends. therefore the following race was possible:
- process A writes a message of type ATM_GETIP into the server pipe
- process B writes a message of type ATM_GETNAME into the server pipe
- process A write transaction is finished, and goes into receive mode
- server thread reads process B's message and responds with a ATM_GETNAME msg
- process A reads the response which was intended for process B into the 4 byte
  ip address buffer, but ATM_GETNAME are much larger than ATM_GETIP responses,
  resulting in stack corruption.

to prevent this issue, the storage of the mutex must reside in shared memory,
which we achieve via mmap. alternatively, shm_open() or sysvipc shm stuff could
be used. the former requires the mmap call to happen before the fork, the latter
not, however the shm would require a named object in /dev/shm (which requires
generating a unique name per proxychains instance, and subsequent cleanup).
so in the end, the mmap is easier to deal with, and we can be reasonably
certain that our constructor is being run before the hooked application forks.
2017-12-19 00:29:23 +00:00
rofl0r 3b5f41028b allocator_thread: use bigger stacksize for Mac OS X 2017-12-15 13:15:13 +00:00
rofl0r bb3df1e440 allocator_thread.c: handle EINTR case when reading/writing pipe data
addressing #163
2017-02-23 01:08:24 +00:00
rofl0r 2237749567 preliminary ipv6 support
only basic testing was done (with 2 socks5 proxies listening on ::1)
but seems to work as intended.

ipv6 support for the hostsreader (/etc/hosts) is not implemented so far.
2015-08-10 17:00:26 +01:00
rofl0r 2182eff358 fix segfault in DNS mapping lookup code
the allocatorthread got pointers to RAM which were reallocated
behind the back, and if realloc() couldn't grow in-place, lead
to segfaults in applications that do a lot of DNS-lookups such
as webbrowsers.

closes #66
closes #31

thanks to @ravomavain for tracking down the issue.
2015-06-06 11:43:53 +01:00
rofl0r 1da09e49e5 hook close() to prevent rude programs like ssh to close our pipes
those pipes are needed to talk with the dns-name allocator thread.

closes #9
2013-01-21 01:54:45 +01:00
rofl0r af5c6f0c6a replace hostent lookup with better performing in-memory copy.
the central dns resolver function proxy_gethostbyname() used
to iterate over the gethostent() db (/etc/hosts) on each dns
request.
since this is not threadsafe, we synchronized access to it
previously using mutexes. the parsing of this file is slow,
and blocking all threads to do it even moreso.
since gethostent_r() is only available on a few platforms,
i decided to read the hostent db once and then use a quick
in-memory lookup on further usage.

+ some further refactoring.
2012-11-08 01:18:19 +01:00
rofl0r bd07ca49b9 use pipe instead of pipe2 2012-11-07 21:31:18 +01:00
rofl0r f570a66902 use mutexes also to protect the pipe communication
wasn't threadsafe before.
2012-11-07 21:31:18 +01:00
rofl0r d0abc93c74 preliminary first fork-safe version
instead of allocating memory in the child, we now use the allocator
thread to do all the necessary allocations himself.
additionally we provide a clean API to query the ip <-> dns mapping.
these functions connect via a pipe to the allocator thread, and
exchange messages.

further cleanup is needed, but it seems to work so far.
thread-safety is not yet guaranteed.

closes #1
2012-11-07 21:31:11 +01:00
rofl0r 25afe98b20 failed attempt to use shared memory for the ip <-> dns mapping
this is in order to get irssi, which forks for DNS lookups,
and similar programs, to work as intended.

in a previous attempt i learned that shared memory created in a
child process is not visible to the parent;
in this attempt i spin off a thread from the parent which listens
on a pipe and manages the shared memory allocation from the parent
address-space. however this doesnt work as expected:
memory allocated in the parent after the child forked is not visi-
ble to the child as well.

so what happens is: irssi starts a child process, the thread allocs
memory and hands it to the child, the child attempts to write and
segfaults. however irssi doesnt crash. since now the memory is
already allocated, doing the dns lookup again will succeed.

i.e. the dns lookup works now in irssi by luck.
all but the first dns lookups will suceed.

however this is not good enough for me to be satisfied, i commit
this only for documentation purposes.
2012-11-07 16:49:14 +01:00