memfd_test.c 21.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#define _GNU_SOURCE
#define __EXPORTED_HEADERS__

#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <linux/falloc.h>
#include <linux/fcntl.h>
#include <linux/memfd.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
19
#include <sys/wait.h>
20 21
#include <unistd.h>

22 23 24
#define MEMFD_STR	"memfd:"
#define SHARED_FT_STR	"(shared file-table)"

25
#define MFD_DEF_SIZE 8192
26
#define STACK_SIZE 65536
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
 * Default is not to test hugetlbfs
 */
static int hugetlbfs_test;
static size_t mfd_def_size = MFD_DEF_SIZE;

/*
 * Copied from mlock2-tests.c
 */
static unsigned long default_huge_page_size(void)
{
	unsigned long hps = 0;
	char *line = NULL;
	size_t linelen = 0;
	FILE *f = fopen("/proc/meminfo", "r");

	if (!f)
		return 0;
	while (getline(&line, &linelen, f) > 0) {
		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
			hps <<= 10;
			break;
		}
	}

	free(line);
	fclose(f);
	return hps;
}

58 59 60
static int sys_memfd_create(const char *name,
			    unsigned int flags)
{
61 62 63
	if (hugetlbfs_test)
		flags |= MFD_HUGETLB;

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	return syscall(__NR_memfd_create, name, flags);
}

static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
{
	int r, fd;

	fd = sys_memfd_create(name, flags);
	if (fd < 0) {
		printf("memfd_create(\"%s\", %u) failed: %m\n",
		       name, flags);
		abort();
	}

	r = ftruncate(fd, sz);
	if (r < 0) {
		printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
		abort();
	}

	return fd;
}

static void mfd_fail_new(const char *name, unsigned int flags)
{
	int r;

	r = sys_memfd_create(name, flags);
	if (r >= 0) {
		printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
		       name, flags);
		close(r);
		abort();
	}
}

100
static unsigned int mfd_assert_get_seals(int fd)
101
{
102
	int r;
103 104 105 106 107 108 109

	r = fcntl(fd, F_GET_SEALS);
	if (r < 0) {
		printf("GET_SEALS(%d) failed: %m\n", fd);
		abort();
	}

110
	return (unsigned int)r;
111 112
}

113
static void mfd_assert_has_seals(int fd, unsigned int seals)
114
{
115
	unsigned int s;
116 117 118

	s = mfd_assert_get_seals(fd);
	if (s != seals) {
119
		printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
120 121 122 123
		abort();
	}
}

124
static void mfd_assert_add_seals(int fd, unsigned int seals)
125
{
126 127
	int r;
	unsigned int s;
128 129 130 131

	s = mfd_assert_get_seals(fd);
	r = fcntl(fd, F_ADD_SEALS, seals);
	if (r < 0) {
132
		printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
133 134 135 136
		abort();
	}
}

137
static void mfd_fail_add_seals(int fd, unsigned int seals)
138
{
139 140
	int r;
	unsigned int s;
141 142 143 144 145

	r = fcntl(fd, F_GET_SEALS);
	if (r < 0)
		s = 0;
	else
146
		s = (unsigned int)r;
147 148 149

	r = fcntl(fd, F_ADD_SEALS, seals);
	if (r >= 0) {
150 151
		printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
				fd, s, seals);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		abort();
	}
}

static void mfd_assert_size(int fd, size_t size)
{
	struct stat st;
	int r;

	r = fstat(fd, &st);
	if (r < 0) {
		printf("fstat(%d) failed: %m\n", fd);
		abort();
	} else if (st.st_size != size) {
		printf("wrong file size %lld, but expected %lld\n",
		       (long long)st.st_size, (long long)size);
		abort();
	}
}

static int mfd_assert_dup(int fd)
{
	int r;

	r = dup(fd);
	if (r < 0) {
		printf("dup(%d) failed: %m\n", fd);
		abort();
	}

	return r;
}

static void *mfd_assert_mmap_shared(int fd)
{
	void *p;

	p = mmap(NULL,
190
		 mfd_def_size,
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
		 PROT_READ | PROT_WRITE,
		 MAP_SHARED,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}

	return p;
}

