Bug 268162 - ktorrent segfaults due to SHA1HashGen::update() always seeing a data length of 0x4000
Summary: ktorrent segfaults due to SHA1HashGen::update() always seeing a data length o...
Status: RESOLVED FIXED
Alias: None
Product: ktorrent
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Slackware Linux
: NOR crash
Target Milestone: ---
Assignee: Joris Guisson
URL:
Keywords:
: 270234 270513 271116 272937 273240 273789 274114 274211 274602 275380 275847 276297 277331 277490 278416 278888 278911 280281 280358 280472 280597 280660 280675 280684 280803 280900 282220 283406 288119 297647 297790 300906 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-03-10 21:33 UTC by Rick
Modified: 2012-05-31 11:54 UTC (History)
29 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
It's what I was going to cut-and-paste into your "Details" (6.92 KB, text/plain)
2011-03-10 21:33 UTC, Rick
Details
test code and logfile showing problems in ChunkDownload::updateHash() (2.59 KB, application/octet-stream)
2011-03-11 14:16 UTC, Rick
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rick 2011-03-10 21:33:02 UTC
Created attachment 57846 [details]
It's what I was going to cut-and-paste into your "Details"

Version:           unspecified
OS:                Linux

I'm running ktorrent-4.0.5.tar.xz and libktorrent-1.0.5-3.tar.xz on a small embedded processor (a SheevaPlug) using ARM Slackware (see ftp://ftp.armedslack.org/armedslack/armedslack-current).  It doesn't run more than a couple of hours without a segment fault.

  Program received signal SIGSEGV, Segmentation fault.
  0x41d0866c in memcpy () from /lib/libc.so.6
  (gdb) bt
  #0  0x41d0866c in memcpy () from /lib/libc.so.6
  #1  0x00000000 in ?? ()

libktorrent-1.0.5-3/src/download/chunkdownload.cpp::ChunkDownload::piece(const Piece & p,bool & ok) calls updateHash() with data length of 1..0x4000.  Most of the time it's 0x4000 but sometimes it's shorter (at the end of file?).

But by the time it gets to libktorrent-1.0.5-3/src/util/sha1hashgen.cpp::SHA1HashGen::update(const Uint8* data,Uint32 len) the data length is always 0x4000.  The call to hash->update(ba) eventually calls memcpy() and sometimes copying from the SRC address runs off the end of the data segment causing a fault.




Reproducible: Always
Comment 1 Rick 2011-03-11 14:16:28 UTC
Created attachment 57869 [details]
test code and logfile showing problems in ChunkDownload::updateHash()
Comment 2 Joris Guisson 2011-03-11 20:53:33 UTC
Interesting, this bug was fixed in libktorrent 1.1beta1, but I did never quite got to the bottom of this issue.

I think however your analysis is not correct, even though you have made me understand the problem completely.

The problem is that the Piece parameter of ChunkDownload::piece is incomplete. The Piece should be a piece of 16K long, but it is only 0xD bytes.  It should be 16K long because piece 0x79 is not the last piece (0x7f would be the last if num is 0x80).

This 0xD bytes long piece is then put into a PieceData object which is put in the piece_data array. We then call updateHash, which then wrongfully assumes that the PieceData object is the entire piece, but there is only 0xD bytes in it. 

And this then directly leads to the segmentation fault, when the hash is being calculated.

What I have fixed in 1.1 is the crash, but the initial problem is still there. A situation like this would lead to the calculated hash not matching the one specified in the torrent, and the chunk would have to be downloaded again.

Either ktorrent is requesting a piece with the wrong size, or another peer is sending back a piece of the wrong length in reply to a request of ktorrent.

Anyway a big thanks for looking into this, now I finally understand what is going wrong.
Comment 3 Joris Guisson 2011-03-13 11:08:35 UTC
Git commit b3cf06c2eb97303c746aed0759f930691002cee0 by Joris.
Committed on 13/03/2011 at 11:07.
Pushed by guisson into branch 'master'.

Fix root cause of SHA1HashGen crashes in 1.0 series

BUG: 268162

M  +1    -0    ChangeLog     
M  +2    -1    src/download/chunkdownload.cpp     

http://commits.kde.org/libktorrent/b3cf06c2eb97303c746aed0759f930691002cee0
Comment 4 Joris Guisson 2011-03-13 11:09:53 UTC
Git commit 904864fe28fa4ad859d527f1b672444a2bb93fa4 by Joris.
Committed on 13/03/2011 at 11:07.
Pushed by guisson into branch '1.1'.

Fix root cause of SHA1HashGen crashes in 1.0 series

CCBUG: 268162

M  +1    -0    ChangeLog     
M  +2    -1    src/download/chunkdownload.cpp     

