proto_socks.c 34 KB

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