static void *mfd_assert_mmap_private(int fd)
{
	void *p;

	p = mmap(NULL,
208
		 mfd_def_size,
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		 PROT_READ,
		 MAP_PRIVATE,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}

	return p;
}

static int mfd_assert_open(int fd, int flags, mode_t mode)
{
	char buf[512];
	int r;

	sprintf(buf, "/proc/self/fd/%d", fd);
	r = open(buf, flags, mode);
	if (r < 0) {
		printf("open(%s) failed: %m\n", buf);
		abort();
	}

	return r;
}

static void mfd_fail_open(int fd, int flags, mode_t mode)
{
	char buf[512];
	int r;

	sprintf(buf, "/proc/self/fd/%d", fd);
	r = open(buf, flags, mode);
	if (r >= 0) {
244
		printf("open(%s) didn't fail as expected\n", buf);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		abort();
	}
}

static void mfd_assert_read(int fd)
{
	char buf[16];
	void *p;
	ssize_t l;

	l = read(fd, buf, sizeof(buf));
	if (l != sizeof(buf)) {
		printf("read() failed: %m\n");
		abort();
	}

	/* verify PROT_READ *is* allowed */
	p = mmap(NULL,
263
		 mfd_def_size,
264 265 266 267 268 269 270 271
		 PROT_READ,
		 MAP_PRIVATE,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}
272
	munmap(p, mfd_def_size);
273 274 275

	/* verify MAP_PRIVATE is *always* allowed (even writable) */
	p = mmap(NULL,
276
		 mfd_def_size,
277 278 279 280 281 282 283 284
		 PROT_READ | PROT_WRITE,
		 MAP_PRIVATE,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}
285
	munmap(p, mfd_def_size);
286 287 288 289 290 291 292 293
}

static void mfd_assert_write(int fd)
{
	ssize_t l;
	void *p;
	int r;

294 295 296 297 298 299 300 301 302 303 304
	/*
	 * huegtlbfs does not support write, but we want to
	 * verify everything else here.
	 */
	if (!hugetlbfs_test) {
		/* verify write() succeeds */
		l = write(fd, "\0\0\0\0", 4);
		if (l != 4) {
			printf("write() failed: %m\n");
			abort();
		}
305 306 307 308
	}

	/* verify PROT_READ | PROT_WRITE is allowed */
	p = mmap(NULL,
309
		 mfd_def_size,
310 311 312 313 314 315 316 317 318
		 PROT_READ | PROT_WRITE,
		 MAP_SHARED,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}
	*(char *)p = 0;
319
	munmap(p, mfd_def_size);
320 321 322

	/* verify PROT_WRITE is allowed */
	p = mmap(NULL,
323
		 mfd_def_size,
324 325 326 327 328 329 330 331 332
		 PROT_WRITE,
		 MAP_SHARED,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}
	*(char *)p = 0;
333
	munmap(p, mfd_def_size);
334 335 336 337

	/* verify PROT_READ with MAP_SHARED is allowed and a following
	 * mprotect(PROT_WRITE) allows writing */
	p = mmap(NULL,
338
		 mfd_def_size,
339 340 341 342 343 344 345 346 347
		 PROT_READ,
		 MAP_SHARED,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}

348
	r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
349 350 351 352 353 354
	if (r < 0) {
		printf("mprotect() failed: %m\n");
		abort();
	}

	*(char *)p = 0;
355
	munmap(p, mfd_def_size);
356 357 358 359 360

	/* verify PUNCH_HOLE works */
	r = fallocate(fd,
		      FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
		      0,
361
		      mfd_def_size);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	if (r < 0) {
		printf("fallocate(PUNCH_HOLE) failed: %m\n");
		abort();
	}
}

