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 bad file descriptor error when ignore_output is enabled #1102

Merged
merged 8 commits into from Aug 20, 2018

Conversation

cristgl
Copy link
Contributor

@cristgl cristgl commented Aug 14, 2018

This PR fixes the issues detected at #1057.

Also, STDERR, STDOUT and STDIN have been opened with O_RDWR permissions so that there is no bad file descriptor error when ignore_output is active.

@cristgl cristgl requested a review from vikman90 August 14, 2018 12:36
@cristgl cristgl added this to Needs review in Wazuh 3.5.1 via automation Aug 14, 2018
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.

Please apply the same stander input/output binds to the wpopen library.

Two options to solve the timeout problem:

  1. If output is disabled, use the classical kill loop (https://github.com/wazuh/wazuh/blob/master/src/wazuh_modules/wm_exec.c#L484-L503)
  2. Replace wm_exec() completely for wm_popen() and add this kill loop.

Don't forget to update the changelog.

Thanks @cristgl !

}
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
dup2(fd, STDIN_FILENO);
Copy link
Member

Choose a reason for hiding this comment

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

The stdin should also be bound in the then part of the if.

case 0:
retval = 0;
break;
switch (secs ? pthread_cond_timedwait(&tinfo.finished, &tinfo.mutex, &timeout) : pthread_cond_wait(&tinfo.finished, &tinfo.mutex)) {
Copy link
Member

Choose a reason for hiding this comment

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

This part has no sense since we have not created a pipe. This thread may fail and return immediately, so the timeout could not be computed.

@vikman90 vikman90 self-assigned this Aug 15, 2018
@vikman90 vikman90 added the type/bug Something isn't working label Aug 15, 2018
@@ -343,24 +349,46 @@ int wm_exec(char *command, char **output, int *exitcode, int secs)

pthread_mutex_destroy(&tinfo.mutex);
pthread_cond_destroy(&tinfo.finished);

// Wait for child process
Copy link
Member

Choose a reason for hiding this comment

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

waitpid() must be called in any case, otherwise the child process will remain as zombie.

@@ -128,32 +128,40 @@ wfd_t * wpopenv(const char * path, char * const * argv, int flags) {
// Error
break;

case 0:
case 0:
Copy link
Member

Choose a reason for hiding this comment

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

This indentation...?

}
// Kill and timeout
do {
sleep(1);
Copy link
Member

Choose a reason for hiding this comment

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

Please let's sleep after checking.

@@ -93,7 +93,7 @@ void wm_destroy();
* If the called program timed-out, returns WM_ERROR_TIMEOUT and output may
* contain data.
*/
int wm_exec(char *command, char **output, int *exitcode, int secs);
int wm_popen(char *command, char **output, int *exitcode, int secs);
Copy link
Member

Choose a reason for hiding this comment

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

Why did you refactor this function?

@cristgl cristgl force-pushed the fix-wodle-command branch 2 times, most recently from d53b2eb to e0f8d3a Compare August 17, 2018 11:33
close(STDOUT_FILENO);
close(STDERR_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
}

close(STDIN_FILENO);
Copy link
Member

Choose a reason for hiding this comment

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

This line should not be there.

case -1:
switch(errno){
case ESRCH:
exit(EXIT_SUCCESS);
Copy link
Member

Choose a reason for hiding this comment

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

This code runs in the parent process. This line would close the daemon.

@@ -112,18 +112,20 @@ int wm_check() {
next = j->next;

if (i->context->name == j->context->name) {
mdebug1("Deleting repeated module '%s'.", j->context->name);
if(strcmp(j->context->name,"command")){
Copy link
Member

Choose a reason for hiding this comment

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

This change fixes a different issue but it would allow repeated modules.

break;

default:

// Parent

wm_append_sid(pid);
close(pipe_fd[1]);
Copy link
Member

Choose a reason for hiding this comment

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

If the output is null, then pipe_fd has trash values.

}

close(STDIN_FILENO);
Copy link
Member

Choose a reason for hiding this comment

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

We must close fd after duplicating it.

Wazuh 3.5.1 automation moved this from Needs review to Reviewer approved Aug 20, 2018
@vikman90 vikman90 merged commit 55b36fa into 3.5 Aug 20, 2018
Wazuh 3.5.1 automation moved this from Reviewer approved to Done Aug 20, 2018
@vikman90 vikman90 deleted the fix-wodle-command branch August 20, 2018 16:17
@vikman90
Copy link
Member

LGTM!
Thanks @cristgl !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
No open projects
Wazuh 3.5.1
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants