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
108108 unsigned int num_out = 0, num_in = 0;
109109 struct virtio_crypto_op_ctrl_req *ctrl;
110110 struct virtio_crypto_session_input *input;
111+ struct virtio_crypto_ctrl_request *vc_ctrl_req;
111112
112113 pkey = kmemdup(key, keylen, GFP_ATOMIC);
113114 if (!pkey)
114115 return -ENOMEM;
115116
116− spin_lock(&vcrypto->ctrl_lock);
117− ctrl = &vcrypto->ctrl;
117+ vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
118+ if (!vc_ctrl_req) {
119+ err = -ENOMEM;
120+ goto out;
121+ }
122+
123+ ctrl = &vc_ctrl_req->ctrl;
118124 memcpy(&ctrl->header, header, sizeof(ctrl->header));
119125 memcpy(&ctrl->u, para, sizeof(ctrl->u));
120− input = &vcrypto->input;
126+ input = &vc_ctrl_req->input;
121127 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
122128
123129 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
129135 sg_init_one(&inhdr_sg, input, sizeof(*input));
130136 sgs[num_out + num_in++] = &inhdr_sg;
131137
138+ spin_lock(&vcrypto->ctrl_lock);
132139 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);
134142 goto out;
143+ }
135144
136145 virtqueue_kick(vcrypto->ctrl_vq);
137146 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &inlen) &&
138147 !virtqueue_is_broken(vcrypto->ctrl_vq))
139148 cpu_relax();
149+ spin_unlock(&vcrypto->ctrl_lock);
140150
141151 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));
142154 err = -EINVAL;
143155 goto out;
144156 }
@@ -148,13 +160,9 @@ static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher · 3 unmodified lines
148160 err = 0;
149161
150162 out:
151− spin_unlock(&vcrypto->ctrl_lock);
163+ kfree(vc_ctrl_req);
152164 kfree_sensitive(pkey);
153165
154− if (err < 0)
155− pr_err("virtio_crypto: Create session failed status: %u\n",
156− le32_to_cpu(input->status));
157−
158166 return err;
159167 }
160168
@@ -167,15 +175,18 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 6 unmodified lines
167175 int err;
168176 struct virtio_crypto_op_ctrl_req *ctrl;
169177 struct virtio_crypto_inhdr *ctrl_status;
178+ struct virtio_crypto_ctrl_request *vc_ctrl_req;
170179
171− spin_lock(&vcrypto->ctrl_lock);
172− if (!ctx->session_valid) {
173− err = 0;
174− goto out;
175− }
176− ctrl_status = &vcrypto->ctrl_status;
180+ if (!ctx->session_valid)
181+ return 0;
182+
183+ vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
184+ if (!vc_ctrl_req)
185+ return -ENOMEM;
186+
187+ ctrl_status = &vc_ctrl_req->ctrl_status;
177188 ctrl_status->status = VIRTIO_CRYPTO_ERR;
178− ctrl = &vcrypto->ctrl;
189+ ctrl = &vc_ctrl_req->ctrl;
179190 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION);
180191 ctrl->header.queue_id = 0;
181192
@@ -188,16 +199,22 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 6 unmodified lines
188199 sg_init_one(&inhdr_sg, &ctrl_status->status, sizeof(ctrl_status->status));
189200 sgs[num_out + num_in++] = &inhdr_sg;
190201
202+ spin_lock(&vcrypto->ctrl_lock);
191203 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);
193206 goto out;
207+ }
194208
195209 virtqueue_kick(vcrypto->ctrl_vq);
196210 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &inlen) &&
197211 !virtqueue_is_broken(vcrypto->ctrl_vq))
198212 cpu_relax();
213+ spin_unlock(&vcrypto->ctrl_lock);
199214
200215 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);
201218 err = -EINVAL;
202219 goto out;
203220 }
@@ -206,11 +223,7 @@ static int virtio_crypto_alg_akcipher_close_session(struct virtio_crypto_akciphe · 2 unmodified lines
206223 ctx->session_valid = false;
207224
208225 out:
209− spin_unlock(&vcrypto->ctrl_lock);
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− }
226+ kfree(vc_ctrl_req);
214227
215228 return err;
216229 }
drivers/crypto/virtio/virtio_crypto_common.h+12−5
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 #include <crypto/aead.h>
1414 #include <crypto/aes.h>
1515 #include <crypto/engine.h>
16+#include <uapi/linux/virtio_crypto.h>
1617
1718
1819 /* Internal representation of a data virtqueue */
@@ -65,11 +66,6 @@ struct virtio_crypto { · 46 unmodified lines
6566 /* Maximum size of per request */
6667 u64 max_size;
6768
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−
7369 unsigned long status;
7470 atomic_t ref_count;
7571 struct list_head list;
@@ -85,6 +81,17 @@ struct virtio_crypto_sym_session_info { · 9 unmodified lines
8581 __u64 session_id;
8682 };
8783
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+ */
89+struct 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+
8895 struct virtio_crypto_request;
8996 typedef void (*virtio_crypto_data_callback)
9097 (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
126126 struct virtio_crypto_op_ctrl_req *ctrl;
127127 struct virtio_crypto_session_input *input;
128128 struct virtio_crypto_sym_create_session_req *sym_create_session;
129+ struct virtio_crypto_ctrl_request *vc_ctrl_req;
129130
130131 /*
131132 * 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
136137 if (!cipher_key)
137138 return -ENOMEM;
138139
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+
140146 /* Pad ctrl header */
141− ctrl = &vcrypto->ctrl;
147+ ctrl = &vc_ctrl_req->ctrl;
142148 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_CREATE_SESSION);
143149 ctrl->header.algo = cpu_to_le32(alg);
144150 /* Set the default dataqueue id to 0 */
145151 ctrl->header.queue_id = 0;
146152
147− input = &vcrypto->input;
153+ input = &vc_ctrl_req->input;
148154 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR);
149155 /* Pad cipher's parameters */
150156 sym_create_session = &ctrl->u.sym_create_session;
@@ -164,12 +170,12 @@ static int virtio_crypto_alg_skcipher_init_session( · 13 unmodified lines
164170 sg_init_one(&inhdr, input, sizeof(*input));
165171 sgs[num_out + num_in++] = &inhdr;
166172
173+ spin_lock(&vcrypto->ctrl_lock);
167174 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
168175 num_in, vcrypto, GFP_ATOMIC);
169176 if (err < 0) {
170177 spin_unlock(&vcrypto->ctrl_lock);
171− kfree_sensitive(cipher_key);
172− return err;
178+ goto out;
173179 }
174180 virtqueue_kick(vcrypto->ctrl_vq);
175181
@@ -180,13 +186,13 @@ static int virtio_crypto_alg_skcipher_init_session( · 4 unmodified lines
180186 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
181187 !virtqueue_is_broken(vcrypto->ctrl_vq))
182188 cpu_relax();
189+ spin_unlock(&vcrypto->ctrl_lock);
183190
184191 if (le32_to_cpu(input->status) != VIRTIO_CRYPTO_OK) {
185− spin_unlock(&vcrypto->ctrl_lock);
186192 pr_err("virtio_crypto: Create session failed status: %u\n",
187193 le32_to_cpu(input->status));
188− kfree_sensitive(cipher_key);
189− return -EINVAL;
194+ err = -EINVAL;
195+ goto out;
190196 }
191197
192198 if (encrypt)
@@ -194,10 +200,11 @@ static int virtio_crypto_alg_skcipher_init_session( · 1 unmodified line
194200 else
195201 ctx->dec_sess_info.session_id = le64_to_cpu(input->session_id);
196202
197− spin_unlock(&vcrypto->ctrl_lock);
198−
203+ err = 0;
204+out:
205+ kfree(vc_ctrl_req);
199206 kfree_sensitive(cipher_key);
200− return 0;
207+ return err;
201208 }
202209
203210 static int virtio_crypto_alg_skcipher_close_session(
@@ -212,12 +219,16 @@ static int virtio_crypto_alg_skcipher_close_session( · 8 unmodified lines
212219 unsigned int num_out = 0, num_in = 0;
213220 struct virtio_crypto_op_ctrl_req *ctrl;
214221 struct virtio_crypto_inhdr *ctrl_status;
222+ struct virtio_crypto_ctrl_request *vc_ctrl_req;
215223
216− spin_lock(&vcrypto->ctrl_lock);
217− ctrl_status = &vcrypto->ctrl_status;
224+ vc_ctrl_req = kzalloc(sizeof(*vc_ctrl_req), GFP_KERNEL);
225+ if (!vc_ctrl_req)
226+ return -ENOMEM;
227+
228+ ctrl_status = &vc_ctrl_req->ctrl_status;
218229 ctrl_status->status = VIRTIO_CRYPTO_ERR;
219230 /* Pad ctrl header */
220− ctrl = &vcrypto->ctrl;
231+ ctrl = &vc_ctrl_req->ctrl;
221232 ctrl->header.opcode = cpu_to_le32(VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION);
222233 /* Set the default virtqueue id to 0 */
223234 ctrl->header.queue_id = 0;
@@ -236,28 +247,31 @@ static int virtio_crypto_alg_skcipher_close_session( · 12 unmodified lines
236247 sg_init_one(&status_sg, &ctrl_status->status, sizeof(ctrl_status->status));
237248 sgs[num_out + num_in++] = &status_sg;
238249
250+ spin_lock(&vcrypto->ctrl_lock);
239251 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, num_out,
240252 num_in, vcrypto, GFP_ATOMIC);
241253 if (err < 0) {
242254 spin_unlock(&vcrypto->ctrl_lock);
243− return err;
255+ goto out;
244256 }
245257 virtqueue_kick(vcrypto->ctrl_vq);
246258
247259 while (!virtqueue_get_buf(vcrypto->ctrl_vq, &tmp) &&
248260 !virtqueue_is_broken(vcrypto->ctrl_vq))
249261 cpu_relax();
262+ spin_unlock(&vcrypto->ctrl_lock);
250263
251264 if (ctrl_status->status != VIRTIO_CRYPTO_OK) {
252− spin_unlock(&vcrypto->ctrl_lock);
253265 pr_err("virtio_crypto: Close session failed status: %u, session_id: 0x%llx\n",
254266 ctrl_status->status, destroy_session->session_id);
255267
256268 return -EINVAL;
257269 }
258− spin_unlock(&vcrypto->ctrl_lock);
259270
260− return 0;
271+ err = 0;
272+out:
273+ kfree(vc_ctrl_req);
274+ return err;
261275 }
262276
263277 static 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