1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright(c) 2016 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Test Name: sendto02
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Description:
11f08c3bdfSopenharmony_ci * When sctp protocol is selected in socket(2) and buffer is invalid,
12f08c3bdfSopenharmony_ci * sendto(2) should fail and set errno to EFAULT, but it sets errno
13f08c3bdfSopenharmony_ci * to ENOMEM.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * This is a regression test and has been fixed by kernel commit:
16f08c3bdfSopenharmony_ci * 6e51fe7572590d8d86e93b547fab6693d305fd0d (sctp: fix -ENOMEM result
17f08c3bdfSopenharmony_ci * with invalid user space pointer in sendto() syscall)
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <errno.h>
21f08c3bdfSopenharmony_ci#include <string.h>
22f08c3bdfSopenharmony_ci#include <unistd.h>
23f08c3bdfSopenharmony_ci#include <sys/types.h>
24f08c3bdfSopenharmony_ci#include <sys/socket.h>
25f08c3bdfSopenharmony_ci#include <netinet/in.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include "tst_test.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci#ifndef IPPROTO_SCTP
30f08c3bdfSopenharmony_ci# define IPPROTO_SCTP	132
31f08c3bdfSopenharmony_ci#endif
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic int sockfd;
34f08c3bdfSopenharmony_cistatic struct sockaddr_in sa;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void setup(void)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
39f08c3bdfSopenharmony_ci	if (sockfd == -1) {
40f08c3bdfSopenharmony_ci		if (errno == EPROTONOSUPPORT)
41f08c3bdfSopenharmony_ci			tst_brk(TCONF, "sctp protocol was not supported");
42f08c3bdfSopenharmony_ci		else
43f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "socket() failed with sctp");
44f08c3bdfSopenharmony_ci	}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	memset(&sa, 0, sizeof(sa));
47f08c3bdfSopenharmony_ci	sa.sin_family = AF_INET;
48f08c3bdfSopenharmony_ci	sa.sin_addr.s_addr = inet_addr("127.0.0.1");
49f08c3bdfSopenharmony_ci	sa.sin_port = htons(11111);
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void cleanup(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	if (sockfd > 0)
55f08c3bdfSopenharmony_ci		SAFE_CLOSE(sockfd);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void verify_sendto(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	TEST(sendto(sockfd, NULL, 1, 0, (struct sockaddr *) &sa, sizeof(sa)));
61f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
62f08c3bdfSopenharmony_ci		tst_res(TFAIL, "sendto(fd, NULL, ...) succeeded unexpectedly");
63f08c3bdfSopenharmony_ci		return;
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if (TST_ERR == EFAULT) {
67f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO,
68f08c3bdfSopenharmony_ci			"sendto(fd, NULL, ...) failed expectedly");
69f08c3bdfSopenharmony_ci		return;
70f08c3bdfSopenharmony_ci	}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	tst_res(TFAIL | TTERRNO,
73f08c3bdfSopenharmony_ci		"sendto(fd, NULL, ...) failed unexpectedly, expected EFAULT");
74f08c3bdfSopenharmony_ci}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic struct tst_test test = {
77f08c3bdfSopenharmony_ci	.setup = setup,
78f08c3bdfSopenharmony_ci	.cleanup = cleanup,
79f08c3bdfSopenharmony_ci	.test_all = verify_sendto,
80f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
81f08c3bdfSopenharmony_ci		{"linux-git", "6e51fe757259"},
82f08c3bdfSopenharmony_ci		{}
83f08c3bdfSopenharmony_ci	}
84f08c3bdfSopenharmony_ci};
85