libcurl-security.3 22 KB

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