Skip to content

Commit 69078b9

Browse files
committed
writer: Use a union for stack buffer to ensure alignment
Adapted from an equivalent patch by Simon for ostree: ostreedev/ostree@67ed2ac Reported-by: Simon McVittie <[email protected]> Signed-off-by: Colin Walters <[email protected]>
1 parent d771778 commit 69078b9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

libcomposefs/lcfs-writer.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,15 @@ int lcfs_compute_fsverity_from_fd(uint8_t *digest, int fd)
568568
// is an error if fsverity is not enabled.
569569
int lcfs_fd_measure_fsverity(uint8_t *digest, int fd)
570570
{
571-
char buf[sizeof(struct fsverity_digest) + MAX_DIGEST_SIZE];
572-
struct fsverity_digest *fsv = (struct fsverity_digest *)&buf;
571+
union {
572+
struct fsverity_digest fsv;
573+
char buf[sizeof(struct fsverity_digest) + MAX_DIGEST_SIZE];
574+
} result;
573575

574576
// First, ask the kernel if the file already has fsverity; if so we just return
575577
// that.
576-
fsv->digest_size = MAX_DIGEST_SIZE;
577-
int res = ioctl(fd, FS_IOC_MEASURE_VERITY, fsv);
578+
result.fsv.digest_size = MAX_DIGEST_SIZE;
579+
int res = ioctl(fd, FS_IOC_MEASURE_VERITY, &result);
578580
if (res == -1) {
579581
if (errno == ENODATA || errno == EOPNOTSUPP || errno == ENOTTY) {
580582
// Canonicalize errno
@@ -584,11 +586,11 @@ int lcfs_fd_measure_fsverity(uint8_t *digest, int fd)
584586
}
585587
// The file has fsverity enabled, but with an unexpected different algorithm (e.g. sha512).
586588
// This is going to be a weird corner case. For now, we error out.
587-
if (fsv->digest_size != LCFS_DIGEST_SIZE) {
589+
if (result.fsv.digest_size != LCFS_DIGEST_SIZE) {
588590
return -EWRONGVERITY;
589591
}
590592

591-
memcpy(digest, buf + sizeof(struct fsverity_digest), LCFS_DIGEST_SIZE);
593+
memcpy(digest, result.buf + sizeof(struct fsverity_digest), LCFS_DIGEST_SIZE);
592594

593595
return 0;
594596
}

0 commit comments

Comments
 (0)