1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright 2018 Google LLC
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * Regression test for commit f43f39958beb ("crypto: user - fix leaking
8*49cdfc7eSAndroid Build Coastguard Worker * uninitialized memory to userspace"), or CVE-2018-19854; it was also a
9*49cdfc7eSAndroid Build Coastguard Worker * re-introduction of CVE-2013-2547. This bug caused uninitialized kernel stack
10*49cdfc7eSAndroid Build Coastguard Worker * memory to be leaked in some string fields in the replies to CRYPTO_MSG_GETALG
11*49cdfc7eSAndroid Build Coastguard Worker * messages over NETLINK_CRYPTO. To try to detect the bug, this test dumps all
12*49cdfc7eSAndroid Build Coastguard Worker * algorithms using NLM_F_DUMP mode and checks all string fields for unexpected
13*49cdfc7eSAndroid Build Coastguard Worker * nonzero bytes.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_crypto.h"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker /*
22*49cdfc7eSAndroid Build Coastguard Worker * include after <sys/socket.h> (via tst_test.h), to work around dependency bug
23*49cdfc7eSAndroid Build Coastguard Worker * in old kernel headers (https://www.spinics.net/lists/netdev/msg171764.html)
24*49cdfc7eSAndroid Build Coastguard Worker */
25*49cdfc7eSAndroid Build Coastguard Worker #include <linux/rtnetlink.h>
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker static struct tst_netlink_context *ctx;
28*49cdfc7eSAndroid Build Coastguard Worker
setup(void)29*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker ctx = NETLINK_CREATE_CONTEXT(NETLINK_CRYPTO);
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker
do_check_for_leaks(const char * name,const char * value,size_t vlen)34*49cdfc7eSAndroid Build Coastguard Worker static void do_check_for_leaks(const char *name, const char *value, size_t vlen)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker size_t i;
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker for (i = strnlen(value, vlen); i < vlen; i++) {
39*49cdfc7eSAndroid Build Coastguard Worker if (value[i] != '\0')
40*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL, "information leak in field '%s'", name);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker #define check_for_leaks(name, field) \
45*49cdfc7eSAndroid Build Coastguard Worker do_check_for_leaks(name, field, sizeof(field))
46*49cdfc7eSAndroid Build Coastguard Worker
validate_attr(const struct rtattr * rta)47*49cdfc7eSAndroid Build Coastguard Worker static void validate_attr(const struct rtattr *rta)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker switch (rta->rta_type) {
50*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_LARVAL: {
51*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_larval *p = RTA_DATA(rta);
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_larval::type", p->type);
54*49cdfc7eSAndroid Build Coastguard Worker break;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_HASH: {
57*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_hash *p = RTA_DATA(rta);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_hash::type", p->type);
60*49cdfc7eSAndroid Build Coastguard Worker break;
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_BLKCIPHER: {
63*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_blkcipher *p = RTA_DATA(rta);
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_blkcipher::type", p->type);
66*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_blkcipher::geniv", p->geniv);
67*49cdfc7eSAndroid Build Coastguard Worker break;
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_AEAD: {
70*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_aead *p = RTA_DATA(rta);
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_aead::type", p->type);
73*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_aead::geniv", p->geniv);
74*49cdfc7eSAndroid Build Coastguard Worker break;
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_COMPRESS: {
77*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_comp *p = RTA_DATA(rta);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_comp::type", p->type);
80*49cdfc7eSAndroid Build Coastguard Worker break;
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_RNG: {
83*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_rng *p = RTA_DATA(rta);
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_rng::type", p->type);
86*49cdfc7eSAndroid Build Coastguard Worker break;
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_CIPHER: {
89*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_cipher *p = RTA_DATA(rta);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_cipher::type", p->type);
92*49cdfc7eSAndroid Build Coastguard Worker break;
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_AKCIPHER: {
95*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_akcipher *p = RTA_DATA(rta);
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_akcipher::type", p->type);
98*49cdfc7eSAndroid Build Coastguard Worker break;
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_KPP: {
101*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_kpp *p = RTA_DATA(rta);
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_kpp::type", p->type);
104*49cdfc7eSAndroid Build Coastguard Worker break;
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker case CRYPTOCFGA_REPORT_ACOMP: {
107*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_report_acomp *p = RTA_DATA(rta);
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_report_acomp::type", p->type);
110*49cdfc7eSAndroid Build Coastguard Worker break;
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker } /* end switch */
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker
validate_one_alg(const struct nlmsghdr * nh)115*49cdfc7eSAndroid Build Coastguard Worker static void validate_one_alg(const struct nlmsghdr *nh)
116*49cdfc7eSAndroid Build Coastguard Worker {
117*49cdfc7eSAndroid Build Coastguard Worker const struct crypto_user_alg *alg = NLMSG_DATA(nh);
118*49cdfc7eSAndroid Build Coastguard Worker const struct rtattr *rta = (void *)alg + NLMSG_ALIGN(sizeof(*alg));
119*49cdfc7eSAndroid Build Coastguard Worker size_t remaining = NLMSG_PAYLOAD(nh, sizeof(*alg));
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_user_alg::cru_name", alg->cru_name);
122*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_user_alg::cru_driver_name",
123*49cdfc7eSAndroid Build Coastguard Worker alg->cru_driver_name);
124*49cdfc7eSAndroid Build Coastguard Worker check_for_leaks("crypto_user_alg::cru_module_name",
125*49cdfc7eSAndroid Build Coastguard Worker alg->cru_module_name);
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker while (RTA_OK(rta, remaining)) {
128*49cdfc7eSAndroid Build Coastguard Worker validate_attr(rta);
129*49cdfc7eSAndroid Build Coastguard Worker rta = RTA_NEXT(rta, remaining);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker }
132*49cdfc7eSAndroid Build Coastguard Worker
validate_alg_list(const struct tst_netlink_message * msg)133*49cdfc7eSAndroid Build Coastguard Worker static void validate_alg_list(const struct tst_netlink_message *msg)
134*49cdfc7eSAndroid Build Coastguard Worker {
135*49cdfc7eSAndroid Build Coastguard Worker for (; msg->header; msg++) {
136*49cdfc7eSAndroid Build Coastguard Worker if (msg->header->nlmsg_type == NLMSG_DONE)
137*49cdfc7eSAndroid Build Coastguard Worker return;
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker if (msg->header->nlmsg_type != CRYPTO_MSG_GETALG) {
140*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK,
141*49cdfc7eSAndroid Build Coastguard Worker "Unexpected message type; type=0x%hx, seq_num=%u",
142*49cdfc7eSAndroid Build Coastguard Worker msg->header->nlmsg_type,
143*49cdfc7eSAndroid Build Coastguard Worker msg->header->nlmsg_seq);
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker validate_one_alg(msg->header);
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker
run(void)150*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
151*49cdfc7eSAndroid Build Coastguard Worker {
152*49cdfc7eSAndroid Build Coastguard Worker struct crypto_user_alg payload = { 0 };
153*49cdfc7eSAndroid Build Coastguard Worker struct nlmsghdr nh = {
154*49cdfc7eSAndroid Build Coastguard Worker .nlmsg_type = CRYPTO_MSG_GETALG,
155*49cdfc7eSAndroid Build Coastguard Worker .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
156*49cdfc7eSAndroid Build Coastguard Worker };
157*49cdfc7eSAndroid Build Coastguard Worker struct tst_netlink_message *msg;
158*49cdfc7eSAndroid Build Coastguard Worker
159*49cdfc7eSAndroid Build Coastguard Worker NETLINK_ADD_MESSAGE(ctx, &nh, &payload, sizeof(payload));
160*49cdfc7eSAndroid Build Coastguard Worker NETLINK_SEND(ctx);
161*49cdfc7eSAndroid Build Coastguard Worker NETLINK_WAIT(ctx);
162*49cdfc7eSAndroid Build Coastguard Worker msg = NETLINK_RECV(ctx);
163*49cdfc7eSAndroid Build Coastguard Worker validate_alg_list(msg);
164*49cdfc7eSAndroid Build Coastguard Worker NETLINK_FREE_MESSAGE(msg);
165*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "No information leaks found");
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)168*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
169*49cdfc7eSAndroid Build Coastguard Worker {
170*49cdfc7eSAndroid Build Coastguard Worker NETLINK_DESTROY_CONTEXT(ctx);
171*49cdfc7eSAndroid Build Coastguard Worker }
172*49cdfc7eSAndroid Build Coastguard Worker
173*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
174*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
175*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
176*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
177*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
178*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "f43f39958beb"},
179*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2013-2547"},
180*49cdfc7eSAndroid Build Coastguard Worker {"CVE", "2018-19854"},
181*49cdfc7eSAndroid Build Coastguard Worker {}
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker };
184