• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Copyright (c) Red Hat Inc., 2007
5  * 11/2007 Copied from sendfile02.c by Masatake YAMATO
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Testcase to test that sendfile(2) system call returns EFAULT when passing
12  * wrong offset pointer.
13  *
14  * [Algorithm]
15  *
16  * Given wrong address or protected buffer as OFFSET argument to sendfile:
17  * - a wrong address is created by munmap a buffer allocated by mmap
18  * - a protected buffer is created by mmap with specifying protection
19  */
20 
21 #include <stdio.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/sendfile.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/mman.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 
32 #include "tst_test.h"
33 
34 static int in_fd;
35 static int out_fd;
36 
37 struct test_case_t {
38 	int protection;
39 	int pass_unmapped_buffer;
40 	const char *desc;
41 } tc[] = {
42 	{PROT_NONE, 0, "pass_mapped_buffer"},
43 	{PROT_READ, 0, "pass_mapped_buffer"},
44 	{PROT_EXEC, 0, "pass_mapped_buffer"},
45 	{PROT_EXEC | PROT_READ, 0, "pass_mapped_buffer"},
46 	{PROT_READ | PROT_WRITE, 1, "pass_unmapped_buffer"}
47 };
48 
setup(void)49 static void setup(void)
50 {
51 	in_fd = SAFE_OPEN("in_file", O_CREAT | O_RDWR, 0600);
52 	out_fd = SAFE_CREAT("out_file", 0600);
53 }
54 
cleanup(void)55 static void cleanup(void)
56 {
57 	SAFE_CLOSE(in_fd);
58 	SAFE_CLOSE(out_fd);
59 }
60 
run(unsigned int i)61 static void run(unsigned int i)
62 {
63 	off_t *protected_buffer;
64 	protected_buffer = SAFE_MMAP(NULL, sizeof(*protected_buffer),
65 			             tc[i].protection,
66 				     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
67 
68 	if (tc[i].pass_unmapped_buffer)
69 		SAFE_MUNMAP(protected_buffer, sizeof(*protected_buffer));
70 
71 	TST_EXP_FAIL(sendfile(out_fd, in_fd, protected_buffer, 1),
72 		     EFAULT, "sendfile(..) with %s, protection=%d",
73 		     tc[i].desc, tc[i].protection);
74 
75 	if (!tc[i].pass_unmapped_buffer)
76 		SAFE_MUNMAP(protected_buffer, sizeof(*protected_buffer));
77 }
78 
79 static struct tst_test test = {
80 	.tcnt = ARRAY_SIZE(tc),
81 	.needs_tmpdir = 1,
82 	.cleanup = cleanup,
83 	.setup = setup,
84 	.test = run,
85 };
86