From patchwork Tue Oct 9 22:20:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 10633329 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7516116B1 for ; Tue, 9 Oct 2018 22:21:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6680929B5F for ; Tue, 9 Oct 2018 22:21:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 643DA29B88; Tue, 9 Oct 2018 22:21:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1241C29B5F for ; Tue, 9 Oct 2018 22:21:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728030AbeJJFkM (ORCPT ); Wed, 10 Oct 2018 01:40:12 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:45899 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728011AbeJJFkL (ORCPT ); Wed, 10 Oct 2018 01:40:11 -0400 Received: by mail-pl1-f194.google.com with SMTP id y15-v6so1488401plr.12 for ; Tue, 09 Oct 2018 15:21:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mSCp3IjXtO6KaCWkQ2EwPMcxQAzHpqecCP58DFd+0XQ=; b=Vdy6DvZH2nbFshP8qYD/ks+zBdEI8OFH1w8BCeB0V47R1wL2lOsyrVi+HOJ66+onyF oJCPdDRjWMRXav9ZvB6GF/UUM9BeNCA+kPT3sDnJiJY22C0+5n++fxUqp7iB2g8CvgfX mJ15DCGbAWj08HnKQVNkhTSIPdSz5T/Nf5Xb8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mSCp3IjXtO6KaCWkQ2EwPMcxQAzHpqecCP58DFd+0XQ=; b=YqvFyAzd19gKTSu+FLR2fEuvAI5jzqA5tI/1bL1v/SVKJATmOJsOENoLOiiXRNXtPL xTK2ZnM87y0uT1zhSyWiweG+PLVV4o5zAAAEMHAampd905XhafeerpIGlFQMKw8g+q+P jhWI90mptmFVpd0OHiGW0vb+G4+GS5oKDT2RW28GDMFB2AtSMGO0lemCe+j/GP7yHOte qM4TRx9l67omTUuODTpvkdsjk/8upctUAUmOC7tN549WsjNiO1ZDUvZEOmNz08VpgOv3 qs+Dusfctx2KcYzahRpQ3Hc2JOs3blrwO3DMeHWvUQk4PBNYzf0K44wAEIcNHvy/mi21 PYNQ== X-Gm-Message-State: ABuFfojGJSWamQnuRK1FVqMsVg5qOzGF/35bImB8DjsIAPANCt6QOg3W gnFE5dPaH0E4i/LRmwa+AXztwA== X-Google-Smtp-Source: ACcGV60yixQ7WkKtFoVo0a2iIypyOXeolvn18ZMkAH0fdyE9vdp77xXrrlRgW8IxyrNqeVzlUSyXbQ== X-Received: by 2002:a17:902:15a8:: with SMTP id m37-v6mr23489770pla.132.1539123665951; Tue, 09 Oct 2018 15:21:05 -0700 (PDT) Received: from joelaf.mtv.corp.google.com ([2620:0:1000:1601:3aef:314f:b9ea:889f]) by smtp.gmail.com with ESMTPSA id z15-v6sm26900856pfn.170.2018.10.09.15.21.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Oct 2018 15:21:04 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: kernel-team@android.com, "Joel Fernandes (Google)" , dancol@google.com, minchan@google.com, Andrew Morton , gregkh@linuxfoundation.org, "J. Bruce Fields" , Jeff Layton , john.stultz@linaro.org, jreck@google.com, Khalid Aziz , linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Mike Kravetz , Shuah Khan , tkjos@google.com Subject: [PATCH v2 2/2] selftests/memfd: Add tests for F_SEAL_FS_WRITE seal Date: Tue, 9 Oct 2018 15:20:42 -0700 Message-Id: <20181009222042.9781-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.19.0.605.g01d371f741-goog In-Reply-To: <20181009222042.9781-1-joel@joelfernandes.org> References: <20181009222042.9781-1-joel@joelfernandes.org> MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add tests to verify sealing memfds with the F_SEAL_FS_WRITE works as expected. Cc: dancol@google.com Cc: minchan@google.com Signed-off-by: Joel Fernandes (Google) Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/memfd/memfd_test.c | 51 +++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 10baa1652fc2..4bd2b6c87bb4 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -27,7 +27,7 @@ #define MFD_DEF_SIZE 8192 #define STACK_SIZE 65536 - +#define F_SEAL_FS_WRITE 0x0010 /* * Default is not to test hugetlbfs */ @@ -170,6 +170,24 @@ static void *mfd_assert_mmap_shared(int fd) return p; } +static void *mfd_fail_mmap_shared(int fd) +{ + void *p; + + p = mmap(NULL, + mfd_def_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0); + if (p != MAP_FAILED) { + printf("mmap() didn't fail as expected\n"); + abort(); + } + + return p; +} + static void *mfd_assert_mmap_private(int fd) { void *p; @@ -692,6 +710,36 @@ static void test_seal_write(void) close(fd); } +/* + * Test SEAL_WRITE + * Test whether SEAL_WRITE actually prevents modifications. + */ +static void test_seal_fs_write(void) +{ + int fd; + void *p; + + printf("%s SEAL-FS-WRITE\n", memfd_str); + + fd = mfd_assert_new("kern_memfd_seal_fs_write", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + + p = mfd_assert_mmap_shared(fd); + + /* FS_WRITE seal can be added even with existing + * writeable mappings */ + mfd_assert_has_seals(fd, 0); + mfd_assert_add_seals(fd, F_SEAL_FS_WRITE); + mfd_assert_has_seals(fd, F_SEAL_FS_WRITE); + + mfd_assert_read(fd); + mfd_fail_write(fd); + + munmap(p, mfd_def_size); + close(fd); +} + /* * Test SEAL_SHRINK * Test whether SEAL_SHRINK actually prevents shrinking @@ -945,6 +993,7 @@ int main(int argc, char **argv) test_basic(); test_seal_write(); + test_seal_fs_write(); test_seal_shrink(); test_seal_grow(); test_seal_resize();