static void mfd_fail_write(int fd)
{
	ssize_t l;
	void *p;
	int r;

	/* verify write() fails */
	l = write(fd, "data", 4);
	if (l != -EPERM) {
		printf("expected EPERM on write(), but got %d: %m\n", (int)l);
		abort();
	}

	/* verify PROT_READ | PROT_WRITE is not allowed */
	p = mmap(NULL,
383
		 mfd_def_size,
384 385 386 387 388 389 390 391 392 393 394
		 PROT_READ | PROT_WRITE,
		 MAP_SHARED,
		 fd,
		 0);
	if (p != MAP_FAILED) {
		printf("mmap() didn't fail as expected\n");
		abort();
	}

	/* verify PROT_WRITE is not allowed */
	p = mmap(NULL,
395
		 mfd_def_size,
396 397 398 399 400 401 402 403 404 405 406 407
		 PROT_WRITE,
		 MAP_SHARED,
		 fd,
		 0);
	if (p != MAP_FAILED) {
		printf("mmap() didn't fail as expected\n");
		abort();
	}

	/* Verify PROT_READ with MAP_SHARED with a following mprotect is not
	 * allowed. Note that for r/w the kernel already prevents the mmap. */
	p = mmap(NULL,
408
		 mfd_def_size,
409 410 411 412 413
		 PROT_READ,
		 MAP_SHARED,
		 fd,
		 0);
	if (p != MAP_FAILED) {
414
		r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
415 416 417 418 419 420 421 422 423 424
		if (r >= 0) {
			printf("mmap()+mprotect() didn't fail as expected\n");
			abort();
		}
	}

	/* verify PUNCH_HOLE fails */
	r = fallocate(fd,
		      FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
		      0,
425
		      mfd_def_size);
426 427 428 429 430 431 432 433 434 435
	if (r >= 0) {
		printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
		abort();
	}
}

static void mfd_assert_shrink(int fd)
{
	int r, fd2;

436
	r = ftruncate(fd, mfd_def_size / 2);
437 438 439 440 441
	if (r < 0) {
		printf("ftruncate(SHRINK) failed: %m\n");
		abort();
	}

442
	mfd_assert_size(fd, mfd_def_size / 2);
443 444 445 446 447 448 449 450 451 452 453 454 455

	fd2 = mfd_assert_open(fd,
			      O_RDWR | O_CREAT | O_TRUNC,
			      S_IRUSR | S_IWUSR);
	close(fd2);

	mfd_assert_size(fd, 0);
}

static void mfd_fail_shrink(int fd)
{
	int r;

456
	r = ftruncate(fd, mfd_def_size / 2);
457 458 459 460 461 462 463 464 465 466 467 468 469 470
	if (r >= 0) {
		printf("ftruncate(SHRINK) didn't fail as expected\n");
		abort();
	}

	mfd_fail_open(fd,
		      O_RDWR | O_CREAT | O_TRUNC,
		      S_IRUSR | S_IWUSR);
}

static void mfd_assert_grow(int fd)
{
	int r;

471
	r = ftruncate(fd, mfd_def_size * 2);
472 473 474 475 476
	if (r < 0) {
		printf("ftruncate(GROW) failed: %m\n");
		abort();
	}

477
	mfd_assert_size(fd, mfd_def_size * 2);
478 479 480 481

	r = fallocate(fd,
		      0,
		      0,
482
		      mfd_def_size * 4);
483 484 485 486 487
	if (r < 0) {
		printf("fallocate(ALLOC) failed: %m\n");
		abort();
	}

488
	mfd_assert_size(fd, mfd_def_size * 4);
489 490 491 492 493 494
}

static void mfd_fail_grow(int fd)
{
	int r;

495
	r = ftruncate(fd, mfd_def_size * 2);
496 497 498 499 500 501 502 503
	if (r >= 0) {
		printf("ftruncate(GROW) didn't fail as expected\n");
		abort();
	}

	r = fallocate(fd,
		      0,
		      0,
504
		      mfd_def_size * 4);
505 506 507 508 509 510 511 512
	if (r >= 0) {
		printf("fallocate(ALLOC) didn't fail as expected\n");
		abort();
	}
}

static void mfd_assert_grow_write(int fd)
{
513
	static char *buf;
514 515
	ssize_t l;

516 517
	buf = malloc(mfd_def_size * 8);
	if (!buf) {
518
		printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
519 520 521 522 523
		abort();
	}

	l = pwrite(fd, buf, mfd_def_size * 8, 0);
	if (l != (mfd_def_size * 8)) {
524 525 526 527
		printf("pwrite() failed: %m\n");
		abort();
	}

528
	mfd_assert_size(fd, mfd_def_size * 8);
529 530 531 532
}

static void mfd_fail_grow_write(int fd)
{
533
	static char *buf;
534 535
	ssize_t l;

536 537
	buf = malloc(mfd_def_size * 8);
	if (!buf) {
538
		printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
539 540 541 542 543
		abort();
	}

	l = pwrite(fd, buf, mfd_def_size * 8, 0);
	if (l == (mfd_def_size * 8)) {
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		printf("pwrite() didn't fail as expected\n");
		abort();
	}
}

static int idle_thread_fn(void *arg)
{
	sigset_t set;
	int sig;

	/* dummy waiter; SIGTERM terminates us anyway */
	sigemptyset(&set);
	sigaddset(&set, SIGTERM);
	sigwait(&set, &sig);

	return 0;
}

static pid_t spawn_idle_thread(unsigned int flags)
{
	uint8_t *stack;
	pid_t pid;

	stack = malloc(STACK_SIZE);
	if (!stack) {
		printf("malloc(STACK_SIZE) failed: %m\n");
		abort();
	}

	pid = clone(idle_thread_fn,
		    stack + STACK_SIZE,
		    SIGCHLD | flags,
		    NULL);
	if (pid < 0) {
		printf("clone() failed: %m\n");
		abort();
	}

	return pid;
}

static void join_idle_thread(pid_t pid)
{
	kill(pid, SIGTERM);
	waitpid(pid, NULL, 0);
}

/*
 * Test memfd_create() syscall
 * Verify syscall-argument validation, including name checks, flag validation
 * and more.
 */
static void test_create(void)
{
	char buf[2048];
	int fd;

601 602
	printf("%s CREATE\n", MEMFD_STR);

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	/* test NULL name */
	mfd_fail_new(NULL, 0);

	/* test over-long name (not zero-terminated) */
	memset(buf, 0xff, sizeof(buf));
	mfd_fail_new(buf, 0);

	/* test over-long zero-terminated name */
	memset(buf, 0xff, sizeof(buf));
	buf[sizeof(buf) - 1] = 0;
	mfd_fail_new(buf, 0);

	/* verify "" is a valid name */
	fd = mfd_assert_new("", 0, 0);
	close(fd);

	/* verify invalid O_* open flags */
	mfd_fail_new("", 0x0100);
	mfd_fail_new("", ~MFD_CLOEXEC);
	mfd_fail_new("", ~MFD_ALLOW_SEALING);
	mfd_fail_new("", ~0);
	mfd_fail_new("", 0x80000000U);

	/* verify MFD_CLOEXEC is allowed */
	fd = mfd_assert_new("", 0, MFD_CLOEXEC);
	close(fd);

630 631 632 633 634 635 636 637 638 639 640 641
	if (!hugetlbfs_test) {
		/* verify MFD_ALLOW_SEALING is allowed */
		fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
		close(fd);

		/* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
		fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
		close(fd);
	} else {
		/* sealing is not supported on hugetlbfs */
		mfd_fail_new("", MFD_ALLOW_SEALING);
	}
642 643 644 645 646 647 648 649 650 651
}

/*
 * Test basic sealing
 * A very basic sealing test to see whether setting/retrieving seals works.
 */
