xref: /xnu-8019.80.24/tests/fcntl.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea) !
1*a325d9c4SApple OSS Distributions #include <darwintest.h>
2*a325d9c4SApple OSS Distributions #include <fcntl.h>
3*a325d9c4SApple OSS Distributions #include <sys/mman.h>
4*a325d9c4SApple OSS Distributions #include <darwintest_utils.h>
5*a325d9c4SApple OSS Distributions #include <mach/vm_page_size.h>
6*a325d9c4SApple OSS Distributions 
7*a325d9c4SApple OSS Distributions /** Verify that F_ADDSIGS does not page fault off the end of the user blob
8*a325d9c4SApple OSS Distributions  * 1. Find VA space for 3 pages
9*a325d9c4SApple OSS Distributions  * 2. Unmap the last page
10*a325d9c4SApple OSS Distributions  * 3. Start fs_blob_start at PAGE_SIZE + 1 bytes away from the end of the
11*a325d9c4SApple OSS Distributions  * VA region (such that any read of more than PAGE_SIZE + 1 bytes will fault)
12*a325d9c4SApple OSS Distributions  * 4. Call fcntl with the arguments and verify the output is not EFAULT
13*a325d9c4SApple OSS Distributions  */
14*a325d9c4SApple OSS Distributions T_DECL(fcntl_addsig, "Verify that fcntl(F_ADDSIGS) doesn't EFAULT", T_META_NAMESPACE("xnu.vfs")) {
15*a325d9c4SApple OSS Distributions 	void* blob_space = mmap(NULL, vm_page_size * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
16*a325d9c4SApple OSS Distributions 	T_ASSERT_NE(blob_space, MAP_FAILED, "Blob Region: %p [%zd]", blob_space, vm_page_size);
17*a325d9c4SApple OSS Distributions 
18*a325d9c4SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(munmap((char*)blob_space + (vm_page_size * 2), vm_page_size), NULL);
19*a325d9c4SApple OSS Distributions 
20*a325d9c4SApple OSS Distributions 	size_t blob_size = vm_page_size + 1;
21*a325d9c4SApple OSS Distributions 	char* blob_start = ((char*)blob_space) + (vm_page_size * 2) - blob_size;
22*a325d9c4SApple OSS Distributions 	fsignatures_t args = { .fs_file_start = 0, .fs_blob_start =  blob_start, .fs_blob_size = blob_size};
23*a325d9c4SApple OSS Distributions 
24*a325d9c4SApple OSS Distributions 	// Create test file to operate on
25*a325d9c4SApple OSS Distributions 	const char * tmp_dir = dt_tmpdir();
26*a325d9c4SApple OSS Distributions 	char tmp_file_name[PATH_MAX];
27*a325d9c4SApple OSS Distributions 	sprintf(tmp_file_name, "%s/foo", tmp_dir);
28*a325d9c4SApple OSS Distributions 	FILE* tmp_file = fopen(tmp_file_name, "wx");
29*a325d9c4SApple OSS Distributions 	fprintf(tmp_file, "Just some random content");
30*a325d9c4SApple OSS Distributions 	fclose(tmp_file);
31*a325d9c4SApple OSS Distributions 
32*a325d9c4SApple OSS Distributions 	int fd = open(tmp_file_name, O_RDONLY);
33*a325d9c4SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "tmp file: %s", tmp_file_name);
34*a325d9c4SApple OSS Distributions 
35*a325d9c4SApple OSS Distributions 	// This command will fail, but should not fail with EFAULT
36*a325d9c4SApple OSS Distributions 	int result = fcntl(fd, F_ADDSIGS, &args);
37*a325d9c4SApple OSS Distributions 	int error = errno;
38*a325d9c4SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(result, -1, NULL);
39*a325d9c4SApple OSS Distributions 	// EBADEXEC is expected, but not required for success of this test
40*a325d9c4SApple OSS Distributions 	T_EXPECT_NE(error, EFAULT, "fcntl: %d (%d:%s)", result, error, strerror(error));
41*a325d9c4SApple OSS Distributions }
42