http://commits.kde.org/libktorrent/904864fe28fa4ad859d527f1b672444a2bb93fa4
Comment 5 Joris Guisson 2011-04-04 18:57:17 UTC
*** Bug 270081 has been marked as a duplicate of this bug. ***
Comment 6 Joris Guisson 2011-04-07 18:18:09 UTC
*** Bug 270234 has been marked as a duplicate of this bug. ***
Comment 7 Joris Guisson 2011-04-10 10:27:51 UTC
*** Bug 270513 has been marked as a duplicate of this bug. ***
Comment 8 Joris Guisson 2011-04-16 20:01:54 UTC
*** Bug 271116 has been marked as a duplicate of this bug. ***
Comment 9 Joris Guisson 2011-05-11 18:15:17 UTC
*** Bug 272937 has been marked as a duplicate of this bug. ***
Comment 10 Joris Guisson 2011-05-14 13:09:36 UTC
*** Bug 273240 has been marked as a duplicate of this bug. ***
Comment 11 Joris Guisson 2011-05-23 17:54:00 UTC
*** Bug 273789 has been marked as a duplicate of this bug. ***
Comment 12 Joris Guisson 2011-05-25 20:49:45 UTC
*** Bug 274114 has been marked as a duplicate of this bug. ***
Comment 13 Joris Guisson 2011-05-27 18:05:02 UTC
*** Bug 274211 has been marked as a duplicate of this bug. ***
Comment 14 Joris Guisson 2011-05-31 20:01:10 UTC
*** Bug 274602 has been marked as a duplicate of this bug. ***
Comment 15 Joris Guisson 2011-06-11 11:57:18 UTC
*** Bug 275380 has been marked as a duplicate of this bug. ***
Comment 16 Joris Guisson 2011-06-17 19:16:01 UTC
*** Bug 275847 has been marked as a duplicate of this bug. ***
Comment 17 Joris Guisson 2011-06-23 18:08:44 UTC
*** Bug 276297 has been marked as a duplicate of this bug. ***
Comment 18 Joris Guisson 2011-07-10 19:30:09 UTC
*** Bug 277490 has been marked as a duplicate of this bug. ***
Comment 19 Joris Guisson 2011-07-11 08:19:10 UTC
*** Bug 277331 has been marked as a duplicate of this bug. ***
Comment 20 Joris Guisson 2011-07-26 10:39:59 UTC
*** Bug 278416 has been marked as a duplicate of this bug. ***
Comment 21 Christoph Feck 2011-08-03 23:07:46 UTC
*** Bug 278911 has been marked as a duplicate of this bug. ***
Comment 22 Christoph Feck 2011-08-03 23:08:35 UTC
*** Bug 278888 has been marked as a duplicate of this bug. ***
Comment 23 Joris Guisson 2011-08-17 16:31:08 UTC
*** Bug 280281 has been marked as a duplicate of this bug. ***
Comment 24 Joris Guisson 2011-08-19 18:58:57 UTC
*** Bug 280358 has been marked as a duplicate of this bug. ***
Comment 25 Joris Guisson 2011-08-20 16:39:27 UTC
*** Bug 280472 has been marked as a duplicate of this bug. ***
Comment 26 Joris Guisson 2011-08-24 16:29:02 UTC
*** Bug 280684 has been marked as a duplicate of this bug. ***
Comment 27 Joris Guisson 2011-08-24 16:29:40 UTC
*** Bug 280675 has been marked as a duplicate of this bug. ***
Comment 28 Joris Guisson 2011-08-24 16:30:08 UTC
*** Bug 280660 has been marked as a duplicate of this bug. ***
Comment 29 Joris Guisson 2011-08-24 16:39:45 UTC
*** Bug 280597 has been marked as a duplicate of this bug. ***
Comment 30 Joris Guisson 2011-08-28 19:38:04 UTC
*** Bug 280900 has been marked as a duplicate of this bug. ***
Comment 31 Joris Guisson 2011-08-28 19:39:44 UTC
*** Bug 280803 has been marked as a duplicate of this bug. ***
Comment 32 Joris Guisson 2011-09-17 14:41:04 UTC
*** Bug 282220 has been marked as a duplicate of this bug. ***
Comment 33 Joris Guisson 2011-10-06 16:22:39 UTC
*** Bug 283406 has been marked as a duplicate of this bug. ***
Comment 34 Joris Guisson 2011-12-03 15:52:34 UTC
*** Bug 288119 has been marked as a duplicate of this bug. ***
Comment 35 Joris Guisson 2012-04-07 15:52:31 UTC
*** Bug 297647 has been marked as a duplicate of this bug. ***
Comment 36 Joris Guisson 2012-04-09 18:27:16 UTC
*** Bug 297790 has been marked as a duplicate of this bug. ***
Comment 37 Joris Guisson 2012-05-31 11:54:51 UTC
*** Bug 300906 has been marked as a duplicate of this bug. ***