static void test_basic(void)
{
	int fd;

652 653 654 655 656 657
	/* hugetlbfs does not contain sealing support */
	if (hugetlbfs_test)
		return;

	printf("%s BASIC\n", MEMFD_STR);

658
	fd = mfd_assert_new("kern_memfd_basic",
659
			    mfd_def_size,
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);

	/* add basic seals */
	mfd_assert_has_seals(fd, 0);
	mfd_assert_add_seals(fd, F_SEAL_SHRINK |
				 F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_SHRINK |
				 F_SEAL_WRITE);

	/* add them again */
	mfd_assert_add_seals(fd, F_SEAL_SHRINK |
				 F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_SHRINK |
				 F_SEAL_WRITE);

	/* add more seals and seal against sealing */
	mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
	mfd_assert_has_seals(fd, F_SEAL_SHRINK |
				 F_SEAL_GROW |
				 F_SEAL_WRITE |
				 F_SEAL_SEAL);

	/* verify that sealing no longer works */
	mfd_fail_add_seals(fd, F_SEAL_GROW);
	mfd_fail_add_seals(fd, 0);

	close(fd);

	/* verify sealing does not work without MFD_ALLOW_SEALING */
	fd = mfd_assert_new("kern_memfd_basic",
690
			    mfd_def_size,
691 692 693 694 695 696 697 698 699
			    MFD_CLOEXEC);
	mfd_assert_has_seals(fd, F_SEAL_SEAL);
	mfd_fail_add_seals(fd, F_SEAL_SHRINK |
			       F_SEAL_GROW |
			       F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_SEAL);
	close(fd);
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
/*
 * hugetlbfs doesn't support seals or write, so just verify grow and shrink
 * on a hugetlbfs file created via memfd_create.
 */
static void test_hugetlbfs_grow_shrink(void)
{
	int fd;

	printf("%s HUGETLBFS-GROW-SHRINK\n", MEMFD_STR);

	fd = mfd_assert_new("kern_memfd_seal_write",
			    mfd_def_size,
			    MFD_CLOEXEC);

	mfd_assert_read(fd);
	mfd_assert_write(fd);
	mfd_assert_shrink(fd);
	mfd_assert_grow(fd);

	close(fd);
}

722 723 724 725 726 727 728 729
/*
 * Test SEAL_WRITE
 * Test whether SEAL_WRITE actually prevents modifications.
 */
static void test_seal_write(void)
{
	int fd;

730 731 732 733 734 735 736 737 738
	/*
	 * hugetlbfs does not contain sealing or write support.  Just test
	 * basic grow and shrink via test_hugetlbfs_grow_shrink.
	 */
	if (hugetlbfs_test)
		return test_hugetlbfs_grow_shrink();

	printf("%s SEAL-WRITE\n", MEMFD_STR);

739
	fd = mfd_assert_new("kern_memfd_seal_write",
740
			    mfd_def_size,
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);
	mfd_assert_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_WRITE);

	mfd_assert_read(fd);
	mfd_fail_write(fd);
	mfd_assert_shrink(fd);
	mfd_assert_grow(fd);
	mfd_fail_grow_write(fd);

	close(fd);
}

/*
 * Test SEAL_SHRINK
 * Test whether SEAL_SHRINK actually prevents shrinking
 */
static void test_seal_shrink(void)
{
	int fd;

763 764 765 766 767 768
	/* hugetlbfs does not contain sealing support */
	if (hugetlbfs_test)
		return;

	printf("%s SEAL-SHRINK\n", MEMFD_STR);

769
	fd = mfd_assert_new("kern_memfd_seal_shrink",
770
			    mfd_def_size,
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);
	mfd_assert_add_seals(fd, F_SEAL_SHRINK);
	mfd_assert_has_seals(fd, F_SEAL_SHRINK);

	mfd_assert_read(fd);
	mfd_assert_write(fd);
	mfd_fail_shrink(fd);
	mfd_assert_grow(fd);
	mfd_assert_grow_write(fd);

	close(fd);
}

/*
 * Test SEAL_GROW
 * Test whether SEAL_GROW actually prevents growing
 */
static void test_seal_grow(void)
{
	int fd;

793 794 795 796 797 798
	/* hugetlbfs does not contain sealing support */
	if (hugetlbfs_test)
		return;

	printf("%s SEAL-GROW\n", MEMFD_STR);

799
	fd = mfd_assert_new("kern_memfd_seal_grow",
800
			    mfd_def_size,
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);
	mfd_assert_add_seals(fd, F_SEAL_GROW);
	mfd_assert_has_seals(fd, F_SEAL_GROW);

	mfd_assert_read(fd);
	mfd_assert_write(fd);
	mfd_assert_shrink(fd);
	mfd_fail_grow(fd);
	mfd_fail_grow_write(fd);

	close(fd);
}

/*
 * Test SEAL_SHRINK | SEAL_GROW
 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
 */
