[PATCH v2] crypto: algif - Mark sgl end at the end of data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
algif_skcipher sends 127 sgl buffers for encryption regardless of how
many buffers acctually have data to process, where the few first with
valid len and the rest with zero len. This is not very eficient and may cause
problems when algs do something like this without checking the buff
lenght:
for_each_sg(sgl, sg, sg_nents, i)
	sg_virt(sg)
This patch marks the last one with data as the last one to process.

Changes:
v2 - use data len to find the last buffer instead of nents in RX list.

Signed-off-by: Tadeusz Struk <tadeusz.struk@xxxxxxxxx>
---
 crypto/algif_skcipher.c |   22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c
index f2a88a7..cde507f 100644
--- a/crypto/algif_skcipher.c
+++ b/crypto/algif_skcipher.c
@@ -414,6 +414,23 @@ unlock:
 	return err ?: size;
 }
 
+static int skcipher_get_nents_with_data(struct scatterlist *sg, int len)
+{
+	int nents;
+
+	for (nents = 0; sg; sg = sg_next(sg)) {
+		if (!sg)
+			return nents;
+
+		len -= sg->length;
+		if (!(len > 0))
+			return nents;
+		nents++;
+	}
+
+	return 0;
+}
+
 static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
 			    struct msghdr *msg, size_t ignored, int flags)
 {
@@ -437,6 +454,8 @@ static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
 		char __user *from = iov->iov_base;
 
 		while (seglen) {
+			int nents;
+
 			sgl = list_first_entry(&ctx->tsgl,
 					       struct skcipher_sg_list, list);
 			sg = sgl->sg;
@@ -464,6 +483,8 @@ static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
 			if (!used)
 				goto free;
 
+			nents = skcipher_get_nents_with_data(sg, used);
+			sg_mark_end(&sg[nents]);
 			ablkcipher_request_set_crypt(&ctx->req, sg,
 						     ctx->rsgl.sg, used,
 						     ctx->iv);
@@ -473,6 +494,7 @@ static int skcipher_recvmsg(struct kiocb *unused, struct socket *sock,
 					crypto_ablkcipher_encrypt(&ctx->req) :
 					crypto_ablkcipher_decrypt(&ctx->req),
 				&ctx->completion);
+			sg_unmark_end(&sg[nents]);
 
 free:
 			af_alg_free_sg(&ctx->rsgl);

--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Kernel]     [Gnu Classpath]     [Gnu Crypto]     [DM Crypt]     [Netfilter]     [Bugtraq]

  Powered by Linux