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