static void test_seal_resize(void)
{
	int fd;

823 824 825 826 827 828
	/* hugetlbfs does not contain sealing support */
	if (hugetlbfs_test)
		return;

	printf("%s SEAL-RESIZE\n", MEMFD_STR);

829
	fd = mfd_assert_new("kern_memfd_seal_resize",
830
			    mfd_def_size,
831 832 833 834 835 836 837 838 839 840 841 842 843 844
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);
	mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
	mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);

	mfd_assert_read(fd);
	mfd_assert_write(fd);
	mfd_fail_shrink(fd);
	mfd_fail_grow(fd);
	mfd_fail_grow_write(fd);

	close(fd);
}

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
/*
 * hugetlbfs does not support seals.  Basic test to dup the memfd created
 * fd and perform some basic operations on it.
 */
static void hugetlbfs_dup(char *b_suffix)
{
	int fd, fd2;

	printf("%s HUGETLBFS-DUP %s\n", MEMFD_STR, b_suffix);

	fd = mfd_assert_new("kern_memfd_share_dup",
			    mfd_def_size,
			    MFD_CLOEXEC);

	fd2 = mfd_assert_dup(fd);

	mfd_assert_read(fd);
	mfd_assert_write(fd);

	mfd_assert_shrink(fd2);
	mfd_assert_grow(fd2);

	close(fd2);
	close(fd);
}

871 872 873 874
/*
 * Test sharing via dup()
 * Test that seals are shared between dupped FDs and they're all equal.
 */
875
static void test_share_dup(char *banner, char *b_suffix)
876 877 878
{
	int fd, fd2;

879 880 881 882 883 884 885 886 887 888 889
	/*
	 * hugetlbfs does not contain sealing support.  Perform some
	 * basic testing on dup'ed fd instead via hugetlbfs_dup.
	 */
	if (hugetlbfs_test) {
		hugetlbfs_dup(b_suffix);
		return;
	}

	printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);

890
	fd = mfd_assert_new("kern_memfd_share_dup",
891
			    mfd_def_size,
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);

	fd2 = mfd_assert_dup(fd);
	mfd_assert_has_seals(fd2, 0);

	mfd_assert_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE);

	mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
	mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);

	mfd_assert_add_seals(fd, F_SEAL_SEAL);
	mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);

	mfd_fail_add_seals(fd, F_SEAL_GROW);
	mfd_fail_add_seals(fd2, F_SEAL_GROW);
	mfd_fail_add_seals(fd, F_SEAL_SEAL);
	mfd_fail_add_seals(fd2, F_SEAL_SEAL);

	close(fd2);

	mfd_fail_add_seals(fd, F_SEAL_GROW);
	close(fd);
}

/*
 * Test sealing with active mmap()s
 * Modifying seals is only allowed if no other mmap() refs exist.
 */
925
static void test_share_mmap(char *banner, char *b_suffix)
926 927 928 929
{
	int fd;
	void *p;

930 931 932 933 934 935
	/* hugetlbfs does not contain sealing support */
	if (hugetlbfs_test)
		return;

	printf("%s %s %s\n", MEMFD_STR,  banner, b_suffix);

936
	fd = mfd_assert_new("kern_memfd_share_mmap",
937
			    mfd_def_size,
938 939 940 941 942 943 944 945 946
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);

	/* shared/writable ref prevents sealing WRITE, but allows others */
	p = mfd_assert_mmap_shared(fd);
	mfd_fail_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, 0);
	mfd_assert_add_seals(fd, F_SEAL_SHRINK);
	mfd_assert_has_seals(fd, F_SEAL_SHRINK);
947
	munmap(p, mfd_def_size);
948 949 950 951 952

	/* readable ref allows sealing */
	p = mfd_assert_mmap_private(fd);
	mfd_assert_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
953 954 955 956 957 958 959 960 961 962 963 964 965 966
	munmap(p, mfd_def_size);

	close(fd);
}

/*
 * Basic test to make sure we can open the hugetlbfs fd via /proc and
 * perform some simple operations on it.
 */
