From patchwork Thu Nov 9 06:40:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 836203 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yXYTF4Qptz9t3h for ; Thu, 9 Nov 2017 17:40:49 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="VA1uWb6j"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="pYH1wy+5"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3yXYTF3BXhzDrKv for ; Thu, 9 Nov 2017 17:40:49 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="VA1uWb6j"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="pYH1wy+5"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.28; helo=out4-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="VA1uWb6j"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="pYH1wy+5"; dkim-atps=neutral Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3yXYT41F5QzDrKG for ; Thu, 9 Nov 2017 17:40:39 +1100 (AEDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 2F9B8205E1; Thu, 9 Nov 2017 01:40:37 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute2.internal (MEProxy); Thu, 09 Nov 2017 01:40:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=cc:date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ZYLoq6eC0tCsYwgiA 7c72V2g+sXEuKsp+zKN4t4fGwM=; b=VA1uWb6jdaTSfTZhrwN4kE+IldaPBEWes Agh71N1TkirB+VMztFTawFnUTSv5d8y7uRNoGI9e3gpBA0NidUC/VRaMh7/wScrt fvBWVA1P3W9yr20AyP3FzfgN2aUmDaG2mKRDkb5kWH/FndmiXl4JVTiJgYzdGNqa uqkKYv7PnoZwOT8olmko53H4SfVMLXY9es+w2wqxfCTI/HckwmBLqpFTcv9MxRlr K7JycoNMsktcCaNz6R58qSXP+5sOqk0sdtElm611flRerx1J+mvsBtRZOlHPnLpD mco4ebHnGWgiiMDX3TEu0PGTLMzwI7/xumUiVQU5AKGIosWifVnWg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ZYLoq6eC0tCsYwgiA 7c72V2g+sXEuKsp+zKN4t4fGwM=; b=pYH1wy+5HKzzpjjPe/vv+zPFEr04aMRqA IyWVjEqegZWoa+HNIv+FHJlvTYaMB8VRYR40uni8u1HOz2O3KqLqD0sO3tfi6iT8 emQ1CqrftHwYwcOcOA8/i0HlhPdBQa7QhZDqc12qsnlMbDPoHMjk7Ed70A6OojzP Dr6yxGUsdFK21Ar6qCi8DlBwc5+4eyA2iLN1u78ooBDpCyZJ/7BJV4SceGsu3oYX QJAlO/0p33HGJhw34vp/IQ5jYLa7xepA5yi/1va5STYLxVdBQ5mVWTiEEyhnBp/p zSkKBKqTz46v0nEjtO1N9LNoWaH4iuJNxi7fQvmT6W54e7r5ekiLw== X-ME-Sender: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id CEE317FA60; Thu, 9 Nov 2017 01:40:35 -0500 (EST) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH] ui/ncurses: Safely handle lost terminal control commands Date: Thu, 9 Nov 2017 17:40:29 +1100 Message-Id: <20171109064029.24372-1-sam@mendozajonas.com> X-Mailer: git-send-email 2.15.0 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.24 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" Normally terminal control commands are caught and handled before ncurses or Petitboot could see them. However several combinations of broken terminal emulators or console connections can cause these command sequences to be seen by Petitboot, usually resulting in Petitboot exiting due to the ESC character and then the rest printed to the console. Aside from confusing the user this can also cancel autoboot, so it's important we don't let these sequences go unnoticed if possible. In ui/ncurses/console-codes we add a state machine that recognises the syntax of these control/escape sequences and handles the lost characters. We don't try to emulate the functionality of these commands, instead just logging them for reference. Signed-off-by: Samuel Mendoza-Jonas Reviewed-by: Cyril Bur --- Makefile.am | 1 + test/ui/Makefile.am | 35 ++++++++++ test/ui/console-sequence.c | 99 ++++++++++++++++++++++++++ ui/ncurses/Makefile.am | 2 + ui/ncurses/console-codes.c | 170 +++++++++++++++++++++++++++++++++++++++++++++ ui/ncurses/console-codes.h | 20 ++++++ ui/ncurses/nc-cui.c | 25 +++++++ 7 files changed, 352 insertions(+) create mode 100644 test/ui/Makefile.am create mode 100644 test/ui/console-sequence.c create mode 100644 ui/ncurses/console-codes.c create mode 100644 ui/ncurses/console-codes.h diff --git a/Makefile.am b/Makefile.am index 15d561f..c0ad839 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,6 +55,7 @@ include test/Makefile.am include test/lib/Makefile.am include test/parser/Makefile.am include test/urls/Makefile.am +include test/ui/Makefile.am include ui/common/Makefile.am if WITH_NCURSES diff --git a/test/ui/Makefile.am b/test/ui/Makefile.am new file mode 100644 index 0000000..19c3637 --- /dev/null +++ b/test/ui/Makefile.am @@ -0,0 +1,35 @@ +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +ui_TESTS = \ + test/ui/console-sequence + +TESTS += $(ui_TESTS) +check_PROGRAMS += $(ui_TESTS) + +test_ui_console_sequence_SOURCES = \ + test/ui/console-sequence.c + +test_ui_console_sequence_CPPFLAGS = \ + -DPETITBOOT_TEST \ + -I$(top_srcdir)/lib + +test_ui_console_sequence_LDFLAGS = \ + $(core_lib) + + + +#$(ui_TESTS): LIBS += \ +# ui/ncurses/libpbnc.la \ +# $(core_lib) \ +# @MENU_LIB@ @FORM_LIB@ @CURSES_LIB@ diff --git a/test/ui/console-sequence.c b/test/ui/console-sequence.c new file mode 100644 index 0000000..0818ebc --- /dev/null +++ b/test/ui/console-sequence.c @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2017 IBM Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include +#include + +#include "talloc/talloc.h" + +#define ERR (-1) +#define pb_log(...) printf(__VA_ARGS__) +#define pb_debug(...) printf(__VA_ARGS__) + +/* + * Several example terminal commands, see: + * https://vt100.net/docs/vt100-ug/chapter3.html + */ +static char identify_rsp[] = {033, 0133, 077, 061, 073, 060, 0143, '\0'}; +static char decdwl[] = {033, 043, 066, '\0'}; +static char attrs[] = {033, 0133, 060, 073, 064, 073, 065, 0155, '\0'}; +static char cursor[] = {033, 070, '\0'}; +static char conf_test[] = {033, 0133, 062, 073, 061, 0171, '\0'}; +static char status[] = {033, 0133, 060, 0156, '\0'}; +static char erase_screen[] = {033, 0133, 062, 0112, '\0'}; +static char status_ok_rsp[] = {033, 0133, 064, 0156, '\0'}; +static char *ptr; + +static char getch(void) +{ + if (!ptr || *ptr == '\0') + return -ERR; + return *ptr++; +} + +#include "ui/ncurses/console-codes.c" + +int main(void) +{ + void *ctx; + char *seq; + + ctx = talloc_new(NULL); + + ptr = &identify_rsp[1]; + printf("Identity response\n"); + seq = handle_control_sequence(ctx, identify_rsp[0]); + assert(strncmp(seq, identify_rsp, strlen(identify_rsp)) == 0); + + printf("DECDWL\n"); + ptr = &decdwl[1]; + seq = handle_control_sequence(ctx, decdwl[0]); + assert(strncmp(seq, decdwl, strlen(decdwl)) == 0); + + printf("Attributes\n"); + ptr = &attrs[1]; + seq = handle_control_sequence(ctx, attrs[0]); + assert(strncmp(seq, attrs, strlen(attrs)) == 0); + + printf("Reset Cursor\n"); + ptr = &cursor[1]; + seq = handle_control_sequence(ctx, cursor[0]); + assert(strncmp(seq, cursor, strlen(cursor)) == 0); + + printf("Confidence Test\n"); + ptr = &conf_test[1]; + seq = handle_control_sequence(ctx, conf_test[0]); + assert(strncmp(seq, conf_test, strlen(conf_test)) == 0); + + printf("Status\n"); + ptr = &status[1]; + seq = handle_control_sequence(ctx, status[0]); + assert(strncmp(seq, status, strlen(status)) == 0); + + printf("Erase Screen\n"); + ptr = &erase_screen[1]; + seq = handle_control_sequence(ctx, erase_screen[0]); + assert(strncmp(seq, erase_screen, strlen(erase_screen)) == 0); + + printf("Status Response\n"); + ptr = &status_ok_rsp[1]; + seq = handle_control_sequence(ctx, status_ok_rsp[0]); + assert(strncmp(seq, status_ok_rsp, strlen(status_ok_rsp)) == 0); + + talloc_free(ctx); + return EXIT_SUCCESS; +} diff --git a/ui/ncurses/Makefile.am b/ui/ncurses/Makefile.am index 40e11b8..3bc5254 100644 --- a/ui/ncurses/Makefile.am +++ b/ui/ncurses/Makefile.am @@ -24,6 +24,8 @@ ui_ncurses_libpbnc_la_SOURCES = \ ui/ncurses/nc-config.c \ ui/ncurses/nc-config.h \ ui/ncurses/nc-config-help.c \ + ui/ncurses/console-codes.c \ + ui/ncurses/console-codes.h \ ui/ncurses/nc-cui.c \ ui/ncurses/nc-cui.h \ ui/ncurses/nc-cui-help.c \ diff --git a/ui/ncurses/console-codes.c b/ui/ncurses/console-codes.c new file mode 100644 index 0000000..71d24ed --- /dev/null +++ b/ui/ncurses/console-codes.c @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2017 IBM Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include "talloc/talloc.h" + +#ifndef PETITBOOT_TEST +#include "log/log.h" +#include "nc-scr.h" +#endif + +#include "console-codes.h" + +#define CSI_CHAR '[' +#define INTER_CHAR_START 040 +#define INTER_CHAR_END 057 +#define ESC_SEQUENCE_FINAL_START 060 +#define ESC_SEQUENCE_FINAL_END 0176 +#define CTRL_SEQUENCE_FINAL_START 0100 +#define CTRL_SEQUENCE_FINAL_END 0176 +#define DEC_PARAMETER 077 + +enum console_sequence_state { + CONSOLE_STATE_START, + CONSOLE_STATE_ESC_SEQ, + CONSOLE_STATE_CTRL_SEQ_START, + CONSOLE_STATE_CTRL_SEQ, + CONSOLE_STATE_DONE, + CONSOLE_STATE_CONFUSED, +}; + +static inline bool is_intermediate(char c) +{ + return c > INTER_CHAR_START && c < INTER_CHAR_END; +} + +static inline bool is_parameter(char c) +{ + return (c >= 060 && c <= 071) || c == 073; +} + +static inline bool is_escape_final(char c) +{ + return c >= ESC_SEQUENCE_FINAL_START && c < ESC_SEQUENCE_FINAL_END; +} + +static inline bool is_control_final(char c) +{ + return c >= CTRL_SEQUENCE_FINAL_START && c <= CTRL_SEQUENCE_FINAL_END; +} + +/* + * Catch terminal control sequences that have accidentally been sent to + * Petitboot. These are of the form + * ESC I .. I F + * where I is an Intermediate Character and F is a Final Character, eg: + * ESC ^ [ ? 1 ; 0 c + * or ESC # 6 + * + * This is based off the definitions provided by + * https://vt100.net/docs/vt100-ug/contents.html + */ +char *handle_control_sequence(void *ctx, char start) +{ + enum console_sequence_state state = CONSOLE_STATE_START; + bool in_sequence = true; + signed char c; + char *seq; + + seq = talloc_asprintf(ctx, "%c", start); + + while (in_sequence) { + switch (state) { + case CONSOLE_STATE_START: + c = getch(); + if (c == CSI_CHAR) + state = CONSOLE_STATE_CTRL_SEQ_START; + else if (is_intermediate(c)) + state = CONSOLE_STATE_ESC_SEQ; + else if (is_escape_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected start: \\x%x\n", c); + state = CONSOLE_STATE_CONFUSED; + } + if (c != ERR) + seq = talloc_asprintf_append(seq, "%c", c); + break; + case CONSOLE_STATE_ESC_SEQ: + c = getch(); + if (is_intermediate(c)) + state = CONSOLE_STATE_ESC_SEQ; + else if (is_escape_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected character after intermediate: \\x%x\n", + c); + state = CONSOLE_STATE_CONFUSED; + } + if (c != ERR) + seq = talloc_asprintf_append(seq, "%c", c); + break; + case CONSOLE_STATE_CTRL_SEQ_START: + c= getch(); + if (is_intermediate(c) || is_parameter(c) || + c == DEC_PARAMETER) + state = CONSOLE_STATE_CTRL_SEQ; + else if (is_control_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected character in param string: \\x%x\n", + c); + state = CONSOLE_STATE_CONFUSED; + } + if (c != ERR) + seq = talloc_asprintf_append(seq, "%c", c); + break; + case CONSOLE_STATE_CTRL_SEQ: + c = getch(); + if (is_intermediate(c) || is_parameter(c)) + state = CONSOLE_STATE_CTRL_SEQ; + else if (is_control_final(c)) + state = CONSOLE_STATE_DONE; + else if (c != ERR) { + /* wait on c == ERR */ + pb_debug("Unexpected character in param string: \\x%x\n", + c); + state = CONSOLE_STATE_CONFUSED; + } + if (c != ERR) + seq = talloc_asprintf_append(seq, "%c", c); + break; + case CONSOLE_STATE_DONE: + in_sequence = false; + break; + case CONSOLE_STATE_CONFUSED: + /* fall-through */ + default: + pb_debug("We got lost interpreting a control sequence!\n"); + seq = talloc_asprintf_append(seq, "..."); + in_sequence = false; + break; + }; + } + + return seq; +} diff --git a/ui/ncurses/console-codes.h b/ui/ncurses/console-codes.h new file mode 100644 index 0000000..6de54a7 --- /dev/null +++ b/ui/ncurses/console-codes.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2017 IBM Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#if !defined(_PB_UI_CONSOLE_CODES) +#define _PB_UI_CONSOLE_CODES + +char *handle_control_sequence(void *ctx, char start); + +#endif diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 6ced24c..44cce44 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -21,6 +21,7 @@ #endif #include +#include #include #include #include @@ -45,6 +46,7 @@ #include "nc-statuslog.h" #include "nc-subset.h" #include "nc-plugin.h" +#include "console-codes.h" extern const struct help_text main_menu_help_text; extern const struct help_text plugin_menu_help_text; @@ -524,6 +526,9 @@ static bool process_global_keys(struct cui *cui, int key) static int cui_process_key(void *arg) { struct cui *cui = cui_from_arg(arg); + unsigned int i; + char *sequence; + int grab; assert(cui->current); @@ -535,6 +540,26 @@ static int cui_process_key(void *arg) if (c == ERR) break; + if (c == 27) { + /* + * If this is a console code sequence try to parse it + * and don't treat this as a key press. + */ + grab = getch(); + if (grab != ERR && grab != 27) { + ungetch(grab); + pb_debug("%s: Caught unhandled command sequence\n", + __func__); + sequence = handle_control_sequence(cui, c); + pb_debug("Caught sequence (%zu): ", + strlen(sequence)); + for (i = 0; i < strlen(sequence); i++) + pb_debug("%x", sequence[i]); + pb_debug("%s\n", sequence ? "" : "(none)"); + continue; + } + } + if (!cui->has_input) { cui->has_input = true; if (cui->client) {