diff options
Diffstat (limited to 'SECURITY')
| -rw-r--r-- | SECURITY/DESIGN | 146 | ||||
| -rw-r--r-- | SECURITY/IMPLEMENTATION | 44 | ||||
| -rw-r--r-- | SECURITY/OVERVIEW | 12 | ||||
| -rw-r--r-- | SECURITY/TRUST | 118 |
4 files changed, 320 insertions, 0 deletions
diff --git a/SECURITY/DESIGN b/SECURITY/DESIGN new file mode 100644 index 0000000..19a3dc2 --- /dev/null +++ b/SECURITY/DESIGN @@ -0,0 +1,146 @@ +This document explains the design goals and decisions behind vsftpd. + +The importance of a secure design +================================= + +In a world full of good, careful coders who do not make mistakes, a secure +design would not be necessary. After all, in the absence of any programming +errors, security would not differ no matter how the program is arranged. + +Unfortunately, this is not an ideal world, and coders make plenty of mistakes. +Even the careful coders make mistakes. Code auditing is important, and goes +some way towards eliminating coding mistakes after the fact. However, we +have no guarantee that an audit will catch all the flaws. + +So, a secure design acknowledges the possibility of undiscovered flaws, and +takes steps to minimise the security impact these flaws can have. An obvious +example of something we want to do is to apply the principle of least +privilege, which ensure that every part of the program runs with the privilege +it needs and no more. + +An example of insecure design +============================= + +Examples of insecure design may be found in most other ftpd's. That's one of +the reasons vsftpd has been written. We'll pick on wu-ftpd as a specific +example, since it is rumoured to run about half of all ftp services. + +If I log on to wu-ftpd as an anonymous user, a process is run on my behalf to +serve my ftp session. Unfortunately, this process typically runs with full +root privileges on the remote machine. This means that any security flaw +present in parsing the copious ftp protocol will lead to full compromise of +that machine. Two concrete examples are the recent wu-ftpd format string bug +(June 1999), and a buffer overflow dealing with large paths a few months +beforehand. + +Even OpenBSD's ftpd-BSD had a format string bug leading to remote root +compromise of the affected machine, illustrating an earlier point about the +requirement for secure design even in the presence of heavy auditing. + +Secure design under UNIX +======================== + +vsftpd is written to run under UNIX-like operating systems, and so its secure +design is constrained by the facilities offered by UNIX. Ideally, UNIX would +have a proper security model which would offer fine grained access control +to all system interactions (files, network, etc). It doesn't, but it does +offer some useful and often overlooked facilities which help us to implement +the principle of least privilege: + +- Strong inter-process communication facilities + +In UNIX, the process is a strongly defined boundary. Different privilege +credentials may be assigned to different processes, which are not able to +interfere with each other. This is a very basic facility of UNIX. + +It makes sense to use this facility to totally separate parts of a program +which do not need to be privileged (most) from those parts that do (typically +minimal). + +The privileged and unprivileged parts of the program then communicate via +one of many UNIX IPC mechanisms - perhaps a socketpair or IPC (the former +is attractive because UNIX lets you pass file handles over a socket). + +The minimal privileged process exercises the "principle of distrust" - it +carefully filters what the unprivileged process asks it to do, so that even +if the unprivileged process is compromised, it cannot ask the privileged +process to do anything we don't want to allow. + +- chroot() + +chroot() is an often overlooked but useful tool. It can be used very +effectively as a damage limitation tool. + +Imagine a remotely compromised process which does not run as root, but also +does not use chroot(). Now look at what the attacker can do. Amongst the worst +items are pilfering of all publicly readable files, and also attempting to +execute any publicly executable suid-root programs to try and elevate +privilege. + +Now imagine the same compromised process with a chroot() to an empty directory. +The attackers options to do unpleasant things are substantially diminished. + +No, chroot() is not the ideal way to do what we have just accomplished, but +it is what we have got to work with. In an ideal environment with fine +grained security, we would default to having access to _no_ files at all, and +deliberately not ask for access to any. + +- Capabilities (Linux 2.2+) + +Like chroot(), capabilities are essentially a damage limitation excercise. +They are also much less widespread than the other UNIX facilities detailled +above. Nonetheless, they warrant mentioning because Linux has them, and they +are used in vsftpd because that is the primary devlopment platform. + +Capabilities split up the all powerful root privilege into lots of sometimes +orthogonal privileges. Some of the capabilities represent privileges which +are often the basis for requiring a program to run with full root privileges. +Examples include CAP_NET_RAW (ping, traceroute) and CAP_NET_BIND_SERVICE +(rlogin). + +By using capabilities to ensure we only have the privilege we need (within +the somewhat disappointing granularity they offer), we again limit the +potential damage of security holes. + +Presenting vsftpd's secure design +================================= + +vsftpd employs a secure design. The UNIX facilities outlined above are used +to good effect. The design decisions taken are as follows: + +1) All parsing and acting on potentially malicious remote network data is +done in a process running as an unprivileged user. Furthermore, this process +runs in a chroot() jail, ensuring only the ftp files area is accessible. + +2) Any privileged operations are handled in a privileged parent process. The +code for this privileged parent process is as small as possible for safety. + +3) This same privileged parent process receives requests from the unprivileged +child over a socket. All requests are distrusted. Here are example requests: +- Login request. The child sends username and password. Only if the details +are correct does the privileged parent launch a new child with the appropriate +user credentials. +- chown() request. The child may request a recently uploaded file gets +chown'ed() to root for security purposes. The parent is careful to only allow +chown() to root, and only from files owned by the ftp user. +- Get privileged socket request. The ftp protocol says we are supposed to +emit data connections from port 20. This requires privilege. The privileged +parent process creates the privileged socket and passes it to child over +the socket. + +4) This same privileged parent process makes use of capabilities and chroot(), +to run with the least privilege required. After login, depending on what +options have been selected, the privileged parent dynamically calculates what +privileges it requires. In some cases, this amounts to no privilege, and the +privileged parent just exits, leaving no part of vsftpd running with +privilege. + +5) vsftpd-2.0.0 introduces SSL / TLS support using OpenSSL. ALL SSL +protocol parsing is performed in a chroot() jail, running under an unprivileged +user. This means both pre-authenticated and post-authenticated SSL protocol +parsing; it's actually quite hard to do, but vsftpd manages it in the name of +being secure. I'm unaware of any other FTP server which supports both SSL / TLS +and privilege separation, and gets this right. + +Comments on this document are welcomed. + diff --git a/SECURITY/IMPLEMENTATION b/SECURITY/IMPLEMENTATION new file mode 100644 index 0000000..f4834d6 --- /dev/null +++ b/SECURITY/IMPLEMENTATION @@ -0,0 +1,44 @@ +This document details a few steps and decisions taken to ensure vsftpd is free +of common implementation flaws. + +Tackling the buffer overflow +============================ + +Probably the most common implementation flaw causing security problems is the +buffer overflow. Buffer overflows come in many shapes and sizes - overflows +onto the stack, overflows off the end of dynamically malloc()'ed areas, +overflows into static data areas. They range from easy to spot (where a user +can put an arbitrary length string into a fixed size buffer), to very +difficult to spot - buffer size miscalculations or single byte overflows. Or +convoluted code where the buffer's definition and various usages are far +apart. + +The problem is that people insist on replicating buffer size handling code +and buffer size security checks many times (or, of course, they omit size +checks altogther). It is little surprise, then, that sometimes errors creep +in to the checks. + +The correct solution is to hide the buffer handling code behind an API. All +buffer allocating, copying, size calculations, extending, etc. are done by +a single piece of generic code. The size security checks need to be written +once. You can concentrate on getting this one instance of code correct. + +From the client's point of view, they are no longer dealing with a buffer. The +buffer is encapsulated within the buffer API. All modifications to the buffer +safely go through the API. If this sounds familiar, it is because what vsftpd +implements is very similar to a C++ string class. You can do OO programming +in C too, you know ;-) + +A key point of having the buffer API in place is that it is MORE DIFFICULT to +abuse the API than it is to use it properly. Try and create a buffer memory +corruption or overflow scenario using just the buffer API. + + +Unfortunately, secure string/buffer usage through a common API has not caught +on much, despite the benefits it brings. Is it under publicised as a solution? +Or do people have too much sentimental attachment to strcpy(), strlen(), +malloc(), strcat() etc? Of notable exception, it is my understanding that at +least the rather secure qmail program uses secure buffer handling, and I'd +expect that to extend to all Dan Bernstein software. (Let me know of other good +examples). + diff --git a/SECURITY/OVERVIEW b/SECURITY/OVERVIEW new file mode 100644 index 0000000..6f50198 --- /dev/null +++ b/SECURITY/OVERVIEW @@ -0,0 +1,12 @@ +The documents in this directory contain information about the security of +vsftpd. They explain why various aspects of vsftpd were coded the way they +are. + +File Contents + +DESIGN Comments on the overall architecture of vsftpd, from a + security standpoint. +IMPLEMENTATION Comments on steps taken to ensure a secure implementation. +TRUST Comments on external components trusted or distrusted by + vsftpd. + diff --git a/SECURITY/TRUST b/SECURITY/TRUST new file mode 100644 index 0000000..a126965 --- /dev/null +++ b/SECURITY/TRUST @@ -0,0 +1,118 @@ +This document describes what the vsftpd code trusts, what it doesn't trust, and +the reasoning behind any trust decisions. + +The importance of trust and trust relationships +=============================================== + +Imagine a largely well written and secure piece of code. Now imagine that this +piece of code delegates a task to an external program, perhaps in the name of +code reuse. Now, if this external program is sloppily coded and insecure, we've +wasted a lot of effort making our original program secure; our erroneous trust +of the buggy external program means we have a security leak, even though we +were careful in _our_ code. + +There is a very similar situation with buggy library APIs. Imagine our secure +program calling some complex library function which lets the side down by +containing a security hole. + +Lets put some concrete examples on the two similar above considerations. We can +even give examples in the context of FTP daemons. + +1) External /bin/ls helper + +A very common operation asked of FTP servers is to provide a directory listing. +Unfortunately, convention seems to be to emit the directory listing in UNIX +"/bin/ls -l" format. Even the Microsoft FTP service can be observed to do this. +When writing an FTP server for the UNIX platform, then, this leads to the +temptation to reuse /bin/ls as a child process, to avoid having to rewrite a +load of code to handle directory listings. + +Even more unfortunately, FTP server writers seem to want to adopt the +versatility of the average /bin/ls implementation. This means they allow +clients to specify arbitrary parameters to /bin/ls. + +By using an external /bin/ls command, we would tie the security of our FTP +server to that of the /bin/ls code. Be careful not to underestimate the amount +of code paths in /bin/ls which are explorable by a remote malicious user. GNU +/bin/ls has a myriad of options. Some of these options are complex such as -I +or the various formatting options. All it takes is a single coding flaw in the +handling of one of these options, and your FTP security is in trouble. + +By using an external /bin/ls, you also inherit the risk of any dangerous or +complex APIs it uses. For example, calls to libc's complex fnmatch() or +glob() functions, which will get given arbitrary malicious user controlled +data as the search patterns. Also remember that users (and sometimes remote +users) can upload/create files, and filenames are a very prominent input +to /bin/ls. + +To conclude: vsftpd has no intention of using an external /bin/ls program +because of the risks outlined above. Even if I were to audit e.g. GNU +fileutils /bin/ls, and also important parts of glibc, this would still leave +security in an unknown state on other platforms. The solution I have employed +is to write a minimal internal implementation of a /bin/ls listing generator; +it's hardly difficult. As a happy side effect, this will boost performance by +avoiding unneccesary fork()s and exec()s! + +Here's some quick data about FTP servers which tend to use external ls +programs: + +ftp.wuftpd.org: +ftp> ls --version +227 Entering Passive Mode (x.x.x.x.x.x) +150 Opening ASCII mode data connection for /bin/ls. +ls (GNU fileutils) 3.16 +226 Transfer complete. + +ftp.digital.com: +ftp> ls -v +227 Entering Passive Mode (x.x.x.x.x.x) +150 Opening ASCII mode data connection for /bin/ls. +/bin/ls: illegal option -- v +usage: ls [ -1ACFLRabcdfgilmnopqrstux ] [files] +226 Transfer complete. + +Note that /bin/ls is not the only external program invoked by common FTP +servers such as wu-ftpd. wu-ftpd also has the ability to invoke "tar" and +"gzip" on the fly, so there are trust relationships there too. + + +2) Complex library APIs + +vsftpd is very careful to avoid using library calls which are potentially +dangerous. I would typically classify calls as dangerous if they interact +with the network non-trivially, or take malicious user supplied data and +start parsing it in a major way. + +Some examples are clearly required (vsftpd avoids using any of the following): + +1) fnmatch(). This is the libc glob pattern matcher. The danger comes +from the fact that the user supplies the glob pattern - "ls *.mp3" would +be a simple example. Furthermore, glob pattern matching is complex and +involves a lot of string handling. + +2) gethostbyaddr(). This is a libc call to resolve an IP address to a hostname. +Unfortunately, doing this is quite complicated. When you call gethostbyaddr(), +a lot of work goes on under the covers. This usually involves making a network +call out to the DNS server, and, dangerously, parsing the response. + +For clarity (and clarity is a very important part of security), all external +APIs used by vsftpd are encapsulated within two "system interaction" files, +named "sysutil.c", and "sysdeputil.c" (for the more variable/system dependent +calls). This provides a convenient audit point for ascertaining which calls +vsftpd trusts. + +vsftpd-2.0.0 introduces SSL / TLS support using OpenSSL. OpenSSL is a massive +quantity of code which is essentially parsing complex protocol under the full +control of remote malicious clients. SSL / TLS is disabled by default, both +at compile time and run time. This forces packagers and administrators to make +the decision that they trust the OpenSSL library. I personally haven't yet +formed an opinion on whether I consider the OpenSSL code trustworthy. + + +Summary +======= + +Be very aware of what APIs and/or programs you are trusting, or you might end +up creating a trust relationship which makes your program exploitable -- +through no direct fault of your own. + |