static void hugetlbfs_proc_open(char *b_suffix)
{
	int fd, fd2;

	printf("%s HUGETLBFS-PROC-OPEN %s\n", MEMFD_STR, b_suffix);
967

968 969 970 971 972 973 974 975 976 977 978 979 980
	fd = mfd_assert_new("kern_memfd_share_open",
			    mfd_def_size,
			    MFD_CLOEXEC);

	fd2 = mfd_assert_open(fd, O_RDWR, 0);

	mfd_assert_read(fd);
	mfd_assert_write(fd);

	mfd_assert_shrink(fd2);
	mfd_assert_grow(fd2);

	close(fd2);
981 982 983 984 985 986 987 988 989
	close(fd);
}

/*
 * Test sealing with open(/proc/self/fd/%d)
 * Via /proc we can get access to a separate file-context for the same memfd.
 * This is *not* like dup(), but like a real separate open(). Make sure the
 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
 */
990
static void test_share_open(char *banner, char *b_suffix)
991 992 993
{
	int fd, fd2;

994 995 996 997 998 999 1000 1001 1002 1003 1004
	/*
	 * hugetlbfs does not contain sealing support.  So test basic
	 * functionality of using /proc fd via hugetlbfs_proc_open
	 */
	if (hugetlbfs_test) {
		hugetlbfs_proc_open(b_suffix);
		return;
	}

	printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);

1005
	fd = mfd_assert_new("kern_memfd_share_open",
1006
			    mfd_def_size,
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);

	fd2 = mfd_assert_open(fd, O_RDWR, 0);
	mfd_assert_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE);

	mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
	mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);

	close(fd);
	fd = mfd_assert_open(fd2, O_RDONLY, 0);

	mfd_fail_add_seals(fd, F_SEAL_SEAL);
	mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);

	close(fd2);
	fd2 = mfd_assert_open(fd, O_RDWR, 0);

	mfd_assert_add_seals(fd2, F_SEAL_SEAL);
	mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
	mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);

	close(fd2);
	close(fd);
}

/*
 * Test sharing via fork()
 * Test whether seal-modifications work as expected with forked childs.
 */
1041
static void test_share_fork(char *banner, char *b_suffix)
1042 1043 1044 1045
{
	int fd;
	pid_t pid;

1046 1047 1048 1049 1050 1051
	/* hugetlbfs does not contain sealing support */
	if (hugetlbfs_test)
		return;

	printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);

1052
	fd = mfd_assert_new("kern_memfd_share_fork",
1053
			    mfd_def_size,
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
			    MFD_CLOEXEC | MFD_ALLOW_SEALING);
	mfd_assert_has_seals(fd, 0);

	pid = spawn_idle_thread(0);
	mfd_assert_add_seals(fd, F_SEAL_SEAL);
	mfd_assert_has_seals(fd, F_SEAL_SEAL);

	mfd_fail_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_SEAL);

	join_idle_thread(pid);

	mfd_fail_add_seals(fd, F_SEAL_WRITE);
	mfd_assert_has_seals(fd, F_SEAL_SEAL);

	close(fd);
}

int main(int argc, char **argv)
{
	pid_t pid;

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	if (argc == 2) {
		if (!strcmp(argv[1], "hugetlbfs")) {
			unsigned long hpage_size = default_huge_page_size();

			if (!hpage_size) {
				printf("Unable to determine huge page size\n");
				abort();
			}

			hugetlbfs_test = 1;
			mfd_def_size = hpage_size * 2;
		}
	}

1090 1091 1092 1093 1094 1095 1096 1097
	test_create();
	test_basic();

	test_seal_write();
	test_seal_shrink();
	test_seal_grow();
	test_seal_resize();

1098 1099 1100 1101
	test_share_dup("SHARE-DUP", "");
	test_share_mmap("SHARE-MMAP", "");
	test_share_open("SHARE-OPEN", "");
	test_share_fork("SHARE-FORK", "");
1102 1103 1104 1105

	/* Run test-suite in a multi-threaded environment with a shared
	 * file-table. */
	pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
1106 1107 1108 1109
	test_share_dup("SHARE-DUP", SHARED_FT_STR);
	test_share_mmap("SHARE-MMAP", SHARED_FT_STR);
	test_share_open("SHARE-OPEN", SHARED_FT_STR);
	test_share_fork("SHARE-FORK", SHARED_FT_STR);
1110 1111 1112 1113 1114 1115
	join_idle_thread(pid);

	printf("memfd: DONE\n");

	return 0;
}