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

Receive the whole IP string to avoid the truncation of the last digit #3615

Merged
merged 3 commits into from Jul 16, 2019

Conversation

Skeptor
Copy link
Contributor

@Skeptor Skeptor commented Jul 3, 2019

Related issue
#3511

Description

When the agent IP had the maximum length, 15, the last digit was truncated because it was being replaced with \0. This PR changes the number of characters received to receive the \0 from the socket too and avoid the truncation.

The field agent_ip of the agent-info was truncated before:
image

After the change, the IP field contains the whole IP
image

Tests

  • Compile agent on Linux
  • Source installation
  • Agent with IP XXX.XXX.XXX.XXX

Memory Test of ossec-agentd
image

@chemamartinez
Copy link
Contributor

I am missing the memory tests.

@chemamartinez chemamartinez added this to In progress in Wazuh 3.9.4 via automation Jul 9, 2019
@chemamartinez chemamartinez moved this from In progress to Review in progress in Wazuh 3.9.4 Jul 9, 2019
@chemamartinez chemamartinez self-assigned this Jul 9, 2019
@chemamartinez
Copy link
Contributor

I've found an invalid write when applying this change. This is why we are allocating a size of 16 for agent_ip here:

os_calloc(16,sizeof(char),agent_ip);

If we set the same size when calling OS_RecvUnix() here (IPSIZE = 16):

if(OS_RecvUnix(sock, IPSIZE, agent_ip) == 0){

At OS_RecvUnix() it is written the position 17 of agent_ip which is out of bounds:

ret[sizet] = '\0';

That's causing the following invalid write:

==6157== Invalid write of size 1
==6157==    at 0x1767D1: OS_RecvUnix (os_net.c:470)
==6157==    by 0x117845: run_notify (notify.c:146)
==6157==    by 0x112349: AgentdStart (agentd.c:200)
==6157==    by 0x114E6B: main (main.c:179)
==6157==  Address 0x62c7580 is 0 bytes after a block of size 16 alloc'd
==6157==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6157==    by 0x117746: run_notify (notify.c:138)
==6157==    by 0x112349: AgentdStart (agentd.c:200)
==6157==    by 0x114E6B: main (main.c:179)

Copy link
Contributor

@chemamartinez chemamartinez left a comment

Choose a reason for hiding this comment

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

Comments at the PR conversation.

@chemamartinez
Copy link
Contributor

More desired changes:

  1. Allocate the memory relative to IPSIZE instead of hardcoding the 16:

    os_calloc(16,sizeof(char),agent_ip);

  2. The buffer allocated and used here:

    switch (length = OS_RecvUnix(peer, IPSIZE - 1, buffer), length) {

    The received content of this buffer is no longer used, in addition we are passing IPSIZE - 1 here too. Review it please.

Copy link
Member

@vikman90 vikman90 left a comment

Choose a reason for hiding this comment

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

LGTM!

OS_RecvUnix(int socket, char *buffer, int buffer_size) reads up to buffer_size - 1 bytes, and it terminates the string.

Edit

OS_RecvUnix(int socket, char *buffer, int buffer_size)

  • First terminates the string, assuming that buffer allocates buffer_size + 1 bytes.
  • Reads up to buffer_size - 1.
  • Terminates the resulting string.

So, if an IP occupies IPSIZE - 1 bytes, we must allocate IPSIZE + 1 bytes and pass IPSIZE to OS_RecvUnix():

char buffer[IPSIZE + 1];
OS_RecvUnix(sock, buffer, IPSIZE);

This way, buffer will be terminated twice.

@chemamartinez chemamartinez requested a review from snaow July 16, 2019 15:32
@vikman90 vikman90 self-requested a review July 16, 2019 15:41
Wazuh 3.9.4 automation moved this from Review in progress to Reviewer approved Jul 16, 2019
Copy link
Member

@vikman90 vikman90 left a comment

Choose a reason for hiding this comment

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

LGTM!

@chemamartinez chemamartinez merged commit 5a6bf1f into 3.9 Jul 16, 2019
Wazuh 3.9.4 automation moved this from Reviewer approved to Done Jul 16, 2019
@chemamartinez chemamartinez deleted the 3511-IP-truncate branch July 16, 2019 15:53
@bah07 bah07 added this to To do in Review 3.9.4 via automation Jul 26, 2019
@bah07 bah07 removed this from Done in Wazuh 3.9.4 Jul 26, 2019
@bah07 bah07 moved this from To do to In progress in Review 3.9.4 Jul 26, 2019
Copy link
Contributor

@bah07 bah07 left a comment

Choose a reason for hiding this comment

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

LGTM!

@bah07 bah07 moved this from In progress to Done in Review 3.9.4 Jul 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Review 3.9.4
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants