Merge pull request #27254 from poettering/cmsg-align-check

socket-util: tighten CMSG_TYPED_DATA() alignment checks
This commit is contained in:
Yu Watanabe
2023-04-14 13:49:04 +09:00
committed by GitHub
14 changed files with 39 additions and 38 deletions
+1 -1
View File
@@ -1047,7 +1047,7 @@ ssize_t receive_one_fd_iov(
}
if (found)
*ret_fd = *(int*) CMSG_DATA(found);
*ret_fd = *CMSG_TYPED_DATA(found, int);
else
*ret_fd = -EBADF;
+7
View File
@@ -175,9 +175,16 @@ int flush_accept(int fd);
#define CMSG_FOREACH(cmsg, mh) \
for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
/* Returns the cmsghdr's data pointer, but safely cast to the specified type. Does two alignment checks: one
* at compile time, that the requested type has a smaller or same alignment as 'struct cmsghdr', and one
* during runtime, that the actual pointer matches the alignment too. This is supposed to catch cases such as
* 'struct timeval' is embedded into 'struct cmsghdr' on architectures where the alignment of the former is 8
* bytes (because of a 64bit time_t), but of the latter is 4 bytes (because size_t is 32bit), such as
* riscv32. */
#define CMSG_TYPED_DATA(cmsg, type) \
({ \
struct cmsghdr *_cmsg = cmsg; \
assert_cc(__alignof__(type) <= __alignof__(struct cmsghdr)); \
_cmsg ? CAST_ALIGN_PTR(type, CMSG_DATA(_cmsg)) : (type*) NULL; \
})