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