linux-user: Implement SOL_ALG encryption support
PATCH 1/2 linux-user: Implement SOL_ALG encryption support
Add suport to handle SOL_ALG packets via sendmsg() and recvmsg(). This allows emulated userspace to use encryption functionality.
Tested with the debian ell package with hppa guest on x86_64 host.
--- linux-user/syscall.c | 8 ++++++++ 1 file changed, 8 insertions(+)
linux-user/syscall.c +8 -0
@@ -1871,6 +1871,14 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,__get_user (cred->pid, &target_cred->pid);__get_user (cred->uid, &target_cred->uid);__get_user (cred->gid, &target_cred->gid); + }else if (cmsg->cmsg_level ==SOL_ALG ) { +uint32_t *dst = (uint32_t *)data; + +memcpy (dst, target_data, len); +/* fix endianess of first 32-bit word */ +if (len >=sizeof (uint32_t )) { + *dst =tswap32 (*dst); + } }else {qemu_log_mask (LOG_UNIMP ,"Unsupported ancillary data: %d/%d\n" , cmsg->cmsg_level, cmsg->cmsg_type);
--
2.38.1
PATCH 2/2 linux-user: Allow sendmsg() without IOV
Applications do call sendmsg() without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0,
msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}],
msg_controllen=40, msg_flags=0}, MSG_MORE) = 0
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}],
msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}],
msg_controllen=24, msg_flags=0}, 0) = 183The function do_sendrecvmsg_locked() is used for sndmsg() and recvmsg() and calls lock_iovec() to lock the IOV into memory. For the first sendmsg() above it returns NULL and thus wrongly skips the call the host sendmsg() syscall, which will break the calling application.
Fix this issue by:
- allowing sendmsg() even with empty IOV
- skip recvmsg() if IOV is NULL
- skip both if the return code of do_sendrecvmsg_locked() != 0, which indicates some failure like EFAULT on the IOV
Tested with the debian "ell" package with hppa guest on x86_64 host.
--- linux-user/syscall.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
linux-user/syscall.c +7 -2
@@ -3330,7 +3330,10 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, target_vec, count, send);if (vec ==NULL ) { ret = -host_to_target_errno (errno); - goto out2; +/* allow sending packet without any iov, e.g. with MSG_MORE flag */ +if (!send || ret) { + goto out2; + } } msg.msg_iovlen = count; msg.msg_iov = vec;
@@ -3382,7 +3385,9 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, } out: -unlock_iovec (vec, target_vec, count, !send); +if (vec) { +unlock_iovec (vec, target_vec, count, !send); + } out2:return ret; }
--
2.38.1
Re: [PATCH 2/2] linux-user: Allow sendmsg() without IOV
Le 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 31 lines
Applications do call
sendmsg()without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0,msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}], msg_controllen=40, msg_flags=0}, MSG_MORE) = 0 sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}], msg_controllen=24, msg_flags=0}, 0) = 183The function
do_sendrecvmsg_locked()is used forsndmsg()andrecvmsg()and callslock_iovec()to lock the IOV into memory. For the firstsendmsg()above it returns NULL and thus wrongly skips the call the hostsendmsg()syscall, which will break the calling application.Fix this issue by:
- allowingsendmsg()even with empty IOV
- skiprecvmsg()if IOV is NULL
- skip both if the return code ofdo_sendrecvmsg_locked()!= 0, which indicates some failure like EFAULT on the IOVTested with the debian "ell" package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>--- linux-user/syscall.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)linux-user/syscall.c +0
-0
linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3333
Quoted 5 lines
linux-user/syscall.c +1
-1@@ -3330,7 +3330,10 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, target_vec, count, send);if (vec ==NULL ) { ret = -host_to_target_errno (errno); - goto out2; +/* allow sending packet without any iov, e.g. with MSG_MORE flag */
why don't you check only for count is 0?
Somehing like:
if (vec == NULL && (count || !send)) {
...
Thanks,
Laurent
Re: [PATCH 2/2] linux-user: Allow sendmsg() without IOV
Quoted 41 lines · Laurent Vivier
Le 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 31 lines
Applications do call
sendmsg()without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0, msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}],
msg_controllen=40, msg_flags=0},MSG_MORE) = 0
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}], msg_controllen=24, msg_flags=0}, 0) = 183The function
do_sendrecvmsg_locked()is used forsndmsg()andrecvmsg()and callslock_iovec()to lock the IOV into memory. For the firstsendmsg()above it returns NULL and thus wrongly skips the call the hostsendmsg()syscall, which will break the calling application.Fix this issue by:
- allowingsendmsg()even with empty IOV
- skiprecvmsg()if IOV is NULL
- skip both if the return code ofdo_sendrecvmsg_locked()!= 0, which indicates some failure like EFAULT on the IOVTested with the debian "ell" package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>---
linux-user/syscall.c| 9 +++++++--1 file changed, 7 insertions(+), 2 deletions(-)linux-user/syscall.c +0
-0linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3333
Quoted 5 lines
linux-user/syscall.c +1
-1@@ -3330,7 +3330,10 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, target_vec, count, send); if (vec == NULL) { ret = -host_to_target_errno(errno); - goto out2; + /* allow sending packet without any iov, e.g. with MSG_MORE flag */why don't you check only for count is 0?
Somehing like:
Quoted 1 line
if (vec == NULL && (count || !send)) {
You mean:
if (vec == NULL && (!count || !send)) {
goto out2;lock_iovec() sets errno=0 if count==0, but for invalid addresses it sets errno!=0, so my current check (for which ret is the same as errno):
if (!send || ret) {
goto out2;exits on memfaults too, while a check for just count==0 would succeed.
Helge
Re: [PATCH 2/2] linux-user: Allow sendmsg() without IOV
Quoted 48 lines · Helge Deller
Quoted 41 lines · Laurent Vivier
Le 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 31 lines
Applications do call
sendmsg()without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0, msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}],
msg_controllen=40, msg_flags=0},MSG_MORE) = 0
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}], msg_controllen=24, msg_flags=0}, 0) = 183The function
do_sendrecvmsg_locked()is used forsndmsg()andrecvmsg()and callslock_iovec()to lock the IOV into memory. For the firstsendmsg()above it returns NULL and thus wrongly skips the call the hostsendmsg()syscall, which will break the calling application.Fix this issue by:
- allowingsendmsg()even with empty IOV
- skiprecvmsg()if IOV is NULL
- skip both if the return code ofdo_sendrecvmsg_locked()!= 0, which indicates some failure like EFAULT on the IOVTested with the debian "ell" package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>---
linux-user/syscall.c| 9 +++++++--1 file changed, 7 insertions(+), 2 deletions(-)linux-user/syscall.c +0
-0linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3333
Quoted 5 lines
linux-user/syscall.c +1
-1@@ -3330,7 +3330,10 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, target_vec, count, send); if (vec == NULL) { ret = -host_to_target_errno(errno); - goto out2; + /* allow sending packet without any iov, e.g. with MSG_MORE flag */why don't you check only for count is 0?
Somehing like:Quoted 1 line
if (vec == NULL && (count || !send)) {
You mean:
if (vec == NULL && (!count || !send)) {
goto out2;
Quoted 5 lines
lock_iovec()sets errno=0 if count==0, but for invalid addresses it sets errno!=0, so my current check (for which ret is the same as errno):
if (!send || ret) {
goto out2;
exits on memfaults too, while a check for just count==0 would succeed.
forget the above...
Maybe just checking for count==0 would be sufficient. I don't know yet if there are any other corner cases where replacing the check of
if (!send || ret) {
with
if (!send || count==0) {would behave different.
Checking against errno which is reported back from lock_iovec() seemed more logical for me. This patch has been as-is in the buildd trees now since weeks without any problems so far at least.
Helge
Re: [PATCH 2/2] linux-user: Allow sendmsg() without IOV
Le 31/01/2023 à 14:50, Helge Deller a écrit :
Quoted 55 lines · Helge Deller
Quoted 48 lines · Laurent Vivier
Quoted 41 lines
Le 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 31 lines
Applications do call
sendmsg()without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0, msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}],
msg_controllen=40, msg_flags=0},MSG_MORE) = 0
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}], msg_controllen=24, msg_flags=0}, 0) = 183The function
do_sendrecvmsg_locked()is used forsndmsg()andrecvmsg()and callslock_iovec()to lock the IOV into memory. For the firstsendmsg()above it returns NULL and thus wrongly skips the call the hostsendmsg()syscall, which will break the calling application.Fix this issue by:
- allowingsendmsg()even with empty IOV
- skiprecvmsg()if IOV is NULL
- skip both if the return code ofdo_sendrecvmsg_locked()!= 0, which indicates some failure like EFAULT on the IOVTested with the debian "ell" package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>---
linux-user/syscall.c| 9 +++++++--1 file changed, 7 insertions(+), 2 deletions(-)linux-user/syscall.c +0
-0linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3333
Quoted 5 lines
linux-user/syscall.c +1
-1@@ -3330,7 +3330,10 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, target_vec, count, send); if (vec == NULL) { ret = -host_to_target_errno(errno); - goto out2; + /* allow sending packet without any iov, e.g. with MSG_MORE flag */why don't you check only for count is 0?
Somehing like:Quoted 1 line
if (vec == NULL && (count || !send)) {
You mean:
if (vec == NULL && (!count || !send)) {
goto out2;Quoted 5 lines
lock_iovec()sets errno=0 if count==0, but for invalid addresses it sets errno!=0, so my current check (for which ret is the same as errno):
if (!send || ret) {
goto out2;
exits on memfaults too, while a check for just count==0 would succeed.
Quoted 11 lines
forget the above...
Maybe just checking for count==0 would be sufficient. I don't know yet if there are any other corner cases where replacing the check of
if (!send || ret) {
with
if (!send || count==0) {
would behave different.
Checking against errno which is reported back fromlock_iovec()seemed more logical for me. This patch has been as-is in the buildd trees now since weeks without any problems so far at least.
It seems kernel also checks for the invalid address even with count=0, so I'm going to apply this patch as-is.
Thanks,
Laurent
Re: [PATCH 2/2] linux-user: Allow sendmsg() without IOV
In reply to Helge DellerLe 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 19 lines
Applications do call
sendmsg()without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0,msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}], msg_controllen=40, msg_flags=0}, MSG_MORE) = 0 sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}], msg_controllen=24, msg_flags=0}, 0) = 183The function
do_sendrecvmsg_locked()is used forsndmsg()andrecvmsg()and callslock_iovec()to lock the IOV into memory. For the firstsendmsg()above it returns NULL and thus wrongly skips the call the hostsendmsg()syscall, which will break the calling application.Fix this issue by:
- allowingsendmsg()even with empty IOV
- skiprecvmsg()if IOV is NULL
- skip both if the return code ofdo_sendrecvmsg_locked()!= 0, which indicates some failure like EFAULT on the IOV
Quoted 7 lines
Tested with the debian "ell" package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>--- linux-user/syscall.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
Laurent Vivier
AppliedRe: [PATCH 2/2] linux-user: Allow sendmsg() without IOV
In reply to Helge DellerLe 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 54 lines
Applications do call
sendmsg()without any IOV, e.g.:
sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=NULL, msg_iovlen=0,msg_control=[{cmsg_len=36, cmsg_level=SOL_ALG, cmsg_type=0x2}], msg_controllen=40, msg_flags=0}, MSG_MORE) = 0 sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="The quick brown fox jumps over t"..., iov_len=183}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_ALG, cmsg_type=0x3}], msg_controllen=24, msg_flags=0}, 0) = 183The function
do_sendrecvmsg_locked()is used forsndmsg()andrecvmsg()and callslock_iovec()to lock the IOV into memory. For the firstsendmsg()above it returns NULL and thus wrongly skips the call the hostsendmsg()syscall, which will break the calling application.Fix this issue by:
- allowingsendmsg()even with empty IOV
- skiprecvmsg()if IOV is NULL
- skip both if the return code ofdo_sendrecvmsg_locked()!= 0, which indicates some failure like EFAULT on the IOVTested with the debian "ell" package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>--- linux-user/syscall.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)linux-user/syscall.c +7
-3@@ -3330,7 +3330,10 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, target_vec, count, send);if (vec ==NULL ) { ret = -host_to_target_errno (errno); - goto out2; +/* allow sending packet without any iov, e.g. with MSG_MORE flag */ +if (!send || ret) { + goto out2; + } } msg.msg_iovlen = count; msg.msg_iov = vec;@@ -3382,7 +3385,9 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, } out: -unlock_iovec (vec, target_vec, count, !send); +if (vec) { +unlock_iovec (vec, target_vec, count, !send); + } out2:return ret; } --
Quoted 3 lines
2.38.1
Applied to my linux-user-for-8.0 branch.
Thanks,
Laurent
Le 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 30 lines
Add suport to handle
SOL_ALGpackets viasendmsg()andrecvmsg(). This allows emulated userspace to use encryption functionality.Tested with the debian ell package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>--- linux-user/syscall.c | 8 ++++++++ 1 file changed, 8 insertions(+)linux-user/syscall.c +8
-1@@ -1871,6 +1871,14 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,__get_user (cred->pid, &target_cred->pid);__get_user (cred->uid, &target_cred->uid);__get_user (cred->gid, &target_cred->gid); + }else if (cmsg->cmsg_level ==SOL_ALG ) { +uint32_t *dst = (uint32_t *)data; + +memcpy (dst, target_data, len); +/* fix endianess of first 32-bit word */ +if (len >=sizeof (uint32_t )) { + *dst =tswap32 (*dst); + } }else {qemu_log_mask (LOG_UNIMP ,"Unsupported ancillary data: %d/%d\n" , cmsg->cmsg_level, cmsg->cmsg_type); --
Quoted 3 lines
2.38.1
Laurent Vivier
AppliedLe 12/12/2022 à 18:34, Helge Deller a écrit :
Quoted 30 lines
Add suport to handle
SOL_ALGpackets viasendmsg()andrecvmsg(). This allows emulated userspace to use encryption functionality.Tested with the debian ell package with hppa guest on
x86_64host.Signed-off-by Helge Deller <[email protected]>--- linux-user/syscall.c | 8 ++++++++ 1 file changed, 8 insertions(+)linux-user/syscall.c +8
-1@@ -1871,6 +1871,14 @@ static inline abi_long target_to_host_cmsg(struct msghdr *msgh,__get_user (cred->pid, &target_cred->pid);__get_user (cred->uid, &target_cred->uid);__get_user (cred->gid, &target_cred->gid); + }else if (cmsg->cmsg_level ==SOL_ALG ) { +uint32_t *dst = (uint32_t *)data; + +memcpy (dst, target_data, len); +/* fix endianess of first 32-bit word */ +if (len >=sizeof (uint32_t )) { + *dst =tswap32 (*dst); + } }else {qemu_log_mask (LOG_UNIMP ,"Unsupported ancillary data: %d/%d\n" , cmsg->cmsg_level, cmsg->cmsg_type); --
Quoted 3 lines
2.38.1
Applied to my linux-user-for-8.0 branch.
Thanks,
Laurent