Home | History | Annotate | Line # | Download | only in kernel
      1  1.7  riastrad /*	$NetBSD: t_memfd_create.c,v 1.7 2025/04/19 01:56:50 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.7  riastrad __RCSID("$NetBSD: t_memfd_create.c,v 1.7 2025/04/19 01:56:50 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.5  riastrad char *write_buf;
     47  1.5  riastrad char *read_buf;
     48  1.5  riastrad size_t rwbuf_size;
     49  1.5  riastrad 
     50  1.5  riastrad static void
     51  1.5  riastrad setupbufs(void)
     52  1.5  riastrad {
     53  1.5  riastrad 
     54  1.5  riastrad 	rwbuf_size = (size_t)sysconf(_SC_PAGESIZE);
     55  1.5  riastrad 	ATF_REQUIRE_MSG(rwbuf_size < SIZE_MAX/2, "rwbuf_size=%zu", rwbuf_size);
     56  1.5  riastrad 	rwbuf_size *= 2;
     57  1.5  riastrad 	REQUIRE_LIBC(write_buf = calloc(1, rwbuf_size), NULL);
     58  1.5  riastrad 	REQUIRE_LIBC(read_buf = calloc(1, rwbuf_size), NULL);
     59  1.5  riastrad }
     60  1.1  christos 
     61  1.1  christos ATF_TC(create_null_name);
     62  1.1  christos ATF_TC_HEAD(create_null_name, tc)
     63  1.1  christos {
     64  1.1  christos 
     65  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     66  1.1  christos 	    "Checks memfd_create fails with EFAULT when invalid memory"
     67  1.1  christos 	    " is provided");
     68  1.1  christos }
     69  1.1  christos ATF_TC_BODY(create_null_name, tc)
     70  1.1  christos {
     71  1.1  christos 	int fd;
     72  1.1  christos 
     73  1.1  christos 	ATF_REQUIRE_EQ_MSG(fd = memfd_create(NULL, 0), -1,
     74  1.1  christos 	    "Unexpected success");
     75  1.1  christos 	ATF_REQUIRE_ERRNO(EFAULT, true);
     76  1.1  christos }
     77  1.1  christos 
     78  1.1  christos ATF_TC(create_long_name);
     79  1.1  christos ATF_TC_HEAD(create_long_name, tc)
     80  1.1  christos {
     81  1.1  christos 
     82  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     83  1.1  christos 	    "Checks memfd_create fails for names longer than NAME_MAX-6");
     84  1.1  christos }
     85  1.1  christos ATF_TC_BODY(create_long_name, tc)
     86  1.1  christos {
     87  1.1  christos 	int fd;
     88  1.1  christos 
     89  1.1  christos         memset(name_buf, 'A', sizeof(name_buf));
     90  1.1  christos 	name_buf[NAME_MAX-6] = '\0';
     91  1.1  christos 
     92  1.1  christos 	ATF_REQUIRE_EQ_MSG(fd = memfd_create(name_buf, 0), -1,
     93  1.1  christos 	    "Unexpected success");
     94  1.1  christos 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, true);
     95  1.1  christos 
     96  1.1  christos 	name_buf[NAME_MAX-7] = '\0';
     97  1.1  christos 
     98  1.1  christos 	RL(fd = memfd_create(name_buf, 0));
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos ATF_TC(read_write);
    102  1.1  christos ATF_TC_HEAD(read_write, tc)
    103  1.1  christos {
    104  1.1  christos 
    105  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    106  1.1  christos 	    "Checks that data can be written to/read from a memfd");
    107  1.1  christos }
    108  1.1  christos ATF_TC_BODY(read_write, tc)
    109  1.1  christos {
    110  1.1  christos 	int fd;
    111  1.1  christos 	off_t offset;
    112  1.1  christos 
    113  1.5  riastrad 	setupbufs();
    114  1.5  riastrad 
    115  1.1  christos 	RL(fd = memfd_create("", 0));
    116  1.1  christos 
    117  1.5  riastrad 	tests_makegarbage(write_buf, rwbuf_size);
    118  1.1  christos 
    119  1.5  riastrad 	RL(write(fd, write_buf, rwbuf_size));
    120  1.1  christos 	offset = lseek(fd, 0, SEEK_CUR);
    121  1.7  riastrad 	ATF_REQUIRE_EQ_MSG(offset, (off_t)rwbuf_size,
    122  1.2       rin 	    "File offset not set after write (%jd != %zu)", (intmax_t)offset,
    123  1.5  riastrad 	    rwbuf_size);
    124  1.1  christos 
    125  1.3  riastrad 	RL(lseek(fd, 0, SEEK_SET));
    126  1.1  christos 
    127  1.5  riastrad 	RL(read(fd, read_buf, rwbuf_size));
    128  1.1  christos 	offset = lseek(fd, 0, SEEK_CUR);
    129  1.7  riastrad 	ATF_REQUIRE_EQ_MSG(offset, (off_t)rwbuf_size,
    130  1.2       rin 	    "File offset not set after read (%jd != %zu)", (intmax_t)offset,
    131  1.5  riastrad 	    rwbuf_size);
    132  1.1  christos 
    133  1.5  riastrad 	for (size_t i = 0; i < rwbuf_size; i++)
    134  1.1  christos 		ATF_REQUIRE_EQ_MSG(read_buf[i], write_buf[i],
    135  1.1  christos 		    "Data read does not match data written");
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos ATF_TC(truncate);
    139  1.1  christos ATF_TC_HEAD(truncate, tc)
    140  1.1  christos {
    141  1.1  christos 
    142  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    143  1.1  christos 	    "Checks that truncation does result in data removal");
    144  1.1  christos }
    145  1.1  christos ATF_TC_BODY(truncate, tc)
    146  1.1  christos {
    147  1.1  christos 	int fd;
    148  1.1  christos 	struct stat st;
    149  1.1  christos 
    150  1.5  riastrad 	setupbufs();
    151  1.5  riastrad 
    152  1.1  christos 	RL(fd = memfd_create("", 0));
    153  1.1  christos 
    154  1.5  riastrad 	tests_makegarbage(write_buf, rwbuf_size);
    155  1.5  riastrad 	tests_makegarbage(read_buf, rwbuf_size);
    156  1.1  christos 
    157  1.5  riastrad 	RL(write(fd, write_buf, rwbuf_size));
    158  1.1  christos 
    159  1.1  christos 	RL(fstat(fd, &st));
    160  1.7  riastrad 	ATF_REQUIRE_EQ_MSG(st.st_size, (off_t)rwbuf_size,
    161  1.5  riastrad 	    "Write did not grow size to %zu (is %jd)", rwbuf_size,
    162  1.2       rin 	    (intmax_t)st.st_size);
    163  1.1  christos 
    164  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size/2));
    165  1.1  christos 	RL(fstat(fd, &st));
    166  1.7  riastrad 	ATF_REQUIRE_EQ_MSG(st.st_size, (off_t)rwbuf_size/2,
    167  1.2       rin 	    "Truncate did not shrink size to %zu (is %jd)",
    168  1.5  riastrad 	    rwbuf_size/2, (intmax_t)st.st_size);
    169  1.1  christos 
    170  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size));
    171  1.1  christos 	RL(fstat(fd, &st));
    172  1.7  riastrad 	ATF_REQUIRE_EQ_MSG(st.st_size, (off_t)rwbuf_size,
    173  1.5  riastrad 	    "Truncate did not grow size to %zu (is %jd)", rwbuf_size,
    174  1.2       rin 	    (intmax_t)st.st_size);
    175  1.1  christos 
    176  1.3  riastrad 	RL(lseek(fd, 0, SEEK_SET));
    177  1.5  riastrad 	RL(read(fd, read_buf, rwbuf_size));
    178  1.1  christos 
    179  1.5  riastrad 	for (size_t i = 0; i < rwbuf_size/2; i++)
    180  1.1  christos 		ATF_REQUIRE_EQ_MSG(read_buf[i], write_buf[i],
    181  1.1  christos 		    "Data read does not match data written");
    182  1.5  riastrad 	for (size_t i = rwbuf_size/2; i < rwbuf_size; i++)
    183  1.1  christos 		ATF_REQUIRE_EQ_MSG(read_buf[i], 0,
    184  1.1  christos 		    "Data read on growed region is not zeroed");
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos ATF_TC(mmap);
    188  1.1  christos ATF_TC_HEAD(mmap, tc)
    189  1.1  christos {
    190  1.1  christos 
    191  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Check that mmap succeeds");
    192  1.1  christos }
    193  1.1  christos ATF_TC_BODY(mmap, tc)
    194  1.1  christos {
    195  1.1  christos 	int fd;
    196  1.1  christos 	void *addr;
    197  1.1  christos 
    198  1.5  riastrad 	setupbufs();
    199  1.5  riastrad 
    200  1.1  christos 	RL(fd = memfd_create("", 0));
    201  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size));
    202  1.1  christos 
    203  1.5  riastrad 	addr = mmap(NULL, rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    204  1.4  riastrad 	ATF_REQUIRE_MSG(addr != MAP_FAILED,
    205  1.5  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) failed: %s",
    206  1.5  riastrad 	    rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    207  1.1  christos 	    strerror(errno));
    208  1.1  christos }
    209  1.1  christos 
    210  1.1  christos ATF_TC(create_no_sealing);
    211  1.1  christos ATF_TC_HEAD(create_no_sealing, tc)
    212  1.1  christos {
    213  1.1  christos 
    214  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    215  1.1  christos 	    "Checks that seals cannot be added if MFD_ALLOW_SEALING is"
    216  1.1  christos 	    " not specified to memfd_create");
    217  1.1  christos }
    218  1.1  christos ATF_TC_BODY(create_no_sealing, tc)
    219  1.1  christos {
    220  1.1  christos 	int fd;
    221  1.1  christos 
    222  1.1  christos 	RL(fd = memfd_create("", 0));
    223  1.1  christos 
    224  1.1  christos 	ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    225  1.1  christos 	    "fcntl succeeded unexpectedly");
    226  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    227  1.1  christos }
    228  1.1  christos 
    229  1.1  christos ATF_TC(seal_seal);
    230  1.1  christos ATF_TC_HEAD(seal_seal, tc)
    231  1.1  christos {
    232  1.1  christos 
    233  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    234  1.1  christos 	    "Checks adding F_SEAL_SEAL prevents adding other seals");
    235  1.1  christos }
    236  1.1  christos ATF_TC_BODY(seal_seal, tc)
    237  1.1  christos {
    238  1.1  christos 	int fd;
    239  1.1  christos 
    240  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    241  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL));
    242  1.1  christos 
    243  1.1  christos         ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    244  1.1  christos 	    "fcntl succeeded unexpectedly");
    245  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    246  1.1  christos }
    247  1.1  christos 
    248  1.1  christos /*
    249  1.1  christos  * Tests that the seals provided in except to not also prevent some
    250  1.1  christos  * other operation.
    251  1.1  christos  *
    252  1.1  christos  * Note: fd must have a positive size.
    253  1.1  christos  */
    254  1.1  christos static void
    255  1.1  christos test_all_seals_except(int fd, int except)
    256  1.1  christos {
    257  1.1  christos 	int rv;
    258  1.1  christos 	struct stat st;
    259  1.1  christos 	void *addr;
    260  1.1  christos 
    261  1.5  riastrad 	ATF_REQUIRE(rwbuf_size > 0);
    262  1.5  riastrad 
    263  1.1  christos 	RL(fstat(fd, &st));
    264  1.1  christos 	ATF_REQUIRE(st.st_size > 0);
    265  1.1  christos 
    266  1.1  christos 	if (except & ~F_SEAL_SEAL) {
    267  1.1  christos 		rv = fcntl(fd, F_ADD_SEALS, F_SEAL_SEAL);
    268  1.1  christos 		if (rv == -1) {
    269  1.1  christos 			ATF_REQUIRE_MSG(errno != EPERM,
    270  1.1  christos 			    "Seal %x prevented F_ADD_SEALS", except);
    271  1.1  christos 			ATF_REQUIRE_MSG(errno == EPERM,
    272  1.1  christos 			    "F_ADD_SEALS failed unexpectedly (%s)",
    273  1.1  christos 			    strerror(errno));
    274  1.1  christos 		}
    275  1.1  christos 	}
    276  1.1  christos 
    277  1.1  christos 	if (except & ~(F_SEAL_WRITE|F_SEAL_FUTURE_WRITE)) {
    278  1.3  riastrad 		RL(lseek(fd, 0, SEEK_SET));
    279  1.5  riastrad 		rv = write(fd, write_buf, rwbuf_size);
    280  1.1  christos 		if (rv == -1) {
    281  1.1  christos 			ATF_REQUIRE_MSG(errno != EPERM,
    282  1.1  christos 			    "Seal %x prevented write", except);
    283  1.1  christos 		        ATF_REQUIRE_MSG(errno == EPERM,
    284  1.1  christos 			    "Write failed unexpectedly (%s)",
    285  1.1  christos 			    strerror(errno));
    286  1.1  christos 		}
    287  1.1  christos 
    288  1.5  riastrad 	        addr = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
    289  1.5  riastrad 		    fd, 0);
    290  1.1  christos 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
    291  1.5  riastrad 		    "mmap(NULL, %llu, 0x%x, 0x%x, %d, 0) failed: %s",
    292  1.4  riastrad 		    (unsigned long long)st.st_size,
    293  1.4  riastrad 		    PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    294  1.4  riastrad 		    strerror(errno));
    295  1.1  christos 	}
    296  1.1  christos 
    297  1.1  christos 	if (except & ~F_SEAL_SHRINK) {
    298  1.1  christos 		rv = ftruncate(fd, st.st_size - 1);
    299  1.1  christos 		if (rv == -1) {
    300  1.1  christos 		        ATF_REQUIRE_MSG(errno != EPERM,
    301  1.1  christos 			    "Seal %x prevented truncate to shrink", except);
    302  1.1  christos 			ATF_REQUIRE_MSG(errno == EPERM,
    303  1.1  christos 			    "Truncate failed unexpectedly (%s)",
    304  1.1  christos 			    strerror(errno));
    305  1.1  christos 		}
    306  1.1  christos 	}
    307  1.1  christos 
    308  1.1  christos 	if (except & ~F_SEAL_GROW) {
    309  1.1  christos 		rv = ftruncate(fd, st.st_size + 1);
    310  1.1  christos 		if (rv == -1) {
    311  1.1  christos 		        ATF_REQUIRE_MSG(errno != EPERM,
    312  1.1  christos 			    "Seal %x prevented truncate to shrink", except);
    313  1.1  christos 		        ATF_REQUIRE_MSG(errno == EPERM,
    314  1.1  christos 			    "Truncate failed unexpectedly (%s)",
    315  1.1  christos 			    strerror(errno));
    316  1.1  christos 		}
    317  1.1  christos 	}
    318  1.1  christos }
    319  1.1  christos 
    320  1.1  christos ATF_TC(seal_shrink);
    321  1.1  christos ATF_TC_HEAD(seal_shrink, tc)
    322  1.1  christos {
    323  1.1  christos 
    324  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    325  1.1  christos 	    "Checks F_SEAL_SHRINK prevents shrinking the file");
    326  1.1  christos }
    327  1.1  christos ATF_TC_BODY(seal_shrink, tc)
    328  1.1  christos {
    329  1.1  christos 	int fd;
    330  1.1  christos 
    331  1.5  riastrad 	setupbufs();
    332  1.5  riastrad 
    333  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    334  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size));
    335  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK));
    336  1.1  christos 
    337  1.5  riastrad 	ATF_REQUIRE_EQ_MSG(ftruncate(fd, rwbuf_size/2), -1,
    338  1.1  christos 	    "Truncate succeeded unexpectedly");
    339  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    340  1.1  christos 
    341  1.1  christos 	test_all_seals_except(fd, F_SEAL_SHRINK);
    342  1.1  christos }
    343  1.1  christos 
    344  1.1  christos ATF_TC(seal_grow);
    345  1.1  christos ATF_TC_HEAD(seal_grow, tc)
    346  1.1  christos {
    347  1.1  christos 
    348  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    349  1.1  christos 	    "Checks F_SEAL_SHRINK prevents growing the file");
    350  1.1  christos }
    351  1.1  christos ATF_TC_BODY(seal_grow, tc)
    352  1.1  christos {
    353  1.1  christos 	int fd;
    354  1.1  christos 
    355  1.5  riastrad 	setupbufs();
    356  1.5  riastrad 
    357  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    358  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size/2));
    359  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_GROW));
    360  1.1  christos 
    361  1.5  riastrad 	ATF_REQUIRE_EQ_MSG(ftruncate(fd, rwbuf_size), -1,
    362  1.1  christos 	    "Truncate succeeded unexpectedly");
    363  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    364  1.1  christos 
    365  1.1  christos 	test_all_seals_except(fd, F_SEAL_GROW);
    366  1.1  christos }
    367  1.1  christos 
    368  1.1  christos ATF_TC(seal_write);
    369  1.1  christos ATF_TC_HEAD(seal_write, tc)
    370  1.1  christos {
    371  1.1  christos 
    372  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    373  1.1  christos 	    "Checks F_SEAL_WRITE prevents writing");
    374  1.1  christos }
    375  1.1  christos ATF_TC_BODY(seal_write, tc)
    376  1.1  christos {
    377  1.1  christos 	int fd;
    378  1.1  christos 
    379  1.5  riastrad 	setupbufs();
    380  1.5  riastrad 
    381  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    382  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size/2));
    383  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE));
    384  1.1  christos 
    385  1.5  riastrad 	ATF_REQUIRE_EQ_MSG(write(fd, write_buf, rwbuf_size), -1,
    386  1.1  christos 	    "Write succeeded unexpectedly");
    387  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    388  1.1  christos 
    389  1.1  christos 	test_all_seals_except(fd, F_SEAL_WRITE);
    390  1.1  christos }
    391  1.1  christos 
    392  1.1  christos ATF_TC(seal_write_mmap);
    393  1.1  christos ATF_TC_HEAD(seal_write_mmap, tc)
    394  1.1  christos {
    395  1.1  christos 
    396  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    397  1.1  christos 	    "Checks that F_SEAL_WRITE cannot be added with open mmaps");
    398  1.1  christos }
    399  1.1  christos ATF_TC_BODY(seal_write_mmap, tc)
    400  1.1  christos {
    401  1.1  christos 	int fd;
    402  1.1  christos 	void *addr;
    403  1.1  christos 
    404  1.5  riastrad 	setupbufs();
    405  1.5  riastrad 
    406  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    407  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size));
    408  1.1  christos 
    409  1.5  riastrad 	addr = mmap(NULL, rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    410  1.4  riastrad 	ATF_REQUIRE_MSG(addr != MAP_FAILED,
    411  1.5  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) failed: %s",
    412  1.5  riastrad 	    rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    413  1.1  christos 	    strerror(errno));
    414  1.1  christos 
    415  1.1  christos 	ATF_REQUIRE_EQ_MSG(fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE), -1,
    416  1.1  christos 	    "fcntl succeeded unexpectedly");
    417  1.1  christos 	ATF_REQUIRE_ERRNO(EBUSY, true);
    418  1.1  christos }
    419  1.1  christos 
    420  1.1  christos ATF_TC(seal_future_write);
    421  1.1  christos ATF_TC_HEAD(seal_future_write, 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 F_SEAL_FUTURE_WRITE prevents writing");
    426  1.1  christos }
    427  1.1  christos ATF_TC_BODY(seal_future_write, tc)
    428  1.1  christos {
    429  1.1  christos 	int fd;
    430  1.1  christos 
    431  1.5  riastrad 	setupbufs();
    432  1.5  riastrad 
    433  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    434  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size/2));
    435  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE));
    436  1.1  christos 
    437  1.5  riastrad 	ATF_REQUIRE_EQ_MSG(write(fd, write_buf, rwbuf_size), -1,
    438  1.1  christos 	    "Write succeeded unexpectedly");
    439  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    440  1.1  christos 
    441  1.1  christos 	test_all_seals_except(fd, F_SEAL_FUTURE_WRITE);
    442  1.1  christos }
    443  1.1  christos 
    444  1.1  christos ATF_TC(seal_future_write_mmap);
    445  1.1  christos ATF_TC_HEAD(seal_future_write_mmap, tc)
    446  1.1  christos {
    447  1.1  christos 
    448  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    449  1.1  christos 	    "Checks that F_SEAL_WRITE can be added with open mmaps but"
    450  1.1  christos 	    " prevents creating new ones");
    451  1.1  christos }
    452  1.1  christos ATF_TC_BODY(seal_future_write_mmap, tc)
    453  1.1  christos {
    454  1.1  christos 	int fd;
    455  1.1  christos 	void *addr;
    456  1.1  christos 
    457  1.5  riastrad 	setupbufs();
    458  1.5  riastrad 
    459  1.1  christos 	RL(fd = memfd_create("", MFD_ALLOW_SEALING));
    460  1.5  riastrad 	RL(ftruncate(fd, rwbuf_size));
    461  1.5  riastrad 	addr = mmap(NULL, rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    462  1.4  riastrad 	ATF_REQUIRE_MSG(addr != MAP_FAILED,
    463  1.5  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) failed: %s",
    464  1.5  riastrad 	    rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
    465  1.1  christos 	    strerror(errno));
    466  1.1  christos 
    467  1.1  christos 	RL(fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE));
    468  1.1  christos 
    469  1.5  riastrad 	ATF_REQUIRE_EQ_MSG(mmap(NULL, rwbuf_size, PROT_READ|PROT_WRITE,
    470  1.4  riastrad 		MAP_SHARED, fd, 0), MAP_FAILED,
    471  1.4  riastrad 	    "mmap(NULL, %zu, 0x%x, 0x%x, %d, 0) succeeded unexpectedly",
    472  1.5  riastrad 	    rwbuf_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd);
    473  1.1  christos 	ATF_REQUIRE_ERRNO(EPERM, true);
    474  1.1  christos }
    475  1.1  christos 
    476  1.1  christos 
    477  1.1  christos ATF_TP_ADD_TCS(tp)
    478  1.1  christos {
    479  1.1  christos 	ATF_TP_ADD_TC(tp, create_null_name);
    480  1.1  christos 	ATF_TP_ADD_TC(tp, create_long_name);
    481  1.1  christos 	ATF_TP_ADD_TC(tp, read_write);
    482  1.1  christos 	ATF_TP_ADD_TC(tp, truncate);
    483  1.1  christos 	ATF_TP_ADD_TC(tp, mmap);
    484  1.1  christos 	ATF_TP_ADD_TC(tp, create_no_sealing);
    485  1.1  christos 	ATF_TP_ADD_TC(tp, seal_seal);
    486  1.1  christos 	ATF_TP_ADD_TC(tp, seal_shrink);
    487  1.1  christos 	ATF_TP_ADD_TC(tp, seal_grow);
    488  1.1  christos 	ATF_TP_ADD_TC(tp, seal_write);
    489  1.1  christos 	ATF_TP_ADD_TC(tp, seal_write_mmap);
    490  1.1  christos 	ATF_TP_ADD_TC(tp, seal_future_write);
    491  1.1  christos 	ATF_TP_ADD_TC(tp, seal_future_write_mmap);
    492  1.1  christos 
    493  1.1  christos 	return atf_no_error();
    494  1.1  christos }
    495