libcurl-security.3 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .\"
  25. .TH libcurl-security 3 "June 27, 2022" "libcurl 7.85.0" "libcurl security"
  26. .SH NAME
  27. libcurl-security \- security considerations when using libcurl
  28. .SH "Security"
  29. The libcurl project takes security seriously. The library is written with
  30. caution and precautions are taken to mitigate many kinds of risks encountered
  31. while operating with potentially malicious servers on the Internet. It is a
  32. powerful library, however, which allows application writers to make trade-offs
  33. between ease of writing and exposure to potential risky operations. If used
  34. the right way, you can use libcurl to transfer data pretty safely.
  35. Many applications are used in closed networks where users and servers can
  36. (possibly) be trusted, but many others are used on arbitrary servers and are
  37. fed input from potentially untrusted users. Following is a discussion about
  38. some risks in the ways in which applications commonly use libcurl and
  39. potential mitigations of those risks. It is not comprehensive, but shows
  40. classes of attacks that robust applications should consider. The Common
  41. Weakness Enumeration project at https://cwe.mitre.org/ is a good reference for
  42. many of these and similar types of weaknesses of which application writers
  43. should be aware.
  44. .SH "Command Lines"
  45. If you use a command line tool (such as curl) that uses libcurl, and you give
  46. options to the tool on the command line those options can get read by other
  47. users of your system when they use 'ps' or other tools to list currently
  48. running processes.
  49. To avoid these problems, never feed sensitive things to programs using command
  50. line options. Write them to a protected file and use the \-K option to avoid
  51. this.
  52. .SH ".netrc"
  53. \&.netrc is a pretty handy file/feature that allows you to login quickly and
  54. automatically to frequently visited sites. The file contains passwords in
  55. clear text and is a real security risk. In some cases, your .netrc is also
  56. stored in a home directory that is NFS mounted or used on another network
  57. based file system, so the clear text password will fly through your network
  58. every time anyone reads that file.
  59. For applications that enable .netrc use, a user who manage to set the right
  60. URL might then be possible to pass on passwords.
  61. To avoid these problems, do not use .netrc files and never store passwords in
  62. plain text anywhere.
  63. .SH "Clear Text Passwords"
  64. Many of the protocols libcurl supports send name and password unencrypted as
  65. clear text (HTTP Basic authentication, FTP, TELNET etc). It is easy for anyone
  66. on your network or a network nearby yours to just fire up a network analyzer
  67. tool and eavesdrop on your passwords. do not let the fact that HTTP Basic uses
  68. base64 encoded passwords fool you. They may not look readable at a first
  69. glance, but they are easily "deciphered" by anyone within seconds.
  70. To avoid this problem, use an authentication mechanism or other protocol that
  71. does not let snoopers see your password: Digest, CRAM-MD5, Kerberos, SPNEGO or
  72. NTLM authentication. Or even better: use authenticated protocols that protect
  73. the entire connection and everything sent over it.
  74. .SH "Un-authenticated Connections"
  75. Protocols that do not have any form of cryptographic authentication cannot
  76. with any certainty know that they communicate with the right remote server.
  77. If your application is using a fixed scheme or fixed host name, it is not safe
  78. as long as the connection is un-authenticated. There can be a
  79. man-in-the-middle or in fact the whole server might have been replaced by an
  80. evil actor.
  81. Un-authenticated protocols are unsafe. The data that comes back to curl may
  82. have been injected by an attacker. The data that curl sends might be modified
  83. before it reaches the intended server. If it even reaches the intended server
  84. at all.
  85. Remedies:
  86. .IP "Restrict operations to authenticated transfers"
  87. Ie use authenticated protocols protected with HTTPS or SSH.
  88. .IP "Make sure the server's certificate etc is verified"
  89. Never ever switch off certificate verification.
  90. .SH "Redirects"
  91. The \fICURLOPT_FOLLOWLOCATION(3)\fP option automatically follows HTTP
  92. redirects sent by a remote server. These redirects can refer to any kind of
  93. URL, not just HTTP. libcurl restricts the protocols allowed to be used in
  94. redirects for security reasons: only HTTP, HTTPS, FTP and FTPS are
  95. enabled by default. Applications may opt to restrict that set further.
  96. A redirect to a file: URL would cause the libcurl to read (or write) arbitrary
  97. files from the local filesystem. If the application returns the data back to
  98. the user (as would happen in some kinds of CGI scripts), an attacker could
  99. leverage this to read otherwise forbidden data (e.g.
  100. file://localhost/etc/passwd).
  101. If authentication credentials are stored in the ~/.netrc file, or Kerberos is
  102. in use, any other URL type (not just file:) that requires authentication is
  103. also at risk. A redirect such as ftp://some-internal-server/private-file would
  104. then return data even when the server is password protected.
  105. In the same way, if an unencrypted SSH private key has been configured for the
  106. user running the libcurl application, SCP: or SFTP: URLs could access password
  107. or private-key protected resources,
  108. e.g. sftp://user@some-internal-server/etc/passwd
  109. The \fICURLOPT_REDIR_PROTOCOLS(3)\fP and \fICURLOPT_NETRC(3)\fP options can be
  110. used to mitigate against this kind of attack.
  111. A redirect can also specify a location available only on the machine running
  112. libcurl, including servers hidden behind a firewall from the attacker.
  113. e.g. http://127.0.0.1/ or http://intranet/delete-stuff.cgi?delete=all or
  114. tftp://bootp-server/pc-config-data
  115. Applications can mitigate against this by disabling
  116. \fICURLOPT_FOLLOWLOCATION(3)\fP and handling redirects itself, sanitizing URLs
  117. as necessary. Alternately, an app could leave \fICURLOPT_FOLLOWLOCATION(3)\fP
  118. enabled but set \fICURLOPT_REDIR_PROTOCOLS(3)\fP and install a
  119. \fICURLOPT_OPENSOCKETFUNCTION(3)\fP or \fICURLOPT_PREREQFUNCTION(3)\fP callback
  120. function in which addresses are sanitized before use.
  121. .SH "CRLF in Headers"
  122. For all options in libcurl which specify headers, including but not limited to
  123. \fICURLOPT_HTTPHEADER(3)\fP, \fICURLOPT_PROXYHEADER(3)\fP,
  124. \fICURLOPT_COOKIE(3)\fP, \fICURLOPT_USERAGENT(3)\fP, \fICURLOPT_REFERER(3)\fP
  125. and \fICURLOPT_RANGE(3)\fP, libcurl will send the headers as-is and will not
  126. apply any special sanitization or normalization to them.
  127. If you allow untrusted user input into these options without sanitizing CRLF
  128. sequences in them, someone malicious may be able to modify the request in a way
  129. you didn't intend such as injecting new headers.
  130. .SH "Local Resources"
  131. A user who can control the DNS server of a domain being passed in within a URL
  132. can change the address of the host to a local, private address which a
  133. server-side libcurl-using application could then use. e.g. the innocuous URL
  134. http://fuzzybunnies.example.com/ could actually resolve to the IP address of a
  135. server behind a firewall, such as 127.0.0.1 or 10.1.2.3. Applications can
  136. mitigate against this by setting a \fICURLOPT_OPENSOCKETFUNCTION(3)\fP
  137. or \fICURLOPT_PREREQFUNCTION(3)\fP and checking the address before a
  138. connection.
  139. All the malicious scenarios regarding redirected URLs apply just as well to
  140. non-redirected URLs, if the user is allowed to specify an arbitrary URL that
  141. could point to a private resource. For example, a web app providing a
  142. translation service might happily translate file://localhost/etc/passwd and
  143. display the result. Applications can mitigate against this with the
  144. \fICURLOPT_PROTOCOLS(3)\fP option as well as by similar mitigation techniques
  145. for redirections.
  146. A malicious FTP server could in response to the PASV command return an IP
  147. address and port number for a server local to the app running libcurl but
  148. behind a firewall. Applications can mitigate against this by using the
  149. \fICURLOPT_FTP_SKIP_PASV_IP(3)\fP option or \fICURLOPT_FTPPORT(3)\fP.
  150. Local servers sometimes assume local access comes from friends and trusted
  151. users. An application that expects https://example.com/file_to_read that and
  152. instead gets http://192.168.0.1/my_router_config might print a file that would
  153. otherwise be protected by the firewall.
  154. Allowing your application to connect to local hosts, be it the same machine
  155. that runs the application or a machine on the same local network, might be
  156. possible to exploit by an attacker who then perhaps can "port-scan" the
  157. particular hosts - depending on how the application and servers acts.
  158. .SH "IPv4 Addresses"
  159. Some users might be tempted to filter access to local resources or similar
  160. based on numerical IPv4 addresses used in URLs. This is a bad and error-prone
  161. idea because of the many different ways a numerical IPv4 address can be
  162. specified and libcurl accepts: one to four dot-separated fields using one of
  163. or a mix of decimal, octal or hexadecimal encoding.
  164. .SH "IPv6 Addresses"
  165. libcurl will normally handle IPv6 addresses transparently and just as easily
  166. as IPv4 addresses. That means that a sanitizing function that filters out
  167. addresses like 127.0.0.1 is not sufficient--the equivalent IPv6 addresses ::1,
  168. ::, 0:00::0:1, ::127.0.0.1 and ::ffff:7f00:1 supplied somehow by an attacker
  169. would all bypass a naive filter and could allow access to undesired local
  170. resources. IPv6 also has special address blocks like link-local and site-local
  171. that generally should not be accessed by a server-side libcurl-using
  172. application. A poorly configured firewall installed in a data center,
  173. organization or server may also be configured to limit IPv4 connections but
  174. leave IPv6 connections wide open. In some cases, setting
  175. \fICURLOPT_IPRESOLVE(3)\fP to CURL_IPRESOLVE_V4 can be used to limit resolved
  176. addresses to IPv4 only and bypass these issues.
  177. .SH Uploads
  178. When uploading, a redirect can cause a local (or remote) file to be
  179. overwritten. Applications must not allow any unsanitized URL to be passed in
  180. for uploads. Also, \fICURLOPT_FOLLOWLOCATION(3)\fP should not be used on
  181. uploads. Instead, the applications should consider handling redirects itself,
  182. sanitizing each URL first.
  183. .SH Authentication
  184. Use of \fICURLOPT_UNRESTRICTED_AUTH(3)\fP could cause authentication
  185. information to be sent to an unknown second server. Applications can mitigate
  186. against this by disabling \fICURLOPT_FOLLOWLOCATION(3)\fP and handling
  187. redirects itself, sanitizing where necessary.
  188. Use of the CURLAUTH_ANY option to \fICURLOPT_HTTPAUTH(3)\fP could result in
  189. user name and password being sent in clear text to an HTTP server. Instead,
  190. use CURLAUTH_ANYSAFE which ensures that the password is encrypted over the
  191. network, or else fail the request.
  192. Use of the CURLUSESSL_TRY option to \fICURLOPT_USE_SSL(3)\fP could result in
  193. user name and password being sent in clear text to an FTP server. Instead,
  194. use CURLUSESSL_CONTROL to ensure that an encrypted connection is used or else
  195. fail the request.
  196. .SH Cookies
  197. If cookies are enabled and cached, then a user could craft a URL which
  198. performs some malicious action to a site whose authentication is already
  199. stored in a cookie. e.g. http://mail.example.com/delete-stuff.cgi?delete=all
  200. Applications can mitigate against this by disabling cookies or clearing them
  201. between requests.
  202. .SH "Dangerous SCP URLs"
  203. SCP URLs can contain raw commands within the scp: URL, which is a side effect
  204. of how the SCP protocol is designed. e.g.
  205. scp://user:pass@host/a;date >/tmp/test;
  206. Applications must not allow unsanitized SCP: URLs to be passed in for
  207. downloads.
  208. .SH "file://"
  209. By default curl and libcurl support file:// URLs. Such a URL is always an
  210. access, or attempted access, to a local resource. If your application wants to
  211. avoid that, keep control of what URLs to use and/or prevent curl/libcurl from
  212. using the protocol.
  213. By default, libcurl prohibits redirects to file:// URLs.
  214. .SH "Warning: file:// on Windows"
  215. The Windows operating system will automatically, and without any way for
  216. applications to disable it, try to establish a connection to another host over
  217. the network and access it (over SMB or other protocols), if only the correct
  218. file path is accessed.
  219. When first realizing this, the curl team tried to filter out such attempts in
  220. order to protect applications for inadvertent probes of for example internal
  221. networks etc. This resulted in CVE-2019-15601 and the associated security fix.
  222. However, we have since been made aware of the fact that the previous fix was far
  223. from adequate as there are several other ways to accomplish more or less the
  224. same thing: accessing a remote host over the network instead of the local file
  225. system.
  226. The conclusion we have come to is that this is a weakness or feature in the
  227. Windows operating system itself, that we as an application cannot safely
  228. protect users against. It would just be a whack-a-mole race we do not want to
  229. participate in. There are too many ways to do it and there's no knob we can
  230. use to turn off the practice.
  231. If you use curl or libcurl on Windows (any version), disable the use of the
  232. FILE protocol in curl or be prepared that accesses to a range of "magic paths"
  233. will potentially make your system try to access other hosts on your
  234. network. curl cannot protect you against this.
  235. .SH "What if the user can set the URL"
  236. Applications may find it tempting to let users set the URL that it can work
  237. on. That is probably fine, but opens up for mischief and trickery that you as
  238. an application author may want to address or take precautions against.
  239. If your curl-using script allow a custom URL do you also, perhaps
  240. unintentionally, allow the user to pass other options to the curl command line
  241. if creative use of special characters are applied?
  242. If the user can set the URL, the user can also specify the scheme part to
  243. other protocols that you did not intend for users to use and perhaps did not
  244. consider. curl supports over 20 different URL schemes. "http://" might be what
  245. you thought, "ftp://" or "imap://" might be what the user gives your
  246. application. Also, cross-protocol operations might be done by using a
  247. particular scheme in the URL but point to a server doing a different protocol
  248. on a non-standard port.
  249. Remedies:
  250. .IP "Use --proto"
  251. curl command lines can use \fI--proto\fP to limit what URL schemes it accepts
  252. .IP "Use CURLOPT_PROTOCOLS"
  253. libcurl programs can use \fICURLOPT_PROTOCOLS(3)\fP to limit what URL schemes it accepts
  254. .IP "consider not allowing the user to set the full URL"
  255. Maybe just let the user provide data for parts of it? Or maybe filter input to
  256. only allow specific choices?
  257. .SH "RFC 3986 vs WHATWG URL"
  258. curl supports URLs mostly according to how they are defined in RFC 3986, and
  259. has done so since the beginning.
  260. Web browsers mostly adhere to the WHATWG URL Specification.
  261. This deviance makes some URLs copied between browsers (or returned over HTTP
  262. for redirection) and curl not work the same way. It can also cause problems if
  263. an application parses URLs differently from libcurl and makes different
  264. assumptions about a link. This can mislead users into getting the wrong thing,
  265. connecting to the wrong host or otherwise not working identically.
  266. Within an application, this can be mitigated by always using the
  267. \fIcurl_url(3)\fP API to parse URLs, ensuring that they are parsed the same way
  268. as within libcurl itself.
  269. .SH "FTP uses two connections"
  270. When performing an FTP transfer, two TCP connections are used: one for setting
  271. up the transfer and one for the actual data.
  272. FTP is not only un-authenticated, but the setting up of the second transfer is
  273. also a weak spot. The second connection to use for data, is either setup with
  274. the PORT/EPRT command that makes the server connect back to the client on the
  275. given IP+PORT, or with PASV/EPSV that makes the server setup a port to listen
  276. to and tells the client to connect to a given IP+PORT.
  277. Again, un-authenticated means that the connection might be meddled with by a
  278. man-in-the-middle or that there's a malicious server pretending to be the
  279. right one.
  280. A malicious FTP server can respond to PASV commands with the IP+PORT of a
  281. totally different machine. Perhaps even a third party host, and when there are
  282. many clients trying to connect to that third party, it could create a
  283. Distributed Denial-Of-Service attack out of it. If the client makes an upload
  284. operation, it can make the client send the data to another site. If the
  285. attacker can affect what data the client uploads, it can be made to work as a
  286. HTTP request and then the client could be made to issue HTTP requests to third
  287. party hosts.
  288. An attacker that manages to control curl's command line options can tell curl
  289. to send an FTP PORT command to ask the server to connect to a third party host
  290. instead of back to curl.
  291. The fact that FTP uses two connections makes it vulnerable in a way that is
  292. hard to avoid.
  293. .SH "Denial of Service"
  294. A malicious server could cause libcurl to effectively hang by sending data
  295. slowly, or even no data at all but just keeping the TCP connection open. This
  296. could effectively result in a denial-of-service attack. The
  297. \fICURLOPT_TIMEOUT(3)\fP and/or \fICURLOPT_LOW_SPEED_LIMIT(3)\fP options can
  298. be used to mitigate against this.
  299. A malicious server could cause libcurl to download an infinite amount of data,
  300. potentially causing all of memory or disk to be filled. Setting the
  301. \fICURLOPT_MAXFILESIZE_LARGE(3)\fP option is not sufficient to guard against
  302. this. Instead, applications should monitor the amount of data received within
  303. the write or progress callback and abort once the limit is reached.
  304. A malicious HTTP server could cause an infinite redirection loop, causing a
  305. denial-of-service. This can be mitigated by using the
  306. \fICURLOPT_MAXREDIRS(3)\fP option.
  307. .SH "Arbitrary Headers"
  308. User-supplied data must be sanitized when used in options like
  309. \fICURLOPT_USERAGENT(3)\fP, \fICURLOPT_HTTPHEADER(3)\fP,
  310. \fICURLOPT_POSTFIELDS(3)\fP and others that are used to generate structured
  311. data. Characters like embedded carriage returns or ampersands could allow the
  312. user to create additional headers or fields that could cause malicious
  313. transactions.
  314. .SH "Server-supplied Names"
  315. A server can supply data which the application may, in some cases, use as a
  316. file name. The curl command-line tool does this with
  317. \fI--remote-header-name\fP, using the Content-disposition: header to generate
  318. a file name. An application could also use \fICURLINFO_EFFECTIVE_URL(3)\fP to
  319. generate a file name from a server-supplied redirect URL. Special care must be
  320. taken to sanitize such names to avoid the possibility of a malicious server
  321. supplying one like "/etc/passwd", "\\autoexec.bat", "prn:" or even ".bashrc".
  322. .SH "Server Certificates"
  323. A secure application should never use the \fICURLOPT_SSL_VERIFYPEER(3)\fP
  324. option to disable certificate validation. There are numerous attacks that are
  325. enabled by applications that fail to properly validate server TLS/SSL
  326. certificates, thus enabling a malicious server to spoof a legitimate
  327. one. HTTPS without validated certificates is potentially as insecure as a
  328. plain HTTP connection.
  329. .SH "Showing What You Do"
  330. Relatedly, be aware that in situations when you have problems with libcurl and
  331. ask someone for help, everything you reveal in order to get best possible help
  332. might also impose certain security related risks. Host names, user names,
  333. paths, operating system specifics, etc. (not to mention passwords of course)
  334. may in fact be used by intruders to gain additional information of a potential
  335. target.
  336. Be sure to limit access to application logs if they could hold private or
  337. security-related data. Besides the obvious candidates like user names and
  338. passwords, things like URLs, cookies or even file names could also hold
  339. sensitive data.
  340. To avoid this problem, you must of course use your common sense. Often, you
  341. can just edit out the sensitive data or just search/replace your true
  342. information with faked data.
  343. .SH "Setuid applications using libcurl"
  344. libcurl-using applications that set the 'setuid' bit to run with elevated or
  345. modified rights also implicitly give that extra power to libcurl and this
  346. should only be done after careful considerations.
  347. Giving setuid powers to the application means that libcurl can save files using
  348. those new rights (if for example the `SSLKEYLOGFILE` environment variable is
  349. set). Also: if the application wants these powers to read or manage secrets
  350. that the user is otherwise not able to view (like credentials for a login
  351. etc), it should be noted that libcurl still might understand proxy environment
  352. variables that allow the user to redirect libcurl operations to use a proxy
  353. controlled by the user.
  354. .SH "File descriptors, fork and ntlm_wb"
  355. An application that uses libcurl and invokes `fork()` will get all file
  356. descriptors duplicated in the child process, including the ones libcurl
  357. created.
  358. libcurl itself uses `fork()` and `execl()` if told to use the
  359. `CURLAUTH_NTLM_WB` authentication method which then will invoke the helper
  360. command in a child process with file descriptors duplicated. Make sure that
  361. only the trusted and reliable helper program is invoked!
  362. .SH "Secrets in memory"
  363. When applications pass user names, passwords or other sensitive data to
  364. libcurl to be used for upcoming transfers, those secrets will be kept around
  365. as-is in memory. In many cases they will be stored in heap for as long as the
  366. handle itself for which the options are set.
  367. If an attacker can access the heap, like maybe by reading swap space or via a
  368. core dump file, such data might be accessible.
  369. Further, when eventually closing a handle and the secrets are no longer
  370. needed, libcurl does not explicitly clear memory before freeing it, so
  371. crendentials may be left in freed data.
  372. .SH "Report Security Problems"
  373. Should you detect or just suspect a security problem in libcurl or curl,
  374. contact the project curl security team immediately. See
  375. https://curl.se/dev/secprocess.html for details.