From: Helge Deller <deller@kernel.org>
To: qemu-devel@nongnu.org
Cc: Laurent Vivier <laurent@vivier.eu>, Helge Deller <deller@gmx.de>,
Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Subject: [PULL 3/4] linux-user: Allow getsockopt() with NULL optval address
Date: Thu, 30 Apr 2026 09:19:21 +0200 [thread overview]
Message-ID: <20260430071922.15341-4-deller@kernel.org> (raw)
In-Reply-To: <20260430071922.15341-1-deller@kernel.org>
From: Helge Deller <deller@gmx.de>
Some programs test availability of socket options by asking for the
value with an NULL optval address, which currenrly always trigger an
EFAULT in qemu. Fix it by allowing a NULL address, in the same manner
as the Linux kernel on physical machines.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2390
Signed-off-by: Helge Deller <deller@gmx.de>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
---
linux-user/syscall.c | 50 +++++++++++++++++++++++++-------------------
1 file changed, 28 insertions(+), 22 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 4594909242..d68edb7afd 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -2644,6 +2644,10 @@ get_timeout:
if (ret < 0) {
return ret;
}
+ /* special case: destination address is NULL, return 0 */
+ if (optval_addr) {
+ len = 0;
+ }
if (len == sizeof(struct target__kernel_sock_timeval)) {
if (copy_to_user_timeval64(optval_addr, &tv)) {
return -TARGET_EFAULT;
@@ -2844,7 +2848,10 @@ get_timeout:
}
if (len > lv)
len = lv;
- if (len == 4) {
+ if (!optval_addr) {
+ /* writing to NULL does not give error */
+ len = 0;
+ } else if (len == 4) {
if (put_user_u32(val, optval_addr))
return -TARGET_EFAULT;
} else {
@@ -2877,18 +2884,24 @@ get_timeout:
return -TARGET_EINVAL;
lv = sizeof(lv);
ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
+write_ret:
if (ret < 0)
return ret;
- if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
+ if (!optval_addr) {
+ len = 0;
+ } else if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
len = 1;
- if (put_user_u32(len, optlen)
- || put_user_u8(val, optval_addr))
+ if (put_user_u8(val, optval_addr)) {
return -TARGET_EFAULT;
+ }
} else {
if (len > sizeof(int))
len = sizeof(int);
- if (put_user_u32(len, optlen)
- || put_user_u32(val, optval_addr))
+ if (put_user_u32(val, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ }
+ if (put_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
break;
@@ -2939,20 +2952,7 @@ get_timeout:
return -TARGET_EINVAL;
lv = sizeof(lv);
ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
- if (ret < 0)
- return ret;
- if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
- len = 1;
- if (put_user_u32(len, optlen)
- || put_user_u8(val, optval_addr))
- return -TARGET_EFAULT;
- } else {
- if (len > sizeof(int))
- len = sizeof(int);
- if (put_user_u32(len, optlen)
- || put_user_u32(val, optval_addr))
- return -TARGET_EFAULT;
- }
+ goto write_ret;
break;
default:
ret = -TARGET_ENOPROTOOPT;
@@ -2986,8 +2986,14 @@ get_timeout:
if (ret < 0) {
return ret;
}
- if (put_user_u32(lv, optlen)
- || put_user_u32(val, optval_addr)) {
+ if (optval_addr) {
+ if (put_user_u32(val, optval_addr)) {
+ return -TARGET_EFAULT;
+ }
+ } else {
+ lv = 0;
+ }
+ if (put_user_u32(lv, optlen)) {
return -TARGET_EFAULT;
}
break;
--
2.53.0
next prev parent reply other threads:[~2026-04-30 7:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-30 7:19 [PULL 0/4] Linux user next patches Helge Deller
2026-04-30 7:19 ` [PULL 1/4] linux-user: Add missing CDROM ioctls Helge Deller
2026-04-30 7:19 ` [PULL 2/4] linux-user: Flush errors by using exit() instead of _exit() in error path Helge Deller
2026-04-30 7:19 ` Helge Deller [this message]
2026-04-30 7:19 ` [PULL 4/4] linux-user: Translate errno in IP_RECVERR and IPV6_RECVERR Helge Deller
2026-04-30 17:35 ` [PULL 0/4] Linux user next patches Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260430071922.15341-4-deller@kernel.org \
--to=deller@kernel.org \
--cc=deller@gmx.de \
--cc=laurent@vivier.eu \
--cc=pierrick.bouvier@oss.qualcomm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.