DESIGN 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. This document explains the design goals and decisions behind vsftpd.
  2. The importance of a secure design
  3. =================================
  4. In a world full of good, careful coders who do not make mistakes, a secure
  5. design would not be necessary. After all, in the absence of any programming
  6. errors, security would not differ no matter how the program is arranged.
  7. Unfortunately, this is not an ideal world, and coders make plenty of mistakes.
  8. Even the careful coders make mistakes. Code auditing is important, and goes
  9. some way towards eliminating coding mistakes after the fact. However, we
  10. have no guarantee that an audit will catch all the flaws.
  11. So, a secure design acknowledges the possibility of undiscovered flaws, and
  12. takes steps to minimise the security impact these flaws can have. An obvious
  13. example of something we want to do is to apply the principle of least
  14. privilege, which ensure that every part of the program runs with the privilege
  15. it needs and no more.
  16. An example of insecure design
  17. =============================
  18. Examples of insecure design may be found in most other ftpd's. That's one of
  19. the reasons vsftpd has been written. We'll pick on wu-ftpd as a specific
  20. example, since it is rumoured to run about half of all ftp services.
  21. If I log on to wu-ftpd as an anonymous user, a process is run on my behalf to
  22. serve my ftp session. Unfortunately, this process typically runs with full
  23. root privileges on the remote machine. This means that any security flaw
  24. present in parsing the copious ftp protocol will lead to full compromise of
  25. that machine. Two concrete examples are the recent wu-ftpd format string bug
  26. (June 1999), and a buffer overflow dealing with large paths a few months
  27. beforehand.
  28. Even OpenBSD's ftpd-BSD had a format string bug leading to remote root
  29. compromise of the affected machine, illustrating an earlier point about the
  30. requirement for secure design even in the presence of heavy auditing.
  31. Secure design under UNIX
  32. ========================
  33. vsftpd is written to run under UNIX-like operating systems, and so its secure
  34. design is constrained by the facilities offered by UNIX. Ideally, UNIX would
  35. have a proper security model which would offer fine grained access control
  36. to all system interactions (files, network, etc). It doesn't, but it does
  37. offer some useful and often overlooked facilities which help us to implement
  38. the principle of least privilege:
  39. - Strong inter-process communication facilities
  40. In UNIX, the process is a strongly defined boundary. Different privilege
  41. credentials may be assigned to different processes, which are not able to
  42. interfere with each other. This is a very basic facility of UNIX.
  43. It makes sense to use this facility to totally separate parts of a program
  44. which do not need to be privileged (most) from those parts that do (typically
  45. minimal).
  46. The privileged and unprivileged parts of the program then communicate via
  47. one of many UNIX IPC mechanisms - perhaps a socketpair or IPC (the former
  48. is attractive because UNIX lets you pass file handles over a socket).
  49. The minimal privileged process exercises the "principle of distrust" - it
  50. carefully filters what the unprivileged process asks it to do, so that even
  51. if the unprivileged process is compromised, it cannot ask the privileged
  52. process to do anything we don't want to allow.
  53. - chroot()
  54. chroot() is an often overlooked but useful tool. It can be used very
  55. effectively as a damage limitation tool.
  56. Imagine a remotely compromised process which does not run as root, but also
  57. does not use chroot(). Now look at what the attacker can do. Amongst the worst
  58. items are pilfering of all publicly readable files, and also attempting to
  59. execute any publicly executable suid-root programs to try and elevate
  60. privilege.
  61. Now imagine the same compromised process with a chroot() to an empty directory.
  62. The attackers options to do unpleasant things are substantially diminished.
  63. No, chroot() is not the ideal way to do what we have just accomplished, but
  64. it is what we have got to work with. In an ideal environment with fine
  65. grained security, we would default to having access to _no_ files at all, and
  66. deliberately not ask for access to any.
  67. - Capabilities (Linux 2.2+)
  68. Like chroot(), capabilities are essentially a damage limitation excercise.
  69. They are also much less widespread than the other UNIX facilities detailled
  70. above. Nonetheless, they warrant mentioning because Linux has them, and they
  71. are used in vsftpd because that is the primary devlopment platform.
  72. Capabilities split up the all powerful root privilege into lots of sometimes
  73. orthogonal privileges. Some of the capabilities represent privileges which
  74. are often the basis for requiring a program to run with full root privileges.
  75. Examples include CAP_NET_RAW (ping, traceroute) and CAP_NET_BIND_SERVICE
  76. (rlogin).
  77. By using capabilities to ensure we only have the privilege we need (within
  78. the somewhat disappointing granularity they offer), we again limit the
  79. potential damage of security holes.
  80. Presenting vsftpd's secure design
  81. =================================
  82. vsftpd employs a secure design. The UNIX facilities outlined above are used
  83. to good effect. The design decisions taken are as follows:
  84. 1) All parsing and acting on potentially malicious remote network data is
  85. done in a process running as an unprivileged user. Furthermore, this process
  86. runs in a chroot() jail, ensuring only the ftp files area is accessible.
  87. 2) Any privileged operations are handled in a privileged parent process. The
  88. code for this privileged parent process is as small as possible for safety.
  89. 3) This same privileged parent process receives requests from the unprivileged
  90. child over a socket. All requests are distrusted. Here are example requests:
  91. - Login request. The child sends username and password. Only if the details
  92. are correct does the privileged parent launch a new child with the appropriate
  93. user credentials.
  94. - chown() request. The child may request a recently uploaded file gets
  95. chown'ed() to root for security purposes. The parent is careful to only allow
  96. chown() to root, and only from files owned by the ftp user.
  97. - Get privileged socket request. The ftp protocol says we are supposed to
  98. emit data connections from port 20. This requires privilege. The privileged
  99. parent process creates the privileged socket and passes it to child over
  100. the socket.
  101. 4) This same privileged parent process makes use of capabilities and chroot(),
  102. to run with the least privilege required. After login, depending on what
  103. options have been selected, the privileged parent dynamically calculates what
  104. privileges it requires. In some cases, this amounts to no privilege, and the
  105. privileged parent just exits, leaving no part of vsftpd running with
  106. privilege.
  107. 5) vsftpd-2.0.0 introduces SSL / TLS support using OpenSSL. ALL SSL
  108. protocol parsing is performed in a chroot() jail, running under an unprivileged
  109. user. This means both pre-authenticated and post-authenticated SSL protocol
  110. parsing; it's actually quite hard to do, but vsftpd manages it in the name of
  111. being secure. I'm unaware of any other FTP server which supports both SSL / TLS
  112. and privilege separation, and gets this right.
  113. Comments on this document are welcomed.