Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in tcp server by wiping priority queues on deletion. #1216

Merged
merged 1 commit into from
Oct 7, 2018

Conversation

zugz
Copy link

@zugz zugz commented Sep 29, 2018

This change is Reviewable

@@ -222,6 +222,23 @@ static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, cons
return index;
}

static void wipe_priority_list(TCP_Secure_Connection *con)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll need to be called something else. https://travis-ci.org/TokTok/c-toxcore/jobs/434939506#L1504

@zugz
Copy link
Author

zugz commented Sep 29, 2018

It turns out that this closes #1214, as this was the memleak exploited there.
@kpp found the flaw first, and chose to communicate it to us non-verbally in this way.
@kpp and I just tested that this patch does nullify that attack.

Copy link
Member

@robinlinden robinlinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r2.
Reviewable status: 1 change requests, 0 of 1 approvals obtained (waiting on @iphydf)

@kpp
Copy link

kpp commented Sep 29, 2018

https://github.com/tox-rs/tox is your best friend =) The second 0day this year.

@zugz zugz changed the title Fix memleak in tcp server by wiping priority queues on deletion WIP: Fix memleak in tcp server by wiping priority queues on deletion Sep 29, 2018
@zugz
Copy link
Author

zugz commented Sep 29, 2018

This doesn't actually fix all the potential memleaks yet. e.g. in kill_TCP_server the TCP_Secure_Connections aren't wiped, so there could be unfreed priority queues. Similarly in realloc_connection. I'm also worried about the potential for double frees. This is going to take some care... is there anyone who's actually familiar with TCP_server.c and wouldn't mind helping out?

@nurupo
Copy link
Member

nurupo commented Sep 29, 2018

@zugz what do you mean by "non-verbally"? How exactly did he communicate? Was it by taking down 3/4 of bootstrap nodes? Because if so, then that's a rather malicious way of communicating and tox-rs ain't no friend.

@zugz
Copy link
Author

zugz commented Sep 29, 2018 via email

@codecov
Copy link

codecov bot commented Sep 30, 2018

Codecov Report

Merging #1216 into master will decrease coverage by 0.1%.
The diff coverage is 85%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #1216     +/-   ##
========================================
- Coverage    82.8%   82.7%   -0.2%     
========================================
  Files          82      82             
  Lines       14634   14647     +13     
========================================
- Hits        12125   12120      -5     
- Misses       2509    2527     +18
Impacted Files Coverage Δ
toxcore/TCP_client.c 64.6% <100%> (+0.3%) ⬆️
toxcore/TCP_server.c 75.9% <84.6%> (+0.9%) ⬆️
auto_tests/toxav_many_test.c 92.5% <0%> (-4.1%) ⬇️
toxcore/net_crypto.c 93.5% <0%> (-1.4%) ⬇️
toxav/msi.c 63.6% <0%> (-1.2%) ⬇️
toxav/toxav.c 67.1% <0%> (-1%) ⬇️
toxcore/Messenger.c 86.6% <0%> (+0.1%) ⬆️
toxcore/friend_connection.c 94.5% <0%> (+0.8%) ⬆️
auto_tests/toxav_basic_test.c 83.5% <0%> (+1.9%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b2b96b1...5beb00c. Read the comment docs.

@zugz zugz changed the title WIP: Fix memleak in tcp server by wiping priority queues on deletion Fix memleak in tcp server by wiping priority queues on deletion Sep 30, 2018
@zugz
Copy link
Author

zugz commented Sep 30, 2018

OK, this is ready for (re)review now, and I believe it now fully seals this source of memleaks rather than just dealing with the case exploited in #1214. There wasn't nearly as much further work as I was fearing in my previous comment.

A word on my methodology here: I haven't actually grokked the entirety of TCP_server.c; rather, I searched for "TCP_Secure_Connection" and followed up on each hit. I don't see that I could be missing anything. Also, with these latest changes asan now passes on #1215 - and asan's previous failure there was what directly led me to finding the memleak, which incidentally I think we can read as a win for our testing regime!

Anyway, please review carefully.

Copy link
Member

@robinlinden robinlinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r4.
Reviewable status: :shipit: complete! 1 change requests, 1 of 1 approvals obtained (waiting on @iphydf)


toxcore/TCP_server.c, line 134 at r4 (raw file):

static int alloc_new_connections(TCP_Server *tcp_server, uint32_t num)
{
    uint32_t new_size = tcp_server->size_accepted_connections + num;

const

irungentoo added a commit to irungentoo/toxcore that referenced this pull request Oct 3, 2018
@irungentoo
Copy link

This is how I fixed it: irungentoo@bf69b54

Copy link

@sudden6 sudden6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm_strong: from my side

Reviewed 2 of 3 files at r2, 1 of 1 files at r5.
Reviewable status: :shipit: complete! 1 change requests, 1 of 1 approvals obtained (waiting on @iphydf)

@iphydf iphydf merged commit 5beb00c into TokTok:master Oct 7, 2018
@robinlinden robinlinden modified the milestones: v0.2.x, v0.2.8 Oct 7, 2018
@iphydf iphydf changed the title Fix memleak in tcp server by wiping priority queues on deletion Fix memory leak in tcp server by wiping priority queues on deletion Oct 7, 2018
@iphydf iphydf changed the title Fix memory leak in tcp server by wiping priority queues on deletion Fix memory leak in tcp server by wiping priority queues on deletion. Oct 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants