Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ktls: send and recv cmsg #4073

Closed
wants to merge 3 commits into from
Closed

ktls: send and recv cmsg #4073

wants to merge 3 commits into from

Conversation

toidiu
Copy link
Contributor

@toidiu toidiu commented Jun 23, 2023

Description of changes:

This PR adds methods to read and write cmsg via sendmsg and recvmsg. These methods can be used to send data but also ancillary data, which is needed to communicate the record type when using kTLS.

Since some of the functionality is linux specific (the first platform for which kTLS support will be added), I gate compilation to only that platform. Finally, since kTLS specific headers are not expose by linux uapi (#4064), I also define these headers inline in s2n_ktls_linux.h.

Testing:

Unit tests.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added the s2n-core team label Jun 23, 2023
@toidiu toidiu force-pushed the ak-ktls0_cmsg branch 6 times, most recently from 6211759 to 7b69fc3 Compare June 26, 2023 04:30
@toidiu toidiu changed the title Ak ktls0 cmsg ktls: send and recv cmsg Jun 26, 2023
@toidiu toidiu marked this pull request as ready for review June 26, 2023 17:18
@toidiu toidiu mentioned this pull request Jun 22, 2023
32 tasks
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
tls/s2n_ktls_io.c Show resolved Hide resolved
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
tls/s2n_ktls_linux.h Outdated Show resolved Hide resolved
tls/s2n_ktls_io.c Show resolved Hide resolved
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
@lrstewart lrstewart self-requested a review June 26, 2023 22:05
@toidiu toidiu force-pushed the ak-ktls0_cmsg branch 4 times, most recently from a29ffda to 7f3d0e2 Compare June 27, 2023 05:04
@toidiu toidiu requested a review from camshaft June 27, 2023 06:04
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
@toidiu toidiu marked this pull request as draft June 30, 2023 21:02
@toidiu toidiu force-pushed the ak-ktls0_cmsg branch 4 times, most recently from 298f03a to 0745051 Compare July 14, 2023 19:50
Comment on lines +105 to +106
char buf[CMSG_SPACE(sizeof(uint8_t))];
struct cmsghdr _align;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To understand why alignment is necessary we need to look at cmsghdr (ancillary data). The struct defines a length cmsg_len but then captures the data, cmsg_data[], as a flexible array, which allow you to capture data of arbitrary size. However since the underlying struct is still a cmsghdr, we need to use a union to ensure that the overall allocation aligns with cmsghdr.

 struct cmsghdr {
     size_t cmsg_len;    /* Data byte count, including header */
     int    cmsg_level;  /* Originating protocol */
     int    cmsg_type;   /* Protocol-specific type */
     unsigned char cmsg_data[]; */
 };

p.s. alignment using union is also included in the man pages for cmsg https://man7.org/linux/man-pages/man3/cmsg.3.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes coming regarding which headers we define and which we can get from linux/tls.h: #4071 (comment)

However its still possible to review this PR and rebase once that PR is merged/vice-versa. Basically, lets not block this PR on feature probe discussion since that is being handled in the other PR and we can focus only on the cmsg stuff here.

@toidiu toidiu marked this pull request as ready for review July 17, 2023 22:03
tls/s2n_ktls_io.c Outdated Show resolved Hide resolved
/* Init msghdr */
struct msghdr msg = { 0 };

#if S2N_KTLS_SUPPORTED /* CMSG_* macros are platform specific */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interleaving this check everywhere makes it a bit hard to follow, IMO. why not just wrap the whole file in this? none of these should even be called if kTLS isn't supported, right?

Copy link
Contributor Author

@toidiu toidiu Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do that but thought we try to limit the ifdef usage to the minimum surface area. Thoughts @lrstewart ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would much rather read a single ifdef at the top of the file and say "ok this whole module is disabled if ktls is disabled" rather than having to make sure each individual function does the right thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this idea. We don't so much try to limit ifdef surface area as keep ifdefs readable and minimal. If we can ifdef just one part of one method in a file, then great, keep the surface area as low as possible. But when more ifdefs start creeping in that becomes a problem and ifdefing the whole file definitely becomes the better option :)

But don't forget to have versions of any methods required outside of this file in the else. Otherwise the ifdefs just spread to other files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 0dfbfb3

tests/unit/s2n_ktls_cmsg_test.c Outdated Show resolved Hide resolved
@toidiu toidiu requested a review from camshaft July 18, 2023 07:37
@toidiu toidiu force-pushed the ak-ktls0_cmsg branch 2 times, most recently from cd2a9a3 to 8171241 Compare July 19, 2023 02:11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For discoverability, this should probably be "s2n_ktls_io_cmsg_test". Or just "s2n_ktls_io_test" if you don't expect to have multiple s2n_ktls_io* test suites.

Comment on lines +39 to +42

/* set send buffer */
msg->msg_iov = msg_iov;
msg->msg_iovlen = count;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this belongs in this method. Mutating your inputs like this seems unexpected. Since you do other initialization of msghdr elsewhere, this initialization should happen elsewhere too.

Comment on lines +105 to +106
char buf[CMSG_SPACE(sizeof(uint8_t))];
struct cmsghdr _align;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +23 to +28
S2N_RESULT s2n_ktls_send_msg_impl(int sock, struct msghdr *msg,
struct iovec *msg_iov, size_t count, s2n_blocked_status *blocked, ssize_t *send_len);
S2N_RESULT s2n_ktls_recv_msg_impl(struct s2n_connection *conn, int sock, struct msghdr *msg,
struct iovec *msg_iov, s2n_blocked_status *blocked, ssize_t *bytes_read);
S2N_RESULT s2n_ktls_set_ancillary_data(struct msghdr *msg, uint8_t record_type);
S2N_RESULT s2n_ktls_parse_ancillary_data(struct msghdr *msg, uint8_t *record_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're doing the model where you test all the component private methods separately again instead of testing the methods of interest :/ And if you can't actually call s2n_ktls_send_msg / s2n_ktls_recv_msg, doesn't that also prevent you from properly testing any method that calls them? What is your plan for the next layer of tests?

Maybe you need to reuse the refactor from https://github.com/aws/s2n-tls/pull/4071/files#r1268745257 for sendmsg and recvmsg. And at that point you've probably got too many "mocks" to reasonably argue they aren't just for testing, so I'd probably switch to the global version instead of the config version.

@toidiu
Copy link
Contributor Author

toidiu commented Jul 25, 2023

I will be adding mock IO testing and there are going to be some significant changes. Closing this for now

@toidiu toidiu closed this Jul 25, 2023
@aws aws deleted a comment from lrstewart Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants