[Openvpn-devel] test_pkcs11.c: set file offset to 0 after ftruncate

Message ID 20240812232158.3776869-1-selva.nair@gmail.com
State Accepted
Headers show
Series [Openvpn-devel] test_pkcs11.c: set file offset to 0 after ftruncate | expand

Commit Message

Selva Nair Aug. 12, 2024, 11:21 p.m. UTC
From: Selva Nair <selva.nair@gmail.com>

Currently key and cert file fd's are reused after ftruncate()
without setting the offset to zero. This causes subsequent
data to be written at some finite offset with the hole in
the file automatically filled by zeros. Fix it by calling
lseek() to set the offset to zero.

The test works nevertheless because p11tool seem to generously
ignore any junk before the "BEGIN" marker.

Change-Id: Ib0fe15a4ba18d89216b0288e6cd6be66ed377bd4
Signed-off-by: Selva Nair <selva.nair@gmail.com>
---
 tests/unit_tests/openvpn/test_pkcs11.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Gert Doering Aug. 13, 2024, 6:33 a.m. UTC | #1
Acked-by: Gert Doering <gert@greenie.muc.de>

"explanation makes sense, man ftruncate clearly says 'fd is not modified'"

smoke tested ("make check") on linux with --enable-pkcs11

Your patch has been applied to the master branch.

Not applied to 2.6 because the code in question does not exist there.

commit dcf735009c8caabf5e4a9feeb0d32907aafe8f17 (master)
Author: Selva Nair
Date:   Mon Aug 12 19:21:58 2024 -0400

     test_pkcs11.c: set file offset to 0 after ftruncate

     Signed-off-by: Selva Nair <selva.nair@gmail.com>
     Acked-by: Gert Doering <gert@greenie.muc.de>
     Message-Id: <20240812232158.3776869-1-selva.nair@gmail.com>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg29010.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/tests/unit_tests/openvpn/test_pkcs11.c b/tests/unit_tests/openvpn/test_pkcs11.c
index 5518fa15..3fe3e460 100644
--- a/tests/unit_tests/openvpn/test_pkcs11.c
+++ b/tests/unit_tests/openvpn/test_pkcs11.c
@@ -273,6 +273,8 @@  init(void **state)
 
         assert_int_equal(ftruncate(cert_fd, 0), 0);
         assert_int_equal(ftruncate(key_fd, 0), 0);
+        assert_int_equal(lseek(cert_fd, 0, SEEK_SET), 0);
+        assert_int_equal(lseek(key_fd, 0, SEEK_SET), 0);
         num_certs++;
     }