From patchwork Tue Jun 13 17:21:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Sekletar X-Patchwork-Id: 13279052 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4223EEB64DB for ; Tue, 13 Jun 2023 17:22:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239238AbjFMRWb (ORCPT ); Tue, 13 Jun 2023 13:22:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238720AbjFMRW1 (ORCPT ); Tue, 13 Jun 2023 13:22:27 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8620210FE for ; Tue, 13 Jun 2023 10:21:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686676896; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BjgBOJODoG0+DT3CMIocMTEhThgFSzfN+D2mFGpmXp4=; b=iJ3/4j4K/MYxTwnwNfSqGjgH8ZMTu0QRh6XutBZ8jnUMAaJOZ7/JterqybZuCgIKTC6HK6 EUPmJxl6H+7gWSFEeTu5eOaybQb2BsfaDodxggR8arZHXtqGYnFbbf5vtxpDrq+ayIWVJ+ UiwkJF6w496PcbJBae0FUHsI3UvCjtk= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-386-KrjrNbCcOd6e_puJhReirg-1; Tue, 13 Jun 2023 13:21:35 -0400 X-MC-Unique: KrjrNbCcOd6e_puJhReirg-1 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-51495d51e0fso4433648a12.2 for ; Tue, 13 Jun 2023 10:21:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686676893; x=1689268893; 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=BjgBOJODoG0+DT3CMIocMTEhThgFSzfN+D2mFGpmXp4=; b=c14Jmk2pjOtddIdG7fLuC7LORUK/mfnEYHZmwD08gnhQ7ANfNG7D+rx7HCgntuQaxz U3R93fhVyYnhWbreii1cGRPdMuCig+iOEmpjR4sfEoJxCeI/C9JwU9QL8pbSl+853nHk xqsivxRwQ0cQ8LxUJOb7BhYTKlvtQ7b4NCCZCoHK8je/iDaQKgSHtNfddSC3T4Dt5iKx 0zh66JV7ZSIfWtj8nS/LLfIOFuK0YGocxf3RTvYpisclUBkbDyICeYlObXY4f0sidk0V JIBfySWrUiRxkojOYYg4Fvi2MAamDO527lvNu5zHH9MLLsv091n5nLbva14APopd6RuU x/Tg== X-Gm-Message-State: AC+VfDxBz3zrdoRJirn9BNVI0armf6WbxO/DZBJ5dLQ2T2c520ywqAfh NkImkecn6p8qE0/6wpKVs7L/Aev5YtYOBsqcTom7oBM4jwnFYOlyHk0kNpJk9o5VZFlytSKxdrz +8E39wUGoSHJINKFwFGn12d756ASeE7yddHbA X-Received: by 2002:aa7:d7d8:0:b0:518:4abe:965c with SMTP id e24-20020aa7d7d8000000b005184abe965cmr2963454eds.4.1686676892729; Tue, 13 Jun 2023 10:21:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7UMkHCOARQuHHToWZY8W7C0GC3djyS9MFRrH1Pb+3d4ezP73Vrq+11XEmagjErFGJsqxgEvA== X-Received: by 2002:aa7:d7d8:0:b0:518:4abe:965c with SMTP id e24-20020aa7d7d8000000b005184abe965cmr2963440eds.4.1686676892332; Tue, 13 Jun 2023 10:21:32 -0700 (PDT) Received: from localhost.localdomain ([46.33.96.32]) by smtp.gmail.com with ESMTPSA id g16-20020aa7d1d0000000b0050bc4600d38sm6697705edp.79.2023.06.13.10.21.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 13 Jun 2023 10:21:31 -0700 (PDT) From: Michal Sekletar To: jirislaby@kernel.org Cc: arozansk@redhat.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, msekleta@redhat.com, shuah@kernel.org Subject: [PATCH v3 1/2] tty: tty_io: update timestamps on all device nodes Date: Tue, 13 Jun 2023 19:21:06 +0200 Message-Id: <20230613172107.78138-1-msekleta@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <2023061359-document-armband-d67d@gregkh> References: <2023061359-document-armband-d67d@gregkh> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org User space applications watch for timestamp changes on character device files in order to determine idle time of a given terminal session. For example, "w" program uses this information to populate the IDLE column of its output [1]. Similarly, systemd-logind has optional feature where it uses atime of the tty character device to determine if there was activity on the terminal associated with the logind's session object. If there was no activity for a configured period of time then logind will terminate such session [2]. Now, usually (e.g. bash running on the terminal) the use of the terminal will update timestamps (atime and mtime) on the corresponding terminal character device. However, if access to the terminal, e.g. /dev/pts/0, is performed through magic character device /dev/tty then such access obviously changes the state of the terminal, however timestamps on the device that correspond to the terminal (/dev/pts/0) are not updated. This patch makes sure that we update timestamps on *all* character devices that correspond to the given tty, because outside observers (w, systemd-logind) are maybe checking these timestamps. Obviously, they can not check timestamps on /dev/tty as that has per-process meaning. [1] https://gitlab.com/procps-ng/procps/-/blob/v4.0.0/w.c#L286 [2] https://github.com/systemd/systemd/blob/v252/NEWS#L477 Signed-off-by: Michal Sekletar --- v2 -> v3: Added more ellaborate commit message for newly introduced kselftest drivers/tty/tty_io.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index c84be40fb8df..a505d2c49110 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -101,6 +101,7 @@ #include #include #include +#include #include #include @@ -811,18 +812,26 @@ void start_tty(struct tty_struct *tty) } EXPORT_SYMBOL(start_tty); -static void tty_update_time(struct timespec64 *time) +static void tty_update_time(struct tty_struct *tty, bool mtime) { time64_t sec = ktime_get_real_seconds(); + struct tty_file_private *priv; - /* - * We only care if the two values differ in anything other than the - * lower three bits (i.e every 8 seconds). If so, then we can update - * the time of the tty device, otherwise it could be construded as a - * security leak to let userspace know the exact timing of the tty. - */ - if ((sec ^ time->tv_sec) & ~7) - time->tv_sec = sec; + spin_lock(&tty->files_lock); + list_for_each_entry(priv, &tty->tty_files, list) { + struct inode *inode = file_inode(priv->file); + struct timespec64 *time = mtime ? &inode->i_mtime : &inode->i_atime; + + /* + * We only care if the two values differ in anything other than the + * lower three bits (i.e every 8 seconds). If so, then we can update + * the time of the tty device, otherwise it could be construded as a + * security leak to let userspace know the exact timing of the tty. + */ + if ((sec ^ time->tv_sec) & ~7) + time->tv_sec = sec; + } + spin_unlock(&tty->files_lock); } /* @@ -928,7 +937,7 @@ static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to) tty_ldisc_deref(ld); if (i > 0) - tty_update_time(&inode->i_atime); + tty_update_time(tty, false); return i; } @@ -1036,7 +1045,7 @@ static inline ssize_t do_tty_write( cond_resched(); } if (written) { - tty_update_time(&file_inode(file)->i_mtime); + tty_update_time(tty, true); ret = written; } out: From patchwork Tue Jun 13 17:21:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Sekletar X-Patchwork-Id: 13279105 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E166EB64D0 for ; Tue, 13 Jun 2023 17:25:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229492AbjFMRZz (ORCPT ); Tue, 13 Jun 2023 13:25:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240716AbjFMRZO (ORCPT ); Tue, 13 Jun 2023 13:25:14 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0FE21FC6 for ; Tue, 13 Jun 2023 10:23:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686676953; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sibWrZTthPa1bJP5sLmxvxVXAGcmZF28m2PyeMA6jMI=; b=KR1c+20pAC+2PBmK+H0YXNH9UD7kKLjokdIO0s6rCcA6gNRvxA3OHu2R989PKD2LruCyVy ckM9S5jBkUqelzPkUP2JckPwx8GpAngBRk10dt3tT3PX9aEUGRLg0OiG6EYau79NSak9yQ 9U5XvA+2sHWuUc0e9TnnzFiSoY19CQ4= Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-338-Mj2q49PRNSKIFmQOc8Mh6A-1; Tue, 13 Jun 2023 13:22:31 -0400 X-MC-Unique: Mj2q49PRNSKIFmQOc8Mh6A-1 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-4ec790b902bso771173e87.1 for ; Tue, 13 Jun 2023 10:22:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686676949; x=1689268949; 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=sibWrZTthPa1bJP5sLmxvxVXAGcmZF28m2PyeMA6jMI=; b=XpBJqrGB/Uh5Hpsf2Y6KCDhRhO3m0Om3Hp9aQtb4YH10A8iDlADRUZ5zcJyPvFuR/k QytPm0rakID6OKjJp/ei2chRJgEYxd5pXFOsoGOF7cDxisDQHSRb5Ooe60TJk3W99/Zh bLnqaAxOx/vm7NjHWLPcQcxilJfBkW/KZEa4+OgH4Tsftu/dgJBWMgyUJotS7z3QH8mO yqiDMTCu6kfjV7lhn6awUT9DcZQvCI9xxDxryFp0iybhyOgYV+63ZD+/lmC2gJWYDUXd 88W3YnBX4pMqUq+x/HBtwP0cOcr8vhDF6lazM6PQnAVf8V4pRhPqjRsp4oAngjjG53MO 6uuQ== X-Gm-Message-State: AC+VfDzk15o9rygFlv+m1wQ4e6mTd9vLvg/n70BjCZoHKrFKjHOmS3UX 2cHSSXTcmQKUD/j8wbofyqaelwFhINF3KD5k652ZQNZC4Sk7GNh4UMxCtWuBw5nVDUtiG/e69gG yWFE+WzJI8Nld2IPom50bNc2IZxSR X-Received: by 2002:a2e:3602:0:b0:2b1:ec1a:88bc with SMTP id d2-20020a2e3602000000b002b1ec1a88bcmr5584358lja.43.1686676948683; Tue, 13 Jun 2023 10:22:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4diH5HAIVXJVx0kiLZAH9mvuOYHkDg0sAOhTLmoatgX3zOqlxnpIQyTeT7f6Z9ryPifSG4VA== X-Received: by 2002:a2e:3602:0:b0:2b1:ec1a:88bc with SMTP id d2-20020a2e3602000000b002b1ec1a88bcmr5584334lja.43.1686676948123; Tue, 13 Jun 2023 10:22:28 -0700 (PDT) Received: from localhost.localdomain ([46.33.96.32]) by smtp.gmail.com with ESMTPSA id g16-20020aa7d1d0000000b0050bc4600d38sm6697705edp.79.2023.06.13.10.22.27 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 13 Jun 2023 10:22:27 -0700 (PDT) From: Michal Sekletar To: jirislaby@kernel.org Cc: arozansk@redhat.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, msekleta@redhat.com, shuah@kernel.org Subject: [PATCH v3 2/2] selftests: tty: add selftest for tty timestamp updates Date: Tue, 13 Jun 2023 19:21:08 +0200 Message-Id: <20230613172107.78138-2-msekleta@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230613172107.78138-1-msekleta@redhat.com> References: <2023061359-document-armband-d67d@gregkh> <20230613172107.78138-1-msekleta@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add new test case which checks that timestamp updates on actual terminal character device (e.g. /dev/pts/0) happen even if the terminal is accessed via magic /dev/tty file. Signed-off-by: Michal Sekletar --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/tty/.gitignore | 2 + tools/testing/selftests/tty/Makefile | 5 ++ .../testing/selftests/tty/tty_tstamp_update.c | 88 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 tools/testing/selftests/tty/.gitignore create mode 100644 tools/testing/selftests/tty/Makefile create mode 100644 tools/testing/selftests/tty/tty_tstamp_update.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 90a62cf75008..862f5f9a76a0 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -86,6 +86,7 @@ TARGETS += timers endif TARGETS += tmpfs TARGETS += tpm2 +TARGETS += tty TARGETS += user TARGETS += vDSO TARGETS += mm diff --git a/tools/testing/selftests/tty/.gitignore b/tools/testing/selftests/tty/.gitignore new file mode 100644 index 000000000000..fe70462a4aad --- /dev/null +++ b/tools/testing/selftests/tty/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +tty_tstamp_update diff --git a/tools/testing/selftests/tty/Makefile b/tools/testing/selftests/tty/Makefile new file mode 100644 index 000000000000..50d7027b2ae3 --- /dev/null +++ b/tools/testing/selftests/tty/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS = -O2 -Wall +TEST_GEN_PROGS := tty_tstamp_update + +include ../lib.mk diff --git a/tools/testing/selftests/tty/tty_tstamp_update.c b/tools/testing/selftests/tty/tty_tstamp_update.c new file mode 100644 index 000000000000..0ee97943dccc --- /dev/null +++ b/tools/testing/selftests/tty/tty_tstamp_update.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#define MIN_TTY_PATH_LEN 8 + +static bool tty_valid(char *tty) +{ + if (strlen(tty) < MIN_TTY_PATH_LEN) + return false; + + if (strncmp(tty, "/dev/tty", MIN_TTY_PATH_LEN) == 0 || + strncmp(tty, "/dev/pts", MIN_TTY_PATH_LEN) == 0) + return true; + + return false; +} + +static int write_dev_tty(void) +{ + FILE *f; + int r = 0; + + f = fopen("/dev/tty", "r+"); + if (!f) + return -errno; + + r = fprintf(f, "hello, world!\n"); + if (r != strlen("hello, world!\n")) + r = -EIO; + + fclose(f); + return r; +} + +int main(int argc, char **argv) +{ + int r; + char tty[PATH_MAX] = {}; + struct stat st1, st2; + + ksft_print_header(); + ksft_set_plan(1); + + r = readlink("/proc/self/fd/0", tty, PATH_MAX); + if (r < 0) + ksft_exit_fail_msg("readlink on /proc/self/fd/0 failed: %m\n"); + + if (!tty_valid(tty)) + ksft_exit_skip("invalid tty path '%s'\n", tty); + + r = stat(tty, &st1); + if (r < 0) + ksft_exit_fail_msg("stat failed on tty path '%s': %m\n", tty); + + /* We need to wait at least 8 seconds in order to observe timestamp change */ + /* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fbf47635315ab308c9b58a1ea0906e711a9228de */ + sleep(10); + + r = write_dev_tty(); + if (r < 0) + ksft_exit_fail_msg("failed to write to /dev/tty: %s\n", + strerror(-r)); + + r = stat(tty, &st2); + if (r < 0) + ksft_exit_fail_msg("stat failed on tty path '%s': %m\n", tty); + + /* We wrote to the terminal so timestamps should have been updated */ + if (st1.st_atim.tv_sec == st2.st_atim.tv_sec && + st1.st_mtim.tv_sec == st2.st_mtim.tv_sec) { + ksft_test_result_fail("tty timestamps not updated\n"); + ksft_exit_fail(); + } + + ksft_test_result_pass( + "timestamps of terminal '%s' updated after write to /dev/tty\n", tty); + return EXIT_SUCCESS; +}