Skip to content

Commit

Permalink
writer: Use a union for stack buffer to ensure alignment
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cgwalters committed Nov 15, 2024
1 parent d771778 commit 69078b9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down

0 comments on commit 69078b9

Please sign in to comment.