memfd_test.c 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#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>
18
#include <sys/wait.h>
19 20
#include <unistd.h>

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

24
#define MFD_DEF_SIZE 8192
25
#define STACK_SIZE 65536
26

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
/*
 * 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;
}

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

63 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
	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();
	}
}

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

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

109
	return (unsigned int)r;
110 111
}

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

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

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

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

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

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

	r = fcntl(fd, F_ADD_SEALS, seals);
	if (r >= 0) {
149 150
		printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
				fd, s, seals);
151 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
		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,
189
		 mfd_def_size,
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		 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,
207
		 mfd_def_size,
208 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
		 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) {
243
		printf("open(%s) didn't fail as expected\n", buf);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		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,
262
		 mfd_def_size,
263 264 265 266 267 268 269 270
		 PROT_READ,
		 MAP_PRIVATE,
		 fd,
		 0);
	if (p == MAP_FAILED) {
		printf("mmap() failed: %m\n");
		abort();
	}
271
	munmap(p, mfd_def_size);
272 273 274

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

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

293 294 295 296 297 298 299 300 301 302 303
	/*
	 * 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();
		}
304 305 306 307
	}

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

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

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

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

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

	/* verify PUNCH_HOLE works */
	r = fallocate(fd,
		      FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
		      0,
360
		      mfd_def_size);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	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,
382
		 mfd_def_size,
383 384 385 386 387 388 389 390 391 392 393
		 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,
394
		 mfd_def_size,
395 396 397 398 399 400 401 402 403 404 405 406
		 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,
407
		 mfd_def_size,
408 409 410 411 412
		 PROT_READ,
		 MAP_SHARED,
		 fd,
		 0);
	if (p != MAP_FAILED) {
413
		r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
414 415 416 417 418 419 420 421 422 423
		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,
424
		      mfd_def_size);
425 426 427 428 429 430 431 432 433 434
	if (r >= 0) {
		printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
		abort();
	}
}

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

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

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

	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;

455
	r = ftruncate(fd, mfd_def_size / 2);
456 457 458 459 460 461 462 463 464 465 466 467 468 469
	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;

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

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

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

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

static void mfd_fail_grow(int fd)
{
	int r;

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

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

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

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

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

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

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

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

	l = pwrite(fd, buf, mfd_def_size * 8, 0);
	if (l == (mfd_def_size * 8)) {
543 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
		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;

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

602 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
	/* 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);

629 630 631 632 633 634 635 636 637 638 639 640
	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);
	}
641 642 643 644 645 646 647 648 649 650
}

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

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

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

657
	fd = mfd_assert_new("kern_memfd_basic",
658
			    mfd_def_size,
659 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
			    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",
689
			    mfd_def_size,
690 691 692 693 694 695 696 697 698
			    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);
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
/*
 * 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);
}

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

729 730 731 732 733 734 735 736 737
	/*
	 * 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);

738
	fd = mfd_assert_new("kern_memfd_seal_write",
739
			    mfd_def_size,
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
			    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;

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

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

768
	fd = mfd_assert_new("kern_memfd_seal_shrink",
769
			    mfd_def_size,
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
			    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;

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

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

798
	fd = mfd_assert_new("kern_memfd_seal_grow",
799
			    mfd_def_size,
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
			    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;

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

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

828
	fd = mfd_assert_new("kern_memfd_seal_resize",
829
			    mfd_def_size,
830 831 832 833 834 835 836 837 838 839 840 841 842 843
			    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);
}

844 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
/*
 * 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);
}

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

878 879 880 881 882 883 884 885 886 887 888
	/*
	 * 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);

889
	fd = mfd_assert_new("kern_memfd_share_dup",
890
			    mfd_def_size,
891 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
			    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.
 */
924
static void test_share_mmap(char *banner, char *b_suffix)
925 926 927 928
{
	int fd;
	void *p;

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

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

935
	fd = mfd_assert_new("kern_memfd_share_mmap",
936
			    mfd_def_size,
937 938 939 940 941 942 943 944 945
			    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);
946
	munmap(p, mfd_def_size);
947 948 949 950 951

	/* 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);
952 953 954 955 956 957 958 959 960 961 962 963 964 965
	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);
966

967 968 969 970 971 972 973 974 975 976 977 978 979
	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);
980 981 982 983 984 985 986 987 988
	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.
 */
989
static void test_share_open(char *banner, char *b_suffix)
990 991 992
{
	int fd, fd2;

993 994 995 996 997 998 999 1000 1001 1002 1003
	/*
	 * 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);

1004
	fd = mfd_assert_new("kern_memfd_share_open",
1005
			    mfd_def_size,
1006 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
			    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.
 */
1040
static void test_share_fork(char *banner, char *b_suffix)
1041 1042 1043 1044
{
	int fd;
	pid_t pid;

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

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

1051
	fd = mfd_assert_new("kern_memfd_share_fork",
1052
			    mfd_def_size,
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
			    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;

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	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;
		}
	}

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

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

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

	/* Run test-suite in a multi-threaded environment with a shared
	 * file-table. */
	pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
1105 1106 1107 1108
	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);
1109 1110 1111 1112 1113 1114
	join_idle_thread(pid);

	printf("memfd: DONE\n");

	return 0;
}