Se você der uma olhada na tor.service
unit, verá que tem um comando para tentar verificar a configuração do Tor antes de iniciar o serviço.
ExecStartPre=/usr/bin/tor --runasdaemon 0 --defaults-torrc /usr/share/tor/defaults-torrc -f /etc/tor/torrc --verify-config
É aí que as coisas estão dando errado. Quando isso é executado, a configuração não é validada devido ao problema de permissão mencionado anteriormente.
Jan 06 16:18:42 dalaran systemd[1]: Starting Anonymizing overlay network for TCP...
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.650 [notice] Tor 0.2.9.14 (git-3f9bd01bf5736ff6) running on Linux with Libevent 2.0.21-stable, OpenSSL 1.0.2k-fips and Zlib 1.2.7.
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.650 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.650 [notice] Read configuration file "/usr/share/tor/defaults-torrc".
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.650 [notice] Read configuration file "/etc/tor/torrc".
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.653 [warn] Directory /var/lib/tor/hidden_service_01/ cannot be read: Permission denied
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.653 [warn] Checking service directory /var/lib/tor/hidden_service_01/ failed.
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.653 [warn] Failed to parse/validate config: Failed to configure rendezvous options. See logs for details.
Jan 06 16:18:42 dalaran tor[28731]: Jan 06 16:18:42.653 [err] Reading config failed--see warnings above.
Jan 06 16:18:42 dalaran systemd[1]: tor.service: control process exited, code=exited status=1
Jan 06 16:18:42 dalaran systemd[1]: Failed to start Anonymizing overlay network for TCP.
Jan 06 16:18:42 dalaran systemd[1]: Unit tor.service entered failed state.
Jan 06 16:18:42 dalaran systemd[1]: tor.service failed.
Eventualmente eu traço isso para baixo para alguns dos endurecimento que o systemd faz aqui. Se você ler mais no arquivo da unidade, verá que o systemd realmente executa o Tor em um contêiner e bloqueia as permissões com muita força. Ele também remove alguns recursos, de modo que mesmo o root não pode executar algumas ações que o usuário root normalmente seria capaz de fazer, como ler arquivos de outros usuários (isso é conhecido como CAP_DAC_OVERRIDE).
Quando tentamos auditar o início com falha, encontramos:
type=PATH msg=audit(1515277122.651:3600): item=0 name="/var/lib/tor/hidden_service_01/" inode=1054341 dev=fd:01 mode=040700 ouid=988 ogid=983 rdev=00:00 obj=system_u:object_r:tor_var_lib_t:s0 objtype=NORMAL
type=CWD msg=audit(1515277122.651:3600): cwd="/"
type=SYSCALL msg=audit(1515277122.651:3600): arch=c000003e syscall=2 success=no exit=-13 a0=561b6881af10 a1=20000 a2=0 a3=1 items=1 ppid=1 pid=28731 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="tor" exe="/usr/bin/tor" subj=system_u:system_r:tor_t:s0 key=(null)
type=AVC msg=audit(1515277122.651:3600): avc: denied { dac_read_search } for pid=28731 comm="tor" capability=2 scontext=system_u:system_r:tor_t:s0 tcontext=system_u:system_r:tor_t:s0 tclass=capability
type=AVC msg=audit(1515277122.651:3600): avc: denied { dac_override } for pid=28731 comm="tor" capability=1 scontext=system_u:system_r:tor_t:s0 tcontext=system_u:system_r:tor_t:s0 tclass=capability
O que eu encontrei aqui é que o comando para verificar a configuração não estava realmente mudando do usuário root para o usuário toranon antes de ler a configuração, então o acesso ao diretório estava sendo negado, pois o systemd não dava o processo CAP_DAC_OVERRIDE. Você pode ver os IDs do usuário registrados como uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0
.
Então, para resolver isso no meu sistema, eu decidi fazer o systemd iniciar o Tor como o toranon ao invés de iniciar como root e o Tor mudar seu próprio uid / gid.
- Eu removi a configuração
User toranon
de/usr/share/tor/torrc-defaults
. -
Eu criei um arquivo de substituição
/etc/systemd/system/tor.service.d/override.conf
contendo:[Service] User=toranon Group=toranon PermissionsStartOnly=no
É necessário usar PermissionsStartOnly=no
para garantir que o comando ExecStartPre=
seja executado sob o usuário toranon. Na documentação :
PermissionsStartOnly=
Takes a boolean argument. If true, the permission-related execution options, as configured withUser=
and similar options (see systemd.exec(5) for more information), are only applied to the process started withExecStart=
, and not to the various otherExecStartPre=
,ExecStartPost=
,ExecReload=
,ExecStop=
, andExecStopPost=
commands. If false, the setting is applied to all configured commands the same way. Defaults to false.
Após um systemctl daemon-reload
, eu consegui systemctl start tor
com êxito.
Jan 06 16:22:02 dalaran systemd[1]: Starting Anonymizing overlay network for TCP...
Jan 06 16:22:02 dalaran tor[32030]: Jan 06 16:22:02.541 [notice] Tor 0.2.9.14 (git-3f9bd01bf5736ff6) running on Linux with Libevent 2.0.21-stable, OpenSSL 1.0.2k-fips and Zlib 1.2.7.
Jan 06 16:22:02 dalaran tor[32030]: Jan 06 16:22:02.541 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Jan 06 16:22:02 dalaran tor[32030]: Jan 06 16:22:02.541 [notice] Read configuration file "/usr/share/tor/defaults-torrc".
Jan 06 16:22:02 dalaran tor[32030]: Jan 06 16:22:02.541 [notice] Read configuration file "/etc/tor/torrc".
Jan 06 16:22:02 dalaran tor[32030]: Configuration was valid
Jan 06 16:22:02 dalaran systemd[1]: Started Anonymizing overlay network for TCP.
Jan 06 16:22:02 dalaran tor[32035]: Jan 06 16:22:02.665 [notice] Tor 0.2.9.14 (git-3f9bd01bf5736ff6) running on Linux with Libevent 2.0.21-stable, OpenSSL 1.0.2k-fips and Zlib 1.2.7.
Jan 06 16:22:02 dalaran tor[32035]: Jan 06 16:22:02.665 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Jan 06 16:22:02 dalaran tor[32035]: Jan 06 16:22:02.665 [notice] Read configuration file "/usr/share/tor/defaults-torrc".
Jan 06 16:22:02 dalaran tor[32035]: Jan 06 16:22:02.665 [notice] Read configuration file "/etc/tor/torrc".
Jan 06 16:22:02 dalaran tor[32035]: Jan 06 16:22:02.668 [notice] Opening Socks listener on 127.0.0.1:9050
Jan 06 16:22:02 dalaran tor[32035]: Jan 06 16:22:02.668 [notice] Opening Control listener on /run/tor/control
Jan 06 16:22:02 dalaran Tor[32035]: OpenSSL version from headers does not match the version we're running with. If you get weird crashes, that might be why. (Compiled with 100020bf: OpenSSL 1.0.2k 26 Jan 2017; running wit
Jan 06 16:22:02 dalaran Tor[32035]: Tor 0.2.9.14 (git-3f9bd01bf5736ff6) running on Linux with Libevent 2.0.21-stable, OpenSSL 1.0.2k-fips and Zlib 1.2.7.
Jan 06 16:22:02 dalaran Tor[32035]: Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Jan 06 16:22:02 dalaran Tor[32035]: Read configuration file "/usr/share/tor/defaults-torrc".
Jan 06 16:22:02 dalaran Tor[32035]: Read configuration file "/etc/tor/torrc".
Jan 06 16:22:02 dalaran Tor[32035]: Opening Socks listener on 127.0.0.1:9050
Jan 06 16:22:02 dalaran Tor[32035]: Opening Control listener on /run/tor/control
Jan 06 16:22:02 dalaran Tor[32035]: Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Jan 06 16:22:02 dalaran Tor[32035]: Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Jan 06 16:22:02 dalaran Tor[32035]: Bootstrapped 0%: Starting
Jan 06 16:22:02 dalaran Tor[32035]: Bootstrapped 80%: Connecting to the Tor network
Jan 06 16:22:03 dalaran Tor[32035]: Bootstrapped 85%: Finishing handshake with first hop
Jan 06 16:22:04 dalaran Tor[32035]: Bootstrapped 90%: Establishing a Tor circuit
Jan 06 16:22:05 dalaran Tor[32035]: Tor has successfully opened a circuit. Looks like client functionality is working.
Jan 06 16:22:05 dalaran Tor[32035]: Bootstrapped 100%: Done
Em última análise, isso (ou algo parecido) precisa ser incorporado na unidade padrão de configuração e central do Tor enviada pelo Fedora / EPEL, que resolverá o problema para todos.