aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/sendfile/sendfile04.c
blob: 9a8ec08b964ca7276452c06390a020742df11942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) International Business Machines  Corp., 2001
 * Copyright (c) Red Hat Inc., 2007
 * 11/2007 Copied from sendfile02.c by Masatake YAMATO
 */

/*\
 * [Description]
 *
 * Testcase to test that sendfile(2) system call returns EFAULT when passing
 * wrong offset pointer.
 *
 * [Algorithm]
 *
 * Given wrong address or protected buffer as OFFSET argument to sendfile:
 * - a wrong address is created by munmap a buffer allocated by mmap
 * - a protected buffer is created by mmap with specifying protection
 */

#include <sys/sendfile.h>
#include "tst_test.h"

static int in_fd;
static int out_fd;

struct test_case_t {
	int protection;
	int pass_unmapped_buffer;
	const char *desc;
} tc[] = {
	{PROT_NONE, 0, "pass_mapped_buffer"},
	{PROT_READ, 0, "pass_mapped_buffer"},
	{PROT_EXEC, 0, "pass_mapped_buffer"},
	{PROT_EXEC | PROT_READ, 0, "pass_mapped_buffer"},
	{PROT_READ | PROT_WRITE, 1, "pass_unmapped_buffer"}
};

static void setup(void)
{
	in_fd = SAFE_OPEN("in_file", O_CREAT | O_RDWR, 0600);
	out_fd = SAFE_CREAT("out_file", 0600);
}

static void cleanup(void)
{
	SAFE_CLOSE(in_fd);
	SAFE_CLOSE(out_fd);
}

static void run(unsigned int i)
{
	off_t *protected_buffer;
	protected_buffer = SAFE_MMAP(NULL, sizeof(*protected_buffer),
			             tc[i].protection,
				     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

	if (tc[i].pass_unmapped_buffer)
		SAFE_MUNMAP(protected_buffer, sizeof(*protected_buffer));

	TST_EXP_FAIL2(sendfile(out_fd, in_fd, protected_buffer, 1),
		     EFAULT, "sendfile(..) with %s, protection=%d",
		     tc[i].desc, tc[i].protection);

	if (!tc[i].pass_unmapped_buffer)
		SAFE_MUNMAP(protected_buffer, sizeof(*protected_buffer));
}

static struct tst_test test = {
	.tcnt = ARRAY_SIZE(tc),
	.needs_tmpdir = 1,
	.cleanup = cleanup,
	.setup = setup,
	.test = run,
};