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

ng_pktbuf: fix alignment overwrite issue #3316

Merged
merged 2 commits into from
Jul 8, 2015

Conversation

miri64
Copy link
Member

@miri64 miri64 commented Jul 4, 2015

Currently it can happen if there is a spot of size n free that a chunk of size n + 1 is inserted, if n is devisable by the word length of the platform. This patch fixes this issue and adds a test to prevent this kind of bug in future implementations.

@miri64 miri64 added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) NSTF labels Jul 4, 2015
@miri64 miri64 added this to the Release 2015.06 milestone Jul 4, 2015
@OlegHahm
Copy link
Member

OlegHahm commented Jul 6, 2015

Can you please document these inline functions? It's hard to understand for me, what's their purpose.

@PeterKietzmann
Copy link
Member

+1 for @OlegHahm 's proposal :-)

@@ -165,7 +168,7 @@ void *_pktbuf_internal_alloc(size_t size)

while ((node->next != NULL)
/* and if space between current and next allocation is not big enough */
&& ((_start_idx(node->next) - _end_idx(node)) < _total_sz(size))) {
&& ((_start_idx(node->next) - _end_idx(node)) < _al_sz(_total_sz(size)))) {
Copy link
Member

Choose a reason for hiding this comment

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

@authmillenon I don't get why it is start - end (simplified). Isn't endalways greater than start? And in that case you'll get negative values there? Just asking dump to get a foolproof answer :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Look again: start - end simplifies too much. It's start(next_node) - end(this_node) so it determines the space between two allocated spaces of 2 nodes. If this is negative, the chunks would overlap, which would be am error ;)

Copy link
Member

Choose a reason for hiding this comment

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

Obviously I overlooked node->next and node in my error-searching-mania. Sorry! Second stupid question: (Why) is the node->next != NULL condition needed? Besides looking at the code I just wonder if there is a situation where you need to create a new node, because the free memory is too small. How/where do you handle this?

Copy link
Member Author

Choose a reason for hiding this comment

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

node->next == NULL means, the current chunk is the last in the packet buffer, so as long as the packet buffer is big enough, exiting the loop here means the new chunk will be appended to the list of nodes. A new node/chunk (a node just describes a chunk that comes immediately after it so the terms are somewhat interchangable) is always created when this function is called (this is the purpose of this function), except of cause if there is no space left (which is checked in https://github.com/RIOT-OS/RIOT/pull/3316/files#diff-2901d789a30fdc0411aca7f3e3ec7249R178).

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the detailed feedback! Last one:

A new node/chunk (a node just describes a chunk that comes immediately after it so the terms are somewhat interchangable) is always created when this function is called

Which function exactly or in which line?

Copy link
Member Author

Choose a reason for hiding this comment

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

This function we are talking in: _pktbuf_internal_alloc(). The node is "created" in https://github.com/RIOT-OS/RIOT/pull/3316/files#diff-2901d789a30fdc0411aca7f3e3ec7249R176, but only filled after it is determined, that there is enough space available. (https://github.com/RIOT-OS/RIOT/pull/3316/files#diff-2901d789a30fdc0411aca7f3e3ec7249R183 and following)

Copy link
Member

Choose a reason for hiding this comment

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

thx

Copy link
Member

Choose a reason for hiding this comment

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

@authmillenon, maybe you add some more comments that would help someone like @PeterKietzmann to understand the code better?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah… or (as I proposed last week) rewrite it as a far less complicated one.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds even better! 👍

@miri64
Copy link
Member Author

miri64 commented Jul 7, 2015

Are the added comments helpful?

@@ -88,12 +88,10 @@ static inline size_t __total_sz(_used_t *node)
}

/**
* @brief aligned size with metadata
* @brief aligns @p size to the next word alignment.
Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking about an other term for "word". But still idealess...

@PeterKietzmann
Copy link
Member

Let's merge this PR as soon as we clarified what's wrong with the unittest.

@OlegHahm
Copy link
Member

OlegHahm commented Jul 7, 2015

Let's merge this PR as soon as we clarified what's wrong with the unittest.

What's wrong with the unittest?

@PeterKietzmann
Copy link
Member

The address sanitizer tool points to an issue which is caused by the newly introduced unittest function. Anyway, as you also agree we can merge this PR and fix the issue in an other PR once we found the problem (That's what @authmillenon stated online. I won't NACK that action)

@PeterKietzmann
Copy link
Member

@authmillenon I think you can squash already.

Currently it can happen if there is a spot of size `n` free that a chunk
of size `n + 1` is inserted, if `n` is devisable by the word length of
the platform. This patch fixes this issue.
@miri64
Copy link
Member Author

miri64 commented Jul 7, 2015

squashed

@miri64
Copy link
Member Author

miri64 commented Jul 7, 2015

I encountered a address sanitizer message in the packet buffer unittests when testing #3327, too. Since that one did not show up in @PeterKietzmann's check I'm assuming, though it does show in thes PR in the newly introduced test, it is not related to it.

@OlegHahm
Copy link
Member

OlegHahm commented Jul 7, 2015

In combination with #3311 I don't get any fatal errors (segfaults, hanging nodes and the like) with native and ICMPv6 pings. I tried with 300.0000 pings and different payloads (up to 1kB).

@miri64
Copy link
Member Author

miri64 commented Jul 7, 2015

Try more, it will break but unrelated to this PR.

@PeterKietzmann PeterKietzmann added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jul 8, 2015
@PeterKietzmann
Copy link
Member

So we'll try to figure out the problems and solve them in an other PR. ACK for this one

PeterKietzmann added a commit that referenced this pull request Jul 8, 2015
ng_pktbuf: fix alignment overwrite issue
@PeterKietzmann PeterKietzmann merged commit c3814a7 into RIOT-OS:master Jul 8, 2015
@miri64 miri64 deleted the ng_pktbuf/fix/align branch July 9, 2015 10:38
@miri64 miri64 added the Area: network Area: Networking label Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: network Area: Networking CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants