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

Try to retrieve exit code of a failed service call #183

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion libqrexec/process_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,16 @@
vchan, stdout_fd, stdout_msg_type,
&prefix, &remote_buffer)) {
case REMOTE_ERROR:
handle_vchan_error("send(handle_input stdout)");
if (!is_service && remote_status == -1) {
/* Even if sending fails, still try to read remaining
* data, if any - especially the exit code. But don't
* send anything anymore.
*/
LOG(ERROR, "Error while vchan send (handle_input stdout), reading remaining data");
close_stdout();
} else {
handle_vchan_error("send(handle_input stdout)");

Check warning on line 357 in libqrexec/process_io.c

View check run for this annotation

Codecov / codecov/patch

libqrexec/process_io.c#L357

Added line #L357 was not covered by tests
}
break;
case REMOTE_EOF:
close_stdout();
Expand Down
61 changes: 61 additions & 0 deletions qrexec/tests/socket/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import time
import itertools
import socket
import signal

import psutil
import pytest
Expand Down Expand Up @@ -664,6 +665,7 @@ def start_client(self, args):
self.client = subprocess.Popen(
cmd,
env=env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
self.addCleanup(self.stop_client)
Expand Down Expand Up @@ -744,6 +746,65 @@ def test_run_vm_command_from_dom0(self):
self.client.wait()
self.assertEqual(self.client.returncode, 42)

def test_run_vm_command_from_dom0_reject_stdin(self):
"""Test if qrexec-client properly returns remote exit code even if
service didn't read all of stdin"""
cmd = "user:command"
target_domain_name = "target_domain"
target_domain_uuid = "d95e1147-2d82-4595-90bb-5a7500cc3196"
target_domain = 42
target_port = 513

target_daemon = self.connect_daemon(
target_domain, target_domain_name, target_domain_uuid
)
self.start_client(["-d", target_domain_name, cmd])
target_daemon.accept()
target_daemon.handshake()

# negotiate_connection_params
self.assertEqual(
target_daemon.recv_message(),
(
qrexec.MSG_EXEC_CMDLINE,
struct.pack("<LL", 0, 0) + cmd.encode() + b"\0",
),
)
target_daemon.send_message(
qrexec.MSG_EXEC_CMDLINE,
struct.pack("<LL", target_domain, target_port),
)

target = self.connect_target(target_domain, target_port)
target.handshake()

self.client.send_signal(signal.SIGSTOP)

# select_loop
target.send_message(qrexec.MSG_DATA_STDOUT, b"stdout data\n")
target.send_message(qrexec.MSG_DATA_STDOUT, b"")
target.send_message(qrexec.MSG_DATA_EXIT_CODE, struct.pack("<L", 42))
target.close()

# ...but still send some data
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.write(b"A" * 4096)
self.client.stdin.flush()

# and only now let the client process both data streams
self.client.send_signal(signal.SIGCONT)

self.assertEqual(self.client.stdout.read(), b"stdout data\n")
self.client.wait()
self.assertEqual(self.client.returncode, 42)

def test_run_vm_command_from_dom0_with_local_command(self):
cmd = "user:command"
local_cmd = "while read x; do echo input: $x; done; exit 44"
Expand Down