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