armsrc: zero the command payload when a packet arrives, not on every idle poll
The main loop cleared the whole PacketCommandNG payload before every call to receive_ng(), so a device sitting idle memset PM3_CMD_DATA_SIZE bytes on each pass of the loop whether anything was arriving or not. The zeroing belongs inside receive_ng_internal(), once the preamble has been read and a packet is known to be coming in, and that is where it is now. It has to be before either branch fills the payload, because neither fills all of it: an NG command writes only its own length, and an old style one only PM3_CMD_DATA_SIZE_OLD. Whatever is not written has to start at zero. Moving it also covers two callers that never had it. appmain.c has a second receive_ng() that drains the buffer on a mode switch, and iclass.c has one that picks up EML_MEMSET commands while waiting for RF. Both declare their PacketCommandNG without initialising it, and the iclass one then reads a struct out of the payload -- so a command shorter than that struct was reading stack garbage beyond its length. That is fixed as a side effect. Builds for RDV4 and PM5. Thanks to @Msprg et al
This commit is contained in:
1 parent
461c252988
commit
592977c217
5 files changed
+254
-26
No files matched your search
+24
-2
@@ -910,6 +910,23 @@ static int write_block(uint32_t address, int magic, uint8_t *data, uint32_t leng
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Hand the signals back to whoever had them. The client needs SIGINT to be
|
||||
// able to quit on CTRL-C once the flashing is done
|
||||
static void restore_progress_signals(void (*old_sigint)(int), void (*old_sigwinch)(int)) {
|
||||
|
||||
if (old_sigint != SIG_ERR) {
|
||||
signal(SIGINT, old_sigint);
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
if (old_sigwinch != SIG_ERR) {
|
||||
signal(SIGWINCH, old_sigwinch);
|
||||
}
|
||||
#else
|
||||
(void) old_sigwinch;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Write a file's segments to Flash
|
||||
int flash_write(flash_file_t *ctx, flash_dev_t *flash_dev) {
|
||||
|
||||
@@ -930,11 +947,14 @@ int flash_write(flash_file_t *ctx, flash_dev_t *flash_dev) {
|
||||
uint32_t baddr = seg->start;
|
||||
|
||||
int pct = 0;
|
||||
void (*old_sigint)(int) = SIG_ERR;
|
||||
void (*old_sigwinch)(int) = SIG_ERR;
|
||||
|
||||
if (blocks > 50) {
|
||||
|
||||
signal(SIGINT, hadouken_on_sigint);
|
||||
old_sigint = signal(SIGINT, hadouken_on_sigint);
|
||||
#ifndef _WIN32
|
||||
signal(SIGWINCH, hadouken_on_sigwinch);
|
||||
old_sigwinch = signal(SIGWINCH, hadouken_on_sigwinch);
|
||||
#endif
|
||||
|
||||
hadouken_start(30.0, 0, 1);
|
||||
@@ -950,6 +970,7 @@ int flash_write(flash_file_t *ctx, flash_dev_t *flash_dev) {
|
||||
if (write_block(baddr, ctx->ver_info->magic, data, block_size, flash_dev) < 0) {
|
||||
if (blocks > 50) {
|
||||
hadouken_stop();
|
||||
restore_progress_signals(old_sigint, old_sigwinch);
|
||||
}
|
||||
PrintAndLogEx(ERR, "Error writing block %d of %u", block, blocks);
|
||||
return PM3_EFATAL;
|
||||
@@ -968,6 +989,7 @@ int flash_write(flash_file_t *ctx, flash_dev_t *flash_dev) {
|
||||
|
||||
if (blocks > 50) {
|
||||
hadouken_stop();
|
||||
restore_progress_signals(old_sigint, old_sigwinch);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
+209
-10
@@ -21,6 +21,9 @@
|
||||
#include <stdio.h> // for Mingw readline and for getline
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h> // write, isatty, STDOUT_FILENO
|
||||
#endif
|
||||
#if defined(HAVE_READLINE)
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
@@ -32,6 +35,8 @@
|
||||
#include "ui.h" // g_session
|
||||
#include "util.h" // str_ndup
|
||||
|
||||
static void pm3line_claim_signals(void);
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
|
||||
static char *rl_command_generator(const char *text, int state) {
|
||||
@@ -77,6 +82,13 @@ static char **rl_command_completion(const char *text, int start, int end) {
|
||||
return rl_completion_matches(text, rl_command_generator);
|
||||
}
|
||||
|
||||
static int (*gs_check_hook)(void) = NULL;
|
||||
|
||||
static int pm3line_startup_hook(void) {
|
||||
pm3line_claim_signals();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#elif defined(HAVE_LINENOISE)
|
||||
static void ln_command_completion(const char *text, linenoiseCompletions *lc) {
|
||||
const char *prev_match = "";
|
||||
@@ -113,6 +125,9 @@ static void ln_command_completion(const char *text, linenoiseCompletions *lc) {
|
||||
}
|
||||
#endif // HAVE_READLINE
|
||||
|
||||
static volatile sig_atomic_t gs_sigint_caught = 0;
|
||||
static volatile sig_atomic_t gs_at_prompt = 0;
|
||||
|
||||
# if defined(_WIN32)
|
||||
/*
|
||||
static bool WINAPI terminate_handler(DWORD t) {
|
||||
@@ -125,13 +140,39 @@ static bool WINAPI terminate_handler(DWORD t) {
|
||||
*/
|
||||
# else
|
||||
static struct sigaction gs_old_sigint_action;
|
||||
static struct sigaction gs_old_sigtstp_action;
|
||||
static volatile sig_atomic_t gs_echo_ctrl_c = 0;
|
||||
static void sigtstp_handler(int signum);
|
||||
static void sigint_handler(int signum) {
|
||||
|
||||
switch (signum) {
|
||||
case SIGINT: {
|
||||
sigaction(SIGINT, &gs_old_sigint_action, NULL);
|
||||
pm3line_flush_history();
|
||||
kill(0, SIGINT);
|
||||
// Second CTRL-C. The graceful path did not take, restore the
|
||||
// default disposition and let this one through.
|
||||
if (gs_sigint_caught) {
|
||||
sigaction(SIGINT, &gs_old_sigint_action, NULL);
|
||||
raise(SIGINT);
|
||||
break;
|
||||
}
|
||||
// Only set a flag here. Saving the history means malloc and stdio,
|
||||
// neither is safe to call from a signal handler in a threaded
|
||||
// client. write() is, and the terminal no longer echoes the
|
||||
// character for us since readline turned ECHO off
|
||||
gs_sigint_caught = 1;
|
||||
|
||||
if (gs_echo_ctrl_c) {
|
||||
static const char at_prompt[] = "^C";
|
||||
// the terminal is out of raw mode while a command runs, so it
|
||||
// echoed the ^C itself. Only say what happens next
|
||||
static const char in_command[] = "\nquitting once this command is done. CTRL-C again to force\n";
|
||||
ssize_t ignored;
|
||||
if (gs_at_prompt) {
|
||||
ignored = write(STDOUT_FILENO, at_prompt, sizeof(at_prompt) - 1);
|
||||
} else {
|
||||
ignored = write(STDOUT_FILENO, in_command, sizeof(in_command) - 1);
|
||||
}
|
||||
(void) ignored;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
@@ -140,24 +181,141 @@ static void sigint_handler(int signum) {
|
||||
}
|
||||
}
|
||||
|
||||
// CTRL-Z. Put the terminal back the way the shell expects it, stop for real
|
||||
// on this thread, and set the line editor up again once we are continued
|
||||
static void sigtstp_handler(int signum) {
|
||||
|
||||
if (signum != SIGTSTP) {
|
||||
return;
|
||||
}
|
||||
|
||||
int at_prompt = gs_at_prompt;
|
||||
(void) at_prompt;
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
if (at_prompt) {
|
||||
rl_cleanup_after_signal();
|
||||
}
|
||||
#endif
|
||||
|
||||
sigaction(SIGTSTP, &gs_old_sigtstp_action, NULL);
|
||||
|
||||
sigset_t set;
|
||||
sigprocmask(SIG_BLOCK, NULL, &set);
|
||||
sigdelset(&set, SIGTSTP);
|
||||
|
||||
raise(SIGTSTP);
|
||||
|
||||
// the signal raised above is blocked while we are inside the handler,
|
||||
// unblocking it is what stops us. We resume here on SIGCONT
|
||||
sigprocmask(SIG_SETMASK, &set, NULL);
|
||||
|
||||
pm3line_claim_signals();
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
if (at_prompt) {
|
||||
rl_reset_after_signal();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Leave the terminal usable when the client is killed instead of quit
|
||||
static void sigfatal_handler(int signum) {
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
if (gs_at_prompt) {
|
||||
rl_cleanup_after_signal();
|
||||
}
|
||||
#endif
|
||||
|
||||
signal(signum, SIG_DFL);
|
||||
raise(signum);
|
||||
}
|
||||
|
||||
static void claim_one_signal(int signum, void (*handler)(int)) {
|
||||
|
||||
struct sigaction current;
|
||||
if (sigaction(signum, NULL, ¤t) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (current.sa_handler == handler) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct sigaction action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
action.sa_handler = handler;
|
||||
sigaction(signum, &action, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Readline answers a caught signal by cleaning up, re-raising it and then
|
||||
// reinstalling its handler. In a threaded client that re-raise lands on
|
||||
// whichever handler is installed at that instant, which can be readline's own
|
||||
// again, and the cycle repeats. Measured on CTRL-C and CTRL-Z alike, one
|
||||
// keypress gave a dozen echoes and a coin flip over whether it did anything.
|
||||
// So the client owns them, and takes them back from anything that grabs one,
|
||||
// like the flasher progress bar, which installs a handler it never restores
|
||||
static void pm3line_claim_signals(void) {
|
||||
# if !defined(_WIN32)
|
||||
claim_one_signal(SIGINT, &sigint_handler);
|
||||
claim_one_signal(SIGTSTP, &sigtstp_handler);
|
||||
# endif
|
||||
}
|
||||
|
||||
void pm3line_install_signals(void) {
|
||||
# if defined(_WIN32)
|
||||
// SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, true);
|
||||
# else
|
||||
gs_echo_ctrl_c = (isatty(STDOUT_FILENO) == 1);
|
||||
|
||||
struct sigaction action;
|
||||
memset(&action, 0, sizeof(action));
|
||||
|
||||
action.sa_handler = &sigint_handler;
|
||||
sigaction(SIGINT, &action, &gs_old_sigint_action);
|
||||
|
||||
action.sa_handler = &sigtstp_handler;
|
||||
sigaction(SIGTSTP, &action, &gs_old_sigtstp_action);
|
||||
|
||||
action.sa_handler = &sigfatal_handler;
|
||||
sigaction(SIGTERM, &action, NULL);
|
||||
sigaction(SIGQUIT, &action, NULL);
|
||||
sigaction(SIGHUP, &action, NULL);
|
||||
# endif
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
rl_catch_signals = 1;
|
||||
rl_set_signals();
|
||||
// Readline must not catch these itself, see pm3line_claim_signals().
|
||||
// rl_catch_sigwinch is a separate flag, window resizes stay with readline
|
||||
rl_catch_signals = 0;
|
||||
rl_startup_hook = pm3line_startup_hook;
|
||||
#endif // HAVE_READLINE
|
||||
}
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
// readline calls this roughly ten times a second while it waits for input
|
||||
static int pm3line_event_hook(void) {
|
||||
|
||||
pm3line_claim_signals();
|
||||
|
||||
if (gs_sigint_caught) {
|
||||
// Drop the line being edited and make readline() return, so the caller
|
||||
// reaches the normal shutdown instead of dying inside a handler
|
||||
rl_free_line_state();
|
||||
rl_replace_line("", 0);
|
||||
rl_done = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (gs_check_hook) {
|
||||
return gs_check_hook();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif // HAVE_READLINE
|
||||
|
||||
void pm3line_init(void) {
|
||||
#if defined(HAVE_READLINE) || defined(HAVE_LINENOISE)
|
||||
// Build the completion vocabulary from the live command tree
|
||||
@@ -175,8 +333,6 @@ void pm3line_init(void) {
|
||||
rl_getc_function = getc;
|
||||
#endif
|
||||
|
||||
pm3line_install_signals();
|
||||
|
||||
#ifdef RL_STATE_READCMD
|
||||
rl_extend_line_buffer(1024);
|
||||
#endif // RL_STATE_READCMD
|
||||
@@ -184,18 +340,50 @@ void pm3line_init(void) {
|
||||
linenoiseInstallWindowChangeHandler();
|
||||
linenoiseSetCompletionCallback(ln_command_completion);
|
||||
#endif // HAVE_READLINE
|
||||
|
||||
pm3line_install_signals();
|
||||
}
|
||||
|
||||
char *pm3line_read(const char *s) {
|
||||
|
||||
pm3line_claim_signals();
|
||||
|
||||
// CTRL-C already asked for a shutdown, do not put up another prompt.
|
||||
// NULL is what CTRL-D returns, the caller exits cleanly on it and that
|
||||
// path flushes the history
|
||||
if (gs_sigint_caught) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gs_at_prompt = 1;
|
||||
|
||||
#if defined(HAVE_READLINE)
|
||||
return readline(s);
|
||||
char *line = readline(s);
|
||||
gs_at_prompt = 0;
|
||||
if (gs_sigint_caught) {
|
||||
free(line);
|
||||
return NULL;
|
||||
}
|
||||
return line;
|
||||
#elif defined(HAVE_LINENOISE)
|
||||
return linenoise(s);
|
||||
char *line = linenoise(s);
|
||||
gs_at_prompt = 0;
|
||||
if (gs_sigint_caught) {
|
||||
free(line);
|
||||
return NULL;
|
||||
}
|
||||
return line;
|
||||
#else
|
||||
printf("%s", s);
|
||||
// MinGW/ProxSpace builds do not provide getline() in this fallback path.
|
||||
char input[1024] = {0};
|
||||
if (fgets(input, sizeof(input), stdin) == NULL) {
|
||||
gs_at_prompt = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gs_at_prompt = 0;
|
||||
if (gs_sigint_caught) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -256,10 +444,20 @@ void pm3line_add_history(const char *line) {
|
||||
// add if not identical to latest recorded line
|
||||
if ((!entry) || (strcmp(entry->line, line) != 0)) {
|
||||
add_history(line);
|
||||
// keep the file in sync, so a crash or a kill -9 does not take the
|
||||
// history with it. append_history fails when the file is not there yet
|
||||
if (g_session.history_path) {
|
||||
if (append_history(1, g_session.history_path) != 0) {
|
||||
write_history(g_session.history_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
#elif defined(HAVE_LINENOISE)
|
||||
// linenoiseHistoryAdd takes already care of duplicate entries
|
||||
linenoiseHistoryAdd(line);
|
||||
if (g_session.history_path) {
|
||||
linenoiseHistorySave(g_session.history_path);
|
||||
}
|
||||
#else
|
||||
(void) line;
|
||||
#endif
|
||||
@@ -279,7 +477,8 @@ void pm3line_flush_history(void) {
|
||||
|
||||
void pm3line_check(int (check)(void)) {
|
||||
#if defined(HAVE_READLINE)
|
||||
rl_event_hook = check;
|
||||
gs_check_hook = check;
|
||||
rl_event_hook = pm3line_event_hook;
|
||||
#else
|
||||
check();
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user