Home | History | Annotate | Line # | Download | only in kernel
t_memfd_create.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: t_memfd_create.c,v 1.3 2023/11/24 17:31:03 riastradh Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Theodore Preduta.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.3  riastrad __RCSID("$NetBSD: t_memfd_create.c,v 1.3 2023/11/24 17:31:03 riastradh Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/param.h>
     35  1.1  christos #include <sys/types.h>
     36  1.1  christos #include <sys/mman.h>
     37  1.1  christos #include <sys/stat.h>
     38  1.1  christos #include <errno.h>
     39  1.1  christos #include <fcntl.h>
     40  1.1  christos 
     41  1.1  christos #include <atf-c.h>
     42  1.1  christos 
     43  1.1  christos #include "h_macros.h"
     44  1.1  christos 
     45  1.1  christos char name_buf[NAME_MAX];
     46  1.1  christos char write_buf[8192];
     47  1.1  christos char read_buf[8192];
     48  1.1  christos 
     49  1.1  christos ATF_TC(create_null_name);
     50  1.1  christos ATF_TC_HEAD(create_null_name, tc)
     51  1.1  christos {
     52  1.1  christos 
     53  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     54  1.1  christos 	    "Checks memfd_create fails with EFAULT when invalid memory"
     55  1.1  christos 	    " is provided");
     56  1.1  christos }
     57  1.1  christos ATF_TC_BODY(create_null_name, tc)
     58  1.1  christos {
     59  1.1  christos 	int fd;
     60  1.1  christos 
     61  1.1  christos 	ATF_REQUIRE_EQ_MSG(fd = memfd_create(NULL, 0), -1,
     62  1.1  christos 	    "Unexpected success");
     63  1.1  christos 	ATF_REQUIRE_ERRNO(EFAULT, true);
     64  1.1  christos }
     65  1.1  christos 
     66  1.1  christos ATF_TC(create_long_name);
     67  1.1  christos ATF_TC_HEAD(create_long_name, tc)
     68  1.1  christos {
     69  1.1  christos 
     70  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     71  1.1  christos 	    "Checks memfd_create fails for names longer than NAME_MAX-6");
     72  1.1  christos }
     73  1.1  christos ATF_TC_BODY(create_long_name, tc)
     74  1.1  christos {
     75  1.1  christos 	int fd;
     76  1.1  christos 
     77  1.1  christos         memset(name_buf, 'A', sizeof(name_buf));
     78  1.1  christos 	name_buf[NAME_MAX-6] = '\0';
     79  1.1  christos 
     80  1.1  christos 	ATF_REQUIRE_EQ_MSG(fd = memfd_create(name_buf, 0), -1,
     81  1.1  christos 	    "Unexpected success");
     82  1.1  christos 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, true);
     83  1.1  christos 
     84  1.1  christos 	name_buf[NAME_MAX-7] = '\0';
     85  1.1  christos 
     86  1.1  christos 	RL(fd = memfd_create(name_buf, 0));
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos ATF_TC(read_write);
     90  1.1  christos ATF_TC_HEAD(read_write, tc)
     91  1.1  christos {
     92  1.1  christos 
     93  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     94  1.1  christos 	    "Checks that data can be written to/read from a memfd");
     95  1.1  christos }
     96  1.1  christos ATF_TC_BODY(read_write, tc)
     97  1.1  christos {
     98  1.1  christos 	int fd;
     99  1.1  christos 	off_t offset;
    100  1.1  christos 
    101  1.1  christos 	RL(fd = memfd_create("", 0));
    102  1.1  christos 
    103  1.1  christos 	tests_makegarbage(write_buf, sizeof(write_buf));
    104  1.1  christos 	memset(read_buf, 0, sizeof(read_buf));
    105  1.1  christos 
    106  1.1  christos 	RL(write(fd, write_buf, sizeof(write_buf)));
    107  1.1  christos 	offset = lseek(fd, 0, SEEK_CUR);
    108  1.1  christos 	ATF_REQUIRE_EQ_MSG(offset, sizeof(write_buf),
    109  1.2       rin 	    "File offset not set after write (%jd != %zu)", (intmax_t)offset,
    110  1.1  christos 	    sizeof(write_buf));
    111  1.1  christos 
    112  1.3  riastrad 	RL(lseek(fd, 0, SEEK_SET));
    113  1.1  christos 
    114  1.1  christos 	RL(read(fd, read_buf, sizeof(read_buf)));
    115  1.1  christos 	offset = lseek(fd, 0, SEEK_CUR);
    116  1.1  christos 	ATF_REQUIRE_EQ_MSG(offset, sizeof(read_buf),
    117  1.2       rin 	    "File offset not set after read (%jd != %zu)", (intmax_t)offset,
    118  1.1  christos 	    sizeof(read_buf));
    119  1.1  christos 
    120  1.1  christos 	for (size_t i = 0; i < sizeof(read_buf); i++)
    121  1.1  christos 		ATF_REQUIRE_EQ_MSG(read_buf[i], write_buf[i],
    122  1.1  christos 		    "Data read does not match data written");
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos ATF_TC(truncate);
    126  1.1  christos ATF_TC_HEAD(truncate, tc)
    127  1.1  christos {
    128  1.1  christos 
    129  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    130  1.1  christos 	    "Checks that truncation does result in data removal");
    131  1.1  christos }
    132  1.1  christos ATF_TC_BODY(truncate, tc)
    133  1.1  christos {
    134  1.1  christos 	int fd;
    135  1.1  christos 	struct stat st;
    136  1.1  christos 
    137  1.1  christos 	RL(fd = memfd_create("", 0));
    138  1.1  christos 
    139  1.1  christos 	tests_makegarbage(write_buf, sizeof(write_buf));
    140  1.1  christos 	tests_makegarbage(read_buf, sizeof(read_buf));
    141  1.1  christos 
    142  1.1  christos 	RL(write(fd, write_buf, sizeof(write_buf)));
    143  1.1  christos 
    144  1.1  christos 	RL(fstat(fd, &st));
    145  1.1  christos 	ATF_REQUIRE_EQ_MSG(st.st_size, sizeof(write_buf),
    146  1.2       rin 	    "Write did not grow size to %zu (is %jd)", sizeof(write_buf),
    147  1.2       rin 	    (intmax_t)st.st_size);
    148  1.1  christos 
    149  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    150  1.1  christos 	RL(fstat(fd, &st));
    151  1.1  christos 	ATF_REQUIRE_EQ_MSG(st.st_size, sizeof(write_buf)/2,
    152  1.2       rin 	    "Truncate did not shrink size to %zu (is %jd)",
    153  1.2       rin 	    sizeof(write_buf)/2, (intmax_t)st.st_size);
    154  1.1  christos 
    155  1.1  christos 	RL(ftruncate(fd, sizeof(read_buf)));
    156  1.1  christos 	RL(fstat(fd, &st));
    157  1.1  christos 	ATF_REQUIRE_EQ_MSG(st.st_size, sizeof(read_buf),
    158  1.2       rin 	    "Truncate did not grow size to %zu (is %jd)", sizeof(read_buf),
    159  1.2       rin 	    (intmax_t)st.st_size);
    160  1.1  christos 
    161  1.3  riastrad 	RL(lseek(fd, 0, SEEK_SET));
    162  1.1  christos 	RL(read(fd, read_buf, sizeof(read_buf)));
    163  1.1  christos 
    164  1.1  christos 	for (size_t i = 0; i < sizeof(read_buf)/2; i++)
    165  1.1  christos 		ATF_REQUIRE_EQ_MSG(read_buf[i], write_buf[i],
    166  1.1  christos 		    "Data read does not match data written");
    167  1.1  christos 	for (size_t i = sizeof(read_buf)/2; i < sizeof(read_buf); i++)
    168  1.1  christos 		ATF_REQUIRE_EQ_MSG(read_buf[i], 0,
    169  1.1  christos 		    "Data read on growed region is not zeroed");
    170  1.1  christos }
    171  1.1  christos 
    172  1.1  christos ATF_TC(mmap);
    173  1.1  christos ATF_TC_HEAD(mmap, tc)
    174  1.1  christos {
    175  1.1  christos 
    176  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Check that mmap succeeds");
    177  1.1  christos }
    178  1.1  christos ATF_TC_BODY(mmap, tc)
    179  1.1  christos {
    180  1.1  christos 	int fd;
    181  1.1  christos 	void *addr;
    182  1.1  christos 
    183  1.1  christos 	RL(fd = memfd_create("", 0));
    184  1.1  christos 	RL(ftruncate(fd, sizeof(read_buf)));
    185  1.1  christos 
    186  1.1  christos 	addr = mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED,
    187  1.1  christos 	    fd, 0);
    188  1.1  christos 	ATF_REQUIRE_MSG(addr != MAP_FAILED, "Mmap failed unexpectedly (%s)",
    189  1.1  christos 	    strerror(errno));
    190  1.1  christos }
    191  1.1  christos 
    192  1.1  christos ATF_TC(create_no_sealing);
    193  1.1  christos ATF_TC_HEAD(create_no_sealing, tc)
    194  1.1  christos {
    195  1.1  christos 
    196  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    197  1.1  christos 	    "Checks that seals cannot be added if MFD_ALLOW_SEALING is"
    198  1.1  christos 	    " not specified to memfd_create");
    199  1.1  christos }
    200  1.1  christos ATF_TC_BODY(create_no_sealing, tc)
    201  1.1  christos {
    202  1.1  christos 	int fd;
    203  1.1  christos 
    204  1.1  christos 	RL(fd = memfd_create("", 0));
    205  1.1  christos 
    206  1.1  christos 	ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    207  1.1  christos 	    "fcntl succeeded unexpectedly");
    208  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    209  1.1  christos }
    210  1.1  christos 
    211  1.1  christos ATF_TC(seal_seal);
    212  1.1  christos ATF_TC_HEAD(seal_seal, tc)
    213  1.1  christos {
    214  1.1  christos 
    215  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    216  1.1  christos 	    "Checks adding F_SEAL_SEAL prevents adding other seals");
    217  1.1  christos }
    218  1.1  christos ATF_TC_BODY(seal_seal, tc)
    219  1.1  christos {
    220  1.1  christos 	int fd;
    221  1.1  christos 
    222  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    223  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL));
    224  1.1  christos 
    225  1.1  christos         ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    226  1.1  christos 	    "fcntl succeeded unexpectedly");
    227  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    228  1.1  christos }
    229  1.1  christos 
    230  1.1  christos /*
    231  1.1  christos  * Tests that the seals provided in except to not also prevent some
    232  1.1  christos  * other operation.
    233  1.1  christos  *
    234  1.1  christos  * Note: fd must have a positive size.
    235  1.1  christos  */
    236  1.1  christos static void
    237  1.1  christos test_all_seals_except(int fd, int except)
    238  1.1  christos {
    239  1.1  christos 	int rv;
    240  1.1  christos 	struct stat st;
    241  1.1  christos 	void *addr;
    242  1.1  christos 
    243  1.1  christos 	RL(fstat(fd, &st));
    244  1.1  christos 	ATF_REQUIRE(st.st_size > 0);
    245  1.1  christos 
    246  1.1  christos 	if (except & ~F_SEAL_SEAL) {
    247  1.1  christos 		rv = fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL);
    248  1.1  christos 		if (rv == -1) {
    249  1.1  christos 			ATF_REQUIRE_MSG(errno != EPERM,
    250  1.1  christos 			    "Seal %x prevented F_ADD_SEALS", except);
    251  1.1  christos 			ATF_REQUIRE_MSG(errno == EPERM,
    252  1.1  christos 			    "F_ADD_SEALS failed unexpectedly (%s)",
    253  1.1  christos 			    strerror(errno));
    254  1.1  christos 		}
    255  1.1  christos 	}
    256  1.1  christos 
    257  1.1  christos 	if (except & ~(F_SEAL_WRITE|F_SEAL_FUTURE_WRITE)) {
    258  1.3  riastrad 		RL(lseek(fd, 0, SEEK_SET));
    259  1.1  christos 		rv = write(fd, write_buf, sizeof(write_buf));
    260  1.1  christos 		if (rv == -1) {
    261  1.1  christos 			ATF_REQUIRE_MSG(errno != EPERM,
    262  1.1  christos 			    "Seal %x prevented write", except);
    263  1.1  christos 		        ATF_REQUIRE_MSG(errno == EPERM,
    264  1.1  christos 			    "Write failed unexpectedly (%s)",
    265  1.1  christos 			    strerror(errno));
    266  1.1  christos 		}
    267  1.1  christos 
    268  1.1  christos 	        addr = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE,
    269  1.1  christos 		    MAP_SHARED, fd, 0);
    270  1.1  christos 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
    271  1.1  christos 		    "Mmap failed unexpectedly (%s)", strerror(errno));
    272  1.1  christos 	}
    273  1.1  christos 
    274  1.1  christos 	if (except & ~F_SEAL_SHRINK) {
    275  1.1  christos 		rv = ftruncate(fd, st.st_size - 1);
    276  1.1  christos 		if (rv == -1) {
    277  1.1  christos 		        ATF_REQUIRE_MSG(errno != EPERM,
    278  1.1  christos 			    "Seal %x prevented truncate to shrink", except);
    279  1.1  christos 			ATF_REQUIRE_MSG(errno == EPERM,
    280  1.1  christos 			    "Truncate failed unexpectedly (%s)",
    281  1.1  christos 			    strerror(errno));
    282  1.1  christos 		}
    283  1.1  christos 	}
    284  1.1  christos 
    285  1.1  christos 	if (except & ~F_SEAL_GROW) {
    286  1.1  christos 		rv = ftruncate(fd, st.st_size + 1);
    287  1.1  christos 		if (rv == -1) {
    288  1.1  christos 		        ATF_REQUIRE_MSG(errno != EPERM,
    289  1.1  christos 			    "Seal %x prevented truncate to shrink", except);
    290  1.1  christos 		        ATF_REQUIRE_MSG(errno == EPERM,
    291  1.1  christos 			    "Truncate failed unexpectedly (%s)",
    292  1.1  christos 			    strerror(errno));
    293  1.1  christos 		}
    294  1.1  christos 	}
    295  1.1  christos }
    296  1.1  christos 
    297  1.1  christos ATF_TC(seal_shrink);
    298  1.1  christos ATF_TC_HEAD(seal_shrink, tc)
    299  1.1  christos {
    300  1.1  christos 
    301  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    302  1.1  christos 	    "Checks F_SEAL_SHRINK prevents shrinking the file");
    303  1.1  christos }
    304  1.1  christos ATF_TC_BODY(seal_shrink, tc)
    305  1.1  christos {
    306  1.1  christos 	int fd;
    307  1.1  christos 
    308  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    309  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)));
    310  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK));
    311  1.1  christos 
    312  1.1  christos 	ATF_REQUIRE_EQ_MSG(ftruncate(fd, sizeof(write_buf)/2), -1,
    313  1.1  christos 	    "Truncate succeeded unexpectedly");
    314  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    315  1.1  christos 
    316  1.1  christos 	test_all_seals_except(fd, F_SEAL_SHRINK);
    317  1.1  christos }
    318  1.1  christos 
    319  1.1  christos ATF_TC(seal_grow);
    320  1.1  christos ATF_TC_HEAD(seal_grow, tc)
    321  1.1  christos {
    322  1.1  christos 
    323  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    324  1.1  christos 	    "Checks F_SEAL_SHRINK prevents growing the file");
    325  1.1  christos }
    326  1.1  christos ATF_TC_BODY(seal_grow, tc)
    327  1.1  christos {
    328  1.1  christos 	int fd;
    329  1.1  christos 
    330  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    331  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    332  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_GROW));
    333  1.1  christos 
    334  1.1  christos 	ATF_REQUIRE_EQ_MSG(ftruncate(fd, sizeof(write_buf)), -1,
    335  1.1  christos 	    "Truncate succeeded unexpectedly");
    336  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    337  1.1  christos 
    338  1.1  christos 	test_all_seals_except(fd, F_SEAL_GROW);
    339  1.1  christos }
    340  1.1  christos 
    341  1.1  christos ATF_TC(seal_write);
    342  1.1  christos ATF_TC_HEAD(seal_write, tc)
    343  1.1  christos {
    344  1.1  christos 
    345  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    346  1.1  christos 	    "Checks F_SEAL_WRITE prevents writing");
    347  1.1  christos }
    348  1.1  christos ATF_TC_BODY(seal_write, tc)
    349  1.1  christos {
    350  1.1  christos 	int fd;
    351  1.1  christos 
    352  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    353  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    354  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE));
    355  1.1  christos 
    356  1.1  christos 	ATF_REQUIRE_EQ_MSG(write(fd, write_buf, sizeof(write_buf)), -1,
    357  1.1  christos 	    "Write succeeded unexpectedly");
    358  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    359  1.1  christos 
    360  1.1  christos 	test_all_seals_except(fd, F_SEAL_WRITE);
    361  1.1  christos }
    362  1.1  christos 
    363  1.1  christos ATF_TC(seal_write_mmap);
    364  1.1  christos ATF_TC_HEAD(seal_write_mmap, tc)
    365  1.1  christos {
    366  1.1  christos 
    367  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    368  1.1  christos 	    "Checks that F_SEAL_WRITE cannot be added with open mmaps");
    369  1.1  christos }
    370  1.1  christos ATF_TC_BODY(seal_write_mmap, tc)
    371  1.1  christos {
    372  1.1  christos 	int fd;
    373  1.1  christos 	void *addr;
    374  1.1  christos 
    375  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    376  1.1  christos 	RL(ftruncate(fd, sizeof(read_buf)));
    377  1.1  christos 
    378  1.1  christos 	addr = mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED,
    379  1.1  christos 	    fd, 0);
    380  1.1  christos 	ATF_REQUIRE_MSG(addr != MAP_FAILED, "Mmap failed unexpectedly (%s)",
    381  1.1  christos 	    strerror(errno));
    382  1.1  christos 
    383  1.1  christos 	ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    384  1.1  christos 	    "fcntl succeeded unexpectedly");
    385  1.1  christos 	ATF_REQUIRE_ERRNO(EBUSY, true);
    386  1.1  christos }
    387  1.1  christos 
    388  1.1  christos ATF_TC(seal_future_write);
    389  1.1  christos ATF_TC_HEAD(seal_future_write, tc)
    390  1.1  christos {
    391  1.1  christos 
    392  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    393  1.1  christos 	    "Checks F_SEAL_FUTURE_WRITE prevents writing");
    394  1.1  christos }
    395  1.1  christos ATF_TC_BODY(seal_future_write, tc)
    396  1.1  christos {
    397  1.1  christos 	int fd;
    398  1.1  christos 
    399  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    400  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    401  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE));
    402  1.1  christos 
    403  1.1  christos 	ATF_REQUIRE_EQ_MSG(write(fd, write_buf, sizeof(write_buf)), -1,
    404  1.1  christos 	    "Write succeeded unexpectedly");
    405  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    406  1.1  christos 
    407  1.1  christos 	test_all_seals_except(fd, F_SEAL_FUTURE_WRITE);
    408  1.1  christos }
    409  1.1  christos 
    410  1.1  christos ATF_TC(seal_future_write_mmap);
    411  1.1  christos ATF_TC_HEAD(seal_future_write_mmap, tc)
    412  1.1  christos {
    413  1.1  christos 
    414  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    415  1.1  christos 	    "Checks that F_SEAL_WRITE can be added with open mmaps but"
    416  1.1  christos 	    " prevents creating new ones");
    417  1.1  christos }
    418  1.1  christos ATF_TC_BODY(seal_future_write_mmap, tc)
    419  1.1  christos {
    420  1.1  christos 	int fd;
    421  1.1  christos 	void *addr;
    422  1.1  christos 
    423  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    424  1.1  christos 	RL(ftruncate(fd, sizeof(read_buf)));
    425  1.1  christos 	addr = mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED,
    426  1.1  christos 	    fd, 0);
    427  1.1  christos 	ATF_REQUIRE_MSG(addr != MAP_FAILED, "Mmap failed unexpectedly (%s)",
    428  1.1  christos 	    strerror(errno));
    429  1.1  christos 
    430  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE));
    431  1.1  christos 
    432  1.1  christos 	ATF_REQUIRE_EQ_MSG(mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE,
    433  1.1  christos 	    MAP_SHARED, fd, 0), MAP_FAILED, "Mmap succeeded unexpectedly");
    434  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    435  1.1  christos }
    436  1.1  christos 
    437  1.1  christos 
    438  1.1  christos ATF_TP_ADD_TCS(tp)
    439  1.1  christos {
    440  1.1  christos 	ATF_TP_ADD_TC(tp, create_null_name);
    441  1.1  christos 	ATF_TP_ADD_TC(tp, create_long_name);
    442  1.1  christos 	ATF_TP_ADD_TC(tp, read_write);
    443  1.1  christos 	ATF_TP_ADD_TC(tp, truncate);
    444  1.1  christos 	ATF_TP_ADD_TC(tp, mmap);
    445  1.1  christos 	ATF_TP_ADD_TC(tp, create_no_sealing);
    446  1.1  christos 	ATF_TP_ADD_TC(tp, seal_seal);
    447  1.1  christos 	ATF_TP_ADD_TC(tp, seal_shrink);
    448  1.1  christos 	ATF_TP_ADD_TC(tp, seal_grow);
    449  1.1  christos 	ATF_TP_ADD_TC(tp, seal_write);
    450  1.1  christos 	ATF_TP_ADD_TC(tp, seal_write_mmap);
    451  1.1  christos 	ATF_TP_ADD_TC(tp, seal_future_write);
    452  1.1  christos 	ATF_TP_ADD_TC(tp, seal_future_write_mmap);
    453  1.1  christos 
    454  1.1  christos 	return atf_no_error();
    455  1.1  christos }
    456