diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d1bfdf96..fc4c64de3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,11 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] -- Changed `memset` - the device's own implementation now fills a word at a time, the same treatment `memcpy` got, taking its bulk path from six instructions per byte to about nine per sixteen (@iceman1001) -- Changed `receive_ng` - the command payload is zeroed when a packet actually arrives rather than on every idle pass of the main loop, which also covers two callers that were never zeroing it at all (@iceman1001) +- Changed `memset` - the device's own implementation now fills a word at a time, the same treatment `memcpy` got. (@iceman1001) +- Changed `receive_ng` - the command payload is zeroed when a packet actually arrives rather than on every idle pass of the main loop. Thanks @Msprg (@iceman1001) - Changed `hf mfdes chk` - runs with no arguments now, falling back to the bundled dictionary the way `hf mfdes detect` already does (@iceman1001) -- Changed `hf mfdes chk` - tidier output: the stray `d` and `p` progress characters are gone, a found key no longer repeats the AID the `Checking aid` line above it just gave, and the crypto algorithm is highlighted the way `hf mfdes detect` already highlights it (@iceman1001) -- Fixed `hf mfdes sim` - a chained write is answered instead of being refused with 91 1C on its second frame, so a write longer than one frame works at all (@iceman1001) +- Changed `hf mfdes chk` - tidier output(@iceman1001) +- Fixed `hf mfdes sim` - a chained write is answered instead of being refused with 91 1C on its second frame. (@iceman1001) - Fixed `hf mfdes sim` - an error status now ends the authenticated session, as M134034 7.3.4 requires of a card (@iceman1001) - Fixed `hf mfdes sim` - answers are sized from the frame size the reader asks for in its RATS instead of a fixed 96 bytes (@iceman1001) - Fixed `hf mfdes sim` - the length of a write is taken from the 3 byte length the command carries rather than guessed from how full a frame looked (@iceman1001) @@ -32,7 +32,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Added `hf mfdes sim` - DESFire EV1 simulation now answers authentication, secure messaging, file reads, writes and transactions from the card image in emulator memory (@iceman1001) - Fixed `hf mfdes sim` - a 2TDEA key whose two halves match is handled as a single DES key, session key generation included, so the session key matches what a genuine PICC derives (@iceman1001) - Fixed `hf mfdes sim` - the CMAC of a chained answer is taken over the whole transfer instead of being restarted on every 0xAF continuation (@iceman1001) -- Fixed `PLATFORM_EXTRAS=BWM` - a command started over the Proxmark5 wireless module can be aborted again, `data_available()` only polled USB so CMD_BREAK_LOOP was never seen on the BLE or WiFi link (@tweathers-sec) +- Fixed `PLATFORM_EXTRAS=BWM` - a command started over the Proxmark5 wireless module can be aborted again. (@tweathers-sec) - Fixed `lf search` - a leftover debug print no longer reports `DEBUG: detectindala | ` on every failed Indala demodulation (@tweathers-sec) - Changed `memcpy` - the device's own implementation now copies a word at a time when source and destination allow it (@iceman1001) - Changed NG replies - the outgoing frame is offset so its payload lands word aligned, taking USB transfer speed from 630 to 789 kB/s on RDV4 (@iceman1001) @@ -79,7 +79,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Changed `hf mf view` - now decode a HID PACS credential (@iceman1001) - Changed `hf mf dump --ns` - now decode a HID PACS credential (@iceman1001) - Fixed `fpga_compress -d` - a single bitstream archive was unpacked 2384 bytes short, the trailing partial interleave chunk was dropped (@iceman1001) -- Fixed `fpga_compress` - a build with only one FPGA bitstream (`SKIP_LF`, `SKIP_FELICA`, `SKIP_ISO15693`) packed it as a single 1 MB block the ARM could not decompress, `inflate returned: -13247` (@iceman1001) +- Fixed `fpga_compress` - a build with only one FPGA bitstream (`SKIP_LF`, `SKIP_FELICA`, `SKIP_ISO15693`) packed it as a single 1 MB block the ARM could not decompress (@iceman1001) - Fixed `FpgaDownloadAndGo` - a failed FPGA decompress leaked the ring buffer, leaving BigBuf 16 kB short for the rest of the session (@iceman1001) - Added `hf mf dump --sk` - save extracted keys to binary file (@iceman1001) - Changed `hf mf dump --ns` - now runs the same analysis as `hf mf view`(@iceman1001) @@ -98,7 +98,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Changed `trace save -1` - now downloads from device by default (@iceman1001) - Added `hf 14a antifuzz --coll` - the UID collides from the 9th bit (@iceman1001) - Changed `trace list -t 14a` - decodes SEL 0x99..0x9F, shows how many UID bits a bit oriented ANTICOLL claims (@iceman1001) -- Fixed `hf iclass encode` / `hf iclass tagsim` / `hf mf encodehid` / `lf hid sim` / `lf hid clone` / `lf hid brute` - `--cn` was read with a 32 bit getter, silently truncating card numbers of wide formats like H10302 (@iceman1001) +- Fixed `hf iclass encode` / `hf iclass tagsim` / `hf mf encodehid` / `lf hid sim` / `lf hid clone` / `lf hid brute` - `--cn` was read with a 32 bit getter (@iceman1001) - Changed `hf mf eload` - the device now zeroes the emulator memory before the upload (@iceman1001) - Fixed `hf mf sim` / `hf 14a sim` - the anticollision responses were prepared in BigBuf before `iso14443a_setup()` (@iceman1001) - Changed `CAPABILITIES_VERSION` to 10 - `hf mf eload` sends a flags byte the device did not have before, so client and firmware must be flashed as a pair (@iceman1001) @@ -134,7 +134,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Fixed `lf fsk demod` - a leading run too short to be a bit was forced to one, fabricating a bit and shifting the word (@mfcarroll) - Fixed `lf nrz demod` - the samples before the first level change were counted as bits, rotating the word whenever that edge moved (@mfcarroll) - Fixed `lf t55xx dump/read` - blocks were extracted at a bit offset cached from the last `detect` The offset is now anchored in graph samples (@iceman1001) -- Fixed `lf t55xx write --verify` - a successful write could be reported as a validation failure, both from the rotated read above and from the pre-write config decoding the post-write signal into garbage (@iceman1001) +- Fixed `lf t55xx write --verify` - a successful write could be reported as a validation failure (@iceman1001) - Fixed `lf t55xx detect` - the automatic detect after a block 0 write reported a password that was never supplied (@iceman1001) - Fixed `hf emrtd info` - now correctly pretty print algos (@iceman1001) - Fixed `trace list -t topaz` - now parses topaz reader frames up to 16 bytes (@iceman1001) @@ -145,10 +145,10 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Fixed `lf hitag list` - now prints in same style like the rest (@iceman1001) - Changed pathing - client now supports `~/` tilde sign in paths (@iceman1001) - Added `hf_legic_rdv4` - Standalone mode for managing 7 persistent legic dumps (@digitalhoax) -- Fixed `lf t55xx detect` - dropped the psk3 candidates, which run the same demodulation as the psk2 one beside them and so can never match a psk3 tag, only fire on demodulation errors (@mfcarroll) -- Fixed `lf t55xx detect` - a psk3 tag read back as psk2 with a block 0 one bit out, and a freshly wiped psk2 tag was reported as psk3 on no evidence; the two are now reported as ambiguous where the waveform cannot separate them, and block 0 is narrowed using the subcarrier, the broadcast period and whether password mode is in use (@mfcarroll) +- Fixed `lf t55xx detect` - dropped the psk3 candidates (@mfcarroll) +- Fixed `lf t55xx detect` - a psk3 tag read back as psk2 with a block 0 one bit out (@mfcarroll) - Added doc/md/PM5_Start_Here/Getting_Started_With_PM5.md now exists as a starting point for those who have their PM5 (@innocentbystanderproxmark) -- Fixed `tools/ePassport` - the personal number read from the MRZ optional-data field showed a filler character where a space belonged, and a document number longer than nine characters left its overflow there (@pkilar) +- Fixed `tools/ePassport` - the personal number read from the MRZ optional-data field showed a filler character where a space belonged (@pkilar) - Fixed `tools/ePassport` - the detail tabs named the files their data came from as fixed text, so a document without EF_DG11 was told otherwise, and the personal-number caption wrapped (@pkilar) - Added `tools/ePassport` - EF_DG13 is decoded, and a Polish document's PESEL is shown as the personal number when it carries no EF_DG11 (@pkilar) - Fixed `tools/ePassport` - a disabled button drew Kivy's banded disabled texture, which left its label unreadable (@pkilar) @@ -177,7 +177,7 @@ This project uses the changelog in accordance with [keepchangelog](http://keepac - Fixed clock `SpinDelayUsPrecision` to not wrap. Now more 43ms issues (@iceman1001) - Changed `hf felica raw` - now can stream per-window envelope for better measurments (@iceman1001) - Updated `fpga_pm3_felica.bit` - FeliCa signal handling got refactored. (@iceman1001) -- Changed client tab completion - vocabulary is now built at runtime from the live command tree and uses the same availability rules as `help`, removing the generated `pm3line_vocabulary.h` and `pm3_help2list.py` (@Msprg) +- Changed client tab completion - vocabulary is now built at runtime from the live command tree and uses the same availability rules as `help` (@Msprg) - Fixed standalone modes - the one second button hold trigger was a no-op on all non-PM5 platforms, leaving `hw standalone` over USB as the only entry point (@ShawInnes) - Added `sim022.bin` - enabled burst mode transfers, which allows us to do TA1=96 in speeds (@iceman1001) - Fixed `hf felica liteauth` - empty long option names for `-c` and `-k` made argtable read past the string (found by ASAN on `--fulltext`) (@Msprg) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index efd3acdff..a6aad1b49 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -4388,9 +4388,9 @@ void __attribute__((noreturn)) AppMain(void) { while (1); } - // Check if there is a packet available + // Check if there is a packet available. receive_ng() zeroes the + // payload itself, once it knows a packet is actually arriving. PacketCommandNG rx; - memset(&rx.data, 0, sizeof(rx.data)); int ret = receive_ng(&rx); if (ret == PM3_SUCCESS) { diff --git a/armsrc/cmd.c b/armsrc/cmd.c index a45b85d5f..ab8d81b5b 100644 --- a/armsrc/cmd.c +++ b/armsrc/cmd.c @@ -191,6 +191,13 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da return PM3_EIO; } + // Zero the payload only once a packet is really coming in: doing it for + // every idle poll of the main loop cost more than the rest of the loop. + // Both branches below leave the tail of data untouched -- an NG command + // fills only its own length, and an old style one only PM3_CMD_DATA_SIZE_OLD + // -- so what is not written has to start at zero. + memset(&rx->data, 0, sizeof(rx->data)); + rx->magic = rx_raw.pre.magic; rx->ng = rx_raw.pre.ng; rx->cmd = rx_raw.pre.cmd; diff --git a/client/src/flash.c b/client/src/flash.c index abfb93106..7ad9f2729 100644 --- a/client/src/flash.c +++ b/client/src/flash.c @@ -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); } diff --git a/client/src/pm3line.c b/client/src/pm3line.c index 51d03b72e..ca624a0d4 100644 --- a/client/src/pm3line.c +++ b/client/src/pm3line.c @@ -21,6 +21,9 @@ #include // for Mingw readline and for getline #include #include +#ifndef _WIN32 +#include // write, isatty, STDOUT_FILENO +#endif #if defined(HAVE_READLINE) #include #include @@ -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