[CRIU] [PATCH] service: allocate buffers for messages dinamically

Andrey Vagin avagin at openvz.org
Fri Apr 17 06:37:40 PDT 2015


Currently we use a static buffer, but it is too small.

Error (cr-service.c:58): Failed unpacking request: Success
Error (cr-service.c:694): Can't recv request: Success
data too short after length-prefix of 1217

Reported-by: Ross Boucher <rboucher at gmail.com>
Cc: Ross Boucher <rboucher at gmail.com>
Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 cr-service.c | 34 ++++++++++++++++++++++++++++------
 lib/criu.c   |  4 +++-
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/cr-service.c b/cr-service.c
index 0f0e2e2..30522b3 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -36,12 +36,32 @@
 
 unsigned int service_sk_ino = -1;
 
-static int recv_criu_msg(int socket_fd, CriuReq **msg)
+struct service_msg {
+	struct msghdr	h;
+	struct iovec	iov[1];
+};
+
+static int recv_criu_msg(int socket_fd, CriuReq **req)
 {
-	unsigned char buf[CR_MAX_MSG_SIZE];
+	struct service_msg msg = {};
+	unsigned char *buf;
 	int len;
 
-	len = read(socket_fd, buf, CR_MAX_MSG_SIZE);
+	msg.h.msg_iov = msg.iov;
+	msg.h.msg_iovlen = 0;
+
+	len = recvmsg(socket_fd, &msg.h, MSG_TRUNC | MSG_PEEK);
+	if (len == -1) {
+		pr_perror("Can't read request");
+		return -1;
+	}
+
+	buf = alloca(len);
+
+	msg.h.msg_iovlen = 1;
+	msg.iov[0].iov_base = buf;
+	msg.iov[0].iov_len = len;
+	len = recvmsg(socket_fd, &msg.h, MSG_TRUNC);
 	if (len == -1) {
 		pr_perror("Can't read request");
 		return -1;
@@ -53,8 +73,8 @@ static int recv_criu_msg(int socket_fd, CriuReq **msg)
 		return -1;
 	}
 
-	*msg = criu_req__unpack(NULL, len, buf);
-	if (!*msg) {
+	*req = criu_req__unpack(NULL, len, buf);
+	if (!*req) {
 		pr_perror("Failed unpacking request");
 		return -1;
 	}
@@ -64,11 +84,13 @@ static int recv_criu_msg(int socket_fd, CriuReq **msg)
 
 static int send_criu_msg(int socket_fd, CriuResp *msg)
 {
-	unsigned char buf[CR_MAX_MSG_SIZE];
+	unsigned char *buf;
 	int len;
 
 	len = criu_resp__get_packed_size(msg);
 
+	buf = alloca(len);
+
 	if (criu_resp__pack(msg, buf) != len) {
 		pr_perror("Failed packing response");
 		return -1;
diff --git a/lib/criu.c b/lib/criu.c
index 9308250..a141b50 100644
--- a/lib/criu.c
+++ b/lib/criu.c
@@ -335,11 +335,13 @@ err:
 
 static int send_req(int socket_fd, CriuReq *req)
 {
-	unsigned char buf[CR_MAX_MSG_SIZE];
+	unsigned char *buf;
 	int len;
 
 	len = criu_req__get_packed_size(req);
 
+	buf = alloca(len);
+
 	if (criu_req__pack(req, buf) != len) {
 		perror("Failed packing request");
 		goto err;
-- 
2.1.0



More information about the CRIU mailing list