python / cpython

The Python programming language
https://www.python.org
Other
63.13k stars 30.22k forks source link

ftplib: unlimited readline() from connection #60242

Closed tiran closed 10 years ago

tiran commented 12 years ago
BPO 16038
Nosy @warsaw, @akuchling, @birkenfeld, @josiahcarlson, @pitrou, @larryhastings, @giampaolo, @tiran, @benjaminp, @serhiy-storchaka
Files
  • ftplib_maxline.patch
  • ftplib_maxline.patch
  • ftplib_maxline.patch
  • ftplib_maxline.patch
  • ftplib.patch
  • ftplib_maxline.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = 'https://github.com/tiran' closed_at = created_at = labels = ['release-blocker', 'library', 'performance'] title = 'ftplib: unlimited readline() from connection' updated_at = user = 'https://github.com/tiran' ``` bugs.python.org fields: ```python activity = actor = 'Jeff Dafoe' assignee = 'christian.heimes' closed = True closed_date = closer = 'georg.brandl' components = ['Library (Lib)'] creation = creator = 'christian.heimes' dependencies = [] files = ['28970', '29019', '29090', '31589', '31777', '31862'] hgrepos = [] issue_num = 16038 keywords = ['patch'] message_count = 52.0 messages = ['171241', '181479', '181546', '181549', '181553', '181559', '181571', '181580', '181635', '181638', '181644', '181745', '181771', '181772', '181773', '182195', '182235', '182236', '185060', '194170', '194551', '194921', '194935', '196861', '196927', '197791', '197817', '197827', '197921', '197957', '197959', '198298', '198323', '198325', '198327', '198344', '198356', '198357', '198368', '198371', '198380', '198388', '200348', '200386', '200387', '200588', '200592', '200602', '214908', '226309', '227889', '323483'] nosy_count = 17.0 nosy_names = ['barry', 'akuchling', 'georg.brandl', 'josiahcarlson', 'pitrou', 'larry', 'giampaolo.rodola', 'christian.heimes', 'benjamin.peterson', 'stutzbach', 'Arfrever', 'neologix', 'python-dev', 'serhiy.storchaka', 'raduv', 'inc0', 'Jeff Dafoe'] pr_nums = [] priority = 'release blocker' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'resource usage' url = 'https://bugs.python.org/issue16038' versions = ['Python 3.2'] ```

    tiran commented 12 years ago

    This bug is similar to bpo-16037.

    The ftplib module doesn't limit the amount of read data in its call to readline(). An erroneous or malicious FTP server can trick the ftplib module to consume large amounts of memory.

    Suggestion: The ftplib module should be modified to use limited readline() with _MAXLINE like the httplib module.

    9899aaec-f1d5-4aa7-b6ea-dfacdb57cac1 commented 11 years ago

    Hello,

    I've made patch which address this issue.

    tiran commented 11 years ago

    Thank you very much!

    Have you read and checked what the RFCs about the FTP protocol say about maximum line length?

    9899aaec-f1d5-4aa7-b6ea-dfacdb57cac1 commented 11 years ago

    Well its my understanding, that there is no maximum length specified in RFC959. There is however option to set it up while telnet connection, and that would be other solution to this issue.

    pitrou commented 11 years ago

    Michał, thanks for the patch. Could you sign and e-mail a contributor's agreement? http://www.python.org/psf/contrib/

    As for the patch:

    79528080-9d85-4d18-8a2a-8b1f07640dd7 commented 11 years ago

    Have you read and checked what the RFCs about the FTP protocol say about maximum line length?

    vsftpd seems to use a 4096 limit (and the guy knows his stuff :-): ftp://vsftpd.beasts.org/users/cevans/untar/vsftpd-3.0.2/defs.h

    9899aaec-f1d5-4aa7-b6ea-dfacdb57cac1 commented 11 years ago

    Well, that is not from RFC (or I hadn't find it):) however I'd lie if I'd call myself an expert, should I change limit to 4096 then?

    79528080-9d85-4d18-8a2a-8b1f07640dd7 commented 11 years ago

    Well, that is not from RFC (or I hadn't find it):) however I'd lie if I'd call myself an expert, should I change limit to 4096 then?

    It's probably not in the RFC: this just shows that the limit chosen should be enough.

    giampaolo commented 11 years ago

    LineTooLong should be added to ftplib.all_errors. 4096 looks enough to me. The longest lines I can think of occur when processing MLSD command which produces an output of like this:

    type=file;size=156;perm=r;modify=20071029155301;unique=801cd2; music.mp3
    type=dir;size=0;perm=el;modify=20071127230206;unique=801e33; ebooks
    type=file;size=211;perm=r;modify=20071103093626;unique=801e32; module.py

    Considering that the file names listed in there are forced to consist of base names (as opposed to *full* path names) I doubt we'll ever hit 4096. In pyftpdlib I used 2048 bytes. I can't recall any reference about this in any FTP-related RFC.

    tiran commented 11 years ago

    I suggest that we use twice the size of the largest limit (8192) for the DoS fix and reduce it to 2048 for Python 3.4. 8192 is still a small number for modern computers.

    I also like to see comments next to the limit that explain on what grounds we have chosen the value. For example

    # vfstpd has a limit of 4096 (ftp://vsftpd.beasts.org/users/cevans/untar/vsftpd-3.0.2/defs.h) # pyftpdlib has a limit of 2048

    pitrou commented 11 years ago

    I suggest that we use twice the size of the largest limit (8192) for the DoS fix and reduce it to 2048 for Python 3.4. 8192 is still a small number for modern computers.

    Why do you want to reduce it? It doesn't bring any additional security.

    9899aaec-f1d5-4aa7-b6ea-dfacdb57cac1 commented 11 years ago

    Hello,

    I've set up maxline limit to 8192. Also I've add some changes Antoine suggested earlier.

    pitrou commented 11 years ago

    Not sure how I nosied Larry by updating this issue, sorry for the mistake.

    pitrou commented 11 years ago

    Ah, but that's because I added 3.4 in the versions field and the issue is a release blocker :)

    larryhastings commented 11 years ago

    My spies are everywhere! You cannot hide your black heart, Pitrou.

    tiran commented 11 years ago

    CVE-2013-1752 Unbound readline() DoS vulnerabilities in Python stdlib

    giampaolo commented 11 years ago

    Patch looks ok. Just add the new exception class to all_errors list.

    9899aaec-f1d5-4aa7-b6ea-dfacdb57cac1 commented 11 years ago

    Thank you Giampaolo,

    I'm attaching patch changed according to your suggestion.

    benjaminp commented 11 years ago

    Not blocking 2.7.4 as discussed on mailing list.

    79528080-9d85-4d18-8a2a-8b1f07640dd7 commented 11 years ago

    So, what now?

    tiran commented 11 years ago

    The patches are languishing in the bug tracker for a while...

    Benjamin: I like to apply them to 3.3 and default before the next release of 3.3. Do you want to have the fixes in 2.7, too?

    benjaminp commented 11 years ago

    I suppose this is fine for 2.7

    serhiy-storchaka commented 11 years ago

    Error message "got more than %d bytes" is misleading because in most cases (except storlines()) we read not bytes but a text string.

    There are 4 changes in the ftplib module but only one of them covered by test.

    warsaw commented 11 years ago

    blocker for 2.6.9

    giampaolo commented 11 years ago

    I'm attaching a slightly different patch including new tests and which uses a 'maxline' class attribute (as opposed to a global var). Christian if that's OK with you I will wait a while and then make a commit for all Python versions.

    akuchling commented 11 years ago

    For 2.6 I'll make a revised version of Giampaolo's patch that doesn't add a new exception class.

    Rationale: Adding a new exception class changes the API of the module, which we'd like to avoid. If someone is writing 2.6 code that wants to catch this exception, they can't write "except ftplib.LineTooLong" because the name isn't present. Instead they'll have to catch the parent Error exception class and analyze either its type or the exception message. My conclusion is that adding the new class isn't actually useful.

    (bwarsaw and I are at a mini-sprint looking at the 2.6.9 blockers, so we're looking at all of these 'unlimited readline' issues and will continue to remove new exceptions introduced by patches.)

    akuchling commented 11 years ago

    2.6 version of the patch. Changes from Giampaolo's version of the patch:

    warsaw commented 11 years ago

    \====================================================================== FAIL: test_retrlines_too_long (main.TestFTPClass) ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "Lib/test/test_ftplib.py", line 374, in test_retrlines_too_long
        self.client.retrlines, 'retr', received.append)
    AssertionError: Error not raised
    giampaolo commented 11 years ago

    Looks legitimate to me. I will come up with a separate patch for later Python versions.

    warsaw commented 11 years ago

    Yep, confirmed that ftplib.patch causes test_ftplib to fail, at least on Ubuntu 10.04 chroot.

    warsaw commented 11 years ago

    Succeeds on OS X 10.8 (although there are other failures)

    warsaw commented 11 years ago

    Okay, this one is quite odd. It's definitely a timing issue.

    If I put a import time; time.sleep(1) at the beginning of test_retrlines_too_line() -- i.e. first line of the method -- then the test reliably passes. If I put a print(len(line)) just before the maxline test in FTP.retrlines(), then the test will pass just as reliably.

    If I put that retrlines() print after the maxline test, then it passes sometimes and fails sometimes. When if fails, it's only ready 12 bytes from the fp.readline() call. When it passes, it's reading 8193 bytes (thus triggering the expected exception).

    I really hate to put a sleep in the test to make it pass. Obviously it would be better not to fudge this race condition, but I don't know the code well enough to know where the race is yet.

    serhiy-storchaka commented 11 years ago

    What about time.sleep(0.1)?

    warsaw commented 11 years ago

    On Sep 23, 2013, at 03:36 PM, Serhiy Storchaka wrote:

    What about time.sleep(0.1)?

    I usually don't like introducing sleeps to fix race conditions, but if that's the only option for landing this patch, maybe we'll have to hold our noses and do it.

    giampaolo commented 11 years ago

    Barry can you paste the traceback caused by the race condition? What's not clear to me is when (what line) it occurs. One solution might be to send a "NOOP" command (self.client.sendcmd('noop')) in order to synchronize client and server.

    warsaw commented 11 years ago

    On Sep 23, 2013, at 06:33 PM, Giampaolo Rodola' wrote:

    Barry can you paste the traceback caused by the race condition? What's not clear to me is when (what line) it occurs. One solution might be to send a "NOOP" command (self.client.sendcmd('noop')) in order to synchronize client and server.

    There's no traceback other than the test failure that I posted. It's a race condition because with a little sleep, the test passes. Without it, it fails.

    This is on various flavors of Ubuntu (only up to 10.04 which is the last version I can build a full 2.6 against) and Debian.

    giampaolo commented 11 years ago

    I believe the problem is the set of next_retr_data attribute here:

        def test_retrlines_too_long(self):
            self.server.handler.next_retr_data = 'x' * self.client.maxline * 2

    ...because self.server.handler runs in a different thread (different than the main one, which is where the setattr() occurs). We should introduce a new command in the dummy FTP server which sets next_retr_data from within the server thread itself. Will try to work on a patch later this week (I'm sorry but I can't make it earlier).

    warsaw commented 11 years ago

    On Sep 24, 2013, at 01:12 PM, Giampaolo Rodola' wrote:

    Giampaolo Rodola' added the comment:

    I believe the problem is the set of next_retr_data attribute here:

    def test_retrlines_too_long(self): self.server.handler.next_retr_data = 'x' self.client.maxline 2

    ...because self.server.handler runs in a different thread (different than the main one, which is where the setattr() occurs). We should introduce a new command in the dummy FTP server which sets next_retr_data from within the server thread itself. Will try to work on a patch later this week (I'm sorry but I can't make it earlier).

    +1 - that explanation makes a lot of sense, thanks!

    Currently 2.6.9rc1 is planned for Monday 30-September. It would be nice to get this one in before then, but if not that's okay. I think it's fairly low risk.

    serhiy-storchaka commented 11 years ago

    Here is a patch.

    warsaw commented 11 years ago

    On Sep 24, 2013, at 09:59 PM, Serhiy Storchaka wrote:

    Added file: http://bugs.python.org/file31862/ftplib_maxline.patch

    This looks great and fixes the test failure problem. Thanks! Serhiy, please feel free to apply this to the 2.6 branch, or let me know if you'd rather I apply it.

    serhiy-storchaka commented 11 years ago

    Please apply it yourself.

    1762cc99-3127-4a62-9baf-30c3d0f51ef7 commented 11 years ago

    New changeset 8b19e7d0be45 by Barry Warsaw in branch '2.6':

    larryhastings commented 11 years ago

    Ping. Please fix before "beta 1".

    giampaolo commented 11 years ago

    I think this is already fixed. Barry can we close this out?

    80036ac5-bb84-4d39-8416-02cd8e51707d commented 11 years ago

    It is fixed in Python 2.6, but not 2.7, 3.1, 3.2, 3.3, 3.4.

    1762cc99-3127-4a62-9baf-30c3d0f51ef7 commented 11 years ago

    New changeset 44ac81e6d584 by Serhiy Storchaka in branch '2.7': Issue bpo-16038: CVE-2013-1752: ftplib: Limit amount of data read by http://hg.python.org/cpython/rev/44ac81e6d584

    New changeset 38db4d0726bd by Serhiy Storchaka in branch '3.3': Issue bpo-16038: CVE-2013-1752: ftplib: Limit amount of data read by http://hg.python.org/cpython/rev/38db4d0726bd

    New changeset 0c48fe975c54 by Serhiy Storchaka in branch 'default': Issue bpo-16038: CVE-2013-1752: ftplib: Limit amount of data read by http://hg.python.org/cpython/rev/0c48fe975c54

    80036ac5-bb84-4d39-8416-02cd8e51707d commented 11 years ago

    (3.1 branch is open to security fixes.)

    giampaolo commented 11 years ago

    You are right. I will try to provide patches for other Python versions later next week.

    On Sun, Oct 20, 2013 at 5:08 PM, Arfrever Frehtes Taifersar Arahesis \< report@bugs.python.org> wrote:

    Arfrever Frehtes Taifersar Arahesis added the comment:

    (3.1 branch is open to security fixes.)

    ---------- versions: +Python 3.1


    Python tracker \report@bugs.python.org\ \http://bugs.python.org/issue16038\


    akuchling commented 10 years ago

    Are we likely to actually apply this change to the 3.1 and 3.2 branches, given that even the later 3.3 branch is now in security-fix mode? If we're not going to change 3.1 or 3.2, this issue can just be closed.

    124dcdec-de91-42cb-9582-49f08cd6687d commented 10 years ago

    I'm a little confused about this patch. Please correct me if I'm wrong, but fp.readline([size + 1]) should return a line of length at most size + 1. This means that the check len(line) > size will always be true when reading a line that has a length greater than self.maxline. Also, wouldn't it make more sense to have the line that logs stuff in debugging mode be before raising a LineTooLong exception ? This way you have the option of actually seeing the line.