From patchwork Fri Jun 23 14:43:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?G=C3=BCnther_Noack?= X-Patchwork-Id: 13290762 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 7F870C0015E for ; Fri, 23 Jun 2023 14:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232069AbjFWOod (ORCPT ); Fri, 23 Jun 2023 10:44:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231888AbjFWOoG (ORCPT ); Fri, 23 Jun 2023 10:44:06 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 200AE2711 for ; Fri, 23 Jun 2023 07:43:45 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5704e551e8bso10399187b3.3 for ; Fri, 23 Jun 2023 07:43:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687531425; x=1690123425; h=content-transfer-encoding:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:from:to:cc:subject:date :message-id:reply-to; bh=UzVf4AQ2Gj+vEw9k0OpeO1MnttP8JRzBxK8284Th/Bs=; b=Rz5mgT4t49UyyDXzm7g/diB2I5T3vJPW+XLqYYgq1VHa8Ft9nKOc7ri3YoHMXsYOhL F7kSW9wSNEWe8BEIleC+ZdwGAeE+zVvdOVXxl26FTioHggOFiFVSp396I8xJgO76VBB4 05knMdp8g3p2lxXlevvRuha8DaJ1mx8V+A9oIZh59e7rD3A9zyqy3fV4LWRRwTQLMkqX pOgPgJpVlDRYSGWWa18nAaZqY+sBmL283PQMvbQ04lQF3MZBu2CajuRmuHcpE36EjsZ4 e6b60gddjQv2U95AXgXS6kdGDz8yJh3PhYnsmmRjlKOtkWrsUWcIwb5AVTTSiC+0zrzN RtqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687531425; x=1690123425; h=content-transfer-encoding:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=UzVf4AQ2Gj+vEw9k0OpeO1MnttP8JRzBxK8284Th/Bs=; b=TJjdqDVL24uumGv+xRhpTbTtFN3EI5eTKRBSLY4VZv9UorMi8sFJdI5w28IIJHVZa5 M1bSy8PjDVwOZPTe+ZpuGdaYg9lihtrh8Wjk+WrmetB1m7ZQCSBOAdmaFvop/JRkqadu oTGTvNtA/s3/pXJZyW0s9c5m90CMFOcw5V1V4hLBhkEaucMtLOLsaY49z1Rmrlgyhs6B UM8frBYPWlOAGshdgW7ugNFtI2qXqtkAQGyERIo0kNg0X3bGB1kz4buBwma9c9ASgcyl J+v1lj/dqEAynK0oipigeEIJaOKFU5lDL+rqKWf1+J8yASoICWCmMI+LobXQwN6Rnppz 7jxA== X-Gm-Message-State: AC+VfDyHTM+2jFLqPsqew/5EjQzou4qoVXQjmMk7FtuzoZr/ydU3Z3y+ bTwNlMMFoDyrBLofzTaQlLksEKCVnGHq3nI4qCZPYgYUdWye5/0rOuHtH2hF/mMVGWsKM7UJAJo ssQvzm214HzIjW9yHG56DUV9P9MRKolXOsh5/6ELZubWGm1SVkru1MKOqdSRi8dZD3NblKabUgt PjQtLTpw== X-Google-Smtp-Source: ACHHUZ5L1Rbyg5OV+PIAv1YSaAvjSV37tCLvlzItTQR5LJBllU3DnqDdfuYMp/9i72ghGwmqJMPeNPRq05Y= X-Received: from sport.zrh.corp.google.com ([2a00:79e0:9d:4:8b55:dee0:6991:c318]) (user=gnoack job=sendgmr) by 2002:a81:ca0c:0:b0:559:e97a:cb21 with SMTP id p12-20020a81ca0c000000b00559e97acb21mr8800943ywi.9.1687531425115; Fri, 23 Jun 2023 07:43:45 -0700 (PDT) Date: Fri, 23 Jun 2023 16:43:26 +0200 In-Reply-To: <20230623144329.136541-1-gnoack@google.com> Message-Id: <20230623144329.136541-4-gnoack@google.com> Mime-Version: 1.0 References: <20230623144329.136541-1-gnoack@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 3/6] selftests/landlock: Test ioctl support From: " =?utf-8?q?G=C3=BCnther_Noack?= " To: linux-security-module@vger.kernel.org, " =?utf-8?q?Micka=C3=ABl_Sala?= =?utf-8?q?=C3=BCn?= " Cc: Jeff Xu , Jorge Lucangeli Obes , Allen Webb , Dmitry Torokhov , Paul Moore , Konstantin Meskhidze , linux-fsdevel@vger.kernel.org, " =?utf-8?q?G=C3=BCnther_Noack?= " Precedence: bulk List-ID: Exercise the use of Landlock's ioctl restriction: If ioctl is restricted, the use of ioctl fails with a freshly opened /dev/tty file. Signed-off-by: Günther Noack --- tools/testing/selftests/landlock/fs_test.c | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c index 09dd1eaac8a9..0f0899768fe7 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -3732,6 +3732,68 @@ TEST(memfd_ftruncate) ASSERT_EQ(0, close(fd)); } +/* + * Invokes ioctl(2) and returns its errno or 0. + * The provided fd needs to be a tty for this to work. + */ +static int test_tty_ioctl(int fd) +{ + struct winsize ws; + + if (ioctl(fd, TIOCGWINSZ, &ws) < 0) + return errno; + return 0; +} + +/* + * Attempt ioctl on /dev/tty0 and /dev/tty1, + * with file descriptors opened before and after landlocking. + */ +TEST_F_FORK(layout0, ioctl) +{ + const struct rule rules[] = { + { + .path = "/dev/tty1", + .access = LANDLOCK_ACCESS_FS_IOCTL, + }, + /* Implicitly: No ioctl access on /dev/tty0. */ + {}, + }; + const __u64 handled = LANDLOCK_ACCESS_FS_IOCTL; + int ruleset_fd; + int old_tty0_fd, tty0_fd, tty1_fd; + + old_tty0_fd = open("/dev/tty0", O_RDWR); + ASSERT_LE(0, old_tty0_fd); + + /* Checks that ioctl works before landlocking. */ + EXPECT_EQ(0, test_tty_ioctl(old_tty0_fd)); + + /* Enable Landlock. */ + ruleset_fd = create_ruleset(_metadata, handled, rules); + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + /* Checks that ioctl with existing FD works after landlocking. */ + EXPECT_EQ(0, test_tty_ioctl(old_tty0_fd)); + + /* Checks that same ioctl fails when file is opened after landlocking. */ + tty0_fd = open("/dev/tty0", O_RDWR); + ASSERT_LE(0, tty0_fd); + EXPECT_EQ(EACCES, test_tty_ioctl(tty0_fd)); + + /* Checks that same ioctl fails when file is opened after landlocking. */ + tty1_fd = open("/dev/tty1", O_RDWR); + ASSERT_LE(0, tty1_fd); + EXPECT_EQ(0, test_tty_ioctl(tty1_fd)); + + /* Close all TTY file descriptors. */ + ASSERT_EQ(0, close(old_tty0_fd)); + ASSERT_EQ(0, close(tty0_fd)); + ASSERT_EQ(0, close(tty1_fd)); +} + /* clang-format off */ FIXTURE(layout1_bind) {}; /* clang-format on */