Repository navigation
Fixed out-of-bounds read in RemoteDirList when parsing a listing #6390
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
Open
Dhanu-dynamic
wants to merge
1
commit into
cfengine:master
Choose a base branch
from
Dhanu-dynamic:remote-dirlist-packet-bound
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #include <test.h> | ||
|
|
||
| #include <cmockery.h> | ||
| #include <client_code.h> /* RemoteDirList */ | ||
| #include <connection_info.h> /* ConnectionInfo* */ | ||
| #include <cfnet.h> /* AgentConnection, CF_MORE, CF_DONE */ | ||
| #include <net.h> /* SendTransaction */ | ||
| #include <item_lib.h> /* Item, ListLen, DeleteItemList */ | ||
| #include <cf3.defs.h> /* CFD_TERMINATOR */ | ||
|
|
||
| #include <sys/socket.h> | ||
| #include <unistd.h> | ||
| #include <string.h> | ||
|
|
||
| #define TERM CFD_TERMINATOR | ||
|
|
||
| /* Entry names are read out of the struct dirent that RemoteDirList() | ||
| * allocates for each of them. */ | ||
| static const char *EntryName(const Item *ip) | ||
| { | ||
| return ((const struct dirent *) ip->name)->d_name; | ||
| } | ||
|
|
||
| /* A server reply that does not end in the double '\0' must not make | ||
| * RemoteDirList() read the bytes left in the receive buffer by the previous | ||
| * reply. Here the stale bytes are a CFD_TERMINATOR, so an unbounded scan ends | ||
| * the listing early and silently drops the entries of the last packet. */ | ||
| static void test_dirlist_stops_at_end_of_packet(void) | ||
| { | ||
| int sv[2]; | ||
| int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); | ||
| assert_int_equal(ret, 0); | ||
|
|
||
| ConnectionInfo *server = ConnectionInfoNew(); | ||
| ConnectionInfoSetSocket(server, sv[1]); | ||
| ConnectionInfoSetProtocolVersion(server, CF_PROTOCOL_CLASSIC); | ||
|
|
||
| /* "entry1", end of packet, and a CFD_TERMINATOR that the client must not | ||
| * reach because the double '\0' comes first. It stays in the receive | ||
| * buffer at offset 8. */ | ||
| const char packet1[] = "entry1\0\0" TERM "\0"; | ||
| ret = SendTransaction(server, packet1, sizeof(packet1) - 1, CF_MORE); | ||
| assert_int_equal(ret, 0); | ||
|
|
||
| /* 7 bytes with no terminating '\0' of their own: the scan resumes at | ||
| * offset 8, i.e. exactly on top of the CFD_TERMINATOR above. */ | ||
| const char packet2[] = "entry2x"; | ||
| ret = SendTransaction(server, packet2, sizeof(packet2) - 1, CF_MORE); | ||
| assert_int_equal(ret, 0); | ||
|
|
||
| /* The real last packet, which an unbounded scan never gets to read. */ | ||
| const char packet3[] = "entry3\0" TERM "\0\0"; | ||
| ret = SendTransaction(server, packet3, sizeof(packet3) - 1, CF_DONE); | ||
| assert_int_equal(ret, 0); | ||
|
|
||
| AgentConnection conn; | ||
| memset(&conn, 0, sizeof(conn)); | ||
| conn.conn_info = ConnectionInfoNew(); | ||
| ConnectionInfoSetSocket(conn.conn_info, sv[0]); | ||
| ConnectionInfoSetProtocolVersion(conn.conn_info, CF_PROTOCOL_CLASSIC); | ||
| conn.this_server = "test-server"; | ||
|
|
||
| Item *list = RemoteDirList("/tmp", false, &conn); | ||
|
|
||
| assert_true(list != NULL); | ||
| assert_int_equal(ListLen(list), 3); | ||
| assert_string_equal(EntryName(list), "entry1"); | ||
| assert_string_equal(EntryName(list->next), "entry2x"); | ||
| assert_string_equal(EntryName(list->next->next), "entry3"); | ||
|
|
||
| DeleteItemList(list); | ||
| ConnectionInfoDestroy(&conn.conn_info); | ||
| ConnectionInfoDestroy(&server); | ||
| close(sv[0]); | ||
| close(sv[1]); | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| PRINT_TEST_BANNER(); | ||
| const UnitTest tests[] = | ||
| { | ||
| unit_test(test_dirlist_stops_at_end_of_packet), | ||
| }; | ||
|
|
||
| return run_tests(tests); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.