1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_LINUX_CRYPTOUSER_H
32 
33 # include <stdio.h>
34 # include <unistd.h>
35 # include <sys/socket.h>
36 # include <linux/cryptouser.h>
37 # include "test_netlink.h"
38 
39 static void
test_nlmsg_type(const int fd)40 test_nlmsg_type(const int fd)
41 {
42 	long rc;
43 	struct nlmsghdr nlh = {
44 		.nlmsg_len = sizeof(nlh),
45 		.nlmsg_type = CRYPTO_MSG_NEWALG,
46 		.nlmsg_flags = NLM_F_REQUEST,
47 	};
48 
49 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
50 	printf("sendto(%d, {len=%u, type=CRYPTO_MSG_NEWALG"
51 	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
52 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
53 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
54 }
55 
56 static void
test_nlmsg_flags(const int fd)57 test_nlmsg_flags(const int fd)
58 {
59 	long rc;
60 	struct nlmsghdr nlh = {
61 		.nlmsg_len = sizeof(nlh),
62 	};
63 
64 	nlh.nlmsg_type = CRYPTO_MSG_GETALG;
65 	nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
66 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
67 	printf("sendto(%d, {len=%u, type=CRYPTO_MSG_GETALG"
68 	       ", flags=NLM_F_REQUEST|NLM_F_DUMP, seq=0, pid=0}"
69 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
70 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
71 
72 	nlh.nlmsg_type = CRYPTO_MSG_NEWALG;
73 	nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_REPLACE;
74 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
75 	printf("sendto(%d, {len=%u, type=CRYPTO_MSG_NEWALG"
76 	       ", flags=NLM_F_ECHO|NLM_F_REPLACE, seq=0, pid=0}"
77 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
78 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
79 
80 	nlh.nlmsg_type = CRYPTO_MSG_DELALG;
81 	nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_NONREC;
82 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
83 	printf("sendto(%d, {len=%u, type=CRYPTO_MSG_DELALG"
84 	       ", flags=NLM_F_ECHO|NLM_F_NONREC, seq=0, pid=0}"
85 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
86 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
87 
88 	nlh.nlmsg_type = CRYPTO_MSG_UPDATEALG;
89 	nlh.nlmsg_flags = NLM_F_REPLACE;
90 	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
91 	printf("sendto(%d, {len=%u, type=CRYPTO_MSG_UPDATEALG"
92 	       ", flags=%#x /* NLM_F_??? */, seq=0, pid=0}"
93 	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
94 	       fd, nlh.nlmsg_len, NLM_F_REPLACE,
95 	       (unsigned) sizeof(nlh), sprintrc(rc));
96 }
97 
98 static void
test_crypto_msg_newalg(const int fd)99 test_crypto_msg_newalg(const int fd)
100 {
101 	struct crypto_user_alg alg = {
102 		.cru_name = "abcd",
103 		.cru_driver_name = "efgh",
104 		.cru_module_name = "dcba",
105 		.cru_type = 0xabcdfabc,
106 		.cru_mask = 0xfedabacd,
107 		.cru_refcnt = 0xbcacfacd,
108 		.cru_flags = 0xefacdbad
109 	};
110 	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(alg));
111 
112 	TEST_NETLINK_OBJECT_EX(fd, nlh0,
113 			       CRYPTO_MSG_NEWALG, NLM_F_REQUEST,
114 			       alg, print_quoted_memory,
115 			       printf("{cru_name=\"abcd\""
116 				      ", cru_driver_name=\"efgh\""
117 				      ", cru_module_name=\"dcba\"");
118 			       PRINT_FIELD_X(", ", alg, cru_type);
119 			       PRINT_FIELD_X(", ", alg, cru_mask);
120 			       PRINT_FIELD_U(", ", alg, cru_refcnt);
121 			       PRINT_FIELD_X(", ", alg, cru_flags);
122 			       printf("}"));
123 
124 	fill_memory_ex(alg.cru_name, sizeof(alg.cru_name), '0', 10);
125 	fill_memory_ex(alg.cru_driver_name, sizeof(alg.cru_driver_name),
126 		       'a', 'z' - 'a' + 1);
127 	fill_memory_ex(alg.cru_module_name, sizeof(alg.cru_module_name),
128 		       'A', 'Z' - 'A' + 1);
129 
130 	TEST_NETLINK_OBJECT_EX(fd, nlh0,
131 			       CRYPTO_MSG_NEWALG, NLM_F_REQUEST,
132 			       alg, print_quoted_memory,
133 			       printf("{cru_name=");
134 			       print_quoted_memory(alg.cru_name,
135 				       sizeof(alg.cru_name) - 1);
136 			       printf("..., cru_driver_name=");
137 			       print_quoted_memory(alg.cru_driver_name,
138 				       sizeof(alg.cru_driver_name) - 1);
139 			       printf("..., cru_module_name=");
140 			       print_quoted_memory(alg.cru_module_name,
141 				       sizeof(alg.cru_module_name) - 1);
142 			       PRINT_FIELD_X("..., ", alg, cru_type);
143 			       PRINT_FIELD_X(", ", alg, cru_mask);
144 			       PRINT_FIELD_U(", ", alg, cru_refcnt);
145 			       PRINT_FIELD_X(", ", alg, cru_flags);
146 			       printf("}"));
147 }
148 
149 static void
test_crypto_msg_unspec(const int fd)150 test_crypto_msg_unspec(const int fd)
151 {
152 	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, 4);
153 
154 	TEST_NETLINK_(fd, nlh0,
155 		      0xffff, "0xffff /* CRYPTO_MSG_??? */",
156 		      NLM_F_REQUEST, "NLM_F_REQUEST",
157 		      4, "abcd", 4, printf("\"\\x61\\x62\\x63\\x64\""));
158 }
159 
160 int
main(void)161 main(void)
162 {
163 	skip_if_unavailable("/proc/self/fd/");
164 
165 	int fd = create_nl_socket(NETLINK_CRYPTO);
166 
167 	test_nlmsg_type(fd);
168 	test_nlmsg_flags(fd);
169 	test_crypto_msg_newalg(fd);
170 	test_crypto_msg_unspec(fd);
171 
172 	printf("+++ exited with 0 +++\n");
173 
174 	return 0;
175 }
176 
177 #else
178 
179 SKIP_MAIN_UNDEFINED("HAVE_LINUX_CRYPTOUSER_H")
180 
181 #endif
182