Home | History | Annotate | Line # | Download | only in kernel
t_memfd_create.c revision 1.4
      1  1.4  riastrad /*	$NetBSD: t_memfd_create.c,v 1.4 2025/04/17 16:02:48 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.4  riastrad __RCSID("$NetBSD: t_memfd_create.c,v 1.4 2025/04/17 16:02:48 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.4  riastrad 	ATF_REQUIRE_MSG(addr != MAP_FAILED,
    189  1.4  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) failed:"
    190  1.4  riastrad 	    " %s",
    191  1.4  riastrad 	    sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    192  1.1  christos 	    strerror(errno));
    193  1.1  christos }
    194  1.1  christos 
    195  1.1  christos ATF_TC(create_no_sealing);
    196  1.1  christos ATF_TC_HEAD(create_no_sealing, tc)
    197  1.1  christos {
    198  1.1  christos 
    199  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    200  1.1  christos 	    "Checks that seals cannot be added if MFD_ALLOW_SEALING is"
    201  1.1  christos 	    " not specified to memfd_create");
    202  1.1  christos }
    203  1.1  christos ATF_TC_BODY(create_no_sealing, tc)
    204  1.1  christos {
    205  1.1  christos 	int fd;
    206  1.1  christos 
    207  1.1  christos 	RL(fd = memfd_create("", 0));
    208  1.1  christos 
    209  1.1  christos 	ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    210  1.1  christos 	    "fcntl succeeded unexpectedly");
    211  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    212  1.1  christos }
    213  1.1  christos 
    214  1.1  christos ATF_TC(seal_seal);
    215  1.1  christos ATF_TC_HEAD(seal_seal, tc)
    216  1.1  christos {
    217  1.1  christos 
    218  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    219  1.1  christos 	    "Checks adding F_SEAL_SEAL prevents adding other seals");
    220  1.1  christos }
    221  1.1  christos ATF_TC_BODY(seal_seal, tc)
    222  1.1  christos {
    223  1.1  christos 	int fd;
    224  1.1  christos 
    225  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    226  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL));
    227  1.1  christos 
    228  1.1  christos         ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    229  1.1  christos 	    "fcntl succeeded unexpectedly");
    230  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos /*
    234  1.1  christos  * Tests that the seals provided in except to not also prevent some
    235  1.1  christos  * other operation.
    236  1.1  christos  *
    237  1.1  christos  * Note: fd must have a positive size.
    238  1.1  christos  */
    239  1.1  christos static void
    240  1.1  christos test_all_seals_except(int fd, int except)
    241  1.1  christos {
    242  1.1  christos 	int rv;
    243  1.1  christos 	struct stat st;
    244  1.1  christos 	void *addr;
    245  1.1  christos 
    246  1.1  christos 	RL(fstat(fd, &st));
    247  1.1  christos 	ATF_REQUIRE(st.st_size > 0);
    248  1.1  christos 
    249  1.1  christos 	if (except & ~F_SEAL_SEAL) {
    250  1.1  christos 		rv = fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL);
    251  1.1  christos 		if (rv == -1) {
    252  1.1  christos 			ATF_REQUIRE_MSG(errno != EPERM,
    253  1.1  christos 			    "Seal %x prevented F_ADD_SEALS", except);
    254  1.1  christos 			ATF_REQUIRE_MSG(errno == EPERM,
    255  1.1  christos 			    "F_ADD_SEALS failed unexpectedly (%s)",
    256  1.1  christos 			    strerror(errno));
    257  1.1  christos 		}
    258  1.1  christos 	}
    259  1.1  christos 
    260  1.1  christos 	if (except & ~(F_SEAL_WRITE|F_SEAL_FUTURE_WRITE)) {
    261  1.3  riastrad 		RL(lseek(fd, 0, SEEK_SET));
    262  1.1  christos 		rv = write(fd, write_buf, sizeof(write_buf));
    263  1.1  christos 		if (rv == -1) {
    264  1.1  christos 			ATF_REQUIRE_MSG(errno != EPERM,
    265  1.1  christos 			    "Seal %x prevented write", except);
    266  1.1  christos 		        ATF_REQUIRE_MSG(errno == EPERM,
    267  1.1  christos 			    "Write failed unexpectedly (%s)",
    268  1.1  christos 			    strerror(errno));
    269  1.1  christos 		}
    270  1.1  christos 
    271  1.1  christos 	        addr = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE,
    272  1.1  christos 		    MAP_SHARED, fd, 0);
    273  1.1  christos 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
    274  1.4  riastrad 		    "mmap(NULL, %llu, 0x%x, 0x%x, %d, 0) failed:"
    275  1.4  riastrad 		    " %s",
    276  1.4  riastrad 		    (unsigned long long)st.st_size,
    277  1.4  riastrad 		    PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    278  1.4  riastrad 		    strerror(errno));
    279  1.1  christos 	}
    280  1.1  christos 
    281  1.1  christos 	if (except & ~F_SEAL_SHRINK) {
    282  1.1  christos 		rv = ftruncate(fd, st.st_size - 1);
    283  1.1  christos 		if (rv == -1) {
    284  1.1  christos 		        ATF_REQUIRE_MSG(errno != EPERM,
    285  1.1  christos 			    "Seal %x prevented truncate to shrink", except);
    286  1.1  christos 			ATF_REQUIRE_MSG(errno == EPERM,
    287  1.1  christos 			    "Truncate failed unexpectedly (%s)",
    288  1.1  christos 			    strerror(errno));
    289  1.1  christos 		}
    290  1.1  christos 	}
    291  1.1  christos 
    292  1.1  christos 	if (except & ~F_SEAL_GROW) {
    293  1.1  christos 		rv = ftruncate(fd, st.st_size + 1);
    294  1.1  christos 		if (rv == -1) {
    295  1.1  christos 		        ATF_REQUIRE_MSG(errno != EPERM,
    296  1.1  christos 			    "Seal %x prevented truncate to shrink", except);
    297  1.1  christos 		        ATF_REQUIRE_MSG(errno == EPERM,
    298  1.1  christos 			    "Truncate failed unexpectedly (%s)",
    299  1.1  christos 			    strerror(errno));
    300  1.1  christos 		}
    301  1.1  christos 	}
    302  1.1  christos }
    303  1.1  christos 
    304  1.1  christos ATF_TC(seal_shrink);
    305  1.1  christos ATF_TC_HEAD(seal_shrink, tc)
    306  1.1  christos {
    307  1.1  christos 
    308  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    309  1.1  christos 	    "Checks F_SEAL_SHRINK prevents shrinking the file");
    310  1.1  christos }
    311  1.1  christos ATF_TC_BODY(seal_shrink, tc)
    312  1.1  christos {
    313  1.1  christos 	int fd;
    314  1.1  christos 
    315  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    316  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)));
    317  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK));
    318  1.1  christos 
    319  1.1  christos 	ATF_REQUIRE_EQ_MSG(ftruncate(fd, sizeof(write_buf)/2), -1,
    320  1.1  christos 	    "Truncate succeeded unexpectedly");
    321  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    322  1.1  christos 
    323  1.1  christos 	test_all_seals_except(fd, F_SEAL_SHRINK);
    324  1.1  christos }
    325  1.1  christos 
    326  1.1  christos ATF_TC(seal_grow);
    327  1.1  christos ATF_TC_HEAD(seal_grow, tc)
    328  1.1  christos {
    329  1.1  christos 
    330  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    331  1.1  christos 	    "Checks F_SEAL_SHRINK prevents growing the file");
    332  1.1  christos }
    333  1.1  christos ATF_TC_BODY(seal_grow, tc)
    334  1.1  christos {
    335  1.1  christos 	int fd;
    336  1.1  christos 
    337  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    338  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    339  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_GROW));
    340  1.1  christos 
    341  1.1  christos 	ATF_REQUIRE_EQ_MSG(ftruncate(fd, sizeof(write_buf)), -1,
    342  1.1  christos 	    "Truncate succeeded unexpectedly");
    343  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    344  1.1  christos 
    345  1.1  christos 	test_all_seals_except(fd, F_SEAL_GROW);
    346  1.1  christos }
    347  1.1  christos 
    348  1.1  christos ATF_TC(seal_write);
    349  1.1  christos ATF_TC_HEAD(seal_write, tc)
    350  1.1  christos {
    351  1.1  christos 
    352  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    353  1.1  christos 	    "Checks F_SEAL_WRITE prevents writing");
    354  1.1  christos }
    355  1.1  christos ATF_TC_BODY(seal_write, tc)
    356  1.1  christos {
    357  1.1  christos 	int fd;
    358  1.1  christos 
    359  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    360  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    361  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE));
    362  1.1  christos 
    363  1.1  christos 	ATF_REQUIRE_EQ_MSG(write(fd, write_buf, sizeof(write_buf)), -1,
    364  1.1  christos 	    "Write succeeded unexpectedly");
    365  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    366  1.1  christos 
    367  1.1  christos 	test_all_seals_except(fd, F_SEAL_WRITE);
    368  1.1  christos }
    369  1.1  christos 
    370  1.1  christos ATF_TC(seal_write_mmap);
    371  1.1  christos ATF_TC_HEAD(seal_write_mmap, tc)
    372  1.1  christos {
    373  1.1  christos 
    374  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    375  1.1  christos 	    "Checks that F_SEAL_WRITE cannot be added with open mmaps");
    376  1.1  christos }
    377  1.1  christos ATF_TC_BODY(seal_write_mmap, tc)
    378  1.1  christos {
    379  1.1  christos 	int fd;
    380  1.1  christos 	void *addr;
    381  1.1  christos 
    382  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    383  1.1  christos 	RL(ftruncate(fd, sizeof(read_buf)));
    384  1.1  christos 
    385  1.1  christos 	addr = mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED,
    386  1.1  christos 	    fd, 0);
    387  1.4  riastrad 	ATF_REQUIRE_MSG(addr != MAP_FAILED,
    388  1.4  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) failed:"
    389  1.4  riastrad 	    " %s",
    390  1.4  riastrad 	    sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    391  1.1  christos 	    strerror(errno));
    392  1.1  christos 
    393  1.1  christos 	ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    394  1.1  christos 	    "fcntl succeeded unexpectedly");
    395  1.1  christos 	ATF_REQUIRE_ERRNO(EBUSY, true);
    396  1.1  christos }
    397  1.1  christos 
    398  1.1  christos ATF_TC(seal_future_write);
    399  1.1  christos ATF_TC_HEAD(seal_future_write, tc)
    400  1.1  christos {
    401  1.1  christos 
    402  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    403  1.1  christos 	    "Checks F_SEAL_FUTURE_WRITE prevents writing");
    404  1.1  christos }
    405  1.1  christos ATF_TC_BODY(seal_future_write, tc)
    406  1.1  christos {
    407  1.1  christos 	int fd;
    408  1.1  christos 
    409  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    410  1.1  christos 	RL(ftruncate(fd, sizeof(write_buf)/2));
    411  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE));
    412  1.1  christos 
    413  1.1  christos 	ATF_REQUIRE_EQ_MSG(write(fd, write_buf, sizeof(write_buf)), -1,
    414  1.1  christos 	    "Write succeeded unexpectedly");
    415  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    416  1.1  christos 
    417  1.1  christos 	test_all_seals_except(fd, F_SEAL_FUTURE_WRITE);
    418  1.1  christos }
    419  1.1  christos 
    420  1.1  christos ATF_TC(seal_future_write_mmap);
    421  1.1  christos ATF_TC_HEAD(seal_future_write_mmap, tc)
    422  1.1  christos {
    423  1.1  christos 
    424  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    425  1.1  christos 	    "Checks that F_SEAL_WRITE can be added with open mmaps but"
    426  1.1  christos 	    " prevents creating new ones");
    427  1.1  christos }
    428  1.1  christos ATF_TC_BODY(seal_future_write_mmap, tc)
    429  1.1  christos {
    430  1.1  christos 	int fd;
    431  1.1  christos 	void *addr;
    432  1.1  christos 
    433  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    434  1.1  christos 	RL(ftruncate(fd, sizeof(read_buf)));
    435  1.1  christos 	addr = mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED,
    436  1.1  christos 	    fd, 0);
    437  1.4  riastrad 	ATF_REQUIRE_MSG(addr != MAP_FAILED,
    438  1.4  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) failed:"
    439  1.4  riastrad 	    " %s",
    440  1.4  riastrad 	    sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    441  1.1  christos 	    strerror(errno));
    442  1.1  christos 
    443  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE));
    444  1.1  christos 
    445  1.1  christos 	ATF_REQUIRE_EQ_MSG(mmap(NULL, sizeof(read_buf), PROT_READ|PROT_WRITE,
    446  1.4  riastrad 		MAP_SHARED, fd, 0), MAP_FAILED,
    447  1.4  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) succeeded unexpectedly",
    448  1.4  riastrad 	    sizeof(read_buf), PROT_READ|PROT_WRITE, MAP_SHARED, fd);
    449  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    450  1.1  christos }
    451  1.1  christos 
    452  1.1  christos 
    453  1.1  christos ATF_TP_ADD_TCS(tp)
    454  1.1  christos {
    455  1.1  christos 	ATF_TP_ADD_TC(tp, create_null_name);
    456  1.1  christos 	ATF_TP_ADD_TC(tp, create_long_name);
    457  1.1  christos 	ATF_TP_ADD_TC(tp, read_write);
    458  1.1  christos 	ATF_TP_ADD_TC(tp, truncate);
    459  1.1  christos 	ATF_TP_ADD_TC(tp, mmap);
    460  1.1  christos 	ATF_TP_ADD_TC(tp, create_no_sealing);
    461  1.1  christos 	ATF_TP_ADD_TC(tp, seal_seal);
    462  1.1  christos 	ATF_TP_ADD_TC(tp, seal_shrink);
    463  1.1  christos 	ATF_TP_ADD_TC(tp, seal_grow);
    464  1.1  christos 	ATF_TP_ADD_TC(tp, seal_write);
    465  1.1  christos 	ATF_TP_ADD_TC(tp, seal_write_mmap);
    466  1.1  christos 	ATF_TP_ADD_TC(tp, seal_future_write);
    467  1.1  christos 	ATF_TP_ADD_TC(tp, seal_future_write_mmap);
    468  1.1  christos 
    469  1.1  christos 	return atf_no_error();
    470  1.1  christos }
    471