Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp80988lqh; Mon, 6 May 2024 11:55:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV5RHZotOMVFp4lcq2LB+JAsTyttwYNdJLntSdwd5ZYdI39TS/iDKFwnrDzJLtgD705XB3gdv9Zm7tHA9WvCBHf6cGUIC5Q3F1zzIhCdw== X-Google-Smtp-Source: AGHT+IHhwFnLgo8H/Nb3hNm96Q7BPTDLh6EyuMqxSpL1f6zfeoIhX/vHqRx01dL3q0bphFH/7UV6 X-Received: by 2002:a17:902:cf01:b0:1e5:560d:8519 with SMTP id i1-20020a170902cf0100b001e5560d8519mr893150plg.0.1715021730270; Mon, 06 May 2024 11:55:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715021729; cv=pass; d=google.com; s=arc-20160816; b=itIzGyNmccZ6Imh5vwBycP9cqRDtPgjE1maSUTv2vaFIuUrPiWD6Ip8MQyCGtcW0MM SLuYZTpff21kFRUZVBP8z1w1OT2gZGM8xZ2AiSGuuUNNS8fKcIk1a4jxLn7ScEqm/4ZM tsBKb8Hh2B+cWQ8xoOxJSV+Ng8b/Ur1eKScMV4hM1sXJs5eTweaIS4nV8dWaqghcrtG+ ryvDHnJPsQ1ZIMcd4NDCU16sWa6JrAta2f4Ve0Jn12JZOKc2R6HpmGc8+8G+HJ6/k2Ez t1rpyxVFo//bNWYki5OYZBI5UkPKUVwN3a5I/9uLrOkR0FYJGofjv4HuI9s9UcM9SMPD +j1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=zFb+/j2lVI6dVZBxEog24QJX60eA/cRB1E70DgXCvn4=; fh=buuWDZRzTMJw9UlEKtKj5NgHIBfKNblfK8W044AXUAk=; b=SJsiXj4r7WczS3QZ76cYPa53uicGpEzsY1TBRsOfM5M/knnSJzRoYVZ/cLtlLMgrEP EORrGkGN93cNrlVMX3nonPKihiplVe4wfb8AEVl0jhq7zUdnXw5OiQYpJtSdpOXn79u/ C99YdY0FtV7kqXA7ltu7tZ4RMOvYp5ZO07grDeSkPmloL0iOs+yMyciM3K4E5nNmb49a gaQr2H2OndvK3jX4LeT2AA/+jXgwxUOfMyiIALdCooBkP9/ynJbe80yz5b3tXOjv6xu4 CHPGNtwxjeTN0U98POLbebBxa79gXvOtD0VHWxNb4seYeu1xsqSNLA57sA79gXYz3xCW nfvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pDJxIxys; arc=pass (i=1 spf=pass spfdomain=flex--justinstitt.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-170312-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170312-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h1-20020a170902f54100b001e09468e715si9009237plf.92.2024.05.06.11.55.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 11:55:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170312-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pDJxIxys; arc=pass (i=1 spf=pass spfdomain=flex--justinstitt.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-170312-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170312-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 3D71F283BC9 for ; Mon, 6 May 2024 18:55:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D3FA158DB6; Mon, 6 May 2024 18:55:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pDJxIxys" Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21E72158D82 for ; Mon, 6 May 2024 18:55:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715021722; cv=none; b=HUgv3ir1h5Z56Cqa1YtCOfCIPT5tAfQ00GLHPghBI8H4+xc1C5KMBvdWSU75PViBlyEoyBKZiYJEB8VMeJfkAe4wxJVDhJH+Vwlc9/VKWw+pA2/8XfAqKAOeXIMrYsTLRNHpPFOgcz8osFTQ0kFOgOOiV9/TPRyrULTiYTAXZbQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715021722; c=relaxed/simple; bh=eQ59GvPtmD0StGAqEgoE1xzghWc3vAliYszQLIkM+hw=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=DJuKBSzD2/dKnKWhF7PmcV+8mN++IdWdv5i6BZxsNIoUvm8Q6YVTUe7DcqpfmrZAslbjGBfU0wYWZeS+CNhBxgADslMs+dA9oOPf7gQJpLc6kIBy5RfXtQ4Qd3HQYR5TwZsRZmHydTenuxmi872Zwm+wp4TX7P21/rBqFeccTL8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pDJxIxys; arc=none smtp.client-ip=209.85.160.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--justinstitt.bounces.google.com Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-23fc26ac65fso1138012fac.0 for ; Mon, 06 May 2024 11:55:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715021720; x=1715626520; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=zFb+/j2lVI6dVZBxEog24QJX60eA/cRB1E70DgXCvn4=; b=pDJxIxys8674EriR8t3UyYDgV5jqZlLwJaBZQsmQjp7lqwHPi7xeeQz7j5/jwu8Edk plyNvcXN93XfADxGLiPFCS8JuBrWMM7IYi71UvIt0W0aA5qTZyUo3ToVQCKNG+a9yUkT g9Bx58UOXNThpIkuqwkhmoym29CTDdjE0Q481sSpn5cRbFLs21dw15pSNBo+FlzhA2c+ lGdiPp/q3KzXA6WGRjqxjyXC3IUbIr6BaojIj62Yk3b4DSse+PLLTUJWbAwJNqLvHwmt D5AUVsgzZtlvgZBaZsV9LOWL+KDrIvv96GcKsKyn+Shn2sqt6SNrufbaVfNh3mY5mHtv Qm1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715021720; x=1715626520; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zFb+/j2lVI6dVZBxEog24QJX60eA/cRB1E70DgXCvn4=; b=sTOTwQiTqjV3LW1V6WAECw14Eyfevbye7ru7oEM0rk0gCoz4h0xch90OTe8jNzazOC 6Pj8+hGQMAOE7XF877gKAi+ebK9pSR9YcWFxjcPZrwXx9CnFqj9vLP//JpVl69JggfgO +nEzBPCtHFmHH73OZ0eGOL2tqwzKhY4ZcEw1TMuE/6gkIjfqQlDisIBSIQOD0icW5m7z v5/8q2kKtdzxrVVyRIEe0UARsvPtajJDMGB9TcTdU5v70ZJjWnekkiPKTfy9sKSFeScE hBflaNIFsCnyfovhiAVbzIlN6GLSyLBWF+soknPnCaVXXp7DjMhZwqYgZnW6Jzz3/+2r 7QOQ== X-Gm-Message-State: AOJu0Yx1vBX5vSyKqWeEyAIZIzJyHeB2uAn0nFpmzJIKc6SLMXiEizdt qK+ON+7FffN+iaNM66IOwxZKfFSkiPTZVLOti70MpszEsMw68vHWaaOmv4Vvbfl+DpoFF5QhpkY XkSIoSUZY0difYNLLg9CNrA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6870:808c:b0:222:1414:d29 with SMTP id q12-20020a056870808c00b0022214140d29mr38884oab.8.1715021720357; Mon, 06 May 2024 11:55:20 -0700 (PDT) Date: Mon, 06 May 2024 18:55:19 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAJYnOWYC/x2MQQqAMAwEvyI5G6hFBf2KeIhtqsGi0ooI4t8NH meZnQcyJ+EMffFA4kuy7JtCVRbgFtpmRvHKYI2tTWNanGpUB7NLe4wTuRU9x5PQh9AR6cC2BX0 fiYPcf3kY3/cDhjl3DWkAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1715021719; l=3808; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=eQ59GvPtmD0StGAqEgoE1xzghWc3vAliYszQLIkM+hw=; b=XVVKygfhbc/BJCZeqQmuwfYK+srJRDUiBofzc7JKHYMaXrvrFCIV5PoAd9QhGRY/XOgmGRvF0 xo/Ugu0sK3RAGr0ImfBUXBRnnyuGe13P17PZRP3P68ZL01qO4fSVZEy X-Mailer: b4 0.12.3 Message-ID: <20240506-b4-sio-scrollback-delta-v1-1-4164d162a2b8@google.com> Subject: [PATCH] tty: vt: saturate scrollback_delta to avoid overflow From: Justin Stitt To: Greg Kroah-Hartman , Jiri Slaby , Nathan Chancellor , Bill Wendling Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, llvm@lists.linux.dev, Justin Stitt Content-Type: text/plain; charset="utf-8" Using the signed overflow sanitizer with syzkaller produces this UBSAN report: [ 31.304043] ------------[ cut here ]------------ [ 31.304048] UBSAN: signed-integer-overflow in ../drivers/tty/vt/vt.c:309:19 [ 31.304055] -2147483648 + -1073741824 cannot be represented in type 'int' [ 31.304066] CPU: 1 PID: 3894 Comm: syz-executor Not tainted 6.8.0-rc2-00035-gb3ef86b5a957 #1 [ 31.304073] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 31.304077] Call Trace: [ 31.304080] [ 31.304083] dump_stack_lvl+0x93/0xd0 [ 31.304177] handle_overflow+0x171/0x1b0 [ 31.304186] scrollfront+0xcb/0xd0 [ 31.304196] tioclinux+0x3cc/0x450 [ 31.304205] tty_ioctl+0x7fc/0xc00 [ 31.304212] ? __pfx_tty_ioctl+0x10/0x10 [ 31.304219] __se_sys_ioctl+0xe0/0x140 [ 31.304228] do_syscall_64+0xd7/0x1b0 [ 31.304236] ? arch_exit_to_user_mode_prepare+0x11/0x60 [ 31.304244] entry_SYSCALL_64_after_hwframe+0x6f/0x77 [ 31.304254] RIP: 0033:0x7fc3902ae539 [ 31.304263] Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 f1 14 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 8 [ 31.304282] RSP: 002b:00007ffc8a457998 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 [ 31.304289] RAX: ffffffffffffffda RBX: 00007fc3903e2f80 RCX: 00007fc3902ae539 [ 31.304293] RDX: 0000000020000040 RSI: 000000000000541c RDI: 0000000000000003 [ 31.304297] RBP: 00007fc39030d496 R08: 0000000000000000 R09: 0000000000000000 [ 31.304300] R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 [ 31.304304] R13: 0000000000000800 R14: 00007fc3903e2f80 R15: 00007fc3903e2f80 [ 31.304310] [ 31.304371] ---[ end trace ]--- This is caused by the scrollback_delta overflowing. Historically, the signed integer overflow sanitizer did not work in the kernel due to its interaction with `-fwrapv` but this has since been changed [1] in the newest version of Clang; It being re-enabled in the kernel with Commit 557f8c582a9ba8ab ("ubsan: Reintroduce signed overflow sanitizer"). Note that it would be difficult to reproduce this bug in a non-fuzzing scenario as it requires inputting tons of scroll inputs via keyboard before the scheduled console callback has had a chance to update. Nonetheless, let's saturate scrollback_delta so it stays clamped to integer bounds without wrapping around. [1]: https://github.com/llvm/llvm-project/pull/82432 Closes: https://github.com/KSPP/linux/issues/351 Signed-off-by: Justin Stitt --- Note: I am using Kees' SIO tree as a base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=dev/v6.8-rc2/signed-overflow-sanitizer --- drivers/tty/vt/vt.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index 9b5b98dfc8b4..b4768336868e 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -308,7 +308,14 @@ static inline void scrolldelta(int lines) /* FIXME */ /* scrolldelta needs some kind of consistency lock, but the BKL was and still is not protecting versus the scheduled back end */ - scrollback_delta += lines; + + /* saturate scrollback_delta so that it never wraps around */ + if (lines > 0 && unlikely(INT_MAX - lines < scrollback_delta)) + scrollback_delta = INT_MAX; + else if (lines < 0 && unlikely(INT_MIN - lines > scrollback_delta)) + scrollback_delta = INT_MIN; + else + scrollback_delta += lines; schedule_console_callback(); } --- base-commit: 0106679839f7c69632b3b9833c3268c316c0a9fc change-id: 20240506-b4-sio-scrollback-delta-dff9aabace26 Best regards, -- Justin Stitt