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