Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oz3fv-0003TU-Fg for mharc-grub-devel@gnu.org; Sat, 26 Nov 2022 17:29:31 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oz3a7-0002b6-4X for grub-devel@gnu.org; Sat, 26 Nov 2022 17:23:31 -0500 Received: from mail-qk1-x734.google.com ([2607:f8b0:4864:20::734]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oz3a5-0004oj-Bf for grub-devel@gnu.org; Sat, 26 Nov 2022 17:23:30 -0500 Received: by mail-qk1-x734.google.com with SMTP id i9so4843498qkl.5 for ; Sat, 26 Nov 2022 14:23:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7Xz52418OdRnw9GvHAsQFSLoRMblouGBu/lpqEIE7T4=; b=UNudowjN4LLPX5xYNCl5rFxyJhXIDq9C3r8q1Z99r9Pixsnn4/aXJvgPJm/NbJPCgZ uNpHxjXtuMU3vHGiVLPV7k/dv0+OzUfE/S5QZm67S/rASlmOmK1vd1uG4GLFCkt6u/fL A2SusJBfu6jLjKws++mIssAYmfHBOGFymU/hvhma7lZPhuNWtFUe6t44/7tsKJsMY62/ SSfl7yPg/jTNdKio0D/cRtpYpJwgsoK+RK/AmUIHOWn/gUdF1nm+EvA1Bqa+99iIEV9p TliEOIKUih75X99pmL4I0Csxw2VgZk+iQanpl98+PqpL3u16IeNm0wKMVSqYH64VrBVv 8m8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7Xz52418OdRnw9GvHAsQFSLoRMblouGBu/lpqEIE7T4=; b=tB2sT/WRO/GSoDqlEYfzdK4hQy3bdxfA5d8mXFJMlFCULWpnd1YTTrGB68glwVxX0H vlt+DC7qV/YPCYrUOl+9ctWyyvhTSRFiR1btk3ejDWvYvtvFZKyWKb4Z2DIlc+nOYXjL ht/8QW5ps1p7KHnVPIrTd9Dfp4R/oyb3dmrrz8qhEQhUAazOuO5OnAJHvArbFH95RI6W NOOxLS+C/pkrC7c0flNiG0b4RlvAQOYNGnSwW/IyyzhUPUUJ/AkBHUUxQQlOHHcQf2hc r+e1ab7q+LMi1t6p049y/fqLbn4BPVmAh8HaHM3g9lCOKcwAJYa3vaz6qcI5Umh7K3Rk sS7g== X-Gm-Message-State: ANoB5pkLXgd//hyKiJolhelBZdu+AUlZE1hRMzsq/sT2S3bQ/Q05LG2r YzAj3kAJSvWFUxBa2ScHK/v2vNxWGuE= X-Google-Smtp-Source: AA0mqf6R/Mi8D6LxLstTc1Hs6ZDVx1UdodWKWhi4q91ji9y+zTj3coE3A0XhFiXt2a3DBVS3YtoO4w== X-Received: by 2002:a37:34a:0:b0:6ea:8eb4:3898 with SMTP id 71-20020a37034a000000b006ea8eb43898mr23715109qkd.588.1669501408264; Sat, 26 Nov 2022 14:23:28 -0800 (PST) Received: from localhost.localdomain ([2601:195:c000:2821:5ced:866d:36e0:6fcd]) by smtp.gmail.com with ESMTPSA id y18-20020a05620a25d200b006eeb3165565sm5314286qko.80.2022.11.26.14.23.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Nov 2022 14:23:28 -0800 (PST) From: Ryan Cohen To: grub-devel@gnu.org Cc: Ryan Cohen Subject: [PATCH 2/2] cmdline: Fix two related integer underflows Date: Sat, 26 Nov 2022 17:22:52 -0500 Message-Id: <20221126222252.764937-3-rcohenprogramming@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221126222252.764937-1-rcohenprogramming@gmail.com> References: <20221126222252.764937-1-rcohenprogramming@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::734; envelope-from=rcohenprogramming@gmail.com; helo=mail-qk1-x734.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 26 Nov 2022 17:29:29 -0500 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Nov 2022 22:23:31 -0000 An unchecked decrement operation in cl_print() would cause a few integers to underflow. Where an output terminal's state is stored in cl_term, the values cl_term->ystart and cl_term->pos.y both underflow. This can be replicated with the following steps: 1. Get to the GRUB command line 2. Hold down the `d` key (or any key that enters a visible character) until it fills the entire row 3. Press `HOME` and then press `CTRL-k`. This will clear every character entered in step 2 4. Continuously press `CTRL-y` until the terminal scrolls the original prompt ("grub> ") passed the terminal's top row. Now, no prompt should be visible. This step causes cl_term->ystart to underflow 5. Press `HOME` and then `d` (or any visible character). This can have different visual effects for different systems, but it will always cause cl_term->pos.y to underflow On BIOS systems, these underflows cause the output terminal to completely stop displaying anything. Characters can still be entered and commands can be run, but nothing will display on the terminal. From here, you can only get the display working by running a command to switch the current output terminal to a different type: terminal_output On UEFI systems, these replication steps do not break the output terminal. Until you press `ENTER`, the cursor stops responding to input, but you can press `ENTER` after step 5 and the command line will work properly again. This patch is mostly important for BIOS systems where the output terminal is rendered unusable after the underflows occur. This patch adds two checks, one for each variable. It ensures that cl_term->ystart does not decrement passed 0. It also ensures that cl_term->pos.y does not get set passed the terminal's bottom row. When the previously listed replication steps are followed with this patch, the terminal's cursor will be set to the top row and the command line is still usable, even on BIOS systems. Signed-off-by: Ryan Cohen --- grub-core/normal/cmdline.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/grub-core/normal/cmdline.c b/grub-core/normal/cmdline.c index 61f098244..781f6bc6f 100644 --- a/grub-core/normal/cmdline.c +++ b/grub-core/normal/cmdline.c @@ -219,6 +219,8 @@ cl_set_pos (struct cmdline_term *cl_term, grub_size_t lpos) cl_term->pos.x = (cl_term->prompt_len + lpos) % cl_term->width; cl_term->pos.y = cl_term->ystart + (cl_term->prompt_len + lpos) / cl_term->width; + if (cl_term->pos.y >= cl_term->height) + cl_term->pos.y = cl_term->height - 1; grub_term_gotoxy (cl_term->term, cl_term->pos); } @@ -248,7 +250,10 @@ cl_print (struct cmdline_term *cl_term, grub_uint32_t c, { cl_term->pos.x = 0; if (cl_term->pos.y >= (unsigned) (cl_term->height - 1)) - cl_term->ystart--; + { + if (cl_term->ystart > 0) + cl_term->ystart--; + } else cl_term->pos.y++; grub_putcode ('\n', cl_term->term); -- 2.38.1