What changed, and why it matters
This change only affects the BitBox02 simulator, a local testing tool, not the real hardware wallet firmware. It makes the simulator shut down its TCP connection more cleanly when stopped with Ctrl-C or a terminate signal, and adds a way to disconnect a connected client with SIGHUP. There is no direct security vulnerability being fixed; it is a robustness improvement for developers running tests.
No security action required. Treat as normal developer-tooling robustness improvement. If reviewing, confirm the simulator is not shipped to end users and that the SO_LINGER behavior is acceptable for local test workflows.
Security signals we found
Use of SO_LINGER with l_linger=0 to force TCP RST on shutdown
Switch from signal(2) to sigaction(2) without SA_RESTART so blocking reads can be interrupted
New SIGHUP handler to disconnect current client
Change only in test/simulator, not production firmware
Evidence from the diff
The patch modifies test/simulator/simulator.c to switch from signal() to sigaction(), handle SIGINT/SIGTERM with a shared quit flag, and handle SIGHUP to disconnect the current client. On shutdown it sets SO_LINGER {1,0} to force an immediate TCP RST, drains the socket, and closes it. These changes avoid the TIME_WAIT state that previously bound the port for tens of seconds after simulator exit. The README is updated to document the signals. No cryptographic, firmware, or production code paths are touched.
Changed components
test/simulator/simulator.ctest/simulator/README.mdInspect captured patch +57 / −10
diff --git a/test/simulator/README.md b/test/simulator/README.md
new file mode 100644
index 0000000..4fed355
--- /dev/null
+++ b/test/simulator/README.md
@@ -0,0 +1,6 @@
+# Simulator
+
+## Signals
+
+The simulator can be forcefully shutdown with SIGTERM. It can also be told to
+disconnect from the client using SIGHUP.
diff --git a/test/simulator/simulator.c b/test/simulator/simulator.c
index 92edaee..271ec1a 100644
--- a/test/simulator/simulator.c
+++ b/test/simulator/simulator.c
@@ -42,7 +42,8 @@ static const char* _simulator_version = "1.0.0";
int data_len;
int commfd;
-static volatile sig_atomic_t sigint_called = false;
+static volatile sig_atomic_t quitting = false;
+static volatile sig_atomic_t hangup = false;
static int sockfd;
static int get_usb_message_socket(uint8_t* input)
@@ -71,17 +72,28 @@ static void simulate_firmware_execution(const uint8_t* input)
usb_processing_process(usb_processing_hww());
}
-static void _int_handler(int signum)
+static void _quit_handler(int signum)
{
(void)signum;
- sigint_called = true;
- close(sockfd);
+ quitting = true;
+}
+
+static void _hup_handler(int signum)
+{
+ (void)signum;
+ hangup = true;
}
int main(int argc, char* argv[])
{
- signal(SIGINT, _int_handler);
- // Default port number
+ struct sigaction psa;
+ memset(&psa, 0, sizeof(struct sigaction));
+ psa.sa_handler = _quit_handler;
+ sigaction(SIGINT, &psa, NULL);
+ sigaction(SIGTERM, &psa, NULL);
+ psa.sa_handler = _hup_handler;
+ sigaction(SIGHUP, &psa, NULL);
+ // Default port number
int portno = 15423;
struct option long_options[] = {
@@ -162,11 +174,12 @@ int main(int argc, char* argv[])
while (1) {
if ((commfd = accept(sockfd, (struct sockaddr*)&serv_addr, (socklen_t*)&serv_addr_len)) <
0) {
- if (sigint_called) {
- printf("\nGot Ctrl-C, exiting\n");
+ if (quitting) {
+ printf("\nGot signal, exiting\n");
+ break;
}
perror("accept");
- return 1;
+ break;
}
printf("Socket connection setup success\n");
@@ -175,7 +188,33 @@ int main(int argc, char* argv[])
int temp_len;
while (1) {
// Simulator polls for USB messages from client and then processes them
- if (!get_usb_message_socket(input)) break;
+ if (get_usb_message_socket(input) < 1) {
+ if (quitting) {
+ printf("\nGot term/int signal, exiting\n");
+ // Using LINGER we force a TCP RST instead of graceful shutdown. Without this
+ // trick the port keeps being bound for 10s of seconds after the application has
+ // died, hindering future executions.
+ struct linger sol = {.l_onoff = 1, .l_linger = 0};
+ int res = setsockopt(commfd, SOL_SOCKET, SO_LINGER, &sol, sizeof(sol));
+ if (res) {
+ perror("setsockopt");
+ }
+ shutdown(commfd, SHUT_RDWR);
+ uint8_t buf[1024];
+ while (read(commfd, buf, sizeof(buf)) > 0);
+ close(commfd);
+ goto after_main_loop;
+ }
+ if (hangup) {
+ hangup = false;
+ printf("\nGot hangup signal, disconnecting\n");
+ shutdown(commfd, SHUT_RDWR);
+ uint8_t buf[1024];
+ while (read(commfd, buf, sizeof(buf)) > 0);
+ close(commfd);
+ }
+ break;
+ }
simulate_firmware_execution(input);
// If the USB message to be sent from firmware is bigger than one packet,
@@ -196,5 +235,7 @@ int main(int argc, char* argv[])
printf("Socket connection closed\n");
printf("Waiting for new clients, CTRL+C to shut down the simulator\n");
}
+after_main_loop:
+ close(sockfd);
return 0;
}
Why this scored 20/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.