proto_socks.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or/or.h"
  7. #include "or/addressmap.h"
  8. #include "common/buffers.h"
  9. #include "or/control.h"
  10. #include "or/config.h"
  11. #include "lib/crypt_ops/crypto_util.h"
  12. #include "or/ext_orport.h"
  13. #include "or/proto_socks.h"
  14. #include "or/reasons.h"
  15. #include "trunnel/socks5.h"
  16. #include "or/socks_request_st.h"
  17. typedef enum {
  18. SOCKS_RESULT_INVALID = -1,
  19. SOCKS_RESULT_TRUNCATED = 0,
  20. SOCKS_RESULT_DONE = 1,
  21. SOCKS_RESULT_MORE_EXPECTED = 2,
  22. } socks_result_t;
  23. static void socks_request_set_socks5_error(socks_request_t *req,
  24. socks5_reply_status_t reason);
  25. static socks_result_t parse_socks(const char *data,
  26. size_t datalen,
  27. socks_request_t *req,
  28. int log_sockstype,
  29. int safe_socks,
  30. size_t *drain_out);
  31. static int parse_socks_client(const uint8_t *data, size_t datalen,
  32. int state, char **reason,
  33. ssize_t *drain_out);
  34. /**
  35. * Wait this many seconds before warning the user about using SOCKS unsafely
  36. * again. */
  37. #define SOCKS_WARN_INTERVAL 5
  38. /** Warn that the user application has made an unsafe socks request using
  39. * protocol <b>socks_protocol</b> on port <b>port</b>. Don't warn more than
  40. * once per SOCKS_WARN_INTERVAL, unless <b>safe_socks</b> is set. */
  41. static void
  42. log_unsafe_socks_warning(int socks_protocol, const char *address,
  43. uint16_t port, int safe_socks)
  44. {
  45. static ratelim_t socks_ratelim = RATELIM_INIT(SOCKS_WARN_INTERVAL);
  46. if (safe_socks) {
  47. log_fn_ratelim(&socks_ratelim, LOG_WARN, LD_APP,
  48. "Your application (using socks%d to port %d) is giving "
  49. "Tor only an IP address. Applications that do DNS resolves "
  50. "themselves may leak information. Consider using Socks4A "
  51. "(e.g. via privoxy or socat) instead. For more information, "
  52. "please see https://wiki.torproject.org/TheOnionRouter/"
  53. "TorFAQ#SOCKSAndDNS.%s",
  54. socks_protocol,
  55. (int)port,
  56. safe_socks ? " Rejecting." : "");
  57. }
  58. control_event_client_status(LOG_WARN,
  59. "DANGEROUS_SOCKS PROTOCOL=SOCKS%d ADDRESS=%s:%d",
  60. socks_protocol, address, (int)port);
  61. }
  62. /** Do not attempt to parse socks messages longer than this. This value is
  63. * actually significantly higher than the longest possible socks message. */
  64. #define MAX_SOCKS_MESSAGE_LEN 512
  65. /** Return a new socks_request_t. */
  66. socks_request_t *
  67. socks_request_new(void)
  68. {
  69. return tor_malloc_zero(sizeof(socks_request_t));
  70. }
  71. /** Free all storage held in the socks_request_t <b>req</b>. */
  72. void
  73. socks_request_free_(socks_request_t *req)
  74. {
  75. if (!req)
  76. return;
  77. if (req->username) {
  78. memwipe(req->username, 0x10, req->usernamelen);
  79. tor_free(req->username);
  80. }
  81. if (req->password) {
  82. memwipe(req->password, 0x04, req->passwordlen);
  83. tor_free(req->password);
  84. }
  85. memwipe(req, 0xCC, sizeof(socks_request_t));
  86. tor_free(req);
  87. }
  88. static socks_result_t
  89. parse_socks4_request(const uint8_t *raw_data, socks_request_t *req,
  90. size_t datalen, int *is_socks4a, size_t *drain_out)
  91. {
  92. // http://ss5.sourceforge.net/socks4.protocol.txt
  93. // http://ss5.sourceforge.net/socks4A.protocol.txt
  94. socks_result_t res = SOCKS_RESULT_DONE;
  95. tor_addr_t destaddr;
  96. tor_assert(is_socks4a);
  97. tor_assert(drain_out);
  98. *is_socks4a = 0;
  99. *drain_out = 0;
  100. req->socks_version = 4;
  101. socks4_client_request_t *trunnel_req;
  102. ssize_t parsed =
  103. socks4_client_request_parse(&trunnel_req, raw_data, datalen);
  104. if (parsed == -1) {
  105. log_warn(LD_APP, "socks4: parsing failed - invalid request.");
  106. res = SOCKS_RESULT_INVALID;
  107. goto end;
  108. } else if (parsed == -2) {
  109. res = SOCKS_RESULT_TRUNCATED;
  110. if (datalen >= MAX_SOCKS_MESSAGE_LEN) {
  111. log_warn(LD_APP, "socks4: parsing failed - invalid request.");
  112. res = SOCKS_RESULT_INVALID;
  113. }
  114. goto end;
  115. }
  116. tor_assert(parsed >= 0);
  117. *drain_out = (size_t)parsed;
  118. uint8_t command = socks4_client_request_get_command(trunnel_req);
  119. req->command = command;
  120. req->port = socks4_client_request_get_port(trunnel_req);
  121. uint32_t dest_ip = socks4_client_request_get_addr(trunnel_req);
  122. if ((!req->port && req->command != SOCKS_COMMAND_RESOLVE) ||
  123. dest_ip == 0) {
  124. log_warn(LD_APP, "socks4: Port or DestIP is zero. Rejecting.");
  125. res = SOCKS_RESULT_INVALID;
  126. goto end;
  127. }
  128. *is_socks4a = (dest_ip >> 8) == 0;
  129. const char *username = socks4_client_request_get_username(trunnel_req);
  130. size_t usernamelen = username ? strlen(username) : 0;
  131. if (username && usernamelen) {
  132. if (usernamelen > MAX_SOCKS_MESSAGE_LEN) {
  133. log_warn(LD_APP, "Socks4 user name too long; rejecting.");
  134. res = SOCKS_RESULT_INVALID;
  135. goto end;
  136. }
  137. req->got_auth = 1;
  138. req->username = tor_strdup(username);
  139. req->usernamelen = usernamelen;
  140. }
  141. if (*is_socks4a) {
  142. // We cannot rely on trunnel here, as we want to detect if
  143. // we have abnormally long hostname field.
  144. const char *hostname = (char *)raw_data + SOCKS4_NETWORK_LEN +
  145. strlen(username) + 1;
  146. size_t hostname_len = (char *)raw_data + datalen - hostname;
  147. if (hostname_len <= sizeof(req->address)) {
  148. const char *trunnel_hostname =
  149. socks4_client_request_get_socks4a_addr_hostname(trunnel_req);
  150. if (trunnel_hostname)
  151. strlcpy(req->address, trunnel_hostname, sizeof(req->address));
  152. } else {
  153. log_warn(LD_APP, "socks4: Destaddr too long. Rejecting.");
  154. res = SOCKS_RESULT_INVALID;
  155. goto end;
  156. }
  157. } else {
  158. tor_addr_from_ipv4h(&destaddr, dest_ip);
  159. if (!tor_addr_to_str(req->address, &destaddr,
  160. MAX_SOCKS_ADDR_LEN, 0)) {
  161. res = SOCKS_RESULT_INVALID;
  162. goto end;
  163. }
  164. }
  165. end:
  166. socks4_client_request_free(trunnel_req);
  167. return res;
  168. }
  169. /**
  170. * Validate SOCKS4/4a related fields in <b>req</b>. Expect SOCKS4a
  171. * if <b>is_socks4a</b> is true. If <b>log_sockstype</b> is true,
  172. * log a notice about possible DNS leaks on local system. If
  173. * <b>safe_socks</b> is true, reject insecure usage of SOCKS
  174. * protocol.
  175. *
  176. * Return SOCKS_RESULT_DONE if validation passed or
  177. * SOCKS_RESULT_INVALID if it failed.
  178. */
  179. static socks_result_t
  180. process_socks4_request(const socks_request_t *req, int is_socks4a,
  181. int log_sockstype, int safe_socks)
  182. {
  183. if (is_socks4a && !addressmap_have_mapping(req->address, 0)) {
  184. log_unsafe_socks_warning(4, req->address, req->port, safe_socks);
  185. if (safe_socks)
  186. return SOCKS_RESULT_INVALID;
  187. }
  188. if (req->command != SOCKS_COMMAND_CONNECT &&
  189. req->command != SOCKS_COMMAND_RESOLVE) {
  190. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  191. * socks4.) */
  192. log_warn(LD_APP, "socks4: command %d not recognized. Rejecting.",
  193. req->command);
  194. return SOCKS_RESULT_INVALID;
  195. }
  196. if (is_socks4a) {
  197. if (log_sockstype)
  198. log_notice(LD_APP,
  199. "Your application (using socks4a to port %d) instructed "
  200. "Tor to take care of the DNS resolution itself if "
  201. "necessary. This is good.", req->port);
  202. }
  203. if (!string_is_valid_dest(req->address)) {
  204. log_warn(LD_PROTOCOL,
  205. "Your application (using socks4 to port %d) gave Tor "
  206. "a malformed hostname: %s. Rejecting the connection.",
  207. req->port, escaped_safe_str_client(req->address));
  208. return SOCKS_RESULT_INVALID;
  209. }
  210. return SOCKS_RESULT_DONE;
  211. }
  212. static socks_result_t
  213. parse_socks5_methods_request(const uint8_t *raw_data, socks_request_t *req,
  214. size_t datalen, int *have_user_pass,
  215. int *have_no_auth, size_t *drain_out)
  216. {
  217. socks_result_t res = SOCKS_RESULT_DONE;
  218. socks5_client_version_t *trunnel_req;
  219. ssize_t parsed = socks5_client_version_parse(&trunnel_req, raw_data,
  220. datalen);
  221. (void)req;
  222. tor_assert(have_no_auth);
  223. tor_assert(have_user_pass);
  224. tor_assert(drain_out);
  225. *drain_out = 0;
  226. if (parsed == -1) {
  227. log_warn(LD_APP, "socks5: parsing failed - invalid version "
  228. "id/method selection message.");
  229. res = SOCKS_RESULT_INVALID;
  230. goto end;
  231. } else if (parsed == -2) {
  232. res = SOCKS_RESULT_TRUNCATED;
  233. if (datalen > MAX_SOCKS_MESSAGE_LEN) {
  234. log_warn(LD_APP, "socks5: parsing failed - invalid version "
  235. "id/method selection message.");
  236. res = SOCKS_RESULT_INVALID;
  237. }
  238. goto end;
  239. }
  240. tor_assert(parsed >= 0);
  241. *drain_out = (size_t)parsed;
  242. size_t n_methods = (size_t)socks5_client_version_get_n_methods(trunnel_req);
  243. if (n_methods == 0) {
  244. res = SOCKS_RESULT_INVALID;
  245. goto end;
  246. }
  247. *have_no_auth = 0;
  248. *have_user_pass = 0;
  249. for (size_t i = 0; i < n_methods; i++) {
  250. uint8_t method = socks5_client_version_get_methods(trunnel_req,
  251. i);
  252. if (method == SOCKS_USER_PASS) {
  253. *have_user_pass = 1;
  254. } else if (method == SOCKS_NO_AUTH) {
  255. *have_no_auth = 1;
  256. }
  257. }
  258. end:
  259. socks5_client_version_free(trunnel_req);
  260. return res;
  261. }
  262. static socks_result_t
  263. process_socks5_methods_request(socks_request_t *req, int have_user_pass,
  264. int have_no_auth)
  265. {
  266. socks_result_t res = SOCKS_RESULT_DONE;
  267. socks5_server_method_t *trunnel_resp = socks5_server_method_new();
  268. socks5_server_method_set_version(trunnel_resp, 5);
  269. if (have_user_pass && !(have_no_auth && req->socks_prefer_no_auth)) {
  270. req->auth_type = SOCKS_USER_PASS;
  271. socks5_server_method_set_method(trunnel_resp, SOCKS_USER_PASS);
  272. req->socks_version = 5; // FIXME: come up with better way to remember
  273. // that we negotiated auth
  274. log_debug(LD_APP,"socks5: accepted method 2 (username/password)");
  275. } else if (have_no_auth) {
  276. req->auth_type = SOCKS_NO_AUTH;
  277. socks5_server_method_set_method(trunnel_resp, SOCKS_NO_AUTH);
  278. req->socks_version = 5;
  279. log_debug(LD_APP,"socks5: accepted method 0 (no authentication)");
  280. } else {
  281. log_warn(LD_APP,
  282. "socks5: offered methods don't include 'no auth' or "
  283. "username/password. Rejecting.");
  284. socks5_server_method_set_method(trunnel_resp, 0xFF); // reject all
  285. res = SOCKS_RESULT_INVALID;
  286. }
  287. const char *errmsg = socks5_server_method_check(trunnel_resp);
  288. if (errmsg) {
  289. log_warn(LD_APP, "socks5: method selection validation failed: %s",
  290. errmsg);
  291. res = SOCKS_RESULT_INVALID;
  292. } else {
  293. ssize_t encoded =
  294. socks5_server_method_encode(req->reply, sizeof(req->reply),
  295. trunnel_resp);
  296. if (encoded < 0) {
  297. log_warn(LD_APP, "socks5: method selection encoding failed");
  298. res = SOCKS_RESULT_INVALID;
  299. } else {
  300. req->replylen = (size_t)encoded;
  301. }
  302. }
  303. socks5_server_method_free(trunnel_resp);
  304. return res;
  305. }
  306. static socks_result_t
  307. parse_socks5_userpass_auth(const uint8_t *raw_data, socks_request_t *req,
  308. size_t datalen, size_t *drain_out)
  309. {
  310. socks_result_t res = SOCKS_RESULT_DONE;
  311. socks5_client_userpass_auth_t *trunnel_req = NULL;
  312. ssize_t parsed = socks5_client_userpass_auth_parse(&trunnel_req, raw_data,
  313. datalen);
  314. tor_assert(drain_out);
  315. *drain_out = 0;
  316. if (parsed == -1) {
  317. log_warn(LD_APP, "socks5: parsing failed - invalid user/pass "
  318. "authentication message.");
  319. res = SOCKS_RESULT_INVALID;
  320. goto end;
  321. } else if (parsed == -2) {
  322. res = SOCKS_RESULT_TRUNCATED;
  323. goto end;
  324. }
  325. tor_assert(parsed >= 0);
  326. *drain_out = (size_t)parsed;
  327. uint8_t usernamelen =
  328. socks5_client_userpass_auth_get_username_len(trunnel_req);
  329. uint8_t passwordlen =
  330. socks5_client_userpass_auth_get_passwd_len(trunnel_req);
  331. const char *username =
  332. socks5_client_userpass_auth_getconstarray_username(trunnel_req);
  333. const char *password =
  334. socks5_client_userpass_auth_getconstarray_passwd(trunnel_req);
  335. if (usernamelen && username) {
  336. req->username = tor_memdup_nulterm(username, usernamelen);
  337. req->usernamelen = usernamelen;
  338. req->got_auth = 1;
  339. }
  340. if (passwordlen && password) {
  341. req->password = tor_memdup_nulterm(password, passwordlen);
  342. req->passwordlen = passwordlen;
  343. req->got_auth = 1;
  344. }
  345. end:
  346. socks5_client_userpass_auth_free(trunnel_req);
  347. return res;
  348. }
  349. static socks_result_t
  350. process_socks5_userpass_auth(socks_request_t *req)
  351. {
  352. socks_result_t res = SOCKS_RESULT_DONE;
  353. socks5_server_userpass_auth_t *trunnel_resp =
  354. socks5_server_userpass_auth_new();
  355. if (req->socks_version != 5) {
  356. res = SOCKS_RESULT_INVALID;
  357. goto end;
  358. }
  359. if (req->auth_type != SOCKS_USER_PASS &&
  360. req->auth_type != SOCKS_NO_AUTH) {
  361. res = SOCKS_RESULT_INVALID;
  362. goto end;
  363. }
  364. socks5_server_userpass_auth_set_version(trunnel_resp, 1);
  365. socks5_server_userpass_auth_set_status(trunnel_resp, 0); // auth OK
  366. const char *errmsg = socks5_server_userpass_auth_check(trunnel_resp);
  367. if (errmsg) {
  368. log_warn(LD_APP, "socks5: server userpass auth validation failed: %s",
  369. errmsg);
  370. res = SOCKS_RESULT_INVALID;
  371. goto end;
  372. }
  373. ssize_t encoded = socks5_server_userpass_auth_encode(req->reply,
  374. sizeof(req->reply),
  375. trunnel_resp);
  376. if (encoded < 0) {
  377. log_warn(LD_APP, "socks5: server userpass auth encoding failed");
  378. res = SOCKS_RESULT_INVALID;
  379. goto end;
  380. }
  381. req->replylen = (size_t)encoded;
  382. end:
  383. socks5_server_userpass_auth_free(trunnel_resp);
  384. return res;
  385. }
  386. static socks_result_t
  387. parse_socks5_client_request(const uint8_t *raw_data, socks_request_t *req,
  388. size_t datalen, size_t *drain_out)
  389. {
  390. socks_result_t res = SOCKS_RESULT_DONE;
  391. tor_addr_t destaddr;
  392. socks5_client_request_t *trunnel_req = NULL;
  393. ssize_t parsed =
  394. socks5_client_request_parse(&trunnel_req, raw_data, datalen);
  395. if (parsed == -1) {
  396. log_warn(LD_APP, "socks5: parsing failed - invalid client request");
  397. res = SOCKS_RESULT_INVALID;
  398. goto end;
  399. } else if (parsed == -2) {
  400. res = SOCKS_RESULT_TRUNCATED;
  401. goto end;
  402. }
  403. tor_assert(parsed >= 0);
  404. *drain_out = (size_t)parsed;
  405. if (socks5_client_request_get_version(trunnel_req) != 5) {
  406. res = SOCKS_RESULT_INVALID;
  407. goto end;
  408. }
  409. req->command = socks5_client_request_get_command(trunnel_req);
  410. req->port = socks5_client_request_get_dest_port(trunnel_req);
  411. uint8_t atype = socks5_client_request_get_atype(trunnel_req);
  412. req->socks5_atyp = atype;
  413. switch (atype) {
  414. case 1: {
  415. uint32_t ipv4 = socks5_client_request_get_dest_addr_ipv4(trunnel_req);
  416. tor_addr_from_ipv4h(&destaddr, ipv4);
  417. tor_addr_to_str(req->address, &destaddr, sizeof(req->address), 1);
  418. } break;
  419. case 3: {
  420. const struct domainname_st *dns_name =
  421. socks5_client_request_getconst_dest_addr_domainname(trunnel_req);
  422. const char *hostname = domainname_getconstarray_name(dns_name);
  423. strlcpy(req->address, hostname, sizeof(req->address));
  424. } break;
  425. case 4: {
  426. const char *ipv6 =
  427. (const char *)socks5_client_request_getarray_dest_addr_ipv6(
  428. trunnel_req);
  429. tor_addr_from_ipv6_bytes(&destaddr, ipv6);
  430. tor_addr_to_str(req->address, &destaddr, sizeof(req->address), 1);
  431. } break;
  432. default: {
  433. res = -1;
  434. } break;
  435. }
  436. end:
  437. socks5_client_request_free(trunnel_req);
  438. return res;
  439. }
  440. static socks_result_t
  441. process_socks5_client_request(socks_request_t *req,
  442. int log_sockstype,
  443. int safe_socks)
  444. {
  445. socks_result_t res = SOCKS_RESULT_DONE;
  446. if (req->command != SOCKS_COMMAND_CONNECT &&
  447. req->command != SOCKS_COMMAND_RESOLVE &&
  448. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  449. socks_request_set_socks5_error(req,SOCKS5_COMMAND_NOT_SUPPORTED);
  450. res = SOCKS_RESULT_INVALID;
  451. goto end;
  452. }
  453. if (req->command == SOCKS_COMMAND_RESOLVE_PTR &&
  454. !string_is_valid_ipv4_address(req->address) &&
  455. !string_is_valid_ipv6_address(req->address)) {
  456. socks_request_set_socks5_error(req, SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  457. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  458. "hostname type. Rejecting.");
  459. res = SOCKS_RESULT_INVALID;
  460. goto end;
  461. }
  462. if (!string_is_valid_dest(req->address)) {
  463. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  464. log_warn(LD_PROTOCOL,
  465. "Your application (using socks5 to port %d) gave Tor "
  466. "a malformed hostname: %s. Rejecting the connection.",
  467. req->port, escaped_safe_str_client(req->address));
  468. res = SOCKS_RESULT_INVALID;
  469. goto end;
  470. }
  471. if (req->socks5_atyp == 1 || req->socks5_atyp == 4) {
  472. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  473. !addressmap_have_mapping(req->address,0)) {
  474. log_unsafe_socks_warning(5, req->address, req->port, safe_socks);
  475. if (safe_socks) {
  476. socks_request_set_socks5_error(req, SOCKS5_NOT_ALLOWED);
  477. res = SOCKS_RESULT_INVALID;
  478. goto end;
  479. }
  480. }
  481. }
  482. if (log_sockstype)
  483. log_notice(LD_APP,
  484. "Your application (using socks5 to port %d) instructed "
  485. "Tor to take care of the DNS resolution itself if "
  486. "necessary. This is good.", req->port);
  487. end:
  488. return res;
  489. }
  490. static socks_result_t
  491. handle_socks_message(const uint8_t *raw_data, size_t datalen,
  492. socks_request_t *req, int log_sockstype,
  493. int safe_socks, size_t *drain_out)
  494. {
  495. socks_result_t res = SOCKS_RESULT_DONE;
  496. uint8_t socks_version = raw_data[0];
  497. if (socks_version == 1)
  498. socks_version = 5; // SOCKS5 username/pass subnegotiation
  499. if (socks_version == 4) {
  500. if (datalen < SOCKS4_NETWORK_LEN) {
  501. res = 0;
  502. goto end;
  503. }
  504. int is_socks4a = 0;
  505. res = parse_socks4_request((const uint8_t *)raw_data, req, datalen,
  506. &is_socks4a, drain_out);
  507. if (res != SOCKS_RESULT_DONE) {
  508. goto end;
  509. }
  510. res = process_socks4_request(req, is_socks4a,log_sockstype,
  511. safe_socks);
  512. if (res != SOCKS_RESULT_DONE) {
  513. goto end;
  514. }
  515. goto end;
  516. } else if (socks_version == 5) {
  517. if (datalen < 2) { /* version and another byte */
  518. res = 0;
  519. goto end;
  520. }
  521. /* RFC1929 SOCKS5 username/password subnegotiation. */
  522. if (!req->got_auth && (raw_data[0] == 1 ||
  523. req->auth_type == SOCKS_USER_PASS)) {
  524. res = parse_socks5_userpass_auth(raw_data, req, datalen,
  525. drain_out);
  526. if (res != SOCKS_RESULT_DONE) {
  527. goto end;
  528. }
  529. res = process_socks5_userpass_auth(req);
  530. if (res != SOCKS_RESULT_DONE) {
  531. goto end;
  532. }
  533. res = SOCKS_RESULT_MORE_EXPECTED;
  534. goto end;
  535. } else if (req->socks_version != 5) {
  536. int have_user_pass, have_no_auth;
  537. res = parse_socks5_methods_request(raw_data, req, datalen,
  538. &have_user_pass,
  539. &have_no_auth,
  540. drain_out);
  541. if (res != SOCKS_RESULT_DONE) {
  542. goto end;
  543. }
  544. res = process_socks5_methods_request(req, have_user_pass,
  545. have_no_auth);
  546. if (res != SOCKS_RESULT_DONE) {
  547. goto end;
  548. }
  549. res = SOCKS_RESULT_MORE_EXPECTED;
  550. goto end;
  551. } else {
  552. res = parse_socks5_client_request(raw_data, req,
  553. datalen, drain_out);
  554. if (res != SOCKS_RESULT_DONE) {
  555. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  556. goto end;
  557. }
  558. res = process_socks5_client_request(req, log_sockstype,
  559. safe_socks);
  560. if (res != SOCKS_RESULT_DONE) {
  561. goto end;
  562. }
  563. }
  564. } else {
  565. *drain_out = datalen;
  566. res = SOCKS_RESULT_INVALID;
  567. }
  568. end:
  569. return res;
  570. }
  571. /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
  572. * of the forms
  573. * - socks4: "socksheader username\\0"
  574. * - socks4a: "socksheader username\\0 destaddr\\0"
  575. * - socks5 phase one: "version #methods methods"
  576. * - socks5 phase two: "version command 0 addresstype..."
  577. * If it's a complete and valid handshake, and destaddr fits in
  578. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  579. * assign to <b>req</b>, and return 1.
  580. *
  581. * If it's invalid or too big, return -1.
  582. *
  583. * Else it's not all there yet, leave buf alone and return 0.
  584. *
  585. * If you want to specify the socks reply, write it into <b>req->reply</b>
  586. * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
  587. *
  588. * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
  589. * the connection is possibly leaking DNS requests locally or not.
  590. *
  591. * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
  592. *
  593. * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
  594. * undefined.
  595. */
  596. int
  597. fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  598. int log_sockstype, int safe_socks)
  599. {
  600. int res = 0;
  601. size_t datalen = buf_datalen(buf);
  602. size_t n_drain;
  603. const char *head = NULL;
  604. socks_result_t socks_res;
  605. size_t n_pullup;
  606. if (buf_datalen(buf) < 2) { /* version and another byte */
  607. res = 0;
  608. goto end;
  609. }
  610. do {
  611. n_drain = 0;
  612. n_pullup = MIN(MAX_SOCKS_MESSAGE_LEN, buf_datalen(buf));
  613. buf_pullup(buf, n_pullup, &head, &datalen);
  614. tor_assert(head && datalen >= 2);
  615. socks_res = parse_socks(head, datalen, req, log_sockstype,
  616. safe_socks, &n_drain);
  617. if (socks_res == SOCKS_RESULT_INVALID)
  618. buf_clear(buf);
  619. else if (socks_res != SOCKS_RESULT_TRUNCATED && n_drain > 0)
  620. buf_drain(buf, n_drain);
  621. switch (socks_res) {
  622. case SOCKS_RESULT_INVALID:
  623. res = -1;
  624. break;
  625. case SOCKS_RESULT_DONE:
  626. res = 1;
  627. break;
  628. case SOCKS_RESULT_TRUNCATED:
  629. if (datalen == n_pullup)
  630. return 0;
  631. /* FALLTHRU */
  632. case SOCKS_RESULT_MORE_EXPECTED:
  633. res = 0;
  634. break;
  635. }
  636. } while (res == 0 && head && buf_datalen(buf) >= 2);
  637. end:
  638. return res;
  639. }
  640. /** Create a SOCKS5 reply message with <b>reason</b> in its REP field and
  641. * have Tor send it as error response to <b>req</b>.
  642. */
  643. static void
  644. socks_request_set_socks5_error(socks_request_t *req,
  645. socks5_reply_status_t reason)
  646. {
  647. req->replylen = 10;
  648. memset(req->reply,0,10);
  649. req->reply[0] = 0x05; // VER field.
  650. req->reply[1] = reason; // REP field.
  651. req->reply[3] = 0x01; // ATYP field.
  652. }
  653. static const char SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG[] =
  654. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  655. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  656. "<html>\n"
  657. "<head>\n"
  658. "<title>This is a SOCKS Proxy, Not An HTTP Proxy</title>\n"
  659. "</head>\n"
  660. "<body>\n"
  661. "<h1>This is a SOCKs proxy, not an HTTP proxy.</h1>\n"
  662. "<p>\n"
  663. "It appears you have configured your web browser to use this Tor port as\n"
  664. "an HTTP proxy.\n"
  665. "</p><p>\n"
  666. "This is not correct: This port is configured as a SOCKS proxy, not\n"
  667. "an HTTP proxy. If you need an HTTP proxy tunnel, use the HTTPTunnelPort\n"
  668. "configuration option in place of, or in addition to, SOCKSPort.\n"
  669. "Please configure your client accordingly.\n"
  670. "</p>\n"
  671. "<p>\n"
  672. "See <a href=\"https://www.torproject.org/documentation.html\">"
  673. "https://www.torproject.org/documentation.html</a> for more "
  674. "information.\n"
  675. "</p>\n"
  676. "</body>\n"
  677. "</html>\n";
  678. /** Implementation helper to implement fetch_from_*_socks. Instead of looking
  679. * at a buffer's contents, we look at the <b>datalen</b> bytes of data in
  680. * <b>data</b>. Instead of removing data from the buffer, we set
  681. * <b>drain_out</b> to the amount of data that should be removed (or -1 if the
  682. * buffer should be cleared). Instead of pulling more data into the first
  683. * chunk of the buffer, we set *<b>want_length_out</b> to the number of bytes
  684. * we'd like to see in the input buffer, if they're available. */
  685. static int
  686. parse_socks(const char *data, size_t datalen, socks_request_t *req,
  687. int log_sockstype, int safe_socks, size_t *drain_out)
  688. {
  689. uint8_t socksver;
  690. if (datalen < 2) {
  691. /* We always need at least 2 bytes. */
  692. return 0;
  693. }
  694. socksver = get_uint8(data);
  695. if (socksver == 5 || socksver == 4 ||
  696. socksver == 1) { // XXX: RFC 1929
  697. return handle_socks_message((const uint8_t *)data, datalen, req,
  698. log_sockstype, safe_socks, drain_out);
  699. }
  700. switch (socksver) { /* which version of socks? */
  701. case 'G': /* get */
  702. case 'H': /* head */
  703. case 'P': /* put/post */
  704. case 'C': /* connect */
  705. strlcpy((char*)req->reply, SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG,
  706. MAX_SOCKS_REPLY_LEN);
  707. req->replylen = strlen((char*)req->reply)+1;
  708. /* fall through */
  709. default: /* version is not socks4 or socks5 */
  710. log_warn(LD_APP,
  711. "Socks version %d not recognized. (This port is not an "
  712. "HTTP proxy; did you want to use HTTPTunnelPort?)",
  713. *(data));
  714. {
  715. /* Tell the controller the first 8 bytes. */
  716. char *tmp = tor_strndup(data, datalen < 8 ? datalen : 8);
  717. control_event_client_status(LOG_WARN,
  718. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  719. escaped(tmp));
  720. tor_free(tmp);
  721. }
  722. return -1;
  723. }
  724. tor_assert_unreached();
  725. return -1;
  726. }
  727. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  728. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  729. * incomplete response, 1 on success and -1 on error, in which case
  730. * <b>reason</b> is set to a descriptive message (free() when finished
  731. * with it).
  732. *
  733. * As a special case, 2 is returned when user/pass is required
  734. * during SOCKS5 handshake and user/pass is configured.
  735. */
  736. int
  737. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  738. {
  739. ssize_t drain = 0;
  740. int r;
  741. const char *head = NULL;
  742. size_t datalen = 0;
  743. if (buf_datalen(buf) < 2)
  744. return 0;
  745. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, &head, &datalen);
  746. tor_assert(head && datalen >= 2);
  747. r = parse_socks_client((uint8_t*)head, datalen,
  748. state, reason, &drain);
  749. if (drain > 0)
  750. buf_drain(buf, drain);
  751. else if (drain < 0)
  752. buf_clear(buf);
  753. return r;
  754. }
  755. /** Implementation logic for fetch_from_*_socks_client. */
  756. static int
  757. parse_socks_client(const uint8_t *data, size_t datalen,
  758. int state, char **reason,
  759. ssize_t *drain_out)
  760. {
  761. unsigned int addrlen;
  762. *drain_out = 0;
  763. if (datalen < 2)
  764. return 0;
  765. switch (state) {
  766. case PROXY_SOCKS4_WANT_CONNECT_OK:
  767. /* Wait for the complete response */
  768. if (datalen < 8)
  769. return 0;
  770. if (data[1] != 0x5a) {
  771. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  772. return -1;
  773. }
  774. /* Success */
  775. *drain_out = 8;
  776. return 1;
  777. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  778. /* we don't have any credentials */
  779. if (data[1] != 0x00) {
  780. *reason = tor_strdup("server doesn't support any of our "
  781. "available authentication methods");
  782. return -1;
  783. }
  784. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  785. *drain_out = -1;
  786. return 1;
  787. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  788. /* we have a username and password. return 1 if we can proceed without
  789. * providing authentication, or 2 otherwise. */
  790. switch (data[1]) {
  791. case 0x00:
  792. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  793. "doesn't require authentication.");
  794. *drain_out = -1;
  795. return 1;
  796. case 0x02:
  797. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  798. *drain_out = -1;
  799. return 2;
  800. /* fall through */
  801. }
  802. *reason = tor_strdup("server doesn't support any of our available "
  803. "authentication methods");
  804. return -1;
  805. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  806. /* handle server reply to rfc1929 authentication */
  807. if (data[1] != 0x00) {
  808. *reason = tor_strdup("authentication failed");
  809. return -1;
  810. }
  811. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  812. *drain_out = -1;
  813. return 1;
  814. case PROXY_SOCKS5_WANT_CONNECT_OK:
  815. /* response is variable length. BND.ADDR, etc, isn't needed
  816. * (don't bother with buf_pullup()), but make sure to eat all
  817. * the data used */
  818. /* wait for address type field to arrive */
  819. if (datalen < 4)
  820. return 0;
  821. switch (data[3]) {
  822. case 0x01: /* ip4 */
  823. addrlen = 4;
  824. break;
  825. case 0x04: /* ip6 */
  826. addrlen = 16;
  827. break;
  828. case 0x03: /* fqdn (can this happen here?) */
  829. if (datalen < 5)
  830. return 0;
  831. addrlen = 1 + data[4];
  832. break;
  833. default:
  834. *reason = tor_strdup("invalid response to connect request");
  835. return -1;
  836. }
  837. /* wait for address and port */
  838. if (datalen < 6 + addrlen)
  839. return 0;
  840. if (data[1] != 0x00) {
  841. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  842. return -1;
  843. }
  844. *drain_out = 6 + addrlen;
  845. return 1;
  846. }
  847. /* LCOV_EXCL_START */
  848. /* shouldn't get here if the input state is one we know about... */
  849. tor_assert(0);
  850. return -1;
  851. /* LCOV_EXCL_STOP */
  852. }