From patchwork Thu Dec 9 22:15:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12695544 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2BAF6C433EF for ; Thu, 9 Dec 2021 22:24:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=I7tL2g9zjJ7dH81X3nK14dBc0ikgrRs4g1GqpbQOH/s=; b=Dpn/1FskfU3wm2ShroLLE9ayO9 574jKLnYaK3YNYDxUWHpiYBFdYc2ZCFoXChZ+gyAZbuoBMQKL4Ra1dhgbUHyUaD6Ef7RRorD4MTSk ZxhYjQnMqYNlfZqomdOzf+OsVD/UtOYpXdeyiLDr/KtfqxACnNhynRK/W/er5WLDYE9orWNPUVMN2 JGfSQicPfAgKioyLhRQr1j84qyv25V8u4szVz/p8clajaiZ70NrtrQ10qOdvbt3e0x/s65/eEqAI8 bhi6kte97uPLzMuRYidTsaux2UMo4ACFK1KaHbfuQRIZFpEwTq9LkuxLGkbkFXP+e8Lx2kIGe1qXj cH6oxNVg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvRmz-000EGn-OW; Thu, 09 Dec 2021 22:21:21 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mvRhz-000CJz-G4 for linux-arm-kernel@lists.infradead.org; Thu, 09 Dec 2021 22:16:13 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id l145-20020a25cc97000000b005c5d04a1d52so12960645ybf.23 for ; Thu, 09 Dec 2021 14:16:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=7s8lEM7vxE9oaPCZlcOUwoJ7eBiwu3eQIZufuhM0E/I=; b=ksiw6rsrwZIql9RdnW0zggcto4PhW/OFyE3GwNIVspYOeSFVcU4OkzOgvF2gkZxccX bAnBjk2/2N5TBjb6PPhpPTI5e3DYoUJD5tZwq0N5PSC/VkLZazfhOdl6845mojjGoYbF Buzlq6livqIu8JOBvfszFn7XyWPq/2p4YBpY951LGKJItM1FxAOT1mmHT1j27pWh888j dld8HWIzS5VL+QI7m+LZ0U0gM8P7ddbKo4Pvn11v8jO49iN2xZKt8n8dIF/5j87eios1 QnYYtIRfkgsBGqFgPTs8F95xXML5xU2T5M8ABRFTtl9jAyzWCGpszQkfWUK+FFOLIxSC MMcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=7s8lEM7vxE9oaPCZlcOUwoJ7eBiwu3eQIZufuhM0E/I=; b=b6HCmPoqxGu+V7CLIhZxn1BMmKA+UFLoGT1+R8TpS5C5zbreji9cqKutQDA7qEu9ut scFWBf2xpbiyhqLcQlUD/BuGUh9PryjHpbxdc54bYt+b7F9S4pN3iK9GntuTb5Zwi/O2 lL70l+tVM+haNVxbYNgD/8kGF0ejC414+ALh6l9mpMYK5RUmJII1jyaaT3bZGGMWM3+Z bjs6q3lVIBPUNuQOMhJ7b39RyLOs4og476Lye9Ql/O3NoY245Llav8cWtR9MaluNKf2o RsSUa2TwJ6hE4e1Mw2FeDdb4hh+5ObW9yI4anV1g+jxFkD2cWuw/0Ez87maEUUg1Wbwc 9fGw== X-Gm-Message-State: AOAM5309W2xNdmejUUYXJQdgi3D2ux/d6Bef9DoZ3uYrpWGCQvEuBJJd fjteU68JbuHNouw8qCE1UKWkiSk= X-Google-Smtp-Source: ABdhPJx6SHlSTXft/4uwAkd2t2Qz2vUlOSahwEB+bAnZk0choINS/4CYmxqXe6LtfY7i28pAboGp1Qc= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:f233:e324:8aa0:f65c]) (user=pcc job=sendgmr) by 2002:a05:6902:1107:: with SMTP id o7mr9802261ybu.120.1639088169471; Thu, 09 Dec 2021 14:16:09 -0800 (PST) Date: Thu, 9 Dec 2021 14:15:44 -0800 In-Reply-To: <20211209221545.2333249-1-pcc@google.com> Message-Id: <20211209221545.2333249-8-pcc@google.com> Mime-Version: 1.0 References: <20211209221545.2333249-1-pcc@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v4 7/7] selftests: test uaccess logging From: Peter Collingbourne To: Catalin Marinas , Will Deacon , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Thomas Gleixner , Andy Lutomirski , Kees Cook , Andrew Morton , Masahiro Yamada , Sami Tolvanen , YiFei Zhu , Mark Rutland , Frederic Weisbecker , Viresh Kumar , Andrey Konovalov , Peter Collingbourne , Gabriel Krisman Bertazi , Chris Hyser , Daniel Vetter , Chris Wilson , Arnd Bergmann , Dmitry Vyukov , Christian Brauner , "Eric W. Biederman" , Alexey Gladkov , Ran Xiaokai , David Hildenbrand , Xiaofeng Cao , Cyrill Gorcunov , Thomas Cedeno , Marco Elver , Alexander Potapenko Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Evgenii Stepanov X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211209_141611_608445_0C17EA67 X-CRM114-Status: GOOD ( 24.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a kselftest for the uaccess logging feature. Link: https://linux-review.googlesource.com/id/I39e1707fb8aef53747c42bd55b46ecaa67205199 Signed-off-by: Peter Collingbourne --- tools/testing/selftests/Makefile | 1 + .../testing/selftests/uaccess_buffer/Makefile | 4 + .../uaccess_buffer/uaccess_buffer_test.c | 126 ++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 tools/testing/selftests/uaccess_buffer/Makefile create mode 100644 tools/testing/selftests/uaccess_buffer/uaccess_buffer_test.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index c852eb40c4f7..291b62430557 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -71,6 +71,7 @@ TARGETS += timers endif TARGETS += tmpfs TARGETS += tpm2 +TARGETS += uaccess_buffer TARGETS += user TARGETS += vDSO TARGETS += vm diff --git a/tools/testing/selftests/uaccess_buffer/Makefile b/tools/testing/selftests/uaccess_buffer/Makefile new file mode 100644 index 000000000000..e6e5fb43ce29 --- /dev/null +++ b/tools/testing/selftests/uaccess_buffer/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 +TEST_GEN_PROGS := uaccess_buffer_test + +include ../lib.mk diff --git a/tools/testing/selftests/uaccess_buffer/uaccess_buffer_test.c b/tools/testing/selftests/uaccess_buffer/uaccess_buffer_test.c new file mode 100644 index 000000000000..051062e4fbf9 --- /dev/null +++ b/tools/testing/selftests/uaccess_buffer/uaccess_buffer_test.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "../kselftest_harness.h" + +#include +#include +#include + +FIXTURE(uaccess_buffer) +{ + uint64_t addr; +}; + +FIXTURE_SETUP(uaccess_buffer) +{ + ASSERT_EQ(0, prctl(PR_SET_UACCESS_DESCRIPTOR_ADDR_ADDR, &self->addr, 0, + 0, 0)); +} + +FIXTURE_TEARDOWN(uaccess_buffer) +{ + ASSERT_EQ(0, prctl(PR_SET_UACCESS_DESCRIPTOR_ADDR_ADDR, 0, 0, 0, 0)); +} + +TEST_F(uaccess_buffer, uname) +{ + struct uaccess_descriptor desc; + struct uaccess_buffer_entry entries[64]; + struct utsname un; + + desc.addr = (uint64_t)(unsigned long)entries; + desc.size = 64; + self->addr = (uint64_t)(unsigned long)&desc; + ASSERT_EQ(0, uname(&un)); + ASSERT_EQ(0, self->addr); + + if (desc.size == 63) { + ASSERT_EQ((uint64_t)(unsigned long)(entries + 1), desc.addr); + + ASSERT_EQ((uint64_t)(unsigned long)&un, entries[0].addr); + ASSERT_EQ(sizeof(struct utsname), entries[0].size); + ASSERT_EQ(UACCESS_BUFFER_FLAG_WRITE, entries[0].flags); + } else { + /* See override_architecture in kernel/sys.c */ + ASSERT_EQ(62, desc.size); + ASSERT_EQ((uint64_t)(unsigned long)(entries + 2), desc.addr); + + ASSERT_EQ((uint64_t)(unsigned long)&un, entries[0].addr); + ASSERT_EQ(sizeof(struct utsname), entries[0].size); + ASSERT_EQ(UACCESS_BUFFER_FLAG_WRITE, entries[0].flags); + + ASSERT_EQ((uint64_t)(unsigned long)&un.machine, + entries[1].addr); + ASSERT_EQ(UACCESS_BUFFER_FLAG_WRITE, entries[1].flags); + } +} + +static bool handled; + +static void usr1_handler(int signo) +{ + handled = true; +} + +TEST_F(uaccess_buffer, blocked_signals) +{ + struct uaccess_descriptor desc; + struct shared_buf { + bool ready; + bool killed; + } volatile *shared = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0); + struct sigaction act = {}, oldact; + int pid; + + handled = false; + act.sa_handler = usr1_handler; + sigaction(SIGUSR1, &act, &oldact); + + pid = fork(); + if (pid == 0) { + /* + * Busy loop to synchronize instead of issuing syscalls because + * we need to test the behavior in the case where no syscall is + * issued by the parent process. + */ + while (!shared->ready) + ; + kill(getppid(), SIGUSR1); + shared->killed = true; + _exit(0); + } else { + int i; + + desc.addr = 0; + desc.size = 0; + self->addr = (uint64_t)(unsigned long)&desc; + + shared->ready = true; + while (!shared->killed) + ; + + /* + * The kernel should have IPI'd us by now, but let's wait a bit + * longer just in case. + */ + for (i = 0; i != 1000000; ++i) + ; + + ASSERT_FALSE(handled); + + /* + * Returning from the waitpid syscall should trigger the signal + * handler. The signal itself may also interrupt waitpid, so + * make sure to handle EINTR. + */ + while (waitpid(pid, NULL, 0) == -1) + ASSERT_EQ(EINTR, errno); + ASSERT_TRUE(handled); + } + + munmap((void *)shared, getpagesize()); + sigaction(SIGUSR1, &oldact, NULL); +} + +TEST_HARNESS_MAIN