History

virtio-crypto: use private buffer for control request

Originally, all of the control requests share a single buffer( ctrl & input & ctrl_status fields in struct virtio_crypto), this allows queue depth 1 only, the performance of control queue gets limited by this design. In this patch, each request allocates request buffer dynamically, and free buffer after request, so the scope protected by ctrl_lock also get optimized here. It's possible to optimize control queue depth in the next step. A necessary comment is already in code, still describe it again: /* * Note: there are padding fields in request, clear them to zero before * sending to host to avoid to divulge any information. * Ex, virtio_crypto_ctrl_request::ctrl::u::destroy_session::padding[48] */ So use kzalloc to allocate buffer of struct virtio_crypto_ctrl_request. Potentially dereferencing uninitialized variables: Reported-by: kernel test robot <lkp@intel.com> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

zhenwei pi committed 4 years ago

Commit 0756ad11 parent 6fd763d3 files · +79 −45Browse files

Unified · Split

3 files
  1. drivers/crypto/virtio/virtio_crypto_akcipher_algs.c +35 −22
  2. drivers/crypto/virtio/virtio_crypto_common.h +12 −5
  3. drivers/crypto/virtio/virtio_crypto_skcipher_algs.c +32 −18
drivers/crypto/virtio/virtio_crypto_akcipher_algs.c+35−22
@@ -108,16 +108,22 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher · 107 unmodified lines
108 unsigned int num_out = 0, num_in = 0;108 unsigned int num_out = 0, num_in = 0;
109 struct virtio_crypto_op_ctrl_req *ctrl;109 struct virtio_crypto_op_ctrl_req *ctrl;
110 struct virtio_crypto_session_input *input;110 struct virtio_crypto_session_input *input;
111 struct virtio_crypto_ctrl_request *vc_ctrl_req;
111 112
112 pkey = kmemdup(key, keylen, GFP_ATOMIC);113 pkey = kmemdup(key, keylen, GFP_ATOMIC);
113 if (!pkey)114 if (!pkey)
114 return -ENOMEM;115 return -ENOMEM;
115 116
116 spin_lock(&vcrypto->ctrl_lock);117 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
117 ctrl = &vcrypto->ctrl;118 if (!vc_ctrl_req) {
119 err = -ENOMEM;
120 goto out;
121 }
122
123 ctrl = &vc_ctrl_req->ctrl;
118 memcpy(&ctrl->header, header, sizeof(ctrl->header));124 memcpy(&ctrl->header, header, sizeof(ctrl->header));
119 memcpy(&ctrl->u, para, sizeof(ctrl->u));125 memcpy(&ctrl->u, para, sizeof(ctrl->u));
120 input = &vcrypto->input;126 input = &vc_ctrl_req->input;
121 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);127 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
122 128
123 sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));129 sg_init_one(&outhdr_sg, ctrl, sizeof(*ctrl));
@@ -129,16 +135,22 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher · 5 unmodified lines
129 sg_init_one(&inhdr_sg, input, sizeof(*input));135 sg_init_one(&inhdr_sg, input, sizeof(*input));
130 sgs[num_out + num_in++] = &inhdr_sg;136 sgs[num_out + num_in++] = &inhdr_sg;
131 137
138 spin_lock(&vcrypto->ctrl_lock);
132 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC);139 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC);
133 if (err < 0)140 if (err < 0) {
141 spin_unlock(&vcrypto->ctrl_lock);
134 goto out;142 goto out;
143 }
135 144
136 virtqueue_kick(vcrypto->ctrl_vq);145 virtqueue_kick(vcrypto->ctrl_vq);
137 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &inlen) &&146 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &inlen) &&
138 !virtqueue_is_broken(vcrypto->ctrl_vq))147 !virtqueue_is_broken(vcrypto->ctrl_vq))
139 cpu_relax();148 cpu_relax();
149 spin_unlock(&vcrypto->ctrl_lock);
140 150
141 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {151 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
152 pr_err("virtio_crypto: Create session failed status: %u\n",
153 le32_to_cpu(input->status));
142 err = -EINVAL;154 err = -EINVAL;
143 goto out;155 goto out;
144 }156 }
@@ -148,13 +160,9 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher · 3 unmodified lines
148 err = 0;160 err = 0;
149 161
150out:162out:
151 spin_unlock(&vcrypto->ctrl_lock);163 kfree(vc_ctrl_req);
152 kfree_sensitive(pkey);164 kfree_sensitive(pkey);
153 165
154 if (err < 0)
155 pr_err("virtio_crypto: Create session failed status: %u\n",
156 le32_to_cpu(input->status));
157
158 return err;166 return err;
159}167}
160 168
@@ -167,15 +175,18 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 6 unmodified lines
167 int err;175 int err;
168 struct virtio_crypto_op_ctrl_req *ctrl;176 struct virtio_crypto_op_ctrl_req *ctrl;
169 struct virtio_crypto_inhdr *ctrl_status;177 struct virtio_crypto_inhdr *ctrl_status;
178 struct virtio_crypto_ctrl_request *vc_ctrl_req;
170 179
171 spin_lock(&vcrypto->ctrl_lock);180 if (!ctx->session_valid)
172 if (!ctx->session_valid) {181 return 0;
173 err = 0;182
174 goto out;183 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
175 }184 if (!vc_ctrl_req)
176 ctrl_status = &vcrypto->ctrl_status;185 return -ENOMEM;
186
187 ctrl_status = &vc_ctrl_req->ctrl_status;
177 ctrl_status->status = VIRTIO_CRYPTO_ERR;188 ctrl_status->status = VIRTIO_CRYPTO_ERR;
178 ctrl = &vcrypto->ctrl;189 ctrl = &vc_ctrl_req->ctrl;
179 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);190 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);
180 ctrl->header.queue_id = 0;191 ctrl->header.queue_id = 0;
181 192
@@ -188,16 +199,22 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 6 unmodified lines
188 sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));199 sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));
189 sgs[num_out + num_in++] = &inhdr_sg;200 sgs[num_out + num_in++] = &inhdr_sg;
190 201
202 spin_lock(&vcrypto->ctrl_lock);
191 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC);203 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out, num_in, vcrypto, GFP_ATOMIC);
192 if (err < 0)204 if (err < 0) {
205 spin_unlock(&vcrypto->ctrl_lock);
193 goto out;206 goto out;
207 }
194 208
195 virtqueue_kick(vcrypto->ctrl_vq);209 virtqueue_kick(vcrypto->ctrl_vq);
196 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &inlen) &&210 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &inlen) &&
197 !virtqueue_is_broken(vcrypto->ctrl_vq))211 !virtqueue_is_broken(vcrypto->ctrl_vq))
198 cpu_relax();212 cpu_relax();
213 spin_unlock(&vcrypto->ctrl_lock);
199 214
200 if (ctrl_status->status != VIRTIO_CRYPTO_OK) {215 if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
216 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
217 ctrl_status->status, destroy_session->session_id);
201 err = -EINVAL;218 err = -EINVAL;
202 goto out;219 goto out;
203 }220 }
@@ -206,11 +223,7 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 2 unmodified lines
206 ctx->session_valid = false;223 ctx->session_valid = false;
207 224
208out:225out:
209 spin_unlock(&vcrypto->ctrl_lock);226 kfree(vc_ctrl_req);
210 if (err < 0) {
211 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
212 ctrl_status->status, destroy_session->session_id);
213 }
214 227
215 return err;228 return err;
216}229}
drivers/crypto/virtio/virtio_crypto_common.h+12−5
@@ -13,6 +13,7 @@ · 12 unmodified lines
13#include <crypto/aead.h>13#include <crypto/aead.h>
14#include <crypto/aes.h>14#include <crypto/aes.h>
15#include <crypto/engine.h>15#include <crypto/engine.h>
16#include <uapi/linux/virtio_crypto.h>
16 17
17 18
18/* Internal representation of a data virtqueue */19/* Internal representation of a data virtqueue */
@@ -65,11 +66,6 @@ struct virtio_crypto { · 46 unmodified lines
65 /* Maximum size of per request */66 /* Maximum size of per request */
66 u64 max_size;67 u64 max_size;
67 68
68 /* Control VQ buffers: protected by the ctrl_lock */
69 struct virtio_crypto_op_ctrl_req ctrl;
70 struct virtio_crypto_session_input input;
71 struct virtio_crypto_inhdr ctrl_status;
72
73 unsigned long status;69 unsigned long status;
74 atomic_t ref_count;70 atomic_t ref_count;
75 struct list_head list;71 struct list_head list;
@@ -85,6 +81,17 @@ struct virtio_crypto_sym_session_info { · 9 unmodified lines
85 __u64 session_id;81 __u64 session_id;
86};82};
87 83
84/*
85 * Note: there are padding fields in request, clear them to zero before
86 * sending to host to avoid to divulge any information.
87 * Ex, virtio_crypto_ctrl_request::ctrl::u::destroy_session::padding[48]
88 */
89struct virtio_crypto_ctrl_request {
90 struct virtio_crypto_op_ctrl_req ctrl;
91 struct virtio_crypto_session_input input;
92 struct virtio_crypto_inhdr ctrl_status;
93};
94
88struct virtio_crypto_request;95struct virtio_crypto_request;
89typedef void (*virtio_crypto_data_callback)96typedef void (*virtio_crypto_data_callback)
90 (struct virtio_crypto_request *vc_req, int len);97 (struct virtio_crypto_request *vc_req, int len);
drivers/crypto/virtio/virtio_crypto_skcipher_algs.c+32−18
@@ -126,6 +126,7 @@ static int virtio_crypto_alg_skcipher_init_session( · 125 unmodified lines
126 struct virtio_crypto_op_ctrl_req *ctrl;126 struct virtio_crypto_op_ctrl_req *ctrl;
127 struct virtio_crypto_session_input *input;127 struct virtio_crypto_session_input *input;
128 struct virtio_crypto_sym_create_session_req *sym_create_session;128 struct virtio_crypto_sym_create_session_req *sym_create_session;
129 struct virtio_crypto_ctrl_request *vc_ctrl_req;
129 130
130 /*131 /*
131 * Avoid to do DMA from the stack, switch to using132 * Avoid to do DMA from the stack, switch to using
@@ -136,15 +137,20 @@ static int virtio_crypto_alg_skcipher_init_session( · 4 unmodified lines
136 if (!cipher_key)137 if (!cipher_key)
137 return -ENOMEM;138 return -ENOMEM;
138 139
139 spin_lock(&vcrypto->ctrl_lock);140 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
141 if (!vc_ctrl_req) {
142 err = -ENOMEM;
143 goto out;
144 }
145
140 /* Pad ctrl header */146 /* Pad ctrl header */
141 ctrl = &vcrypto->ctrl;147 ctrl = &vc_ctrl_req->ctrl;
142 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);148 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
143 ctrl->header.algo = cpu_to_le32(alg);149 ctrl->header.algo = cpu_to_le32(alg);
144 /* Set the default dataqueue id to 0 */150 /* Set the default dataqueue id to 0 */
145 ctrl->header.queue_id = 0;151 ctrl->header.queue_id = 0;
146 152
147 input = &vcrypto->input;153 input = &vc_ctrl_req->input;
148 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);154 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
149 /* Pad cipher's parameters */155 /* Pad cipher's parameters */
150 sym_create_session = &ctrl->u.sym_create_session;156 sym_create_session = &ctrl->u.sym_create_session;
@@ -164,12 +170,12 @@ static int virtio_crypto_alg_skcipher_init_session( · 13 unmodified lines
164 sg_init_one(&inhdr, input, sizeof(*input));170 sg_init_one(&inhdr, input, sizeof(*input));
165 sgs[num_out + num_in++] = &inhdr;171 sgs[num_out + num_in++] = &inhdr;
166 172
173 spin_lock(&vcrypto->ctrl_lock);
167 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,174 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
168 num_in, vcrypto, GFP_ATOMIC);175 num_in, vcrypto, GFP_ATOMIC);
169 if (err < 0) {176 if (err < 0) {
170 spin_unlock(&vcrypto->ctrl_lock);177 spin_unlock(&vcrypto->ctrl_lock);
171 kfree_sensitive(cipher_key);178 goto out;
172 return err;
173 }179 }
174 virtqueue_kick(vcrypto->ctrl_vq);180 virtqueue_kick(vcrypto->ctrl_vq);
175 181
@@ -180,13 +186,13 @@ static int virtio_crypto_alg_skcipher_init_session( · 4 unmodified lines
180 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&186 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
181 !virtqueue_is_broken(vcrypto->ctrl_vq))187 !virtqueue_is_broken(vcrypto->ctrl_vq))
182 cpu_relax();188 cpu_relax();
189 spin_unlock(&vcrypto->ctrl_lock);
183 190
184 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {191 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
185 spin_unlock(&vcrypto->ctrl_lock);
186 pr_err("virtio_crypto: Create session failed status: %u\n",192 pr_err("virtio_crypto: Create session failed status: %u\n",
187 le32_to_cpu(input->status));193 le32_to_cpu(input->status));
188 kfree_sensitive(cipher_key);194 err = -EINVAL;
189 return -EINVAL;195 goto out;
190 }196 }
191 197
192 if (encrypt)198 if (encrypt)
@@ -194,10 +200,11 @@ static int virtio_crypto_alg_skcipher_init_session( · 1 unmodified line
194 else200 else
195 ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id);201 ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id);
196 202
197 spin_unlock(&vcrypto->ctrl_lock);203 err = 0;
198 204out:
205 kfree(vc_ctrl_req);
199 kfree_sensitive(cipher_key);206 kfree_sensitive(cipher_key);
200 return 0;207 return err;
201}208}
202 209
203static int virtio_crypto_alg_skcipher_close_session(210static int virtio_crypto_alg_skcipher_close_session(
@@ -212,12 +219,16 @@ static int virtio_crypto_alg_skcipher_close_session( · 8 unmodified lines
212 unsigned int num_out = 0, num_in = 0;219 unsigned int num_out = 0, num_in = 0;
213 struct virtio_crypto_op_ctrl_req *ctrl;220 struct virtio_crypto_op_ctrl_req *ctrl;
214 struct virtio_crypto_inhdr *ctrl_status;221 struct virtio_crypto_inhdr *ctrl_status;
222 struct virtio_crypto_ctrl_request *vc_ctrl_req;
215 223
216 spin_lock(&vcrypto->ctrl_lock);224 vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
217 ctrl_status = &vcrypto->ctrl_status;225 if (!vc_ctrl_req)
226 return -ENOMEM;
227
228 ctrl_status = &vc_ctrl_req->ctrl_status;
218 ctrl_status->status = VIRTIO_CRYPTO_ERR;229 ctrl_status->status = VIRTIO_CRYPTO_ERR;
219 /* Pad ctrl header */230 /* Pad ctrl header */
220 ctrl = &vcrypto->ctrl;231 ctrl = &vc_ctrl_req->ctrl;
221 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);232 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
222 /* Set the default virtqueue id to 0 */233 /* Set the default virtqueue id to 0 */
223 ctrl->header.queue_id = 0;234 ctrl->header.queue_id = 0;
@@ -236,28 +247,31 @@ static int virtio_crypto_alg_skcipher_close_session( · 12 unmodified lines
236 sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status));247 sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status));
237 sgs[num_out + num_in++] = &status_sg;248 sgs[num_out + num_in++] = &status_sg;
238 249
250 spin_lock(&vcrypto->ctrl_lock);
239 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,251 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
240 num_in, vcrypto, GFP_ATOMIC);252 num_in, vcrypto, GFP_ATOMIC);
241 if (err < 0) {253 if (err < 0) {
242 spin_unlock(&vcrypto->ctrl_lock);254 spin_unlock(&vcrypto->ctrl_lock);
243 return err;255 goto out;
244 }256 }
245 virtqueue_kick(vcrypto->ctrl_vq);257 virtqueue_kick(vcrypto->ctrl_vq);
246 258
247 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&259 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
248 !virtqueue_is_broken(vcrypto->ctrl_vq))260 !virtqueue_is_broken(vcrypto->ctrl_vq))
249 cpu_relax();261 cpu_relax();
262 spin_unlock(&vcrypto->ctrl_lock);
250 263
251 if (ctrl_status->status != VIRTIO_CRYPTO_OK) {264 if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
252 spin_unlock(&vcrypto->ctrl_lock);
253 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",265 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
254 ctrl_status->status, destroy_session->session_id);266 ctrl_status->status, destroy_session->session_id);
255 267
256 return -EINVAL;268 return -EINVAL;
257 }269 }
258 spin_unlock(&vcrypto->ctrl_lock);
259 270
260 return 0;271 err = 0;
272out:
273 kfree(vc_ctrl_req);
274 return err;
261}275}
262 276
263static int virtio_crypto_alg_skcipher_init_sessions(277static int virtio_crypto_alg_skcipher_init_sessions(

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t