From 69078b9f8216d97a0637dc09a73f4dd7c1648ab4 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 15 Nov 2024 15:59:06 +0000 Subject: [PATCH] writer: Use a union for stack buffer to ensure alignment Adapted from an equivalent patch by Simon for ostree: https://github.com/ostreedev/ostree/pull/3340/commits/67ed2acad47642c5253d66aa70b5035687ed5728 Reported-by: Simon McVittie Signed-off-by: Colin Walters --- libcomposefs/lcfs-writer.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libcomposefs/lcfs-writer.c b/libcomposefs/lcfs-writer.c index 18c20f8..f6e202e 100644 --- a/libcomposefs/lcfs-writer.c +++ b/libcomposefs/lcfs-writer.c @@ -568,13 +568,15 @@ int lcfs_compute_fsverity_from_fd(uint8_t *digest, int fd) // is an error if fsverity is not enabled. int lcfs_fd_measure_fsverity(uint8_t *digest, int fd) { - char buf[sizeof(struct fsverity_digest) + MAX_DIGEST_SIZE]; - struct fsverity_digest *fsv = (struct fsverity_digest *)&buf; + union { + struct fsverity_digest fsv; + char buf[sizeof(struct fsverity_digest) + MAX_DIGEST_SIZE]; + } result; // First, ask the kernel if the file already has fsverity; if so we just return // that. - fsv->digest_size = MAX_DIGEST_SIZE; - int res = ioctl(fd, FS_IOC_MEASURE_VERITY, fsv); + result.fsv.digest_size = MAX_DIGEST_SIZE; + int res = ioctl(fd, FS_IOC_MEASURE_VERITY, &result); if (res == -1) { if (errno == ENODATA || errno == EOPNOTSUPP || errno == ENOTTY) { // Canonicalize errno @@ -584,11 +586,11 @@ int lcfs_fd_measure_fsverity(uint8_t *digest, int fd) } // The file has fsverity enabled, but with an unexpected different algorithm (e.g. sha512). // This is going to be a weird corner case. For now, we error out. - if (fsv->digest_size != LCFS_DIGEST_SIZE) { + if (result.fsv.digest_size != LCFS_DIGEST_SIZE) { return -EWRONGVERITY; } - memcpy(digest, buf + sizeof(struct fsverity_digest), LCFS_DIGEST_SIZE); + memcpy(digest, result.buf + sizeof(struct fsverity_digest), LCFS_DIGEST_SIZE); return 0; }