From 738b9ee6450f7d83359ff57d81851ae4baf44067 Mon Sep 17 00:00:00 2001 From: Ondrej Kozina Date: Mon, 6 May 2024 11:18:46 +0200 Subject: [PATCH] Use proper write_buffer in LUKS1 reencryption code. The raw write() syscal may write less bytes than requested. We have write_buffer in utils_io.c that handles it properly. --- src/utils_reencrypt_luks1.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/utils_reencrypt_luks1.c b/src/utils_reencrypt_luks1.c index 3f20af77..a0bbfba3 100644 --- a/src/utils_reencrypt_luks1.c +++ b/src/utils_reencrypt_luks1.c @@ -733,18 +733,14 @@ static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new, goto out; } - /* - * FIXME: this may fail with shorter write. - * Replace ir with write_buffer from lib/utils_io.c - */ - s2 = write(fd_new, buf, s1); + s2 = write_buffer(fd_new, buf, s1); if (s2 < 0 || s2 != s1) { - log_dbg("Write error, expecting %zu, got %zd.", - block_size, s2); + log_dbg("Write error, expecting %zd, got %zd.", + s1, s2); goto out; } - rc->device_offset += s1; + rc->device_offset += s2; if (ARG_SET(OPT_WRITE_LOG_ID) && write_log(rc) < 0) goto out; @@ -820,18 +816,14 @@ static int copy_data_backward(struct reenc_ctx *rc, int fd_old, int fd_new, goto out; } - /* - * FIXME: this may fail with shorter write. - * Replace ir with write_buffer from lib/utils_io.c - */ - s2 = write(fd_new, buf, working_block); - if (s2 < 0) { - log_dbg("Write error, expecting %zu, got %zd.", - block_size, s2); + s2 = write_buffer(fd_new, buf, s1); + if (s2 < 0 || s2 != s1) { + log_dbg("Write error, expecting %zd, got %zd.", + s1, s2); goto out; } - rc->device_offset -= s1; + rc->device_offset -= s2; if (ARG_SET(OPT_WRITE_LOG_ID) && write_log(rc) < 0) goto out;