From patchwork Tue Nov 20 05:21:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 10689863 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 9A34314BD for ; Tue, 20 Nov 2018 05:22:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C26F29D46 for ; Tue, 20 Nov 2018 05:22:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 803672A327; Tue, 20 Nov 2018 05:22:03 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CFE32A0A1 for ; Tue, 20 Nov 2018 05:22:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5DC16B1E56; Tue, 20 Nov 2018 00:22:00 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BC2AA6B1E57; Tue, 20 Nov 2018 00:22:00 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AEE26B1E58; Tue, 20 Nov 2018 00:22:00 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by kanga.kvack.org (Postfix) with ESMTP id 49AB46B1E56 for ; Tue, 20 Nov 2018 00:22:00 -0500 (EST) Received: by mail-pl1-f200.google.com with SMTP id e68so545851plb.3 for ; Mon, 19 Nov 2018 21:22:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=eR81rLySgIbDOhD8bNr9ndHHVi59qZndCmsNdWWRUlQ=; b=Pc+OD5teWetoMGQtXYQ+Sm2PeU/azClltpL2/iwDxy89+3kZBEKLS5eXH7l0nkILSX wdbRNLEtku0sl89mqiieYahdkzOj90OW5ac74EAJjb1FtnSQ/H+qouTsVkYnklbUnjIZ mA2Krk3FruRoy0tAQ+MSmaY+WyopabkP2muFX5kSjmIUJM68FIYfrio0DlWmRIsTft+Y F/ds85ORIy9Tqferx9J7+jDFiSXxSBTnUiSUDTkfFZipAGdjtelSiMKkI7cfIzBv41XI 5z4GqE6/MDdZQjhYdJyTFmRY2v6WB0PokV3QuyAvq2puyk14lyTbckrlfd1uQyLQgkKZ wKOA== X-Gm-Message-State: AA+aEWZfp/ognuQKUDzDei9Yflc6Qh/By8fgPWTHhjMBbBBfLGdDLU0a aUL6sHstjqoyCjQHeHKAuwauWkoGfM9lWnbOdU9TQRYNEL91GWWiwxsr+gp8oPh1ENtho4NEB+f FTdgCEYts3Lat9xpSVWucZSlcAXaRYVTL88HtPA4qeYGmdv2NHNFHE/Ec9yz2QzyflzQc/RYetN wRQW5VMtauD0kO3ihaMO4i/wdXbHlPkpSBQ8ir7rcZlBH/c2wutW/kmkFq+XG2Cy8iRQKmA7Pug GeCPPY/NOda58NKqBXgn47S3OLoIwKkrVB/yNlrTZmonbRN5P45/0A3+IycS+XcfDdECOuJ41Ie /lhFU3gVdIrShmz/Z+u77HbEDW0pHvqS9UersbEdOw0d7kBaoWfuUqFK5qcR/60wdI8vn8hTWeV C X-Received: by 2002:a63:990a:: with SMTP id d10mr621741pge.279.1542691319826; Mon, 19 Nov 2018 21:21:59 -0800 (PST) X-Received: by 2002:a63:990a:: with SMTP id d10mr621714pge.279.1542691319041; Mon, 19 Nov 2018 21:21:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542691319; cv=none; d=google.com; s=arc-20160816; b=VymK5vw65QOgrpyMs9g0OzmGTa0wDcVbCnCvIafeIJf862P2UQIz/V01S8+MZSJTB/ NHBcVkLEv2gbnAu9ZCs13uiNtJMeGcZFExg9zwll0GlQv21Zv7uPjrmI75CEm3dBwESe 2j1peW59pHsgludkmW5JjhCBkvQmiCMJqxHR/4YVsuc6jERAwFtSNuV48GeJ4GJoazDb x3Yy7cktI1Ff5Kdy52XX1DKkIVNS8wsaGR4L5B7pf8LurAwtB+8x3IZ1n8eFSdEjGcmq 0074w7LYwn3bnPOjgi0yrApP0Kwr0Af4qJfAJx5rv20sEOIxfKl6nsHkqoWodiPlknmQ Ufew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=eR81rLySgIbDOhD8bNr9ndHHVi59qZndCmsNdWWRUlQ=; b=fPGTl7Ck+9EMHblx5xOSOgrjBGMg8dlfHr7GWYyiTwcUCKRl18KLweSHDM1fSLsvIS Tqk0XmbhCF/qTO0JQa+hSWLvQgAzMScvrnpc04oWFoHcOr0qb/b7FYSmu7exP2Rw2You rv3HcP9ipfOw3goUraIzijV3/b7JTVMaTdQBS1Zx9HaMxsBn4UmpSc0q2xzIXsnMIp+0 veGva38lHZ1a8/KSk9PIdyOeQMPUrHZkXpbmjzYZqWnJISTDynvUYRPA28wy0Oe79MGp Q64e0JX85wyyKDiUE1aby0bk1DMKbLVif9HNjFN0kaiT7OS+vaFGoWhH6Hvrmr5J3kNg V2sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=o3lrtmuz; spf=pass (google.com: domain of joel@joelfernandes.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=joel@joelfernandes.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id h4-v6sor49092142plk.55.2018.11.19.21.21.58 for (Google Transport Security); Mon, 19 Nov 2018 21:21:58 -0800 (PST) Received-SPF: pass (google.com: domain of joel@joelfernandes.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=o3lrtmuz; spf=pass (google.com: domain of joel@joelfernandes.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=joel@joelfernandes.org 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=eR81rLySgIbDOhD8bNr9ndHHVi59qZndCmsNdWWRUlQ=; b=o3lrtmuznJQ69cKw2Zm19SroG4DVyNaUlDT0/Sob5PFF0C7I/USX1eBAnxxrXDke7p w0BYdYfVuFU7H3NMe0gnkqQQ81HTQjKC/COIp5mitnc399grM9MqqT3piCurPUBJ0MNL y8U6o6EJ9sFcGtxsGMtIjuKdras2AK5sNwWLc= X-Google-Smtp-Source: AFSGD/W6hBaX3uX6kwGz1b1Ho5J/1rdkjv+kXaTxehYbSVp1p3dkkt42U4BBV6zxLt9PpR28q9LKbw== X-Received: by 2002:a17:902:163:: with SMTP id 90-v6mr749732plb.87.1542691318562; Mon, 19 Nov 2018 21:21:58 -0800 (PST) Received: from joelaf.mtv.corp.google.com ([2620:0:1000:1601:3aef:314f:b9ea:889f]) by smtp.gmail.com with ESMTPSA id q199sm34237451pfc.97.2018.11.19.21.21.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Nov 2018 21:21:57 -0800 (PST) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Jann Horn , Andrew Morton , Andy Lutomirski , Hugh Dickins , Khalid Aziz , linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, =?utf-8?q?Marc-Andr?= =?utf-8?q?=C3=A9_Lureau?= , Matthew Wilcox , Mike Kravetz , Shuah Khan , Stephen Rothwell Subject: [PATCH -next 2/2] selftests/memfd: modify tests for F_SEAL_FUTURE_WRITE seal Date: Mon, 19 Nov 2018 21:21:37 -0800 Message-Id: <20181120052137.74317-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.19.1.1215.g8438c0b245-goog In-Reply-To: <20181120052137.74317-1-joel@joelfernandes.org> References: <20181120052137.74317-1-joel@joelfernandes.org> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Modify the tests for F_SEAL_FUTURE_WRITE based on the changes introduced in previous patch. Also add a test to make sure the reopen issue pointed by Jann Horn [1] is fixed. [1] https://lore.kernel.org/lkml/CAG48ez1h=v-JYnDw81HaYJzOfrNhwYksxmc2r=cJvdQVgYM+NA@mail.gmail.com/ Cc: Jann Horn Signed-off-by: Joel Fernandes (Google) Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/memfd/memfd_test.c | 88 +++++++++++----------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index 32b207ca7372..c67d32eeb668 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -54,6 +54,22 @@ static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) return fd; } +static int mfd_assert_reopen_fd(int fd_in) +{ + int r, fd; + char path[100]; + + sprintf(path, "/proc/self/fd/%d", fd_in); + + fd = open(path, O_RDWR); + if (fd < 0) { + printf("re-open of existing fd %d failed\n", fd_in); + abort(); + } + + return fd; +} + static void mfd_fail_new(const char *name, unsigned int flags) { int r; @@ -255,6 +271,25 @@ static void mfd_assert_read(int fd) munmap(p, mfd_def_size); } +/* Test that PROT_READ + MAP_SHARED mappings work. */ +static void mfd_assert_read_shared(int fd) +{ + void *p; + + /* verify PROT_READ and MAP_SHARED *is* allowed */ + p = mmap(NULL, + mfd_def_size, + PROT_READ, + MAP_SHARED, + fd, + 0); + if (p == MAP_FAILED) { + printf("mmap() failed: %m\n"); + abort(); + } + munmap(p, mfd_def_size); +} + static void mfd_assert_write(int fd) { ssize_t l; @@ -698,7 +733,7 @@ static void test_seal_write(void) */ static void test_seal_future_write(void) { - int fd; + int fd, fd2; void *p; printf("%s SEAL-FUTURE-WRITE\n", memfd_str); @@ -710,58 +745,23 @@ static void test_seal_future_write(void) p = mfd_assert_mmap_shared(fd); mfd_assert_has_seals(fd, 0); - /* Not adding grow/shrink seals makes the future write - * seal fail to get added - */ - mfd_fail_add_seals(fd, F_SEAL_FUTURE_WRITE); - - mfd_assert_add_seals(fd, F_SEAL_GROW); - mfd_assert_has_seals(fd, F_SEAL_GROW); - - /* Should still fail since shrink seal has - * not yet been added - */ - mfd_fail_add_seals(fd, F_SEAL_FUTURE_WRITE); - - mfd_assert_add_seals(fd, F_SEAL_SHRINK); - mfd_assert_has_seals(fd, F_SEAL_GROW | - F_SEAL_SHRINK); - /* Now should succeed, also verifies that the seal - * could be added with an existing writable mmap - */ mfd_assert_add_seals(fd, F_SEAL_FUTURE_WRITE); - mfd_assert_has_seals(fd, F_SEAL_SHRINK | - F_SEAL_GROW | - F_SEAL_FUTURE_WRITE); + mfd_assert_has_seals(fd, F_SEAL_FUTURE_WRITE); /* read should pass, writes should fail */ mfd_assert_read(fd); + mfd_assert_read_shared(fd); mfd_fail_write(fd); - munmap(p, mfd_def_size); - close(fd); - - /* Test adding all seals (grow, shrink, future write) at once */ - fd = mfd_assert_new("kern_memfd_seal_future_write2", - mfd_def_size, - MFD_CLOEXEC | MFD_ALLOW_SEALING); - - p = mfd_assert_mmap_shared(fd); - - mfd_assert_has_seals(fd, 0); - mfd_assert_add_seals(fd, F_SEAL_SHRINK | - F_SEAL_GROW | - F_SEAL_FUTURE_WRITE); - mfd_assert_has_seals(fd, F_SEAL_SHRINK | - F_SEAL_GROW | - F_SEAL_FUTURE_WRITE); - - /* read should pass, writes should fail */ - mfd_assert_read(fd); - mfd_fail_write(fd); + fd2 = mfd_assert_reopen_fd(fd); + /* read should pass, writes should still fail */ + mfd_assert_read(fd2); + mfd_assert_read_shared(fd2); + mfd_fail_write(fd2); munmap(p, mfd_def_size); + close(fd2); close(fd); }